Skip to content

Commit 4c9e19a

Browse files
committed
fix(editor): run the context menu's cut and copy as the editor's own commands
Cut and Copy run as native commands on the refocused editor, like ⌘X and ⌘C: one synchronous step that copies every selection, cuts exactly what it copied, keeps the chat selection reference, and leaves focus in the editor. Copy all returns focus too.
1 parent 4d8b69c commit 4c9e19a

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/files/components/file-viewer

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,14 @@ export const TextEditor = memo(function TextEditor({
285285
return false
286286
}
287287

288-
/** Every selected passage, joined by line breaks as a multi-cursor copy is. */
289-
const selectedText = (): string => {
290-
const editor = editorRef.current
291-
if (!editor) return ''
292-
const text = editor.getText()
293-
return editor
294-
.getSelectionRanges()
295-
.filter(({ start, end }) => start < end)
296-
.map(({ start, end }) => text.slice(start, end))
297-
.join('\n')
288+
/**
289+
* Runs the menu's Cut or Copy as the native command on the focused editor, as ⌘X and ⌘C do:
290+
* the editor's own handler copies every selection and cuts in one step, and the copy carries
291+
* the chat selection reference.
292+
*/
293+
const runClipboardCommand = (command: 'cut' | 'copy') => {
294+
editorRef.current?.focus()
295+
document.execCommand(command)
298296
}
299297

300298
const handleEditorPasteCapture = (event: ReactClipboardEvent<HTMLDivElement>) => {
@@ -455,22 +453,16 @@ export const TextEditor = memo(function TextEditor({
455453
}}
456454
onCut={() => {
457455
closeContextMenu()
458-
const text = selectedText()
459-
if (!text) return
460-
/* Delete only once the text is safely on the clipboard. */
461-
navigator.clipboard
462-
.writeText(text)
463-
.then(() => editorRef.current?.replaceSelection(''))
464-
.catch(() => {})
456+
runClipboardCommand('cut')
465457
}}
466458
onCopy={() => {
467459
closeContextMenu()
468-
const text = selectedText()
469-
if (text) navigator.clipboard.writeText(text).catch(() => {})
460+
runClipboardCommand('copy')
470461
}}
471462
onCopyAll={() => {
472-
navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {})
473463
closeContextMenu()
464+
navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {})
465+
editorRef.current?.focus()
474466
}}
475467
onPaste={() => {
476468
closeContextMenu()

0 commit comments

Comments
 (0)