From 0a20eede96db3dfbee7ad14355a9627aeacb43d1 Mon Sep 17 00:00:00 2001 From: Jason N Date: Sun, 26 Nov 2023 12:48:47 +1100 Subject: [PATCH] Allow importing from URL --- src/App.svelte | 34 ++++-- src/lib/NewFileModal.svelte | 224 ++++++++++++++++++++++++------------ src/lib/Playground.svelte | 23 +++- src/lib/loadFromUrl.js | 70 +++++++++++ 4 files changed, 266 insertions(+), 85 deletions(-) create mode 100644 src/lib/loadFromUrl.js diff --git a/src/App.svelte b/src/App.svelte index 47501f2..8cdfe28 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,6 +1,7 @@ - dispatch('cancel')} -> - - - + class="input" + type="text" + pattern=".+\.(mzn|dzn|json|js|html|css|mzp|mzc)" + bind:value={url} + required + /> +

+ +
+ + +
+ +{:else} + dispatch('cancel')} + > + + +{/if} diff --git a/src/lib/Playground.svelte b/src/lib/Playground.svelte index 0590084..2d4ef1c 100644 --- a/src/lib/Playground.svelte +++ b/src/lib/Playground.svelte @@ -127,14 +127,33 @@ files = []; openFiles(project.files, autoFocus); currentIndex = project.tab || 0; + await loadSolvers(); if (project.solverId) { - await loadSolvers(); currentSolverIndex = solvers.findIndex( (s) => s.id === project.solverId, ); + } else { + currentSolverIndex = + solvers.findIndex( + (s) => s.extraInfo && s.extraInfo.isDefault, + ) || 0; } if (project.solverConfig) { solverConfig.load(project.solverConfig); + } else { + solverConfig.reset(); + } + } + + async function importFiles(e) { + const offset = files.length; + openFiles(e.files); + if (e.tab !== undefined && e.tab !== null && e.tab >= 0) { + selectTab(offset + e.tab); + } + if (e.solverId) { + await loadSolvers(); + currentSolverIndex = solvers.findIndex((s) => s.id === e.solverId); } } @@ -1388,7 +1407,7 @@ active={newFileRequested} on:cancel={() => (newFileRequested = false)} on:new={(e) => newFile(e.detail.type)} - on:open={(e) => openFiles(e.detail.files)} + on:open={(e) => importFiles(e.detail)} /> !name.endsWith(ext)) + ) { + throw new Error('File type not recognised'); + } + const response = await fetch(new URL(src)); + if (!response.ok) { + throw new Error( + `Request failed (${response.status} ${response.statusText})` + ); + } + if (name.endsWith('.mzp')) { + const project = await response.json(); + const files = []; + const openFile = project.openFiles[project.openTab]; + let tab = 0; + for (const file of project.projectFiles) { + const name = file.split('/').pop(); + if (allowedExtensions.every((ext) => !name.endsWith(ext))) { + continue; + } + if (file === openFile) { + tab = files.length; + } + const res = await fetch(new URL(file, src)); + if (!res.ok) { + throw new Error( + `Request failed (${res.status} ${res.statusText})` + ); + } + const contents = await res.text(); + files.push({ + name, + contents, + hidden: project.openFiles.indexOf(file) === -1, + }); + } + let solverId = project.selectedBuiltinConfigId; + if (solverId === 'org.gecode.gecode') { + solverId = 'org.minizinc.gecode_presolver'; + } else if (solverId === 'org.chuffed.chuffed') { + solverId = 'org.minizinc.chuffed'; + } + return { + files, + tab, + solverId, + }; + } else { + const contents = await response.text(); + return { files: [{ name, contents }] }; + } +}