|
13 | 13 | import type {IRichString} from '$lib/dotnet-types/generated-types/MiniLcm/Models/IRichString'; |
14 | 14 | import {Label} from '$lib/components/ui/label'; |
15 | 15 | import {EditorView} from 'prosemirror-view'; |
16 | | - import {AllSelection, EditorState} from 'prosemirror-state'; |
| 16 | + import {AllSelection, EditorState, Selection} from 'prosemirror-state'; |
17 | 17 | import {keymap} from 'prosemirror-keymap'; |
18 | 18 | import {baseKeymap} from 'prosemirror-commands'; |
19 | 19 | import {undo, redo, history} from 'prosemirror-history'; |
|
22 | 22 | import type {HTMLAttributes} from 'svelte/elements'; |
23 | 23 | import {IsUsingKeyboard} from 'bits-ui'; |
24 | 24 | import type {IRichSpan} from '$lib/dotnet-types/generated-types/MiniLcm/Models/IRichSpan'; |
25 | | - import {inputVariants} from '../ui/input/input.svelte'; |
26 | 25 | import {on} from 'svelte/events'; |
| 26 | + import {IsMobile} from '$lib/hooks/is-mobile.svelte'; |
| 27 | + import {findNextTabbable} from '$lib/utils/tabbable'; |
| 28 | + import {inputVariants} from '../ui/input/input.svelte'; |
27 | 29 |
|
28 | 30 | let { |
29 | 31 | value = $bindable(), |
|
36 | 38 | readonly = false, |
37 | 39 | onchange = () => {}, |
38 | 40 | autofocus, |
| 41 | + class: className, |
| 42 | + enterkeyhint = 'next', |
39 | 43 | ...rest |
40 | 44 | }: |
41 | 45 | { |
|
54 | 58 | let editor: EditorView | null = null; |
55 | 59 |
|
56 | 60 | const isUsingKeyboard = new IsUsingKeyboard(); |
| 61 | + // isUsingKeyboard.current isn't entirely reliable on mobile due to virtual keyboards |
| 62 | + let pointerDown = false; |
57 | 63 |
|
58 | 64 | onMount(() => { |
59 | 65 | // docs https://prosemirror.net/docs/ |
|
72 | 78 | editor.updateState(newState); |
73 | 79 | }, |
74 | 80 | attributes: { |
75 | | - class: inputVariants({class: 'min-h-10 h-auto block'}), |
| 81 | + class: inputVariants({class: 'min-h-10 h-auto pb-1.5'}), |
76 | 82 | // todo: the distribution of props between the editor and the elementRef is not good |
77 | 83 | // there should probably be a wrapper component that provides the elementRef to this one |
78 | 84 | ...(id ? {id} : {}), |
|
81 | 87 | role: 'textbox', |
82 | 88 | ...(autocapitalize ? {autocapitalize} : {}), |
83 | 89 | spellcheck: 'false', |
| 90 | + ...(enterkeyhint ? {enterkeyhint} : {}), |
84 | 91 | }, |
85 | 92 | editable() { |
86 | 93 | return !readonly; |
87 | 94 | }, |
88 | 95 | handleDOMEvents: { |
89 | | - 'focus': onfocus, |
| 96 | + pointerdown() { |
| 97 | + pointerDown = true; |
| 98 | + setTimeout(() => pointerDown = false, 100); // yes, apparently we need a decently high timeout value |
| 99 | + }, |
| 100 | + 'focus'(editor) { |
| 101 | + onfocus(editor, !pointerDown); |
| 102 | + }, |
90 | 103 | 'blur': onblur, |
| 104 | + keydown(_view, event) { |
| 105 | + if (event.key === 'Enter') { |
| 106 | + onEnterKey(); |
| 107 | + } |
| 108 | + }, |
91 | 109 | } |
92 | 110 | }); |
93 | 111 | editor.dom.setAttribute('tabindex', '0'); |
|
97 | 115 | if (relatedLabel) return on(relatedLabel, 'click', onFocusTargetClick); |
98 | 116 | }); |
99 | 117 |
|
100 | | - function onfocus(editor: EditorView) { |
101 | | - if (isUsingKeyboard.current) { // tabbed in |
102 | | - selectAll(editor); |
| 118 | + let prevSelection: Selection | undefined; |
| 119 | + function onfocus(editor: EditorView, viaKeyboard: boolean) { |
| 120 | + const usingKeyboard = isUsingKeyboard.current || |
| 121 | + IsMobile.value && viaKeyboard; |
| 122 | + if (usingKeyboard) { // tabbed in |
| 123 | + if (IsMobile.value) { |
| 124 | + if (prevSelection) { |
| 125 | + const prevSelectionForCurrentDoc = Selection.fromJSON(editor.state.doc, prevSelection.toJSON()); |
| 126 | + setSelection(prevSelectionForCurrentDoc); |
| 127 | + prevSelection = undefined; |
| 128 | + } else { |
| 129 | + setSelection(Selection.atEnd(editor.state.doc)); |
| 130 | + } |
| 131 | + } else { |
| 132 | + selectAll(editor); |
| 133 | + } |
103 | 134 | } |
104 | 135 | } |
105 | 136 |
|
|
108 | 139 | onchange(value); |
109 | 140 | dirty = false; |
110 | 141 | } |
| 142 | + prevSelection = editor.state.selection; |
111 | 143 | clearSelection(editor); |
112 | 144 | } |
113 | 145 |
|
|
139 | 171 | keymap({ |
140 | 172 | 'Mod-z': undo, |
141 | 173 | 'Mod-y': redo, |
| 174 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 175 | + 'Enter': () => { |
| 176 | + // This handler is not triggered reliably on mobile, so we're using handleDOMEvents.keydown instead |
| 177 | + // if (IsMobile.value) onEnterKey(); |
| 178 | +
|
| 179 | + // and we never want to do anything on desktop |
| 180 | + // (partially because it causes range errors - on mobile too) |
| 181 | + return true; |
| 182 | + }, |
142 | 183 | 'Shift-Enter': (state, dispatch) => { |
143 | 184 | if (dispatch) dispatch(state.tr.insertText(newLine)); |
144 | 185 | return true; |
|
149 | 190 | }); |
150 | 191 | } |
151 | 192 |
|
| 193 | + function onEnterKey(): void { |
| 194 | + if (IsMobile.value && enterkeyhint === 'next' && editor?.dom) { |
| 195 | + // mimic <input> 'next' behaviour |
| 196 | + const nextTabbable = findNextTabbable(editor.dom); |
| 197 | + nextTabbable?.focus(); |
| 198 | + } |
| 199 | + } |
| 200 | +
|
152 | 201 | function valueToDoc(): Node { |
153 | 202 | return textSchema.node('doc', {}, value?.spans |
154 | 203 | .filter(s => s.text) |
155 | | - .map(s => richSpanToNode(s)) ?? []); |
| 204 | + .map((s, i, all) => richSpanToNode(s, i === all.length - 1)) ?? []); |
156 | 205 | } |
157 | 206 |
|
158 | | - function richSpanToNode(s: IRichSpan) { |
| 207 | + function richSpanToNode(s: IRichSpan, isLast: boolean): Node { |
159 | 208 | //we must pull text out of what is stored on the node attrs |
160 | 209 | //ProseMirror will keep the text up to date itself, if we store it on the richSpan attr then it will become out of date |
161 | 210 | let {text, ...rest} = s; |
162 | 211 | //if the ws doesn't match expected, or there's more than just the ws key in props |
163 | 212 | const isCustomized = (!!s.ws && !!normalWs && normalWs !== s.ws) || Object.keys(rest).length > 1; |
164 | | - return textSchema.node('span', {richSpan: rest, className: cn(isCustomized && 'customized')}, [textSchema.text(replaceLineSeparatorWithNewLine(text))]); |
| 213 | + return textSchema.node('span', {richSpan: rest, className: cn(isCustomized && 'customized')}, [ |
| 214 | + textSchema.text(replaceLineSeparatorWithNewLine(text)), |
| 215 | + // a <br> seems to be the only thing that will cause a trailing \n to be rendered |
| 216 | + // this is what Prose-Mirror does if inline: false, which we can't use |
| 217 | + ...(isLast ? [textSchema.node('br')] : []) |
| 218 | + ]); |
165 | 219 | } |
166 | 220 |
|
167 | 221 | function richSpanFromNode(node: Node) { |
168 | 222 | return {...node.attrs.richSpan, text: replaceNewLineWithLineSeparator(node.textContent)}; |
169 | 223 | } |
170 | 224 |
|
171 | 225 | function selectAll(editor: EditorView) { |
172 | | - editor.dispatch(editor.state.tr.setSelection(new AllSelection(editor.state.doc))); |
| 226 | + setSelection(new AllSelection(editor.state.doc)); |
| 227 | +
|
| 228 | + if (!readonly) return; // happens automatically if not readonly |
| 229 | +
|
| 230 | + const selection = window.getSelection(); |
| 231 | + if (!selection) return; |
| 232 | + const range = document.createRange(); |
| 233 | + range.selectNodeContents(editor.dom); |
| 234 | + selection.removeAllRanges(); |
| 235 | + selection.addRange(range); |
173 | 236 | } |
174 | 237 |
|
175 | 238 | function clearSelection(editor: EditorView) { |
|
181 | 244 | } |
182 | 245 | } |
183 | 246 |
|
| 247 | + function setSelection(selection: Selection): void { |
| 248 | + if (!editor) return; |
| 249 | + editor.dispatch(editor.state.tr.setSelection(selection)); |
| 250 | + setTimeout(() => { |
| 251 | + if (!editor) return; |
| 252 | + if (editor.state.selection.eq(selection) || selection.$anchor.doc !== editor.state.doc) return; |
| 253 | + // when tabbing back and forth between 2 rich text editors, the previous setSelection is not sufficient 🤷 |
| 254 | + // (It doesn't have anything to do with the clearSelection() below) |
| 255 | + // The first call is kept, because it avoid a visible delay when possible |
| 256 | + editor.dispatch(editor.state.tr.setSelection(selection)); |
| 257 | + }, 1); // 0 is not enough on Firefox |
| 258 | + } |
| 259 | +
|
184 | 260 | //lcm expects line separators, but html does not render them, so we replace them with \n |
185 | 261 | function replaceNewLineWithLineSeparator(text: string) { |
186 | 262 | return text.replaceAll(newLine, lineSeparator); |
|
190 | 266 | } |
191 | 267 |
|
192 | 268 | function onFocusTargetClick(event: MouseEvent) { |
193 | | - if (!editor) return; |
194 | | - if (event.target === editor?.dom) return; // the editor will handle focus itself |
195 | | - editor.focus(); |
| 269 | + if (!editor || !event.target) return; |
| 270 | + if (event.target === editor.dom || event.target instanceof globalThis.Node && editor.dom.contains(event.target)) |
| 271 | + return; // the editor will handle focus itself |
| 272 | + editor?.focus(); |
| 273 | + // Minorly dissatisfying is that when you click on the padding to the right of prose-mirror, |
| 274 | + // the caret is not intelligently placed at the end of the text (and on the correct line if multi-line) |
| 275 | + // Everything else seems good |
196 | 276 | } |
197 | 277 | </script> |
198 | | -<style lang="postcss"> |
199 | | - :global(.ProseMirror) { |
| 278 | + |
| 279 | +<style lang="postcss" global> |
| 280 | + .ProseMirror { |
| 281 | + display: block; |
| 282 | + place-content: center; |
200 | 283 | flex-grow: 1; |
201 | 284 | outline: none; |
202 | 285 | cursor: text; |
203 | | - /*white-space must be here, if it's directly on span then it will crash with a null node error*/ |
| 286 | +
|
204 | 287 | white-space: pre-wrap; |
205 | 288 |
|
206 | 289 | :global(.customized) { |
|
210 | 293 | :global(.customized ~ .customized) { |
211 | 294 | margin-left: 2px; |
212 | 295 | } |
| 296 | +
|
| 297 | + &::before { |
| 298 | + /* Ensure baseline-alignment even works when empty */ |
| 299 | + content: '\200B'; |
| 300 | + } |
213 | 301 | } |
214 | 302 | </style> |
215 | 303 |
|
216 | 304 | {#if label} |
217 | | - <div {...rest}> |
218 | | - <Label onclick={onFocusTargetClick}>{label}</Label> |
| 305 | + <div class={className} {...rest}> |
| 306 | + <Label>{label}</Label> |
219 | 307 | <div bind:this={elementRef}></div> |
220 | 308 | </div> |
221 | 309 | {:else} |
222 | | - <div bind:this={elementRef} {...rest}></div> |
| 310 | + <div bind:this={elementRef} class={className} {...rest}></div> |
223 | 311 | {/if} |
0 commit comments