Skip to content

Commit a0bc90f

Browse files
stackdumpclaude
andcommitted
Fix editor template/CID loading: wait for petri-view custom element
The editor's init code ran before the petri-view web component was defined, so loadModel() didn't exist yet. Fix: use customElements.whenDefined('petri-view') to defer loading until the component is ready. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c95ec86 commit a0bc90f

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

public/bitwrap.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,28 +342,43 @@ function downloadFile(filename, content) {
342342
// Load by CID: /editor?cid=<cid>
343343
const cid = params.get('cid');
344344
if (cid) {
345-
fetch('/o/' + cid)
346-
.then(r => r.ok ? r.json() : null)
347-
.then(data => {
348-
if (data && petriView && petriView.loadModel) {
349-
petriView.loadModel(data);
350-
}
351-
})
352-
.catch(err => console.error('Failed to load model:', err));
345+
const loadCid = () => {
346+
fetch('/o/' + cid)
347+
.then(r => r.ok ? r.json() : null)
348+
.then(data => {
349+
if (data && petriView && petriView.loadModel) {
350+
petriView.loadModel(data);
351+
}
352+
})
353+
.catch(err => console.error('Failed to load model:', err));
354+
};
355+
if (petriView && petriView.loadModel) {
356+
loadCid();
357+
} else if (customElements) {
358+
customElements.whenDefined('petri-view').then(loadCid);
359+
}
353360
return;
354361
}
355362

356363
// Load by template: /editor?template=<id>&poll=<pollId>
357364
const template = params.get('template');
358365
if (template) {
359-
fetch('/api/templates/' + template)
360-
.then(r => r.ok ? r.json() : null)
361-
.then(data => {
362-
if (data && petriView && petriView.loadModel) {
363-
petriView.loadModel(data);
364-
}
365-
})
366-
.catch(err => console.error('Failed to load template:', err));
366+
// Wait for petri-view custom element to be ready before loading
367+
const loadTemplate = () => {
368+
fetch('/api/templates/' + template)
369+
.then(r => r.ok ? r.json() : null)
370+
.then(data => {
371+
if (data && petriView && petriView.loadModel) {
372+
petriView.loadModel(data);
373+
}
374+
})
375+
.catch(err => console.error('Failed to load template:', err));
376+
};
377+
if (petriView && petriView.loadModel) {
378+
loadTemplate();
379+
} else if (customElements) {
380+
customElements.whenDefined('petri-view').then(loadTemplate);
381+
}
367382

368383
// If a poll ID is provided, show poll info in the toolbar
369384
const pollId = params.get('poll');

0 commit comments

Comments
 (0)