Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions apps/web/src/components/ComposerPromptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -894,20 +894,16 @@ interface ComposerPromptEditorProps {
editorRef: React.RefObject<ComposerPromptEditorHandle | null>;
}

function ComposerCommandKeyPlugin(props: {
onCommandKeyDown?: (
key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
event: KeyboardEvent,
) => boolean;
}) {
const [editor] = useLexicalComposerContext();

useEffect(() => {
const handleCommand = (
key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
event: KeyboardEvent | null,
): boolean => {
if (!props.onCommandKeyDown || !event) {
type ComposerCommandKey = "ArrowDown" | "ArrowUp" | "Enter" | "Tab";
type ComposerCommandKeyHandler = (key: ComposerCommandKey, event: KeyboardEvent) => boolean;

function useComposerCommandKeyBindings(
editor: ReturnType<typeof useLexicalComposerContext>[0],
onCommandKeyDown: ComposerCommandKeyHandler | undefined,
) {
const handleCommandKeyDown = useEffectEvent(
(key: ComposerCommandKey, event: KeyboardEvent | null): boolean => {
if (!onCommandKeyDown || !event) {
return false;
}

Expand All @@ -916,32 +912,34 @@ function ComposerCommandKeyPlugin(props: {
return true;
}

const handled = props.onCommandKeyDown(key, event);
const handled = onCommandKeyDown(key, event);
if (handled) {
event.preventDefault();
event.stopPropagation();
}
return handled;
};
},
);

useEffect(() => {
const unregisterArrowDown = editor.registerCommand(
KEY_ARROW_DOWN_COMMAND,
(event) => handleCommand("ArrowDown", event),
(event) => handleCommandKeyDown("ArrowDown", event),
COMMAND_PRIORITY_HIGH,
);
const unregisterArrowUp = editor.registerCommand(
KEY_ARROW_UP_COMMAND,
(event) => handleCommand("ArrowUp", event),
(event) => handleCommandKeyDown("ArrowUp", event),
COMMAND_PRIORITY_HIGH,
);
const unregisterEnter = editor.registerCommand(
KEY_ENTER_COMMAND,
(event) => handleCommand("Enter", event),
(event) => handleCommandKeyDown("Enter", event),
COMMAND_PRIORITY_HIGH,
);
const unregisterTab = editor.registerCommand(
KEY_TAB_COMMAND,
(event) => handleCommand("Tab", event),
(event) => handleCommandKeyDown("Tab", event),
COMMAND_PRIORITY_HIGH,
);

Expand All @@ -951,9 +949,7 @@ function ComposerCommandKeyPlugin(props: {
unregisterEnter();
unregisterTab();
};
}, [editor, props]);

return null;
}, [editor, handleCommandKeyDown]);
}

function ComposerInlineTokenArrowPlugin() {
Expand Down Expand Up @@ -1398,6 +1394,7 @@ function ComposerPromptEditorInner({
editorRef,
}: ComposerPromptEditorProps) {
const [editor] = useLexicalComposerContext();
useComposerCommandKeyBindings(editor, onCommandKeyDown);
const onChangeRef = useRef(onChange);
const initialCursor = clampCollapsedComposerCursor(value, cursor);
const terminalContextsSignature = terminalContextSignature(terminalContexts);
Expand Down Expand Up @@ -1629,7 +1626,6 @@ function ComposerPromptEditorInner({
ErrorBoundary={LexicalErrorBoundary}
/>
<OnChangePlugin onChange={handleEditorChange} />
<ComposerCommandKeyPlugin {...(onCommandKeyDown ? { onCommandKeyDown } : {})} />
<ComposerSurroundSelectionPlugin terminalContexts={terminalContexts} skills={skills} />
<ComposerInlineTokenArrowPlugin />
<ComposerInlineTokenSelectionNormalizePlugin />
Expand Down
Loading