@@ -27,23 +27,52 @@ export class Editor {
2727 tabSize : 2
2828 } ) ;
2929
30- // Load initial content from localStorage
31- const savedYaml = localStorage . getItem ( this . STORAGE_KEY ) ;
32- if ( savedYaml ) {
33- this . ace . setValue ( savedYaml , - 1 ) ; // -1 moves cursor to the start
34- } else {
35- // Optional: Set a default value if nothing is saved
36- // this.ace.setValue("title: My PCB\nwidth: 5\nheight: 5\npins:\n left: []\n right: []\n top: []\n bottom: []", -1);
37- }
38-
39- // register change handler
40- this . ace . session . on ( 'change' , this . debounce ( this . onChange . bind ( this ) , 300 ) ) ;
41- this . onChange ( ) ; // Trigger initial processing
30+ // Defer content loading and event binding
31+ this . initializeEditor ( ) ;
4232
4333 // register download handler
4434 this . output . addEventListener ( 'click' , this . onDownloadClick ) ;
4535 }
4636
37+ async initializeEditor ( ) {
38+ // Load content first
39+ await this . loadInitialContent ( ) ;
40+
41+ // Then register change handler and trigger initial processing
42+ this . ace . session . on ( 'change' , this . debounce ( this . onChange . bind ( this ) , 300 ) ) ;
43+ // Check if editor has content before triggering initial processing
44+ if ( this . ace . getValue ( ) ) {
45+ this . onChange ( ) ; // Trigger initial processing only if content loaded
46+ }
47+ }
48+
49+ async loadInitialContent ( ) {
50+ const urlParams = new URLSearchParams ( window . location . search ) ;
51+ const loadUrl = urlParams . get ( 'load' ) ;
52+
53+ if ( loadUrl ) {
54+ try {
55+ const response = await fetch ( loadUrl ) ;
56+ if ( ! response . ok ) {
57+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
58+ }
59+ const yamlContent = await response . text ( ) ;
60+ this . ace . setValue ( yamlContent , - 1 ) ; // -1 moves cursor to the start
61+ console . log ( `Loaded content from: ${ loadUrl } ` ) ;
62+ // Optionally clear localStorage if loaded from URL to avoid conflict?
63+ // localStorage.removeItem(this.STORAGE_KEY);
64+ } catch ( error ) {
65+ console . error ( 'Failed to load content from URL:' , loadUrl , error ) ;
66+ }
67+ } else {
68+ // Load initial content from localStorage if no loadUrl
69+ const savedYaml = localStorage . getItem ( this . STORAGE_KEY ) ;
70+ if ( savedYaml ) {
71+ this . ace . setValue ( savedYaml , - 1 ) ;
72+ }
73+ }
74+ }
75+
4776 debounce ( func , wait ) {
4877 let timeout ;
4978 return function ( ...args ) {
0 commit comments