| 1 | /** |
| 2 | * @vitest-environment happy-dom |
| 3 | */ |
| 4 | import { createEditor } from 'slate' |
| 5 | import { describe, expect, it } from 'vitest' |
| 6 | import { applyColorMark } from '../../../src/renderer/src/components/ui/RichTextBox' |
| 7 | |
| 8 | function createRichTextEditor(text: string) { |
| 9 | const editor = createEditor() |
| 10 | editor.children = [{ type: 'paragraph', children: [{ text }] }] |
| 11 | editor.selection = null |
| 12 | return editor |
| 13 | } |
| 14 | |
| 15 | describe('applyColorMark', () => { |
| 16 | it('restores an expanded selection cached before the color input receives focus', () => { |
| 17 | const editor = createRichTextEditor('Hello') |
| 18 | const value = applyColorMark(editor, '#ff0000', { |
| 19 | anchor: { path: [0, 0], offset: 1 }, |
| 20 | focus: { path: [0, 0], offset: 4 } |
| 21 | }) |
| 22 | |
| 23 | expect(value).toEqual({ |
| 24 | html: 'H<span style="color: #ff0000">ell</span>o', |
| 25 | text: 'Hello' |
| 26 | }) |
| 27 | }) |
| 28 | |
| 29 | it('applies color to the full text when the native picker has cleared the selection', () => { |
| 30 | const editor = createRichTextEditor('Hello') |
| 31 | |
| 32 | expect(applyColorMark(editor, '#00aa00')).toEqual({ |
| 33 | html: '<span style="color: #00aa00">Hello</span>', |
| 34 | text: 'Hello' |
| 35 | }) |
| 36 | }) |
| 37 | |
| 38 | it('ignores invalid color values without changing the editor content', () => { |
| 39 | const editor = createRichTextEditor('Hello') |
| 40 | |
| 41 | expect(applyColorMark(editor, 'not-a-color')).toBeNull() |
| 42 | expect(editor.children).toEqual([{ type: 'paragraph', children: [{ text: 'Hello' }] }]) |
| 43 | }) |
| 44 | }) |
| 45 |