Files
Sable/src/app/hooks/useTextAreaCodeEditor.ts
T
7w1 1717ca872f migrate to oxlint and oxfmt
ran formatter
fixed lint issues, primarily getting rid of "any"s and swapping to .toSorted()
2026-04-16 22:15:13 -05:00

46 lines
1.4 KiB
TypeScript

import type { KeyboardEventHandler, MutableRefObject } from 'react';
import { useMemo, useCallback } from 'react';
import { isKeyHotkey } from 'is-hotkey';
import { TextArea, Intent, TextAreaOperations, Cursor } from '$plugins/text-area';
import type { GetTarget } from '$plugins/text-area/type';
import { useTextAreaIntentHandler } from './useTextAreaIntent';
export const useTextAreaCodeEditor = (
textAreaRef: MutableRefObject<HTMLTextAreaElement | null>,
intentSpaceCount: number
) => {
const getTarget: GetTarget = useCallback(() => {
const target = textAreaRef.current;
if (!target) throw new Error('TextArea element not found!');
return target;
}, [textAreaRef]);
const { textArea, operations, intent } = useMemo(() => {
const ta = new TextArea(getTarget);
const op = new TextAreaOperations(getTarget);
return {
textArea: ta,
operations: op,
intent: new Intent(intentSpaceCount, ta, op),
};
}, [getTarget, intentSpaceCount]);
const intentHandler = useTextAreaIntentHandler(textArea, operations, intent);
const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (evt) => {
intentHandler(evt);
if (isKeyHotkey('escape', evt)) {
const cursor = Cursor.fromTextAreaElement(getTarget());
operations.deselect(cursor);
}
};
return {
handleKeyDown,
textArea,
intent,
getTarget,
operations,
};
};