1+ /* eslint-disable no-shadow */
12import { Script , ScriptDAO } from "@App/app/repo/scripts" ;
23import CodeEditor from "@App/pages/components/CodeEditor" ;
3- import React , { useEffect , useRef , useState } from "react" ;
4+ import React , { useCallback , useEffect , useState } from "react" ;
45import { useNavigate , useParams , useSearchParams } from "react-router-dom" ;
56import { editor , KeyCode , KeyMod } from "monaco-editor" ;
67import {
@@ -34,10 +35,9 @@ import { lazyScriptName } from "@App/pkg/config/config";
3435const { Row } = Grid ;
3536const { Col } = Grid ;
3637
37- // 声明一个Map存储Script
38- const ScriptMap = new Map ( ) ;
39-
4038type HotKey = {
39+ id : string ;
40+ title : string ;
4141 hotKey : number ;
4242 action : ( script : Script , codeEditor : editor . IStandaloneCodeEditor ) => void ;
4343} ;
@@ -49,49 +49,67 @@ const Editor: React.FC<{
4949 callbackEditor : ( e : editor . IStandaloneCodeEditor ) => void ;
5050 onChange : ( code : string ) => void ;
5151} > = ( { id, script, hotKeys, callbackEditor, onChange } ) => {
52- const [ init , setInit ] = useState ( false ) ;
53- const codeEditor = useRef < { editor : editor . IStandaloneCodeEditor } > ( null ) ;
54- // Script.uuid为key,Script为value,储存Script
55- ScriptMap . has ( script . uuid ) || ScriptMap . set ( script . uuid , script ) ;
52+ const [ node , setNode ] = useState < { editor : editor . IStandaloneCodeEditor } > ( ) ;
53+ const ref = useCallback < ( node : { editor : editor . IStandaloneCodeEditor } ) => void > (
54+ ( inlineNode ) => {
55+ if ( inlineNode && inlineNode . editor && ! node ) {
56+ setNode ( inlineNode ) ;
57+ }
58+ } ,
59+ [ node ]
60+ ) ;
61+
5662 useEffect ( ( ) => {
57- if ( ! codeEditor . current || ! codeEditor . current . editor ) {
58- setTimeout ( ( ) => {
59- setInit ( true ) ;
60- } , 200 ) ;
61- return ( ) => { } ;
63+ if ( ! node || ! node . editor ) {
64+ return ;
6265 }
63- // 初始化editor时将Script的uuid绑定到editor上
6466 // @ts -ignore
65- if ( ! codeEditor . current . editor . uuid ) {
67+ if ( ! node . editor . uuid ) {
6668 // @ts -ignore
67- codeEditor . current . editor . uuid = script . uuid ;
69+ node . editor . uuid = script . uuid ;
6870 }
6971 hotKeys . forEach ( ( item ) => {
70- codeEditor . current ?. editor . addCommand ( item . hotKey , ( ) => {
71- // 获取当前激活的editor(通过editor._focusTracker._hasFocus判断editor激活状态 可能有更好的方法)
72- const activeEditor = editor
73- . getEditors ( )
74- // @ts -ignore
75- // eslint-disable-next-line no-underscore-dangle
76- . find ( ( i ) => i . _focusTracker . _hasFocus ) ;
72+ /*
73+
74+
75+ hotKeys.forEach((item) => {
76+ codeEditor.current?.editor.addCommand(item.hotKey, () => {
77+ // 获取当前激活的editor(通过editor._focusTracker._hasFocus判断editor激活状态 可能有更好的方法)
78+ const activeEditor = editor
79+ .getEditors()
80+ // @ts -ignore
81+ // eslint-disable-next-line no-underscore-dangle
82+ .find((i) => i._focusTracker._hasFocus);
7783
78- // 仅在获取到激活的editor时,通过editor上绑定的uuid获取Script,并指定激活的editor执行快捷键action
79- activeEditor &&
84+ // 仅在获取到激活的editor时,通过editor上绑定的uuid获取Script,并指定激活的editor执行快捷键action
85+ activeEditor &&
86+ // @ts -ignore
87+ item.action(ScriptMap.get(activeEditor.uuid), activeEditor);
88+ });
89+ });
90+ */
91+ node . editor . addAction ( {
92+ id : item . id ,
93+ label : item . title ,
94+ keybindings : [ item . hotKey ] ,
95+ run ( editor ) {
8096 // @ts -ignore
81- item . action ( ScriptMap . get ( activeEditor . uuid ) , activeEditor ) ;
97+ item . action ( script , editor ) ;
98+ } ,
8299 } ) ;
83100 } ) ;
84- codeEditor . current . editor . onKeyUp ( ( ) => {
85- onChange ( codeEditor . current ? .editor . getValue ( ) || "" ) ;
101+ node . editor . onKeyUp ( ( ) => {
102+ onChange ( node . editor . getValue ( ) || "" ) ;
86103 } ) ;
87- callbackEditor ( codeEditor . current . editor ) ;
88- return ( ) => { } ;
89- } , [ init ] ) ;
104+ callbackEditor ( node . editor ) ;
105+ return node . editor . dispose . bind ( node . editor ) ;
106+ } , [ node ?. editor ] ) ;
90107
91108 return (
92109 < CodeEditor
110+ key = { id }
93111 id = { id }
94- ref = { codeEditor }
112+ ref = { ref }
95113 code = { script . code }
96114 diffCode = ""
97115 editable
@@ -104,6 +122,7 @@ type EditorMenu = {
104122 tooltip ?: string ;
105123 action ?: ( script : Script , e : editor . IStandaloneCodeEditor ) => void ;
106124 items ?: {
125+ id : string ;
107126 title : string ;
108127 tooltip ?: string ;
109128 hotKey ?: number ;
@@ -137,7 +156,7 @@ const emptyScript = async (template: string, hotKeys: any, target?: string) => {
137156 chrome . storage . local . remove ( [ "activeTabUrl" ] ) ;
138157 } else {
139158 chrome . storage . local . remove ( [ "activeTabUrl" ] ) ;
140- const pageUrl = result ?. activeTabUrl ?. url ;
159+ const pageUrl = ( result ?. activeTabUrl as any ) ?. url ;
141160 if ( pageUrl ) {
142161 try {
143162 const { protocol, pathname, hostname } = new URL ( pageUrl ) ;
@@ -321,12 +340,14 @@ function ScriptEditor() {
321340 title : t ( "file" ) ,
322341 items : [
323342 {
343+ id : "save" ,
324344 title : t ( "save" ) ,
325345 hotKey : KeyMod . CtrlCmd | KeyCode . KeyS ,
326346 hotKeyString : "Ctrl+S" ,
327347 action : save ,
328348 } ,
329349 {
350+ id : "saveAs" ,
330351 title : t ( "save_as" ) ,
331352 hotKey : KeyMod . CtrlCmd | KeyMod . Shift | KeyCode . KeyS ,
332353 hotKeyString : "Ctrl+Shift+S" ,
@@ -338,7 +359,8 @@ function ScriptEditor() {
338359 title : t ( "run" ) ,
339360 items : [
340361 {
341- title : t ( "debug" ) ,
362+ id : "run" ,
363+ title : t ( "run" ) ,
342364 hotKey : KeyMod . CtrlCmd | KeyCode . F5 ,
343365 hotKeyString : "Ctrl+F5" ,
344366 tooltip : t ( "only_background_scheduled_can_run" ) ,
@@ -375,6 +397,7 @@ function ScriptEditor() {
375397 title : t ( "tools" ) ,
376398 items : [
377399 {
400+ id : "scriptStorage" ,
378401 title : t ( "script_storage" ) ,
379402 tooltip : t ( "script_storage_tooltip" ) ,
380403 action ( script ) {
@@ -384,6 +407,7 @@ function ScriptEditor() {
384407 } ,
385408 } ,
386409 {
410+ id : "scriptResource" ,
387411 title : t ( "script_resource" ) ,
388412 tooltip : t ( "script_resource_tooltip" ) ,
389413 action ( script ) {
@@ -417,6 +441,8 @@ function ScriptEditor() {
417441 item . items . forEach ( ( menuItem ) => {
418442 if ( menuItem . hotKey ) {
419443 hotKeys . push ( {
444+ id : menuItem . id ,
445+ title : menuItem . title ,
420446 hotKey : menuItem . hotKey ,
421447 action : menuItem . action ,
422448 } ) ;
0 commit comments