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);
}
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 () {
template = new jugl.Template('template');
target = document.getElementById('examples');
const params = parseParams();
const text = params['q'] || '';
const params = new URLSearchParams(window.location.search);
const text = params.get('q') || '';
const input = document.getElementById('keywords');
input.addEventListener('input', inputChange);
input.value = text;

View File

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