|
34 | 34 | <script lang="ts"> |
35 | 35 | import type {IRichString} from '$lib/dotnet-types/generated-types/MiniLcm/Models/IRichString'; |
36 | 36 | import {Label} from '$lib/components/ui/label'; |
37 | | - import InputShell from '../ui/input/input-shell.svelte'; |
38 | 37 | import {EditorView} from 'prosemirror-view'; |
39 | | - import {AllSelection, EditorState, TextSelection} from 'prosemirror-state'; |
| 38 | + import {AllSelection, EditorState} from 'prosemirror-state'; |
40 | 39 | import {keymap} from 'prosemirror-keymap'; |
41 | 40 | import {baseKeymap} from 'prosemirror-commands'; |
42 | 41 | import {undo, redo, history} from 'prosemirror-history'; |
43 | 42 | import {onDestroy, onMount} from 'svelte'; |
44 | 43 | import {watch} from 'runed'; |
45 | 44 | import type {HTMLAttributes} from 'svelte/elements'; |
46 | | - import {IsUsingKeyboard, mergeProps} from 'bits-ui'; |
| 45 | + import {IsUsingKeyboard} from 'bits-ui'; |
47 | 46 | import type {IRichSpan} from '$lib/dotnet-types/generated-types/MiniLcm/Models/IRichSpan'; |
| 47 | + import {inputVariants} from '../ui/input/input.svelte'; |
| 48 | + import {on} from 'svelte/events'; |
48 | 49 |
|
49 | 50 | let { |
50 | 51 | value = $bindable(), |
|
83 | 84 | } |
84 | 85 | editor.updateState(newState); |
85 | 86 | }, |
| 87 | + attributes: { |
| 88 | + class: inputVariants({class: 'min-h-10 h-auto block'}), |
| 89 | + }, |
86 | 90 | editable() { |
87 | 91 | return !readonly; |
88 | 92 | }, |
|
92 | 96 | } |
93 | 97 | }); |
94 | 98 | editor.dom.setAttribute('tabindex', '0'); |
| 99 | +
|
| 100 | + const parentLabel = elementRef?.closest('label'); |
| 101 | + if (parentLabel) return on(parentLabel, 'click', onFocusTargetClick); |
95 | 102 | }); |
96 | 103 |
|
97 | 104 | function onfocus(editor: EditorView) { |
|
174 | 181 | if (selection && editor.dom.contains(selection.anchorNode) && editor.dom.contains(selection.focusNode)) { |
175 | 182 | selection.removeAllRanges(); |
176 | 183 | } |
177 | | - editor.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 0))); |
178 | 184 | } |
179 | 185 |
|
180 | 186 | //lcm expects line separators, but html does not render them, so we replace them with \n |
|
184 | 190 | function replaceLineSeparatorWithNewLine(text: string) { |
185 | 191 | return text.replaceAll(lineSeparator, newLine); |
186 | 192 | } |
| 193 | +
|
| 194 | + function onFocusTargetClick(event: MouseEvent) { |
| 195 | + if (!editor) return; |
| 196 | + if (event.target === editor?.dom) return; // the editor will handle focus itself |
| 197 | + editor.focus(); |
| 198 | + } |
187 | 199 | </script> |
188 | 200 | <style> |
189 | 201 | :global(.ProseMirror) { |
|
201 | 213 |
|
202 | 214 | {#if label} |
203 | 215 | <div {...rest}> |
204 | | - <Label>{label}</Label> |
205 | | - <InputShell {autofocus} class="p-2 h-auto" bind:ref={elementRef}/> |
| 216 | + <Label onclick={onFocusTargetClick}>{label}</Label> |
| 217 | + <div bind:this={elementRef}></div> |
206 | 218 | </div> |
207 | 219 | {:else} |
208 | | - <InputShell {autofocus} {...mergeProps({ class: 'p-2 h-auto'}, rest)} bind:ref={elementRef}/> |
| 220 | + <div bind:this={elementRef}></div> |
209 | 221 | {/if} |
0 commit comments