Fix loading of demos

This commit is contained in:
ochafik 2024-12-25 13:49:51 +00:00 • committed by Olivier Chafik
commit b76ac89d44
3 changed files with 19 additions and 6 deletions

View file

@ -61,7 +61,7 @@ window.addEventListener('load', async () => {
registerCustomAppHeightCSSProperty(); registerCustomAppHeightCSSProperty();
const fs = await createEditorFS({prefix: '/', allowPersistence: isInStandaloneMode}); const fs = await createEditorFS({prefix: '/libraries/', allowPersistence: isInStandaloneMode});
await registerOpenSCADLanguage(fs, '/', zipArchives); await registerOpenSCADLanguage(fs, '/', zipArchives);

View file

@ -93,6 +93,11 @@ addEventListener('message', async (e) => {
if (inputs) { if (inputs) {
for (const source of inputs) { for (const source of inputs) {
try { try {
if (source.path.startsWith('/libraries/')) {
console.log(`Skipping ${source.path}`);
continue;
}
console.log(`Writing ${source.path}`);
instance.FS.writeFile(source.path, await fetchSource(source)); instance.FS.writeFile(source.path, await fetchSource(source));
} catch (e) { } catch (e) {
console.trace(e); console.trace(e);

View file

@ -118,17 +118,24 @@ export class Model {
} }
openFile(path: string) { openFile(path: string) {
// alert(`TODO: open ${path}`); // console.log(`openFile: ${path}`);
if (this.mutate(s => { if (this.mutate(s => {
if (s.params.activePath != path) { if (s.params.activePath != path) {
s.params.activePath = path; const readSource = (path: string) => {
if (!s.params.sources.find(src => src.path === path)) {
let content = '';
try { try {
content = new TextDecoder("utf-8").decode(this.fs.readFileSync(path)); return new TextDecoder("utf-8").decode(this.fs.readFileSync(path));
} catch (e) { } catch (e) {
console.error('Error while reading file:', e); console.error('Error while reading file:', e);
return '';
} }
};
// Remove source of previous active path if it's unmodified
const activePathContent = readSource(s.params.activePath);
s.params.sources = s.params.sources.filter(src => src.path !== s.params.activePath || src.content != activePathContent);
s.params.activePath = path;
if (!s.params.sources.find(src => src.path === path)) {
const content = readSource(path);
s.params.sources = [...s.params.sources, {path, content}]; s.params.sources = [...s.params.sources, {path, content}];
} }
s.lastCheckerRun = undefined; s.lastCheckerRun = undefined;
@ -307,6 +314,7 @@ export class Model {
} }
async render({isPreview, mountArchives, now, retryInOtherDim}: {isPreview: boolean, mountArchives?: boolean, now: boolean, retryInOtherDim?: boolean}) { async render({isPreview, mountArchives, now, retryInOtherDim}: {isPreview: boolean, mountArchives?: boolean, now: boolean, retryInOtherDim?: boolean}) {
// console.log(JSON.stringify(this.state, null, 2));
mountArchives ??= true; mountArchives ??= true;
retryInOtherDim ??= true; retryInOtherDim ??= true;
const setRendering = (s: State, value: boolean) => { const setRendering = (s: State, value: boolean) => {