Autohide loader

This commit is contained in:
Nicholas Jitkoff 2022-12-04 10:03:10 -08:00
parent 2080884233
commit ffda6ea2b3

View file

@ -23,19 +23,24 @@
// document.body.classList.remove("toasting") // document.body.classList.remove("toasting")
// } // }
let autohideTimeout;
function showLoader(state) { function showLoader(state) {
let loader = document.getElementById("loader"); let loader = document.getElementById("loader");
if (autohideTimeout) {
clearTimeout(autohideTimeout);
autohideTimeout = undefined;
}
if (state) { if (state) {
if (!loader) { if (!loader) {
loader = document.createElement("div"); loader = document.createElement("div");
loader.id = "loader"; loader.id = "loader";
document.body.appendChild(loader); document.body.appendChild(loader);
} }
autohideTimeout = setTimeout(() => {showLoader(false)}, 3000)
} else if (loader) { } else if (loader) {
setTimeout(() => loader?.parentElement?.removeChild(loader), 500) setTimeout(() => loader?.parentElement?.removeChild(loader), 1000)
} }
setTimeout(() => document.body.classList.toggle("loading", state), 1) setTimeout(() => document.body.classList.toggle("loading", state), 500)
} }
window.showLoader = showLoader; window.showLoader = showLoader;
@ -64,11 +69,12 @@
} }
const renderers = { const renderers = {
"application/ld+json": {script:"/render/recipe.min.html"}, "application/ld+json": {script:"/render/recipe.html"},
"text/canvas+javascript": {script:"canvas"}, "text/canvas+javascript": {script:"canvas"},
"text/javascript": {script:"script"}, "text/javascript": {script:"script"},
"application/bitsy": {script:"/render/bitsy.html", sandbox:"bitsy"}, "application/bitsy": {script:"/render/bitsy.html", sandbox:"bitsy"},
"c": {script:"color"}, "c": {script:"color"},
"e": {rewrite: "data:text/html;cipher=aes;format=gz;base64,"},
"text/rawhtml": {script:"parse"}, "text/rawhtml": {script:"parse"},
"javascript": {script:"bookmarklet"}, "javascript": {script:"bookmarklet"},
"ipfs": {script:"ipfs", sandbox:"ipfs"}, "ipfs": {script:"ipfs", sandbox:"ipfs"},
@ -83,7 +89,7 @@
if (navigator.share) { if (navigator.share) {
navigator.share(info) navigator.share(info)
.then(() => { console.log('Thanks for sharing!');}) .then(() => { console.log('Shared!');})
.catch(console.error); .catch(console.error);
} else { } else {
copyLink(info) copyLink(info)
@ -111,7 +117,7 @@
if (navigator.wakeLock) { if (navigator.wakeLock) {
wakeLock = await navigator.wakeLock.request(); wakeLock = await navigator.wakeLock.request();
wakeLock.addEventListener('release', () => {}); wakeLock.addEventListener('release', () => {});
console.log('Keeping Screen Awake:', !wakeLock.released); console.log('💡 Keeping Screen Awake:', !wakeLock.released);
} else { } else {
// keepAwake(); // keepAwake();
} }
@ -196,11 +202,13 @@
}, false); }, false);
async function renderContent() { async function renderContent() {
showLoader(true) showLoader(true)
var fragment = window.location.hash.substring(1); var fragment = window.location.hash.substring(1);
if (fragment.length < 3 && !isFramed) { let content = null //sessionStorage.getItem("editor-content");
if (content || (fragment.length < 3 && !isFramed)) {
return location.href = "/edit"; return location.href = "/edit";
} }
@ -284,6 +292,10 @@
renderMode = "download"; renderMode = "download";
} }
if (durl.params.style == "default") {
dataPrefix = bitty.HEAD_TAGS_EXTENDED();
}
if (renderer) { if (renderer) {
var script = renderer.script; var script = renderer.script;
if (script.indexOf("/") == -1) script = location.origin + '/render/' + script + '.js' if (script.indexOf("/") == -1) script = location.origin + '/render/' + script + '.js'
@ -317,7 +329,13 @@
'Edge only supports shorter URLs (maximum 2083 bytes).<br>Larger sites may require a different browser.<br><a href="http://reference.bitty.site">Learn more</a>'; 'Edge only supports shorter URLs (maximum 2083 bytes).<br>Larger sites may require a different browser.<br><a href="http://reference.bitty.site">Learn more</a>';
} }
await durl.decompress(); await durl.decompress()
if (durl.error) {
writeDocContent(iframe.contentWindow.document, atob(bitty.HEAD_TAGS_EXTENDED()) + `<style>.error{color:red; background:pink; max-width:320px; margin:auto; padding:1em;}</style><div class="error">${durl.error}</div>`)
showLoader(false)
return
}
durl.dataPrefix = dataPrefix; durl.dataPrefix = dataPrefix;
let dataURL = durl.href; let dataURL = durl.href;
@ -327,16 +345,15 @@
iframe.sandbox = "allow-same-origin allow-downloads allow-scripts allow-forms allow-top-navigation allow-popups allow-modals allow-popups-to-escape-sandbox"; iframe.sandbox = "allow-same-origin allow-downloads allow-scripts allow-forms allow-top-navigation allow-popups allow-modals allow-popups-to-escape-sandbox";
if (isIE && renderMode == "data") renderMode = "frame"; if (isIE && renderMode == "data") renderMode = "frame";
let contentTarget// = iframe.contentWindow.document; let contentTarget = iframe.contentWindow.document;
if (isWatch) { if (isWatch) {
console.log("Rendering for watch") console.log("Rendering for watch")
contentTarget = document; contentTarget = document;
} }
console.log("🖋 Rendering mode: " + "\x1B[1m" + renderMode, {url:durl}) console.log("🖋 Rendering mode: " + "\x1B[1m" + renderMode, {url:durl})
// dataURL = dataURL.replace("application/ld+json", "text/plain");
if (renderMode == "download") { if (renderMode == "download") {
try { try {
//let dl = document.querySelector("#download");
let extension = title.split(".") let extension = title.split(".")
let dl = el("a", {id: "download", href:dataURL, download: title}, let dl = el("a", {id: "download", href:dataURL, download: title},
el("div", {id: "dl-image", innerText:extension.pop() ?? ""}), el("div", {id: "dl-image", innerText:extension.pop() ?? ""}),
@ -420,15 +437,16 @@
} else { // Render using data url (storage disabled) } else { // Render using data url (storage disabled)
src = "data:text/html," + SCRIPT_LOADER; src = "data:text/html," + SCRIPT_LOADER;
} }
console.log("📜Loading script with source:", src) console.log("Loading script with source:", src)
iframe.src = src; iframe.src = src;
} }
} }
function writeDocContent(doc, content) { function writeDocContent(doc, content) {
doc.open(); return doc.documentElement.innerHTML = content;
doc.write(content); doc.open();
doc.close(); doc.write(content);
doc.close();
} }
function extractTerms(...args) { function extractTerms(...args) {
@ -439,20 +457,14 @@ function extractTerms(...args) {
return Object.keys(wordSet); return Object.keys(wordSet);
} }
async function recordToHistory() { async function recordToHistory(durl) {
let location = window.location;
let fragment = location.hash;
fragment = fragment.substring(fragment.indexOf("/") + 1);
// var slashIndex = fragment.indexOf("/");
// var title = fragment.substring(0, slashIndex) || info.title;
let hash = await bitty.hashString(fragment);
let durl = new bitty.DataURL(fragment)
let type = durl.mediatype; let type = durl.mediatype;
let hash = await bitty.hashString(durl.href);
let metadata = bitty.pathToMetadata(location.pathname); let metadata = bitty.pathToMetadata(location.pathname);
if (!metadata.title) { if (!metadata.title && !metadata.title.length) {
let dom = await (await durl.decompress()).parseDom(); if (!durl.rawData) durl = await durl.decompress();
let dom = await durl.parseDom();
if (dom) { if (dom) {
for (let el of dom.getElementsByTagName('script')) { el.parentNode.removeChild(el) } for (let el of dom.getElementsByTagName('script')) { el.parentNode.removeChild(el) }