|
| 1 | +import { |
| 2 | + Compartment, |
| 3 | + EditorSelection, |
| 4 | + EditorState, |
| 5 | + Transaction, |
| 6 | + type Extension, |
| 7 | + type SelectionRange, |
| 8 | +} from "@codemirror/state"; |
| 9 | +import { EditorView } from "@codemirror/view"; |
| 10 | + |
| 11 | +interface PointerTapSnapshot { |
| 12 | + pointerId: number; |
| 13 | + x: number; |
| 14 | + y: number; |
| 15 | + timeStamp: number; |
| 16 | + isPrimary?: boolean; |
| 17 | + button?: number; |
| 18 | + cancelled?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +interface PointerTapEnd { |
| 22 | + pointerId: number; |
| 23 | + x: number; |
| 24 | + y: number; |
| 25 | + timeStamp: number; |
| 26 | + isPrimary?: boolean; |
| 27 | + button?: number; |
| 28 | +} |
| 29 | + |
| 30 | +interface PointerTapOptions { |
| 31 | + maxDelay?: number; |
| 32 | + maxDistance?: number; |
| 33 | +} |
| 34 | + |
| 35 | +const readOnlyUserChangePattern = /^(?:input|delete|move|undo|redo)(?:\.|$)/; |
| 36 | + |
| 37 | +const readOnlyInputGuard = EditorView.inputHandler.of((view) => { |
| 38 | + return view.state.readOnly; |
| 39 | +}); |
| 40 | + |
| 41 | +const readOnlyUserChangeFilter = EditorState.transactionFilter.of( |
| 42 | + (transaction) => { |
| 43 | + if (!transaction.startState.readOnly || !transaction.docChanged) { |
| 44 | + return transaction; |
| 45 | + } |
| 46 | + |
| 47 | + const userEvent = transaction.annotation(Transaction.userEvent); |
| 48 | + return isReadOnlyUserChange(userEvent) ? [] : transaction; |
| 49 | + }, |
| 50 | +); |
| 51 | + |
| 52 | +const readOnlyFocusGuard = EditorView.domEventHandlers({ |
| 53 | + focus(event, view) { |
| 54 | + event.preventDefault(); |
| 55 | + blurEditorIfReadOnly(view, view.state.readOnly); |
| 56 | + return true; |
| 57 | + }, |
| 58 | + beforeinput(event, view) { |
| 59 | + if (!view.state.readOnly) return false; |
| 60 | + event.preventDefault(); |
| 61 | + blurEditorIfReadOnly(view, true); |
| 62 | + return true; |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +/** |
| 67 | + * Keep CodeMirror's document and DOM editability in sync. |
| 68 | + */ |
| 69 | +export function createEditorReadOnlyExtension(readOnly: boolean): Extension { |
| 70 | + return [ |
| 71 | + EditorState.readOnly.of(readOnly), |
| 72 | + EditorView.editable.of(!readOnly), |
| 73 | + ...(readOnly |
| 74 | + ? [readOnlyFocusGuard, readOnlyInputGuard, readOnlyUserChangeFilter] |
| 75 | + : []), |
| 76 | + ]; |
| 77 | +} |
| 78 | + |
| 79 | +/** Identify document-changing transactions produced by user interaction. */ |
| 80 | +export function isReadOnlyUserChange(userEvent: string | undefined): boolean { |
| 81 | + return !!userEvent && readOnlyUserChangePattern.test(userEvent); |
| 82 | +} |
| 83 | + |
| 84 | +/** Dismiss an active soft keyboard when an editor becomes read-only. */ |
| 85 | +export function blurEditorIfReadOnly( |
| 86 | + view: EditorView, |
| 87 | + readOnly: boolean, |
| 88 | +): void { |
| 89 | + if (!readOnly) return; |
| 90 | + const activeElement = document.activeElement; |
| 91 | + if ( |
| 92 | + activeElement instanceof HTMLElement && |
| 93 | + view.dom.contains(activeElement) |
| 94 | + ) { |
| 95 | + activeElement.blur(); |
| 96 | + } |
| 97 | + view.contentDOM.blur(); |
| 98 | +} |
| 99 | + |
| 100 | +/** Atomically update all read-only behavior controlled by the compartment. */ |
| 101 | +export function reconfigureEditorReadOnly( |
| 102 | + view: EditorView, |
| 103 | + compartment: Compartment, |
| 104 | + readOnly: boolean, |
| 105 | +): void { |
| 106 | + view.dispatch({ |
| 107 | + effects: compartment.reconfigure(createEditorReadOnlyExtension(readOnly)), |
| 108 | + }); |
| 109 | + blurEditorIfReadOnly(view, readOnly); |
| 110 | +} |
| 111 | + |
| 112 | +/** Focus editable editors and actively settle read-only editors unfocused. */ |
| 113 | +export function focusEditorIfEditable(view: EditorView): boolean { |
| 114 | + if (view.state.readOnly) { |
| 115 | + blurEditorIfReadOnly(view, true); |
| 116 | + return false; |
| 117 | + } |
| 118 | + view.focus(); |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +/** Classify a completed pointer gesture as a short primary tap. */ |
| 123 | +export function shouldCommitReadOnlyTap( |
| 124 | + start: PointerTapSnapshot, |
| 125 | + end: PointerTapEnd, |
| 126 | + options: PointerTapOptions = {}, |
| 127 | +): boolean { |
| 128 | + const { maxDelay = 500, maxDistance = 20 } = options; |
| 129 | + if (start.cancelled) return false; |
| 130 | + if (start.pointerId !== end.pointerId) return false; |
| 131 | + if (start.isPrimary === false || end.isPrimary === false) return false; |
| 132 | + if ((start.button ?? 0) !== 0 || (end.button ?? 0) !== 0) return false; |
| 133 | + const duration = end.timeStamp - start.timeStamp; |
| 134 | + if (!Number.isFinite(duration) || duration < 0 || duration > maxDelay) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + return Math.hypot(end.x - start.x, end.y - start.y) <= maxDistance; |
| 138 | +} |
| 139 | + |
| 140 | +/** Collapse an existing read-only selection without focusing or editing. */ |
| 141 | +export function collapseReadOnlySelection( |
| 142 | + view: EditorView, |
| 143 | + pos: number, |
| 144 | +): boolean { |
| 145 | + if (!view.state.readOnly || view.state.selection.main.empty) return false; |
| 146 | + const position = Math.max(0, Math.min(pos, view.state.doc.length)); |
| 147 | + view.dispatch({ |
| 148 | + selection: EditorSelection.cursor(position), |
| 149 | + userEvent: "select.pointer", |
| 150 | + }); |
| 151 | + blurEditorIfReadOnly(view, true); |
| 152 | + return true; |
| 153 | +} |
| 154 | + |
| 155 | +/** Resolve the text selected by a read-only long-press. */ |
| 156 | +export function resolveReadOnlyContextSelection( |
| 157 | + state: EditorState, |
| 158 | + pos: number, |
| 159 | +): SelectionRange { |
| 160 | + const docLength = state.doc.length; |
| 161 | + const position = Math.max( |
| 162 | + 0, |
| 163 | + Math.min(Number.isFinite(pos) ? pos : state.selection.main.head, docLength), |
| 164 | + ); |
| 165 | + const current = state.selection.main; |
| 166 | + |
| 167 | + if (!current.empty && position >= current.from && position <= current.to) { |
| 168 | + return current; |
| 169 | + } |
| 170 | + |
| 171 | + const word = state.wordAt(position); |
| 172 | + if (word) return word; |
| 173 | + if (docLength === 0) return EditorSelection.cursor(0); |
| 174 | + |
| 175 | + let from = Math.min(position, docLength - 1); |
| 176 | + const unit = state.doc.sliceString(from, from + 1).charCodeAt(0); |
| 177 | + if (unit >= 0xdc00 && unit <= 0xdfff && from > 0) { |
| 178 | + const previousUnit = state.doc.sliceString(from - 1, from).charCodeAt(0); |
| 179 | + if (previousUnit >= 0xd800 && previousUnit <= 0xdbff) from -= 1; |
| 180 | + } |
| 181 | + |
| 182 | + const codePoint = state.doc |
| 183 | + .sliceString(from, Math.min(from + 2, docLength)) |
| 184 | + .codePointAt(0); |
| 185 | + const width = codePoint != null && codePoint > 0xffff ? 2 : 1; |
| 186 | + return EditorSelection.range(from, Math.min(from + width, docLength)); |
| 187 | +} |
0 commit comments