formats.exposed/public/gif/gif.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-08-11 20:06:09 +00:00
// @ts-check
import * as base64 from "../common/vendor/base64-js.js";
import parseGif from "./parseGif.js";
import getNodeUi from "./getNodeUi.js";
import explorer from "../common/explorer.js";
const errorEl = document.getElementById("error");
const explorerEl = document.getElementById("explorer");
if (!errorEl || !explorerEl) throw new Error("HTML is not set up correctly");
2023-08-11 20:06:09 +00:00
2023-11-10 18:01:53 +00:00
/**
* @param {Element} element
* @returns {number}
*/
const totalElementChildren = (element) => {
let result = 1;
for (const child of element.children) {
result += totalElementChildren(child);
}
return result;
};
2023-08-11 20:06:09 +00:00
const main = () => {
// TODO: We may want a better UI here.
// TODO: Handle errors.
const fileDataBase64 = window.sessionStorage.getItem("fileData");
if (!fileDataBase64) {
2023-08-11 20:06:09 +00:00
location.href = "..";
return;
}
const bytes = base64.toByteArray(fileDataBase64);
2023-08-11 20:06:09 +00:00
2023-11-10 18:01:53 +00:00
console.time("parseGif");
const rootNode = parseGif(bytes);
2023-11-10 18:01:53 +00:00
console.timeEnd("parseGif");
if (!rootNode) {
// TODO: Is there better UI than this?
errorEl.removeAttribute("hidden");
return;
}
2023-11-10 18:01:53 +00:00
console.time("render");
explorerEl.innerHTML = "";
explorerEl.append(explorer(rootNode, getNodeUi));
explorerEl.removeAttribute("hidden");
2023-11-10 18:01:53 +00:00
console.timeEnd("render");
console.log(`Root node has ${totalElementChildren(explorerEl)} children`);
2023-08-11 20:06:09 +00:00
};
main();