61a1f008d0
- i18next 23->26 + react-i18next 15->17 - prettier 2->3, reformat all files - replace @fontsource/inter with @fontsource-variable/inter 5, update import path - domhandler 5->6 (aligns with transitive deps) - lint-staged 16->17
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { useMemo, useCallback, KeyboardEventHandler, MutableRefObject } from 'react';
|
|
import { isKeyHotkey } from 'is-hotkey';
|
|
import { TextArea, Intent, TextAreaOperations, Cursor } from '../plugins/text-area';
|
|
import { useTextAreaIntentHandler } from './useTextAreaIntent';
|
|
import { GetTarget } from '../plugins/text-area/type';
|
|
|
|
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,
|
|
};
|
|
};
|