Files
cinny/src/app/hooks/useTextAreaIntent.ts
T
Lotus Bot 23008670f3
CI / Build & Quality Checks (push) Successful in 10m13s
chore: upgrade i18next 26, prettier 3, fontsource-variable, domhandler 6, lint-staged 17
- 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
2026-05-21 23:30:50 -04:00

59 lines
1.8 KiB
TypeScript

import { isKeyHotkey } from 'is-hotkey';
import { KeyboardEventHandler, useCallback } from 'react';
import { Cursor, Intent, Operations, TextArea } from '../plugins/text-area';
export const useTextAreaIntentHandler = (
textArea: TextArea,
operations: Operations,
intent: Intent,
) => {
const handler: KeyboardEventHandler<HTMLTextAreaElement> = useCallback(
(evt) => {
const target = evt.currentTarget;
if (isKeyHotkey('tab', evt)) {
evt.preventDefault();
const cursor = Cursor.fromTextAreaElement(target);
if (textArea.selection(cursor)) {
operations.select(intent.moveForward(cursor));
} else {
operations.deselect(operations.insert(cursor, intent.str));
}
target.focus();
}
if (isKeyHotkey('shift+tab', evt)) {
evt.preventDefault();
const cursor = Cursor.fromTextAreaElement(target);
const intentCursor = intent.moveBackward(cursor);
if (textArea.selection(cursor)) {
operations.select(intentCursor);
} else {
operations.deselect(intentCursor);
}
target.focus();
}
if (isKeyHotkey('enter', evt) || isKeyHotkey('shift+enter', evt)) {
evt.preventDefault();
const cursor = Cursor.fromTextAreaElement(target);
operations.select(intent.addNewLine(cursor));
}
if (isKeyHotkey('mod+enter', evt)) {
evt.preventDefault();
const cursor = Cursor.fromTextAreaElement(target);
operations.select(intent.addNextLine(cursor));
}
if (isKeyHotkey('mod+shift+enter', evt)) {
evt.preventDefault();
const cursor = Cursor.fromTextAreaElement(target);
operations.select(intent.addPreviousLine(cursor));
}
},
[textArea, operations, intent],
);
return handler;
};