Convert PDFBug, in web/debugger.js, to a class with static methods

This gets rid of one more closure from the code-base.
This commit is contained in:
Jonas Jenwald 2023-09-17 08:06:43 +02:00
parent c9cd934f8a
commit 2d79be941e

View File

@ -491,13 +491,14 @@ const Stats = (function Stats() {
})(); })();
// Manages all the debugging tools. // Manages all the debugging tools.
const PDFBug = (function PDFBugClosure() { class PDFBug {
const buttons = []; static #buttons = [];
let activePanel = null;
return { static #activePanel = null;
tools: [FontInspector, StepperManager, Stats],
enable(ids) { static tools = [FontInspector, StepperManager, Stats];
static enable(ids) {
const all = ids.length === 1 && ids[0] === "all"; const all = ids.length === 1 && ids[0] === "all";
const tools = this.tools; const tools = this.tools;
for (const tool of tools) { for (const tool of tools) {
@ -515,8 +516,9 @@ const PDFBug = (function PDFBugClosure() {
return indexA - indexB; return indexA - indexB;
}); });
} }
}, }
init(container, ids) {
static init(container, ids) {
this.loadCSS(); this.loadCSS();
this.enable(ids); this.enable(ids);
/* /*
@ -562,11 +564,12 @@ const PDFBug = (function PDFBugClosure() {
`${tool.name} is disabled. To enable add "${tool.id}" to ` + `${tool.name} is disabled. To enable add "${tool.id}" to ` +
"the pdfBug parameter and refresh (separate multiple by commas)."; "the pdfBug parameter and refresh (separate multiple by commas).";
} }
buttons.push(panelButton); this.#buttons.push(panelButton);
} }
this.selectPanel(0); this.selectPanel(0);
}, }
loadCSS() {
static loadCSS() {
const { url } = import.meta; const { url } = import.meta;
const link = document.createElement("link"); const link = document.createElement("link");
@ -574,31 +577,32 @@ const PDFBug = (function PDFBugClosure() {
link.href = url.replace(/.js$/, ".css"); link.href = url.replace(/.js$/, ".css");
document.head.append(link); document.head.append(link);
}, }
cleanup() {
static cleanup() {
for (const tool of this.tools) { for (const tool of this.tools) {
if (tool.enabled) { if (tool.enabled) {
tool.cleanup(); tool.cleanup();
} }
} }
}, }
selectPanel(index) {
static selectPanel(index) {
if (typeof index !== "number") { if (typeof index !== "number") {
index = this.tools.indexOf(index); index = this.tools.indexOf(index);
} }
if (index === activePanel) { if (index === this.#activePanel) {
return; return;
} }
activePanel = index; this.#activePanel = index;
for (const [j, tool] of this.tools.entries()) { for (const [j, tool] of this.tools.entries()) {
const isActive = j === index; const isActive = j === index;
buttons[j].classList.toggle("active", isActive); this.#buttons[j].classList.toggle("active", isActive);
tool.active = isActive; tool.active = isActive;
tool.panel.hidden = !isActive; tool.panel.hidden = !isActive;
} }
}, }
}; }
})();
globalThis.FontInspector = FontInspector; globalThis.FontInspector = FontInspector;
globalThis.StepperManager = StepperManager; globalThis.StepperManager = StepperManager;