2023-08-02 18:48:36 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
import crel, { fragment } from "../common/crel.js";
|
2023-08-02 18:48:36 +00:00
|
|
|
import formatBytes from "../common/formatBytes.js";
|
|
|
|
/** @typedef {import("./nodePath.js").NodePath} NodePath */
|
|
|
|
/** @typedef {import("../../types/png.d.ts").PngNode} PngNode */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Function} NodeUiFn
|
|
|
|
* @param {PngNode} node
|
|
|
|
* @returns {NodeUi}
|
|
|
|
*/
|
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
/**
|
|
|
|
* @param {PngNode} rootNode
|
|
|
|
* @param {NodeUiFn} getNodeUi
|
|
|
|
* @returns {DocumentFragment}
|
|
|
|
*/
|
|
|
|
export default (rootNode, getNodeUi) => {
|
|
|
|
const [bytesRootEl, treeRootEl] = traverse(rootNode, [], getNodeUi);
|
2023-08-02 18:48:36 +00:00
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
const outerBytesEl = crel("div", { class: "bytes" }, bytesRootEl);
|
|
|
|
const outerTreeEl = crel("div", { class: "tree" }, treeRootEl);
|
2023-08-02 18:48:36 +00:00
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
return fragment(outerBytesEl, outerTreeEl);
|
|
|
|
};
|
2023-08-02 18:48:36 +00:00
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
/**
|
|
|
|
* @param {PngNode} node
|
|
|
|
* @param {NodePath} path
|
|
|
|
* @param {NodeUiFn} getNodeUi
|
|
|
|
* @returns [HTMLElement, HTMLElement] Each node's bytes and tree elements.
|
|
|
|
*/
|
|
|
|
const traverse = (node, path, getNodeUi) => {
|
|
|
|
const nodeBytesEl = crel("span", { "data-path": path });
|
2023-08-02 18:48:36 +00:00
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
const isRoot = path.length === 0;
|
|
|
|
const { title, description } = getNodeUi(node);
|
|
|
|
const nodeTreeEl = crel(
|
|
|
|
"details",
|
|
|
|
{ "data-path": path, ...(isRoot ? { open: "open" } : {}) },
|
|
|
|
crel(
|
|
|
|
"summary",
|
|
|
|
{},
|
|
|
|
crel("span", { "class": "title" }, title),
|
|
|
|
crel(
|
|
|
|
"span",
|
|
|
|
{ "class": "bytecount" },
|
|
|
|
"TODO: X bytes",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
description,
|
|
|
|
);
|
2023-08-02 18:48:36 +00:00
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
if (node.children) {
|
|
|
|
const treeChildrenEl = crel("div", { class: "children" });
|
|
|
|
node.children.forEach((child, index) => {
|
|
|
|
const [childBytesEl, childTreeEl] = traverse(
|
|
|
|
child,
|
|
|
|
path.concat(index),
|
|
|
|
getNodeUi,
|
|
|
|
);
|
|
|
|
if (index > 0) nodeBytesEl.append(" ");
|
|
|
|
nodeBytesEl.append(childBytesEl);
|
|
|
|
treeChildrenEl.append(childTreeEl);
|
|
|
|
});
|
|
|
|
nodeTreeEl.append(treeChildrenEl);
|
|
|
|
} else {
|
|
|
|
nodeBytesEl.innerHTML = formatBytes(node.bytes, 256);
|
2023-08-02 18:48:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 18:52:02 +00:00
|
|
|
return [nodeBytesEl, nodeTreeEl];
|
|
|
|
};
|