Some improvements to the edit-example script

This commit is contained in:
Maximilian Krög
2022-08-13 16:35:55 +02:00
parent cf42592f78
commit 5fb74d21e5
2 changed files with 21 additions and 55 deletions

View File

@@ -82,26 +82,11 @@
listExamples(examples); listExamples(examples);
} }
function parseParams() {
const params = {};
const list = window.location.search
.substring(1)
.replace(/\+/g, '%20')
.split('&');
for (let i = 0; i < list.length; ++i) {
const pair = list[i].split('=');
if (pair.length === 2) {
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
}
return params;
}
window.addEventListener('load', function () { window.addEventListener('load', function () {
template = new jugl.Template('template'); template = new jugl.Template('template');
target = document.getElementById('examples'); target = document.getElementById('examples');
const params = parseParams(); const params = new URLSearchParams(window.location.search);
const text = params['q'] || ''; const text = params.get('q') || '';
const input = document.getElementById('keywords'); const input = document.getElementById('keywords');
input.addEventListener('input', inputChange); input.addEventListener('input', inputChange);
input.value = text; input.value = text;

View File

@@ -1,4 +1,6 @@
(function() { (function() {
"use strict"
/* global LZString */
function compress(json) { function compress(json) {
return LZString.compressToBase64(JSON.stringify(json)) return LZString.compressToBase64(JSON.stringify(json))
@@ -43,55 +45,34 @@
const pkgJson = document.getElementById('example-pkg-source').innerText; const pkgJson = document.getElementById('example-pkg-source').innerText;
const unique = new Set(); const unique = new Set();
const localResources = (js.match(/'(\.\/)?data\/[^']*/g) || []) const localResources = (js.match(/'(?:\.\/)?(?:data|resources)\/[^']*'/g) || [])
.concat(js.match(/'(\.\/)?resources\/[^']*/g) || []) .map(function (f) {
.map( return f.replace(/^'(?:\.\/)?|'$/g, '');
function (f) { })
return f.replace(/^'(\.\/)?/, ''); .filter(function (f) {
} return unique.has(f) ? false : unique.add(f);
) });
.filter(
function (f) {
return unique.has(f) ? false : (unique.add(f) || unique);
}
);
const promises = localResources.map( const promises = localResources.map(function (resource) {
function (resource) { return fetchResource(resource);
return fetchResource(resource); });
}
);
Promise.all(promises).then( Promise.all(promises).then(
function (results) { function (results) {
const files = { const files = {
'index.html': { 'index.html': {content: html},
content: html 'main.js': {content: js},
}, 'package.json': {content: pkgJson},
'main.js': { 'sandbox.config.json': {content: '{"template": "parcel"}'}
content: js
},
'package.json': {
content: pkgJson
},
'sandbox.config.json': {
content: '{"template": "parcel"}'
}
}; };
if (worker) { if (worker) {
files['worker.js'] = { files['worker.js'] = {content: worker}
content: worker
}
} }
const data = {
files: files
};
for (let i = 0; i < localResources.length; i++) { for (let i = 0; i < localResources.length; i++) {
data.files[localResources[i]] = results[i]; files[localResources[i]] = results[i];
} }
form.parameters.value = compress(data); form.parameters.value = compress({files: files});
form.submit(); form.submit();
} }
); );