Elffuss BraidHub

kikocis/continue public

main · 4.0 KiBRawPermalink
1import { Editor } from "@tiptap/react";
2import { KeyboardEvent } from "react";
3import { getPlatform, isWebEnvironment } from "../../../util";
4import { CodeBlock } from "../TipTapEditor/extensions";
5
6const isWebEnv = isWebEnvironment();
7
8/**
9 * This handles various keypress issues when OSR is enabled
10 */
11export const handleJetBrainsOSRMetaKeyIssues = (
12 e: KeyboardEvent,
13 editor: Editor,
14) => {
15 const selection = window.getSelection();
16 const alter = e.shiftKey ? "extend" : "move";
17 const platform = getPlatform();
18
19 const handlers: Record<string, () => void> = {
20 Backspace: () => handleJetBrainsMetaBackspace(editor),
21 ArrowLeft: () =>
22 selection?.modify(
23 alter,
24 "backward",
25 platform === "mac" ? "lineboundary" : "word",
26 ),
27 ArrowRight: () =>
28 selection?.modify(
29 alter,
30 "forward",
31 platform === "mac" ? "lineboundary" : "word",
32 ),
33 ArrowDown: () => selection?.modify(alter, "forward", "documentboundary"),
34 ArrowUp: () => {
35 selection?.modify(alter, "backward", "documentboundary");
36 },
37 };
38
39 if (e.key in handlers) {
40 e.stopPropagation();
41 e.preventDefault();
42 handlers[e.key]();
43 }
44};
45
46/**
47 * This handles reported issues with cut/copy/paste in .ipynb files in VSC
48 */
49export const handleVSCMetaKeyIssues = async (
50 e: KeyboardEvent,
51 editor: Editor,
52) => {
53 const text = editor.state.doc.textBetween(
54 editor.state.selection.from,
55 editor.state.selection.to,
56 );
57
58 const handlers: Record<string, () => Promise<void>> = {
59 x: () => handleCutOperation(text, editor),
60 c: () => handleCopyOperation(text),
61 v: () => handlePasteOperation(editor),
62 z: () => {
63 return e.shiftKey
64 ? handleRedoOperation(editor)
65 : handleUndoOperation(editor);
66 },
67 };
68
69 if (e.key in handlers) {
70 e.stopPropagation();
71 e.preventDefault();
72 await handlers[e.key]();
73 }
74};
75
76const deleteSingleWord = (editor: Editor) => {
77 const textContent =
78 editor.state.doc.resolve(editor.state.selection.from).parent.textContent ??
79 "";
80
81 const cursorPosition = editor.state.selection.from;
82 const nodeStartPosition = editor.state.doc.resolve(cursorPosition).start();
83
84 const textBeforeCursor = textContent.slice(
85 0,
86 cursorPosition - nodeStartPosition,
87 );
88
89 // Match the last word including any trailing whitespace
90 const lastWordMatch = textBeforeCursor.match(/\S+\s*$/);
91
92 if (lastWordMatch) {
93 const lastWordWithSpace = lastWordMatch[0];
94 editor.commands.deleteRange({
95 from: editor.state.selection.from - lastWordWithSpace.length,
96 to: editor.state.selection.from,
97 });
98 }
99};
100
101export const handleJetBrainsMetaBackspace = (editor: Editor) => {
102 const { doc } = editor.state;
103
104 for (let i = doc.content.childCount - 1; i >= 0; i--) {
105 const node = doc.content.child(i);
106
107 if (node.type.name === CodeBlock.name) {
108 continue;
109 }
110
111 // For Linux/Windows, only delete the word to the left of the cursor
112 const platform = getPlatform();
113 if (platform !== "mac") {
114 deleteSingleWord(editor);
115 break;
116 }
117
118 editor.commands.deleteNode(node.type.name);
119 }
120
121 // Add an empty string so the user can keep typing
122 editor.commands.createParagraphNear();
123};
124
125export const handleCutOperation = async (text: string, editor: Editor) => {
126 if (isWebEnv) {
127 await navigator.clipboard.writeText(text);
128 editor.commands.deleteSelection();
129 } else {
130 document.execCommand("cut");
131 }
132};
133
134export const handleCopyOperation = async (text: string) => {
135 if (isWebEnv) {
136 await navigator.clipboard.writeText(text);
137 } else {
138 document.execCommand("copy");
139 }
140};
141
142export const handlePasteOperation = async (editor: Editor) => {
143 if (isWebEnv) {
144 const clipboardText = await navigator.clipboard.readText();
145 editor.commands.insertContent(clipboardText);
146 } else {
147 document.execCommand("paste");
148 }
149};
150
151export const handleUndoOperation = async (editor: Editor) => {
152 editor.commands.undo();
153};
154
155export const handleRedoOperation = async (editor: Editor) => {
156 editor.commands.redo();
157};