Skip to content

Commit 8e2c3f1

Browse files
committed
更新:下載插件程式碼
1. 添加了下載已建立程式碼的功能 2. 重新組織部分零散程式 3. 修正建立程式碼時混入額外字符的錯誤
1 parent a6c0de5 commit 8e2c3f1

2 files changed

Lines changed: 62 additions & 31 deletions

File tree

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@
360360
</head>
361361

362362
<body>
363-
<button type="button" id="build_result_button" onclick="setBuildResult()">建立</button>
363+
<button type="button" id="build_result_button" onclick="Builder.setBuildResult()">建立</button>
364364
<div id="root">
365365
<div id="root_meta" class="root">
366366
<div class="root_title" onclick="onRootTitleClick('meta')">
@@ -726,10 +726,11 @@
726726
</div>
727727
<!-- plugin display -->
728728
<div id="display_plugin" class="block" style="display: block;">
729-
<button type="button" id="copy_result_button" onclick="setCopyToClipBoard()">複製到剪貼簿</button>
730-
<span style="font-size: small; color: #0005;">
729+
<span style="font-size: small; color: #0005; display: block;">
731730
請注意,如果你在此編輯器內修改了任何內容,再次建立插件時將被覆蓋
732731
</span>
732+
<button type="button" id="copy_result_button" onclick="Builder.setCopyToClipBoard()">複製到剪貼簿</button>
733+
<button type="button" id="copy_result_button" onclick="Builder.onDownloadClick()">下載為程式檔</button>
733734
<textarea placeholder="Your plugin" hidden></textarea>
734735
<div id="monaco_build_display" style="height: 1000px;"></div>
735736
</div>

src/index.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -986,44 +986,74 @@ class CommandBuilder {
986986
}
987987
}
988988

989+
class Builder {
990+
static useDownloadFile(data) {
991+
const file = new Blob([data], { type: "text/javascript" })
989992

993+
if (window?.navigator?.msSaveOrOpenBlob)
994+
return void window.navigator.msSaveOrOpenBlob(file, "plugin.js")
990995

991-
const build = () => {
992-
const [commandsNote, commandsCode] = CommandBuilder.build()
993-
const code = commandsCode.length > 0
994-
? `\n(()=>{
996+
const a = document.createElement("a")
997+
const url = URL.createObjectURL(file)
998+
999+
a.href = url
1000+
a.download = "plugin.js"
1001+
a.style.display = "none"
1002+
1003+
document.body.appendChild(a)
1004+
a.click()
1005+
1006+
setTimeout(() => {
1007+
document.body.removeChild(a)
1008+
window.URL.revokeObjectURL(url)
1009+
}, 0)
1010+
}
1011+
1012+
static onDownloadClick() {
1013+
if (!EDITOR_RESULT) return
1014+
1015+
const code = EDITOR_RESULT.getValue()
1016+
Builder.useDownloadFile(code)
1017+
}
1018+
1019+
static build() {
1020+
const [commandsNote, commandsCode] = CommandBuilder.build()
1021+
const code = commandsCode.length > 0
1022+
? `\n(()=>{
9951023
const filename = document.currentScript.src.split("/").pop().replace(".js", "")
9961024
9971025
${commandsCode}
9981026
})()`
999-
: ""
1000-
1001-
return "/**:"
1002-
+ [
1003-
["info", BasicInfoBuilder.build()],
1004-
["params", ParameterBuilder.build()],
1005-
["command", commandsNote.length > 0 ? commandsNote : false],
1006-
]
1007-
.filter(([_, v]) => v !== false)
1008-
.join("")
1009-
+ "\n */"
1010-
+ code
1011-
+ "\n"
1012-
}
1027+
: false
1028+
1029+
return "/**:"
1030+
+ [
1031+
BasicInfoBuilder.build(),
1032+
ParameterBuilder.build(),
1033+
commandsNote,
1034+
]
1035+
.filter(([_, v]) => v !== false)
1036+
.join("")
1037+
+ "\n */"
1038+
+ code
1039+
+ "\n"
1040+
}
10131041

1014-
const setBuildResult = () => {
1015-
if (!EL_RESULT) return
1042+
static setBuildResult() {
1043+
if (!EL_RESULT) return
10161044

1017-
const res = build()
1018-
EDITOR_RESULT.setValue(res)
1019-
}
1045+
const res = Builder.build()
1046+
EDITOR_RESULT.setValue(res)
1047+
}
10201048

1021-
const setCopyToClipBoard = () => {
1022-
if (!EDITOR_RESULT) return
1049+
static setCopyToClipBoard() {
1050+
if (!EDITOR_RESULT) return
1051+
1052+
navigator.clipboard.writeText(EDITOR_RESULT.getValue())
1053+
.then(() => console.log("ok"))
1054+
.catch(console.error)
1055+
}
10231056

1024-
navigator.clipboard.writeText(EDITOR_RESULT.getValue())
1025-
.then(() => console.log("ok"))
1026-
.catch(console.error)
10271057
}
10281058

10291059

0 commit comments

Comments
 (0)