From 03d7a673ed80f0f8ad12071c2ba64f3348a4f65f Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 5 Dec 2024 18:30:15 -0800 Subject: [PATCH] bugfix: delete single word on linux/windows --- .../mainInput/handleMetaKeyIssues.ts | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/gui/src/components/mainInput/handleMetaKeyIssues.ts b/gui/src/components/mainInput/handleMetaKeyIssues.ts index 77cbbb4dce..867203a14d 100644 --- a/gui/src/components/mainInput/handleMetaKeyIssues.ts +++ b/gui/src/components/mainInput/handleMetaKeyIssues.ts @@ -1,8 +1,9 @@ import { Editor } from "@tiptap/react"; import { KeyboardEvent } from "react"; -import { isWebEnvironment } from "../../util"; +import { getPlatform, isWebEnvironment } from "../../util"; const isWebEnv = isWebEnvironment(); +const platform = getPlatform(); /** * This handles various keypress issues when OSR is enabled @@ -56,15 +57,48 @@ export const handleVSCMetaKeyIssues = async ( } }; +const deleteSingleWord = (editor: Editor) => { + const textContent = + editor.state.doc.resolve(editor.state.selection.from).parent.textContent ?? + ""; + + const cursorPosition = editor.state.selection.from; + const nodeStartPosition = editor.state.doc.resolve(cursorPosition).start(); + + const textBeforeCursor = textContent.slice( + 0, + cursorPosition - nodeStartPosition, + ); + + // Match the last word including any trailing whitespace + const lastWordMatch = textBeforeCursor.match(/\S+\s*$/); + + if (lastWordMatch) { + const lastWordWithSpace = lastWordMatch[0]; + editor.commands.deleteRange({ + from: editor.state.selection.from - lastWordWithSpace.length, + to: editor.state.selection.from, + }); + } +}; + export const handleJetBrainsMetaBackspace = (editor: Editor) => { const { doc } = editor.state; for (let i = doc.content.childCount - 1; i >= 0; i--) { const node = doc.content.child(i); - if (node.type.name !== "codeBlock") { - editor.commands.deleteNode(node.type.name); + if (node.type.name === "codeBlock") { + continue; } + + // For Linux/Windows, only delete the word to the left of the cursor + if (platform !== "mac") { + deleteSingleWord(editor); + break; + } + + editor.commands.deleteNode(node.type.name); } // Add an empty string so the user can keep typing