Skip to content
Open
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
99 changes: 94 additions & 5 deletions src/pages/options/routes/script/ScriptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,95 @@ function ScriptEditor() {
setTimeout(editor.focus.bind(editor), delayMs);
}
};
const getSelectedText = (editor: editor.ICodeEditor) => {
const model = editor.getModel();
if (!model) return "";

const selections = editor.getSelections()?.filter((selection) => !selection.isEmpty()) || [];
return selections.map((selection) => model.getValueInRange(selection)).join(model.getEOL());
};
const writeClipboardText = async (text: string) => {
if (navigator.clipboard?.writeText) {
try {
await navigator.clipboard.writeText(text);
return;
} catch {
// Fall through to execCommand fallback below.
}
}

const textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.position = "fixed";
textarea.style.left = "-9999px";
textarea.style.top = "-9999px";
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const ok = document.execCommand("copy");
textarea.remove();
if (!ok) throw new Error("copy failed");
};
const copyEditorSelection = (editor: editor.ICodeEditor) => {
const text = getSelectedText(editor);
if (!text) return;
writeClipboardText(text)
.catch((err) => {
LoggerCore.logger(Logger.E(err)).debug("copy editor selection error");
})
.finally(() => {
editor.focus();
});
};
const cutEditorSelection = (editor: editor.ICodeEditor) => {
const text = getSelectedText(editor);
const selections = editor.getSelections()?.filter((selection) => !selection.isEmpty()) || [];
if (!text || !selections.length) return;
writeClipboardText(text)
.then(() => {
editor.pushUndoStop();
editor.executeEdits(
"menu",
selections.map((selection) => ({ range: selection, text: "" }))
);
editor.pushUndoStop();
})
.catch((err) => {
LoggerCore.logger(Logger.E(err)).debug("cut editor selection error");
})
.finally(() => {
editor.focus();
});
};
const pasteEditorClipboard = (editor: editor.ICodeEditor) => {
if (!navigator.clipboard?.readText) {
editor.focus();
editor.getAction("editor.action.clipboardPasteAction")?.run();
return;
}
navigator.clipboard
.readText()
.then((text) => {
if (!text) return;
editor.focus();
editor.trigger("keyboard", "paste", {
text,
pasteOnNewLine: false,
multicursorText: null,
mode: null,
});
})
.catch((err) => {
LoggerCore.logger(Logger.E(err)).debug("paste editor clipboard error");
editor.focus();
editor.getAction("editor.action.clipboardPasteAction")?.run();
});
};
const triggerEditorCommand = (editor: editor.ICodeEditor, handlerId: string) => {
editor.focus();
editor.trigger("menu", handlerId, null);
requestAnimationFrame(() => editor.focus());
};
const [scriptList, setScriptList] = useState<Script[]>([]);
const [currentScript, setCurrentScript] = useState<Script>();
const [selectedScript, setSelectSciptButtonAndTab] = useState<string>("");
Expand Down Expand Up @@ -527,15 +616,15 @@ function ScriptEditor() {
title: t("undo"),
hotKeyString: "Ctrl+Z",
action(_script, e) {
e.trigger("menu", "undo", null);
triggerEditorCommand(e, "undo");
},
},
{
id: "redo",
title: t("redo"),
hotKeyString: "Ctrl+Shift+Z",
action(_script, e) {
e.trigger("menu", "redo", null);
triggerEditorCommand(e, "redo");
},
},
{ divider: true },
Expand All @@ -544,23 +633,23 @@ function ScriptEditor() {
title: t("cut"),
hotKeyString: "Ctrl+X",
action(_script, e) {
e.trigger("menu", "editor.action.clipboardCutAction", null);
cutEditorSelection(e);
},
},
{
id: "copy",
title: t("copy"),
hotKeyString: "Ctrl+C",
action(_script, e) {
e.trigger("menu", "editor.action.clipboardCopyAction", null);
copyEditorSelection(e);
},
},
{
id: "paste",
title: t("paste"),
hotKeyString: "Ctrl+V",
action(_script, e) {
e.trigger("menu", "editor.action.clipboardPasteAction", null);
pasteEditorClipboard(e);
},
},
{ divider: true },
Expand Down
Loading