add inital attempt at cursor position preservation

This commit is contained in:
Chris Kruining 2025-02-13 17:15:22 +11:00
parent 67b76e41c5
commit 69fd9a1753
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
4 changed files with 43 additions and 495 deletions

View file

@ -1,6 +1,7 @@
import { createEffect, createMemo, createSignal } from 'solid-js';
import { createEffect, createMemo, createSignal, untrack } from 'solid-js';
import { decode } from '~/utilities';
import { debounce } from '@solid-primitives/scheduled';
import { createSelection } from '@solid-primitives/selection';
import { defaultChecker as spellChecker } from './spellChecker';
import { defaultChecker as grammarChecker } from './grammarChecker';
import css from './textarea.module.css';
@ -15,6 +16,7 @@ interface TextareaProps {
}
export function Textarea(props: TextareaProps) {
const [selection, setSelection] = createSelection();
const [value, setValue] = createSignal<string>(decode(props.value));
const [element, setElement] = createSignal<HTMLTextAreaElement>();
@ -22,6 +24,23 @@ export function Textarea(props: TextareaProps) {
setValue(decode(props.value));
});
createEffect(() => {
// For tracking
value();
const root = untrack(() => element());
const [el, start, end] = untrack(() => selection());
if (el !== root) {
return;
}
// TODO :: this needs to be calculated based on the modification done
const offset = 1;
setSelection([el, start + offset, end + offset]);
});
const resize = () => {
const el = element();
@ -43,7 +62,7 @@ export function Textarea(props: TextareaProps) {
e.stopPropagation();
e.preventDefault();
setValue(element()!.innerText.trim());
setValue(element()!.textContent!);
resize();
mutate();