Skip to content

Commit 8e10e4c

Browse files
committed
feat: Load editor content from URL parameter or localStorage
1 parent fae5729 commit 8e10e4c

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

public/index.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
<div class="editor">
1616
<div class="panel panel-editor">
1717
<div id="editor"></div>
18+
<p>
19+
Example:
20+
<a href="?load=pinouts/esp32-c3-super-mini.yaml">ESP32-C3 Super Mini</a>
21+
</p>
22+
23+
<p>
24+
When you create the pinout for a new commonly available PCB please submit a pull request or issue, so
25+
it can be added to the list of available pinouts.
26+
</p>
1827
</div>
1928
<div class="panel panel-output">
2029
<div id="output"></div>
@@ -23,12 +32,5 @@
2332
</div>
2433
</div>
2534

26-
<div class="container">
27-
<p>
28-
When you create the pinout for a new commonly available PCB please submit a pull request or issue, so
29-
it can be added to the list of available pinouts.
30-
</p>
31-
</div>
32-
3335
</body>
3436
</html>

src/Editor.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)