|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/06/2026 |
| 5 | +createInspectorSurfaceIntegration.js |
| 6 | +*/ |
| 7 | + |
| 8 | +import { createInspectorCommandPack, registerInspectorCommands } from "../commands/registerInspectorCommands.js"; |
| 9 | +import { createDebugInspectorHost } from "../host/debugInspectorHost.js"; |
| 10 | +import { createDebugInspectorRegistry } from "../registry/debugInspectorRegistry.js"; |
| 11 | +import { asObject, sanitizeText } from "../shared/inspectorUtils.js"; |
| 12 | + |
| 13 | +const DEFAULT_INSPECTOR_PANELS = Object.freeze([ |
| 14 | + Object.freeze({ |
| 15 | + id: "inspector-entity", |
| 16 | + title: "Inspector: Entity", |
| 17 | + modelKey: "entity", |
| 18 | + priority: 1170 |
| 19 | + }), |
| 20 | + Object.freeze({ |
| 21 | + id: "inspector-component", |
| 22 | + title: "Inspector: Component", |
| 23 | + modelKey: "component", |
| 24 | + priority: 1171 |
| 25 | + }), |
| 26 | + Object.freeze({ |
| 27 | + id: "inspector-state-diff", |
| 28 | + title: "Inspector: State Diff", |
| 29 | + modelKey: "stateDiff", |
| 30 | + priority: 1172 |
| 31 | + }), |
| 32 | + Object.freeze({ |
| 33 | + id: "inspector-timeline", |
| 34 | + title: "Inspector: Timeline", |
| 35 | + modelKey: "timeline", |
| 36 | + priority: 1173 |
| 37 | + }), |
| 38 | + Object.freeze({ |
| 39 | + id: "inspector-event-stream", |
| 40 | + title: "Inspector: Event Stream", |
| 41 | + modelKey: "eventStream", |
| 42 | + priority: 1174 |
| 43 | + }) |
| 44 | +]); |
| 45 | + |
| 46 | +function toPanelDescriptor(host, definition, enabledByDefault) { |
| 47 | + const source = asObject(definition); |
| 48 | + const id = sanitizeText(source.id) || "inspector-panel"; |
| 49 | + const title = sanitizeText(source.title) || id; |
| 50 | + const modelKey = sanitizeText(source.modelKey); |
| 51 | + const priority = Number.isFinite(source.priority) ? Number(source.priority) : 1170; |
| 52 | + return { |
| 53 | + id, |
| 54 | + title, |
| 55 | + enabled: enabledByDefault === true, |
| 56 | + priority, |
| 57 | + source: "inspectors", |
| 58 | + renderMode: "text-block", |
| 59 | + render(_panel, contextSnapshot = {}) { |
| 60 | + host.update(asObject(contextSnapshot)); |
| 61 | + const snapshot = asObject(host.getSnapshot()); |
| 62 | + const model = asObject(asObject(snapshot.models)[modelKey]); |
| 63 | + const lines = Array.isArray(model.lines) ? model.lines.slice(0, 12) : []; |
| 64 | + return { |
| 65 | + id, |
| 66 | + title, |
| 67 | + lines: lines.length > 0 ? lines : [`No ${modelKey || "inspector"} data available.`] |
| 68 | + }; |
| 69 | + } |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +function toProviderDescriptors() { |
| 74 | + return [ |
| 75 | + { |
| 76 | + providerId: "inspector.entity.snapshot", |
| 77 | + title: "Inspector Entity Snapshot", |
| 78 | + readOnly: true, |
| 79 | + sourcePath: "inspectors.models.entity" |
| 80 | + }, |
| 81 | + { |
| 82 | + providerId: "inspector.component.snapshot", |
| 83 | + title: "Inspector Component Snapshot", |
| 84 | + readOnly: true, |
| 85 | + sourcePath: "inspectors.models.component" |
| 86 | + }, |
| 87 | + { |
| 88 | + providerId: "inspector.stateDiff.snapshot", |
| 89 | + title: "Inspector State Diff Snapshot", |
| 90 | + readOnly: true, |
| 91 | + sourcePath: "inspectors.models.stateDiff" |
| 92 | + }, |
| 93 | + { |
| 94 | + providerId: "inspector.timeline.snapshot", |
| 95 | + title: "Inspector Timeline Snapshot", |
| 96 | + readOnly: true, |
| 97 | + sourcePath: "inspectors.models.timeline" |
| 98 | + }, |
| 99 | + { |
| 100 | + providerId: "inspector.eventStream.snapshot", |
| 101 | + title: "Inspector Event Stream Snapshot", |
| 102 | + readOnly: true, |
| 103 | + sourcePath: "inspectors.models.eventStream" |
| 104 | + } |
| 105 | + ]; |
| 106 | +} |
| 107 | + |
| 108 | +export function createInspectorSurfaceIntegration(options = {}) { |
| 109 | + const source = asObject(options); |
| 110 | + const registry = source.registry && typeof source.registry.getSnapshot === "function" |
| 111 | + ? source.registry |
| 112 | + : createDebugInspectorRegistry({ |
| 113 | + inspectors: source.inspectors |
| 114 | + }); |
| 115 | + const host = source.host && typeof source.host.getSnapshot === "function" |
| 116 | + ? source.host |
| 117 | + : createDebugInspectorHost({ |
| 118 | + registry, |
| 119 | + limits: source.limits |
| 120 | + }); |
| 121 | + const commandRegistration = source.commandRegistry |
| 122 | + ? registerInspectorCommands({ |
| 123 | + host, |
| 124 | + commandRegistry: source.commandRegistry, |
| 125 | + packId: sanitizeText(source.commandPackId) || "inspectorx", |
| 126 | + namespace: sanitizeText(source.commandNamespace) || "inspectorx" |
| 127 | + }) |
| 128 | + : null; |
| 129 | + |
| 130 | + return { |
| 131 | + registry, |
| 132 | + host, |
| 133 | + commandRegistration, |
| 134 | + update(context = {}) { |
| 135 | + return host.update(context); |
| 136 | + }, |
| 137 | + getSnapshot() { |
| 138 | + return host.getSnapshot(); |
| 139 | + } |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +export function createAdvancedInspectorDebugPluginDefinition(options = {}) { |
| 144 | + const source = asObject(options); |
| 145 | + const integration = createInspectorSurfaceIntegration({ |
| 146 | + registry: source.registry, |
| 147 | + host: source.host, |
| 148 | + limits: source.limits |
| 149 | + }); |
| 150 | + const host = integration.host; |
| 151 | + const defaultPanels = Array.isArray(source.panels) ? source.panels : DEFAULT_INSPECTOR_PANELS; |
| 152 | + const includeCommandPack = source.includeCommandPack === true; |
| 153 | + const commandPackId = sanitizeText(source.commandPackId) || "inspectorx"; |
| 154 | + const commandNamespace = sanitizeText(source.commandNamespace) || commandPackId; |
| 155 | + |
| 156 | + return { |
| 157 | + pluginId: sanitizeText(source.pluginId) || "inspectors.advanced", |
| 158 | + title: sanitizeText(source.title) || "Advanced Inspectors", |
| 159 | + featureFlag: sanitizeText(source.featureFlag) || "advancedInspectors", |
| 160 | + autoActivate: source.autoActivate === true, |
| 161 | + capabilities: [ |
| 162 | + { capabilityId: "debug.overlay.panel", version: "1.0.0", required: true }, |
| 163 | + { capabilityId: "debug.overlay.provider", version: "1.0.0", required: true }, |
| 164 | + { capabilityId: "debug.command-pack", version: "1.0.0", required: includeCommandPack } |
| 165 | + ], |
| 166 | + getProviders() { |
| 167 | + return toProviderDescriptors(); |
| 168 | + }, |
| 169 | + getPanels() { |
| 170 | + return defaultPanels.map((panel) => toPanelDescriptor(host, panel, source.panelsEnabled === true)); |
| 171 | + }, |
| 172 | + getCommandPacks() { |
| 173 | + if (!includeCommandPack) { |
| 174 | + return []; |
| 175 | + } |
| 176 | + return [ |
| 177 | + createInspectorCommandPack({ |
| 178 | + host, |
| 179 | + packId: commandPackId, |
| 180 | + namespace: commandNamespace, |
| 181 | + label: sanitizeText(source.commandLabel) || "Advanced Inspector" |
| 182 | + }) |
| 183 | + ]; |
| 184 | + }, |
| 185 | + update(context = {}) { |
| 186 | + return host.update(context); |
| 187 | + }, |
| 188 | + getSnapshot() { |
| 189 | + return host.getSnapshot(); |
| 190 | + } |
| 191 | + }; |
| 192 | +} |
0 commit comments