Skip to content

Commit

Permalink
新增脚本编辑器脚本另存为功能
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamNya committed May 4, 2023
1 parent 2a02ecb commit 7790137
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/pages/options/routes/script/ScriptEditor.tsx
Expand Up @@ -240,6 +240,27 @@ function ScriptEditor() {
});
});
};
const saveAs = (script: Script, e: editor.IStandaloneCodeEditor) => {
return new Promise<void>((resolve) => {
try {
chrome.downloads.download(
{
url: URL.createObjectURL(
new Blob([e.getValue()], { type: "text/javascript" })
),
saveAs: true, // true直接弹出对话框;false弹出下载选项
filename: `${script.name}.user.js`,
},
() => {
Message.success("另存为成功");
resolve();
}
);
} catch (err) {
Message.error(`另存为失败: ${err}`);
}
});
};
const menu: EditorMenu[] = [
{
title: "文件",
Expand All @@ -249,6 +270,11 @@ function ScriptEditor() {
hotKey: KeyMod.CtrlCmd | KeyCode.KeyS,
action: save,
},
{
title: "另存为",
hotKey: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyS,
action: saveAs,
},
],
},
{
Expand Down Expand Up @@ -557,6 +583,10 @@ function ScriptEditor() {
{item.items.map((menuItem, i) => {
const btn = (
<Button
style={{
width: "100%",
textAlign: "left",
}}
key={`sm_${menuItem.title}`}
size="mini"
onClick={() => {
Expand Down

0 comments on commit 7790137

Please sign in to comment.