allow expanding objects in console

This commit is contained in:
Reimar 2025-10-13 12:23:07 +02:00
parent c756d6de95
commit 8c5f713501
3 changed files with 101 additions and 25 deletions

View File

@ -110,8 +110,7 @@ function loop(deltaT) {
lib.startGameLoop(loop); lib.startGameLoop(loop);
</pre </pre>
>
</div> </div>
</section> </section>
</div> </div>

View File

@ -3,12 +3,76 @@ export class PlaygroundConsole {
this.elem = elem; this.elem = elem;
} }
log(text) { #addKeyValue(entryType, parent, property, arg) {
const el = document.createElement("span"); if (property) {
el.className = "log"; const keyEl = document.createElement("span");
el.textContent = "" + text; keyEl.className = property === "__proto__" ? "prototype" : "property";
el.dataset.type = typeof text; keyEl.textContent = property + ": ";
this.elem.appendChild(el); parent.appendChild(keyEl);
}
const argString = typeof arg === "function" ? `function ${arg.name}()` : `${arg}`;
const value = document.createElement("span");
value.className = entryType;
value.textContent = argString;
value.dataset.type = arg === null ? "null" : typeof arg;
parent.appendChild(value);
}
#addEntry(entryType, property, parent, ...args) {
for (const arg of args) {
if (typeof arg === "object" && arg !== null) {
const summary = document.createElement("summary");
summary.className = entryType;
summary.dataset.type = "object";
summary.style.marginLeft = "-1rem";
this.#addKeyValue(entryType, summary, property, arg);
const details = document.createElement("details");
details.style.marginLeft = "1rem";
details.open = entryType === "dir";
details.appendChild(summary);
for (const prop in arg) {
if (!arg.hasOwnProperty(prop)) continue;
this.#addEntry(
entryType,
prop,
details,
arg.__lookupGetter__(prop) ?? arg[prop],
);
}
if (Object.getPrototypeOf(arg) && Object.keys(Object.getPrototypeOf(arg)).length) {
this.#addEntry(entryType, "__proto__", details, Object.getPrototypeOf(arg));
}
parent.appendChild(details);
} else {
const wrapper = document.createElement("p");
this.#addKeyValue(entryType, wrapper, property, arg);
parent.appendChild(wrapper);
}
}
}
log() {
this.#addEntry("log", "", this.elem, ...arguments);
}
dir() {
this.#addEntry("dir", "", this.elem, ...arguments);
}
debug() {
this.#addEntry("debug", "", this.elem, ...arguments);
}
info() {
this.#addEntry("info", "", this.elem, ...arguments);
} }
error(text) { error(text) {
@ -18,20 +82,6 @@ export class PlaygroundConsole {
this.elem.appendChild(el); this.elem.appendChild(el);
} }
debug(text) {
const el = document.createElement("span");
el.className = "debug";
el.textContent = "" + text;
this.elem.appendChild(el);
}
info(text) {
const el = document.createElement("span");
el.className = "info";
el.textContent = "" + text;
this.elem.appendChild(el);
}
clear() { clear() {
this.elem.textContent = ""; this.elem.textContent = "";
} }

View File

@ -128,16 +128,43 @@ div#buttons button {
white-space: pre-wrap; white-space: pre-wrap;
} }
#console p {
margin: 0;
}
#console .error { #console .error {
color: #d32f2f; color: #d32f2f;
} }
#console .info, #console .debug, #console .log[data-type="undefined"] { #console .info,
#console .debug,
#console .prototype,
#console [data-type="undefined"],
#console [data-type="object"],
#console [data-type="function"] {
color: #bdbdbd; color: #bdbdbd;
} }
#console .log[data-type="number"] { #console [data-type="boolean"],
color: #1976d2; #console [data-type="null"] {
color: #ff9800;
}
#console .property {
color: #9c27b0;
}
#console .property + [data-type="string"]:before,
#console .property + [data-type="string"]:after {
content: '"';
}
#console .property + [data-type="string"] {
color: #4caf50;
}
#console [data-type="number"] {
color: #2196f3;
} }
#console input { #console input {