mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-02-13 16:13:24 +01:00
add text area intent handler hook
This commit is contained in:
parent
6b75bc82b0
commit
81738d4f5d
1 changed files with 53 additions and 0 deletions
53
src/app/hooks/useTextAreaIntent.ts
Normal file
53
src/app/hooks/useTextAreaIntent.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
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.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;
|
||||
};
|
Loading…
Reference in a new issue