|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest'; |
| 2 | + |
| 3 | +import { clickToPosition } from '@superdoc/layout-bridge'; |
| 4 | +import { TextSelection } from 'prosemirror-state'; |
| 5 | + |
| 6 | +import { |
| 7 | + EditorInputManager, |
| 8 | + type EditorInputDependencies, |
| 9 | + type EditorInputCallbacks, |
| 10 | +} from '../pointer-events/EditorInputManager.js'; |
| 11 | + |
| 12 | +vi.mock('@superdoc/layout-bridge', () => ({ |
| 13 | + clickToPosition: vi.fn(() => ({ pos: 12, layoutEpoch: 1, pageIndex: 0, blockId: 'body-1' })), |
| 14 | + getFragmentAtPosition: vi.fn(() => null), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('prosemirror-state', async (importOriginal) => { |
| 18 | + const original = await importOriginal<typeof import('prosemirror-state')>(); |
| 19 | + return { |
| 20 | + ...original, |
| 21 | + TextSelection: { |
| 22 | + ...original.TextSelection, |
| 23 | + create: vi.fn(() => ({ |
| 24 | + empty: true, |
| 25 | + $from: { parent: { inlineContent: true } }, |
| 26 | + })), |
| 27 | + }, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +describe('EditorInputManager - Footnote click selection behavior', () => { |
| 32 | + let manager: EditorInputManager; |
| 33 | + let viewportHost: HTMLElement; |
| 34 | + let visibleHost: HTMLElement; |
| 35 | + let mockEditor: { |
| 36 | + isEditable: boolean; |
| 37 | + state: { |
| 38 | + doc: { content: { size: number }; nodesBetween: Mock }; |
| 39 | + tr: { setSelection: Mock; setStoredMarks: Mock }; |
| 40 | + selection: { $anchor: null }; |
| 41 | + storedMarks: null; |
| 42 | + }; |
| 43 | + view: { |
| 44 | + dispatch: Mock; |
| 45 | + dom: HTMLElement; |
| 46 | + focus: Mock; |
| 47 | + hasFocus: Mock; |
| 48 | + }; |
| 49 | + on: Mock; |
| 50 | + off: Mock; |
| 51 | + emit: Mock; |
| 52 | + }; |
| 53 | + let mockDeps: EditorInputDependencies; |
| 54 | + let mockCallbacks: EditorInputCallbacks; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + viewportHost = document.createElement('div'); |
| 58 | + viewportHost.className = 'presentation-editor__viewport'; |
| 59 | + visibleHost = document.createElement('div'); |
| 60 | + visibleHost.className = 'presentation-editor__visible'; |
| 61 | + visibleHost.appendChild(viewportHost); |
| 62 | + |
| 63 | + const container = document.createElement('div'); |
| 64 | + container.className = 'presentation-editor'; |
| 65 | + container.appendChild(visibleHost); |
| 66 | + document.body.appendChild(container); |
| 67 | + |
| 68 | + mockEditor = { |
| 69 | + isEditable: true, |
| 70 | + state: { |
| 71 | + doc: { |
| 72 | + content: { size: 100 }, |
| 73 | + nodesBetween: vi.fn((from, to, cb) => { |
| 74 | + cb({ isTextblock: true }, 0); |
| 75 | + }), |
| 76 | + }, |
| 77 | + tr: { |
| 78 | + setSelection: vi.fn().mockReturnThis(), |
| 79 | + setStoredMarks: vi.fn().mockReturnThis(), |
| 80 | + }, |
| 81 | + selection: { $anchor: null }, |
| 82 | + storedMarks: null, |
| 83 | + }, |
| 84 | + view: { |
| 85 | + dispatch: vi.fn(), |
| 86 | + dom: document.createElement('div'), |
| 87 | + focus: vi.fn(), |
| 88 | + hasFocus: vi.fn(() => false), |
| 89 | + }, |
| 90 | + on: vi.fn(), |
| 91 | + off: vi.fn(), |
| 92 | + emit: vi.fn(), |
| 93 | + }; |
| 94 | + |
| 95 | + mockDeps = { |
| 96 | + getActiveEditor: vi.fn(() => mockEditor as unknown as ReturnType<EditorInputDependencies['getActiveEditor']>), |
| 97 | + getEditor: vi.fn(() => mockEditor as unknown as ReturnType<EditorInputDependencies['getEditor']>), |
| 98 | + getLayoutState: vi.fn(() => ({ layout: {} as any, blocks: [], measures: [] })), |
| 99 | + getEpochMapper: vi.fn(() => ({ |
| 100 | + mapPosFromLayoutToCurrentDetailed: vi.fn(() => ({ ok: true, pos: 12, toEpoch: 1 })), |
| 101 | + })) as unknown as EditorInputDependencies['getEpochMapper'], |
| 102 | + getViewportHost: vi.fn(() => viewportHost), |
| 103 | + getVisibleHost: vi.fn(() => visibleHost), |
| 104 | + getLayoutMode: vi.fn(() => 'vertical'), |
| 105 | + getHeaderFooterSession: vi.fn(() => null), |
| 106 | + getPageGeometryHelper: vi.fn(() => null), |
| 107 | + getZoom: vi.fn(() => 1), |
| 108 | + isViewLocked: vi.fn(() => false), |
| 109 | + getDocumentMode: vi.fn(() => 'editing'), |
| 110 | + getPageElement: vi.fn(() => null), |
| 111 | + isSelectionAwareVirtualizationEnabled: vi.fn(() => false), |
| 112 | + }; |
| 113 | + |
| 114 | + mockCallbacks = { |
| 115 | + normalizeClientPoint: vi.fn((clientX: number, clientY: number) => ({ x: clientX, y: clientY })), |
| 116 | + scheduleSelectionUpdate: vi.fn(), |
| 117 | + updateSelectionDebugHud: vi.fn(), |
| 118 | + }; |
| 119 | + |
| 120 | + manager = new EditorInputManager(); |
| 121 | + manager.setDependencies(mockDeps); |
| 122 | + manager.setCallbacks(mockCallbacks); |
| 123 | + manager.bind(); |
| 124 | + }); |
| 125 | + |
| 126 | + afterEach(() => { |
| 127 | + manager.destroy(); |
| 128 | + document.body.innerHTML = ''; |
| 129 | + vi.clearAllMocks(); |
| 130 | + }); |
| 131 | + |
| 132 | + function getPointerEventImpl(): typeof PointerEvent | typeof MouseEvent { |
| 133 | + return ( |
| 134 | + (globalThis as unknown as { PointerEvent?: typeof PointerEvent; MouseEvent: typeof MouseEvent }).PointerEvent ?? |
| 135 | + globalThis.MouseEvent |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + it('does not change editor selection on direct footnote fragment click', () => { |
| 140 | + const fragmentEl = document.createElement('span'); |
| 141 | + fragmentEl.setAttribute('data-block-id', 'footnote-1-0'); |
| 142 | + const nestedEl = document.createElement('span'); |
| 143 | + fragmentEl.appendChild(nestedEl); |
| 144 | + viewportHost.appendChild(fragmentEl); |
| 145 | + |
| 146 | + const PointerEventImpl = getPointerEventImpl(); |
| 147 | + nestedEl.dispatchEvent( |
| 148 | + new PointerEventImpl('pointerdown', { |
| 149 | + bubbles: true, |
| 150 | + cancelable: true, |
| 151 | + button: 0, |
| 152 | + buttons: 1, |
| 153 | + clientX: 10, |
| 154 | + clientY: 10, |
| 155 | + } as PointerEventInit), |
| 156 | + ); |
| 157 | + |
| 158 | + // Expected behavior: footnote click should not relocate caret to start of the document. |
| 159 | + expect(TextSelection.create as unknown as Mock).not.toHaveBeenCalled(); |
| 160 | + expect(mockEditor.state.tr.setSelection).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('does not change editor selection when hit-test resolves to a footnote block', () => { |
| 164 | + (clickToPosition as unknown as Mock).mockReturnValue({ |
| 165 | + pos: 22, |
| 166 | + layoutEpoch: 1, |
| 167 | + pageIndex: 0, |
| 168 | + blockId: 'footnote-1-1', |
| 169 | + }); |
| 170 | + |
| 171 | + const target = document.createElement('span'); |
| 172 | + viewportHost.appendChild(target); |
| 173 | + |
| 174 | + const PointerEventImpl = getPointerEventImpl(); |
| 175 | + target.dispatchEvent( |
| 176 | + new PointerEventImpl('pointerdown', { |
| 177 | + bubbles: true, |
| 178 | + cancelable: true, |
| 179 | + button: 0, |
| 180 | + buttons: 1, |
| 181 | + clientX: 12, |
| 182 | + clientY: 14, |
| 183 | + } as PointerEventInit), |
| 184 | + ); |
| 185 | + |
| 186 | + // Expected behavior: block edits in footnotes without resetting user selection. |
| 187 | + expect(TextSelection.create as unknown as Mock).not.toHaveBeenCalled(); |
| 188 | + expect(mockEditor.state.tr.setSelection).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments