@@ -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