|
1 | 1 | import { Editor } from "@tiptap/react";
|
2 | 2 | import { KeyboardEvent } from "react";
|
3 |
| -import { isWebEnvironment } from "../../util"; |
| 3 | +import { getPlatform, isWebEnvironment } from "../../util"; |
4 | 4 |
|
5 | 5 | const isWebEnv = isWebEnvironment();
|
| 6 | +const platform = getPlatform(); |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * This handles various keypress issues when OSR is enabled
|
@@ -56,15 +57,48 @@ export const handleVSCMetaKeyIssues = async (
|
56 | 57 | }
|
57 | 58 | };
|
58 | 59 |
|
| 60 | +const deleteSingleWord = (editor: Editor) => { |
| 61 | + const textContent = |
| 62 | + editor.state.doc.resolve(editor.state.selection.from).parent.textContent ?? |
| 63 | + ""; |
| 64 | + |
| 65 | + const cursorPosition = editor.state.selection.from; |
| 66 | + const nodeStartPosition = editor.state.doc.resolve(cursorPosition).start(); |
| 67 | + |
| 68 | + const textBeforeCursor = textContent.slice( |
| 69 | + 0, |
| 70 | + cursorPosition - nodeStartPosition, |
| 71 | + ); |
| 72 | + |
| 73 | + // Match the last word including any trailing whitespace |
| 74 | + const lastWordMatch = textBeforeCursor.match(/\S+\s*$/); |
| 75 | + |
| 76 | + if (lastWordMatch) { |
| 77 | + const lastWordWithSpace = lastWordMatch[0]; |
| 78 | + editor.commands.deleteRange({ |
| 79 | + from: editor.state.selection.from - lastWordWithSpace.length, |
| 80 | + to: editor.state.selection.from, |
| 81 | + }); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
59 | 85 | export const handleJetBrainsMetaBackspace = (editor: Editor) => {
|
60 | 86 | const { doc } = editor.state;
|
61 | 87 |
|
62 | 88 | for (let i = doc.content.childCount - 1; i >= 0; i--) {
|
63 | 89 | const node = doc.content.child(i);
|
64 | 90 |
|
65 |
| - if (node.type.name !== "codeBlock") { |
66 |
| - editor.commands.deleteNode(node.type.name); |
| 91 | + if (node.type.name === "codeBlock") { |
| 92 | + continue; |
67 | 93 | }
|
| 94 | + |
| 95 | + // For Linux/Windows, only delete the word to the left of the cursor |
| 96 | + if (platform !== "mac") { |
| 97 | + deleteSingleWord(editor); |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + editor.commands.deleteNode(node.type.name); |
68 | 102 | }
|
69 | 103 |
|
70 | 104 | // Add an empty string so the user can keep typing
|
|
0 commit comments