22import query from "./query.js" ;
33import ls from "./local_storage.js" ;
44
5- const STYLE_OPTIONS = {
6- null : {
7- showLogo : null ,
8- showFileMenu : null ,
9- showHelpMenu : null ,
10- showSelectLanguage : null ,
11- showCompilerOptions : null ,
12- showCommandLineArguments : null ,
13- showRunButton : null ,
14- showThemeButton : null ,
15- showPuterSignInOutButton : null ,
16- showStatusLine : null ,
17- showCopyright : null
18- } ,
5+ const DEFAULT_CONFIGURATIONS = {
196 default : {
20- showLogo : true ,
21- showFileMenu : true ,
22- showHelpMenu : true ,
23- showSelectLanguage : true ,
24- showCompilerOptions : true ,
25- showCommandLineArguments : true ,
26- showRunButton : true ,
27- showThemeButton : true ,
28- showPuterSignInOutButton : true ,
29- showStatusLine : true ,
30- showCopyright : true
7+ theme : "system" ,
8+ style : "default" ,
9+ styleOptions : {
10+ showLogo : true ,
11+ showFileMenu : true ,
12+ showHelpMenu : true ,
13+ showSelectLanguage : true ,
14+ showCompilerOptions : true ,
15+ showCommandLineArguments : true ,
16+ showRunButton : true ,
17+ showThemeButton : true ,
18+ showPuterSignInOutButton : true ,
19+ showStatusLine : true ,
20+ showCopyright : true
21+ } ,
22+ appOptions : {
23+ showAIAssistant : true
24+ }
3125 } ,
3226 minimal : {
33- showLogo : false ,
34- showFileMenu : false ,
35- showHelpMenu : false ,
36- showSelectLanguage : true ,
37- showCompilerOptions : false ,
38- showCommandLineArguments : false ,
39- showRunButton : true ,
40- showThemeButton : false ,
41- showPuterSignInOutButton : false ,
42- showStatusLine : false ,
43- showCopyright : false
27+ theme : "system" ,
28+ style : "minimal" ,
29+ styleOptions : {
30+ showLogo : false ,
31+ showFileMenu : false ,
32+ showHelpMenu : false ,
33+ showSelectLanguage : true ,
34+ showCompilerOptions : false ,
35+ showCommandLineArguments : false ,
36+ showRunButton : true ,
37+ showThemeButton : false ,
38+ showPuterSignInOutButton : false ,
39+ showStatusLine : false ,
40+ showCopyright : false
41+ } ,
42+ appOptions : {
43+ showAIAssistant : false
44+ }
4445 } ,
4546 standalone : {
46- showLogo : false ,
47- showFileMenu : true ,
48- showHelpMenu : true ,
49- showSelectLanguage : true ,
50- showCompilerOptions : true ,
51- showCommandLineArguments : true ,
52- showRunButton : true ,
53- showThemeButton : true ,
54- showPuterSignInOutButton : true ,
55- showStatusLine : true ,
56- showCopyright : false
47+ theme : "system" ,
48+ style : "standalone" ,
49+ styleOptions : {
50+ showLogo : false ,
51+ showFileMenu : true ,
52+ showHelpMenu : true ,
53+ showSelectLanguage : true ,
54+ showCompilerOptions : true ,
55+ showCommandLineArguments : true ,
56+ showRunButton : true ,
57+ showThemeButton : true ,
58+ showPuterSignInOutButton : true ,
59+ showStatusLine : true ,
60+ showCopyright : false
61+ } ,
62+ appOptions : {
63+ showAIAssistant : true
64+ }
5765 } ,
5866 electron : {
59- showLogo : false ,
60- showFileMenu : true ,
61- showHelpMenu : true ,
62- showSelectLanguage : true ,
63- showCompilerOptions : true ,
64- showCommandLineArguments : true ,
65- showRunButton : true ,
66- showThemeButton : true ,
67- showPuterSignInOutButton : true ,
68- showStatusLine : true ,
69- showCopyright : false
67+ theme : "system" ,
68+ style : "electron" ,
69+ styleOptions : {
70+ showLogo : false ,
71+ showFileMenu : true ,
72+ showHelpMenu : true ,
73+ showSelectLanguage : true ,
74+ showCompilerOptions : true ,
75+ showCommandLineArguments : true ,
76+ showRunButton : true ,
77+ showThemeButton : true ,
78+ showPuterSignInOutButton : true ,
79+ showStatusLine : true ,
80+ showCopyright : false
81+ } ,
82+ appOptions : {
83+ showAIAssistant : true
84+ }
7085 } ,
7186 puter : {
72- showLogo : false ,
73- showFileMenu : true ,
74- showHelpMenu : true ,
75- showSelectLanguage : true ,
76- showCompilerOptions : true ,
77- showCommandLineArguments : true ,
78- showRunButton : true ,
79- showThemeButton : true ,
80- showPuterSignInOutButton : false ,
81- showStatusLine : true ,
82- showCopyright : true
87+ theme : "system" ,
88+ style : "puter" ,
89+ styleOptions : {
90+ showLogo : false ,
91+ showFileMenu : true ,
92+ showHelpMenu : true ,
93+ showSelectLanguage : true ,
94+ showCompilerOptions : true ,
95+ showCommandLineArguments : true ,
96+ showRunButton : true ,
97+ showThemeButton : true ,
98+ showPuterSignInOutButton : false ,
99+ showStatusLine : true ,
100+ showCopyright : true
101+ } ,
102+ appOptions : {
103+ showAIAssistant : true
104+ }
83105 }
84106} ;
85107
86- const APP_OPTIONS = {
87- null : {
88- showAIAssistant : null
89- } ,
90- default : {
91- showAIAssistant : true
92- } ,
93- minimal : {
94- showAIAssistant : false
95- } ,
96- standalone : {
97- showAIAssistant : true
98- } ,
99- electron : {
100- showAIAssistant : true
101- } ,
102- puter : {
103- showAIAssistant : true
108+ const PROXY_GET = function ( obj , key ) {
109+ if ( ! key ) {
110+ return null ;
104111 }
112+
113+ for ( const k of key . split ( "." ) ) {
114+ obj = obj [ k ] ;
115+ if ( ! obj ) {
116+ break ;
117+ }
118+ }
119+
120+ return obj ;
121+ } ;
122+
123+ const PROXT_SET = function ( obj , key , val ) {
124+ if ( ! key ) {
125+ return false ;
126+ }
127+
128+ const keys = key . split ( "." ) ;
129+ const lastKey = keys [ keys . length - 1 ] ;
130+
131+ for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
132+ if ( ! obj [ keys [ i ] ] ) {
133+ obj [ keys [ i ] ] = { } ;
134+ }
135+ obj = obj [ keys [ i ] ] ;
136+ }
137+
138+ obj [ lastKey ] = val ;
139+
140+ return true ;
105141} ;
106142
143+ const PROXY_HANDLER = {
144+ get : PROXY_GET ,
145+ set : PROXT_SET
146+ } ;
147+
148+ const LEGAL_VALUES = new Proxy ( {
149+ theme : [ "system" , "reverse-system" , "light" , "dark" ] ,
150+ style : Object . keys ( DEFAULT_CONFIGURATIONS )
151+ } , PROXY_HANDLER ) ;
152+
107153const configuration = {
108- STYLE_OPTIONS : STYLE_OPTIONS ,
109- APP_OPTIONS : APP_OPTIONS ,
110- DEFAULT : {
111- theme : "system" ,
112- style : "default" ,
113- styleOptions : STYLE_OPTIONS . null ,
114- appOptions : APP_OPTIONS . null
115- } ,
116- LEGAL_VALUES : {
117- theme : [ "system" , "reverse-system" , "light" , "dark" ] ,
118- style : Object . keys ( STYLE_OPTIONS )
119- } ,
120154 CONFIGURATION : null ,
155+ LOADED_CONFIGURATION : null ,
121156 load ( ) {
157+ configuration . getConfig ( ) ;
158+ } ,
159+ getConfig ( ) {
122160 if ( ! configuration . CONFIGURATION ) {
123- configuration . CONFIGURATION = JSON . parse ( JSON . stringify ( configuration . DEFAULT ) ) ;
124- for ( const key of configuration . getKeys ( ) ) {
161+ configuration . CONFIGURATION = new Proxy ( JSON . parse ( JSON . stringify ( DEFAULT_CONFIGURATIONS . default ) ) , {
162+ get : PROXY_GET ,
163+ set : function ( obj , key , val ) {
164+ if ( LEGAL_VALUES [ key ] && ! LEGAL_VALUES [ key ] . includes ( val ) ) {
165+ return true ;
166+ }
167+
168+ if ( PROXY_GET ( obj , key ) === val ) {
169+ return true ;
170+ }
171+
172+ PROXT_SET ( obj , key , val ) ;
173+
174+ if ( key === "style" ) {
175+ obj . styleOptions = DEFAULT_CONFIGURATIONS [ val ] . styleOptions ;
176+ obj . appOptions = DEFAULT_CONFIGURATIONS [ val ] . appOptions ;
177+ configuration . merge ( configuration . getConfig ( ) , configuration . getLoadedConfig ( ) ) ;
178+ }
179+
180+ return true ;
181+ }
182+ } ) ;
183+ configuration . merge ( configuration . CONFIGURATION , configuration . getLoadedConfig ( ) ) ;
184+ }
185+ return configuration . CONFIGURATION ;
186+ } ,
187+ getLoadedConfig ( ) {
188+ if ( ! configuration . LOADED_CONFIGURATION ) {
189+ configuration . LOADED_CONFIGURATION = new Proxy ( { } , PROXY_HANDLER ) ;
190+ for ( const key of configuration . getKeys ( DEFAULT_CONFIGURATIONS . default ) ) {
125191 const val = query . get ( `${ ls . PREFIX } ${ key } ` ) || ls . get ( key ) ;
126192 if ( val ) {
127- configuration . set ( key , val , false ) ;
193+ configuration . LOADED_CONFIGURATION [ key ] = val ;
128194 }
129195 }
130196 }
131- return configuration . CONFIGURATION ;
132- } ,
133- getConfig ( ) {
134- return configuration . load ( ) ;
197+ return configuration . LOADED_CONFIGURATION ;
135198 } ,
136199 get ( key ) {
137- if ( ! key ) {
138- return null ;
139- }
140-
141- let config = configuration . getConfig ( ) ;
142- for ( const k of key . split ( "." ) ) {
143- config = config [ k ] ;
144- if ( ! config ) {
145- break ;
146- }
147- }
148-
149- return config || ls . get ( key ) ;
200+ const config = configuration . getConfig ( ) ;
201+ return config [ key ] || ls . get ( key ) ;
150202 } ,
151- set ( key , val , save = true ) {
152- if ( ! key ) {
153- return ;
154- }
155-
156- let config = configuration . getConfig ( ) ;
157-
158- const keys = key . split ( "." ) ;
159- for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
160- if ( ! config [ keys [ i ] ] ) {
161- config [ keys [ i ] ] = { } ;
162- }
163- config = config [ keys [ i ] ] ;
164- }
165- config [ keys [ keys . length - 1 ] ] = val ;
166-
203+ set ( key , val , save = false ) {
204+ const config = configuration . getConfig ( ) ;
205+ config [ key ] = val ;
167206 if ( save ) {
168- ls . set ( key , val ) ;
207+ ls . set ( key , config [ key ] ) ;
169208 }
209+ return config [ key ] ;
170210 } ,
171211 getKeys ( obj = configuration . getConfig ( ) , prefix = "" ) {
172212 return Object . keys ( obj ) . flatMap ( key => {
@@ -176,6 +216,19 @@ const configuration = {
176216 }
177217 return fullKey ;
178218 } ) ;
219+ } ,
220+ merge ( dest , src ) {
221+ for ( const key of configuration . getKeys ( src ) ) {
222+ const val = src [ key ] ;
223+ const valStr = String ( val || "" ) . toLowerCase ( ) ;
224+ if ( [ "true" , "on" , "yes" ] . includes ( valStr ) ) {
225+ dest [ key ] = true ;
226+ } else if ( [ "false" , "off" , "no" ] . includes ( valStr ) ) {
227+ dest [ key ] = false ;
228+ } else {
229+ dest [ key ] = val ;
230+ }
231+ }
179232 }
180233} ;
181234
0 commit comments