|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { isCursorAtBlockStart } from './selection'; |
| 3 | + |
| 4 | +// `isCursorAtBlockStart` only inspects `editor.selection` and `editor.children`, |
| 5 | +// so plain editor-shaped objects are enough (no full Volto config registry). |
| 6 | +const makeEditor = (children, selection) => ({ children, selection }); |
| 7 | +const at = (path, offset) => ({ path, offset }); |
| 8 | +const collapsed = (path, offset) => ({ |
| 9 | + anchor: at(path, offset), |
| 10 | + focus: at(path, offset), |
| 11 | +}); |
| 12 | + |
| 13 | +describe('isCursorAtBlockStart', () => { |
| 14 | + it('returns true when the caret is at the first leaf of the block', () => { |
| 15 | + const editor = makeEditor( |
| 16 | + [{ type: 'p', children: [{ text: 'Hello' }] }], |
| 17 | + collapsed([0, 0], 0), |
| 18 | + ); |
| 19 | + expect(isCursorAtBlockStart(editor)).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns true for the first leaf of a nested list item (path [0,0,0])', () => { |
| 23 | + const editor = makeEditor( |
| 24 | + [ |
| 25 | + { |
| 26 | + type: 'ul', |
| 27 | + children: [ |
| 28 | + { |
| 29 | + type: 'li', |
| 30 | + children: [{ text: 'first item' }], |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + ], |
| 35 | + collapsed([0, 0, 0], 0), |
| 36 | + ); |
| 37 | + expect(isCursorAtBlockStart(editor)).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns false at offset 0 of a non-first leaf inside a list item (#8347)', () => { |
| 41 | + // A list item: {text:""} + link + {text:""} — the empty text leaf after |
| 42 | + // the link is at path [0,0,2]. Backspace there must NOT count as block start. |
| 43 | + const editor = makeEditor( |
| 44 | + [ |
| 45 | + { |
| 46 | + type: 'ul', |
| 47 | + children: [ |
| 48 | + { |
| 49 | + type: 'li', |
| 50 | + children: [ |
| 51 | + { text: '' }, |
| 52 | + { |
| 53 | + type: 'link', |
| 54 | + data: { url: 'https://demo.plone.org/' }, |
| 55 | + children: [{ text: 'Plone 6 (this site)' }], |
| 56 | + }, |
| 57 | + { text: '' }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + ], |
| 63 | + collapsed([0, 0, 2], 0), |
| 64 | + ); |
| 65 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns false at offset 0 of a non-first leaf of a plain paragraph', () => { |
| 69 | + const editor = makeEditor( |
| 70 | + [ |
| 71 | + { |
| 72 | + type: 'p', |
| 73 | + children: [ |
| 74 | + { text: 'before' }, |
| 75 | + { |
| 76 | + type: 'link', |
| 77 | + data: { url: 'https://example.com' }, |
| 78 | + children: [{ text: 'link' }], |
| 79 | + }, |
| 80 | + { text: 'after' }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + ], |
| 84 | + collapsed([0, 2], 0), |
| 85 | + ); |
| 86 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns false when the offset is greater than 0', () => { |
| 90 | + const editor = makeEditor( |
| 91 | + [{ type: 'p', children: [{ text: 'Hello' }] }], |
| 92 | + collapsed([0, 0], 2), |
| 93 | + ); |
| 94 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns false when there is no selection', () => { |
| 98 | + const editor = makeEditor( |
| 99 | + [{ type: 'p', children: [{ text: 'Hi' }] }], |
| 100 | + null, |
| 101 | + ); |
| 102 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns false when the selection is expanded', () => { |
| 106 | + const editor = makeEditor([{ type: 'p', children: [{ text: 'Hello' }] }], { |
| 107 | + anchor: at([0, 0], 0), |
| 108 | + focus: at([0, 0], 3), |
| 109 | + }); |
| 110 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns false when the editor has no children', () => { |
| 114 | + const editor = makeEditor([], collapsed([0, 0], 0)); |
| 115 | + expect(isCursorAtBlockStart(editor)).toBe(false); |
| 116 | + }); |
| 117 | +}); |
0 commit comments