@playcanvas/inspector 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +110 -0
- package/package.json +62 -0
- package/src/asset-usage.js +108 -0
- package/src/asset-view.js +370 -0
- package/src/camera-view.js +128 -0
- package/src/debug.js +31 -0
- package/src/describe.js +302 -0
- package/src/entity-inspector-script.js +130 -0
- package/src/frame-graph-view.js +767 -0
- package/src/hierarchy-view.js +475 -0
- package/src/index.js +4 -0
- package/src/input/fly-controller.js +116 -0
- package/src/input/input.js +299 -0
- package/src/input/math.js +8 -0
- package/src/input/orbit-controller.js +158 -0
- package/src/input/pose.js +207 -0
- package/src/inspector.js +3293 -0
- package/src/instance-survey.js +50 -0
- package/src/list-view.js +410 -0
- package/src/material-view.js +197 -0
- package/src/memory-view.js +344 -0
- package/src/mesh-view.js +235 -0
- package/src/model.js +170 -0
- package/src/node-model.js +404 -0
- package/src/physics-debug.js +527 -0
- package/src/physics-view.js +314 -0
- package/src/picker/picker.js +545 -0
- package/src/picker/render-pass-picker.js +206 -0
- package/src/property-view.js +628 -0
- package/src/render-target-view.js +288 -0
- package/src/renderers/texture-renderer-shaders.js +139 -0
- package/src/renderers/texture-renderer.js +430 -0
- package/src/renderers/wire-renderer.js +954 -0
- package/src/script-view.js +288 -0
- package/src/shader-view.js +287 -0
- package/src/styles.js +918 -0
- package/src/texture-view.js +186 -0
- package/src/tooltip.js +72 -0
- package/src/viewport-tools.js +695 -0
- package/types/asset-usage.d.ts +23 -0
- package/types/asset-view.d.ts +94 -0
- package/types/camera-view.d.ts +45 -0
- package/types/debug.d.ts +14 -0
- package/types/describe.d.ts +43 -0
- package/types/entity-inspector-script.d.ts +87 -0
- package/types/frame-graph-view.d.ts +155 -0
- package/types/hierarchy-view.d.ts +172 -0
- package/types/index.d.ts +3 -0
- package/types/input/fly-controller.d.ts +42 -0
- package/types/input/input.d.ts +192 -0
- package/types/input/math.d.ts +1 -0
- package/types/input/orbit-controller.d.ts +63 -0
- package/types/input/pose.d.ts +126 -0
- package/types/inspector.d.ts +1304 -0
- package/types/instance-survey.d.ts +35 -0
- package/types/list-view.d.ts +151 -0
- package/types/material-view.d.ts +63 -0
- package/types/memory-view.d.ts +95 -0
- package/types/mesh-view.d.ts +89 -0
- package/types/model.d.ts +104 -0
- package/types/node-model.d.ts +54 -0
- package/types/physics-debug.d.ts +254 -0
- package/types/physics-view.d.ts +81 -0
- package/types/picker/picker.d.ts +255 -0
- package/types/picker/render-pass-picker.d.ts +47 -0
- package/types/property-view.d.ts +189 -0
- package/types/render-target-view.d.ts +98 -0
- package/types/renderers/texture-renderer-shaders.d.ts +9 -0
- package/types/renderers/texture-renderer.d.ts +231 -0
- package/types/renderers/wire-renderer.d.ts +347 -0
- package/types/script-view.d.ts +56 -0
- package/types/shader-view.d.ts +46 -0
- package/types/styles.d.ts +22 -0
- package/types/texture-view.d.ts +30 -0
- package/types/tooltip.d.ts +17 -0
- package/types/viewport-tools.d.ts +237 -0
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
import { INDENT_COLORS } from './styles.js';
|
|
2
|
+
import { setTip } from './tooltip.js';
|
|
3
|
+
|
|
4
|
+
/** @import { Described } from './describe.js' */
|
|
5
|
+
/** @import { PropertyRow, PropertySection } from './model.js' */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} RowElements
|
|
9
|
+
* @ignore
|
|
10
|
+
* @property {HTMLElement} el - The row.
|
|
11
|
+
* @property {HTMLElement} labelEl - The label.
|
|
12
|
+
* @property {HTMLElement} valueEl - The value.
|
|
13
|
+
* @property {HTMLElement|null} swatchEl - The color chip, if any.
|
|
14
|
+
* @property {string} text - The value text last rendered.
|
|
15
|
+
* @property {string} cls - The value class last rendered.
|
|
16
|
+
* @property {*} target - The object the value links to, if any.
|
|
17
|
+
* @property {HTMLElement|null} codeEl - The code block under the row, once expanded.
|
|
18
|
+
* @property {HTMLElement|null} copyEl - The copy button of a code row.
|
|
19
|
+
* @property {string} code - The code last rendered.
|
|
20
|
+
* @property {number} depth - How far the row is indented.
|
|
21
|
+
* @property {boolean} group - Whether the row is drawn with a divider above it.
|
|
22
|
+
* @property {boolean} groupEnd - Whether the row is drawn with a divider below it.
|
|
23
|
+
* @property {HTMLElement|null} actionsEl - The buttons of the row, if any.
|
|
24
|
+
* @property {string} actionsSignature - What the buttons last rendered, to rebuild them only on change.
|
|
25
|
+
* @property {Described['actions']|null} actions - The actions the buttons run, kept current each refresh.
|
|
26
|
+
* @property {HTMLElement} caretEl - The caret of a row that expands, its own click target.
|
|
27
|
+
* @property {(() => void)|null} select - What clicking the row chooses, kept current each refresh.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} SectionElements
|
|
32
|
+
* @ignore
|
|
33
|
+
* @property {HTMLElement} el - The section.
|
|
34
|
+
* @property {HTMLElement} titleEl - The clickable title.
|
|
35
|
+
* @property {HTMLElement} rowsEl - The container the rows live in.
|
|
36
|
+
* @property {Map<string, RowElements>} rows - The rows by key.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
// where the first indent guide sits, and how far apart the levels are, in CSS pixels
|
|
40
|
+
const INDENT_BASE = 8;
|
|
41
|
+
const INDENT_STEP = 14;
|
|
42
|
+
|
|
43
|
+
/** @type {{ image: string, position: string }[]} */
|
|
44
|
+
const indentGuideCache = [];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The background that draws one vertical guide per indent level of a row, in the manner of a code
|
|
48
|
+
* editor. Rows are flat siblings, so the guides are painted per row rather than drawn around a
|
|
49
|
+
* container. Built once per depth, of which there are only a handful.
|
|
50
|
+
*
|
|
51
|
+
* @param {number} depth - How far the row is indented.
|
|
52
|
+
* @returns {{ image: string, position: string }|null} The layers and where they sit, or null when
|
|
53
|
+
* the row is not indented.
|
|
54
|
+
*/
|
|
55
|
+
function indentGuides(depth) {
|
|
56
|
+
if (depth <= 0) return null;
|
|
57
|
+
let guides = indentGuideCache[depth];
|
|
58
|
+
if (!guides) {
|
|
59
|
+
const layers = [];
|
|
60
|
+
const positions = [];
|
|
61
|
+
for (let i = 0; i < depth; i++) {
|
|
62
|
+
const color = INDENT_COLORS[i % INDENT_COLORS.length];
|
|
63
|
+
layers.push(`linear-gradient(${color}, ${color})`);
|
|
64
|
+
positions.push(`${INDENT_BASE + i * INDENT_STEP}px 0`);
|
|
65
|
+
}
|
|
66
|
+
guides = { image: layers.join(', '), position: positions.join(', ') };
|
|
67
|
+
indentGuideCache[depth] = guides;
|
|
68
|
+
}
|
|
69
|
+
return guides;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} tag - The element tag.
|
|
74
|
+
* @param {string} className - The class name.
|
|
75
|
+
* @returns {HTMLElement} The element.
|
|
76
|
+
*/
|
|
77
|
+
function el(tag, className) {
|
|
78
|
+
const element = document.createElement(tag);
|
|
79
|
+
element.className = className;
|
|
80
|
+
return element;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A read-only property panel for any subject a model builder can describe: a node, a render pass,
|
|
85
|
+
* a render target. Sections and rows are keyed and reconciled in place, so refreshing at a high
|
|
86
|
+
* rate only touches the text that actually changed and never resets the scroll position, the
|
|
87
|
+
* collapsed sections or the rows opened in place.
|
|
88
|
+
*
|
|
89
|
+
* A value carrying `expand` opens under its own row, to any depth, and one carrying `code` opens
|
|
90
|
+
* as a numbered block of text. Both are built on every refresh, so they follow the live object.
|
|
91
|
+
*
|
|
92
|
+
* @ignore
|
|
93
|
+
*/
|
|
94
|
+
class PropertyView {
|
|
95
|
+
/**
|
|
96
|
+
* The element the panel is rendered into.
|
|
97
|
+
*
|
|
98
|
+
* @type {HTMLElement}
|
|
99
|
+
*/
|
|
100
|
+
container;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Called with the target of a value link when it is clicked.
|
|
104
|
+
*
|
|
105
|
+
* @type {(target: *) => void}
|
|
106
|
+
*/
|
|
107
|
+
onSelect;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Called when the pointer enters a link, with the link element and a function reading the
|
|
111
|
+
* link's current target, and with nulls when it leaves. The target is read through the
|
|
112
|
+
* function because a refresh can point the same element at a new target while it is hovered.
|
|
113
|
+
*
|
|
114
|
+
* @type {((el: HTMLElement|null, target: (() => *)|null) => void)|undefined}
|
|
115
|
+
*/
|
|
116
|
+
onHover;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @type {*}
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
_subject = null;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @type {*}
|
|
126
|
+
* @private
|
|
127
|
+
*/
|
|
128
|
+
_key = null;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @type {((subject: *) => PropertySection[])|null}
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
_buildModel = null;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @type {Map<string, SectionElements>}
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
_sections = new Map();
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @type {Set<string>}
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
_collapsed = new Set();
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Keys of the rows opened in place, as section key and row key.
|
|
150
|
+
*
|
|
151
|
+
* @type {Set<string>}
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
_expanded = new Set();
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* How deep a chain of expanded rows may go, in case a model ever builds a cycle.
|
|
158
|
+
*
|
|
159
|
+
* @type {number}
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
_maxDepth = 8;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* How far each subject was scrolled when it was last shown, so following a link to another tab
|
|
166
|
+
* and coming back lands where it left off. Object keys are held weakly, so remembering where a
|
|
167
|
+
* texture or an entity was read does not keep it alive.
|
|
168
|
+
*
|
|
169
|
+
* @type {WeakMap<object, number>}
|
|
170
|
+
* @private
|
|
171
|
+
*/
|
|
172
|
+
_scrollByObject = new WeakMap();
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The same, for subjects identified by a key rather than by the object itself, such as the
|
|
176
|
+
* render passes of a frame, which are recreated every frame.
|
|
177
|
+
*
|
|
178
|
+
* @type {Map<*, number>}
|
|
179
|
+
* @private
|
|
180
|
+
*/
|
|
181
|
+
_scrollByKey = new Map();
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @type {HTMLElement|null}
|
|
185
|
+
* @private
|
|
186
|
+
*/
|
|
187
|
+
_emptyEl = null;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {HTMLElement} container - The element to render into.
|
|
191
|
+
* @param {(target: *) => void} onSelect - The value link callback.
|
|
192
|
+
*/
|
|
193
|
+
constructor(container, onSelect) {
|
|
194
|
+
this.container = container;
|
|
195
|
+
this.onSelect = onSelect;
|
|
196
|
+
this.refresh();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The subject being shown.
|
|
201
|
+
*
|
|
202
|
+
* @type {*}
|
|
203
|
+
*/
|
|
204
|
+
get subject() {
|
|
205
|
+
return this._subject;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Shows a subject. The DOM is rebuilt only when the key changes, so a subject that is
|
|
210
|
+
* recreated every frame under a stable key keeps its scroll position.
|
|
211
|
+
*
|
|
212
|
+
* @param {*} subject - The subject, or null to show nothing.
|
|
213
|
+
* @param {(subject: *) => PropertySection[]} [buildModel] - Turns the subject into sections.
|
|
214
|
+
* @param {*} [key] - Identifies the subject across refreshes. Defaults to the subject itself.
|
|
215
|
+
*/
|
|
216
|
+
setSubject(subject, buildModel, key = subject) {
|
|
217
|
+
const changed = key !== this._key || !subject;
|
|
218
|
+
if (changed) {
|
|
219
|
+
this._saveScroll(this._key);
|
|
220
|
+
this._key = subject ? key : null;
|
|
221
|
+
this.container.textContent = '';
|
|
222
|
+
this._sections.clear();
|
|
223
|
+
this._emptyEl = null;
|
|
224
|
+
}
|
|
225
|
+
this._subject = subject;
|
|
226
|
+
this._buildModel = buildModel ?? null;
|
|
227
|
+
this.refresh();
|
|
228
|
+
|
|
229
|
+
// the rows are rebuilt by now, so the remembered position has something to scroll to
|
|
230
|
+
if (changed) this.container.scrollTop = subject ? this._loadScroll(key) : 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @param {*} key - The subject key to remember the current position of, if any.
|
|
235
|
+
* @private
|
|
236
|
+
*/
|
|
237
|
+
_saveScroll(key) {
|
|
238
|
+
const top = this.container.scrollTop;
|
|
239
|
+
if (key === null || key === undefined) return;
|
|
240
|
+
if (typeof key === 'object') this._scrollByObject.set(key, top);
|
|
241
|
+
else this._scrollByKey.set(key, top);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @param {*} key - The subject key.
|
|
246
|
+
* @returns {number} How far it was scrolled when last shown, or the top.
|
|
247
|
+
* @private
|
|
248
|
+
*/
|
|
249
|
+
_loadScroll(key) {
|
|
250
|
+
const top = typeof key === 'object' && key !== null ?
|
|
251
|
+
this._scrollByObject.get(key) : this._scrollByKey.get(key);
|
|
252
|
+
return top ?? 0;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Re-reads every shown property and updates the text that changed.
|
|
257
|
+
*/
|
|
258
|
+
refresh() {
|
|
259
|
+
const subject = this._subject;
|
|
260
|
+
|
|
261
|
+
if (!subject || !this._buildModel) {
|
|
262
|
+
if (!this._emptyEl) {
|
|
263
|
+
this._emptyEl = el('div', 'pci-empty');
|
|
264
|
+
this._emptyEl.textContent = 'Select an item in the list above';
|
|
265
|
+
this.container.appendChild(this._emptyEl);
|
|
266
|
+
}
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this._renderSections(this._buildModel(subject));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* @param {PropertySection[]} model - The sections to show, in order.
|
|
275
|
+
* @private
|
|
276
|
+
*/
|
|
277
|
+
_renderSections(model) {
|
|
278
|
+
const seen = new Set();
|
|
279
|
+
let cursor = this.container.firstChild;
|
|
280
|
+
|
|
281
|
+
for (const section of model) {
|
|
282
|
+
const elements = this._sections.get(section.key) ?? this._createSection(section);
|
|
283
|
+
seen.add(section.key);
|
|
284
|
+
|
|
285
|
+
if (elements.titleEl.textContent !== section.title) {
|
|
286
|
+
elements.titleEl.textContent = section.title;
|
|
287
|
+
}
|
|
288
|
+
if (elements.el !== cursor) {
|
|
289
|
+
this.container.insertBefore(elements.el, cursor);
|
|
290
|
+
} else {
|
|
291
|
+
cursor = cursor.nextSibling;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const collapsed = this._collapsed.has(section.key);
|
|
295
|
+
elements.el.classList.toggle('pci-collapsed', collapsed);
|
|
296
|
+
if (!collapsed) {
|
|
297
|
+
this._renderRows(elements, section.rows);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const [key, elements] of this._sections) {
|
|
302
|
+
if (!seen.has(key)) {
|
|
303
|
+
elements.el.remove();
|
|
304
|
+
this._sections.delete(key);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {SectionElements} section - The section.
|
|
311
|
+
* @param {PropertyRow[]} rows - The rows to show, in order.
|
|
312
|
+
* @private
|
|
313
|
+
*/
|
|
314
|
+
_renderRows(section, rows) {
|
|
315
|
+
const seen = new Set();
|
|
316
|
+
let cursor = section.rowsEl.firstChild;
|
|
317
|
+
|
|
318
|
+
for (const row of this._flatten(section.key, rows, 0, '')) {
|
|
319
|
+
const elements = section.rows.get(row.key) ?? this._createRow(section, row);
|
|
320
|
+
seen.add(row.key);
|
|
321
|
+
|
|
322
|
+
if (elements.labelEl.textContent !== row.label) {
|
|
323
|
+
elements.labelEl.textContent = row.label;
|
|
324
|
+
}
|
|
325
|
+
const depth = row.depth ?? 0;
|
|
326
|
+
if (elements.depth !== depth) {
|
|
327
|
+
elements.depth = depth;
|
|
328
|
+
const guides = indentGuides(depth);
|
|
329
|
+
elements.el.classList.toggle('pci-indent', depth > 0);
|
|
330
|
+
elements.el.style.paddingLeft = depth ? `${INDENT_BASE + depth * INDENT_STEP}px` : '';
|
|
331
|
+
elements.el.style.backgroundImage = guides?.image ?? '';
|
|
332
|
+
elements.el.style.backgroundPosition = guides?.position ?? '';
|
|
333
|
+
elements.el.style.backgroundSize = guides ? '1px 100%' : '';
|
|
334
|
+
elements.el.style.backgroundRepeat = guides ? 'no-repeat' : '';
|
|
335
|
+
}
|
|
336
|
+
const group = !!row.group;
|
|
337
|
+
if (elements.group !== group) {
|
|
338
|
+
elements.group = group;
|
|
339
|
+
elements.el.classList.toggle('pci-group', group);
|
|
340
|
+
}
|
|
341
|
+
const groupEnd = !!row.groupEnd;
|
|
342
|
+
if (elements.groupEnd !== groupEnd) {
|
|
343
|
+
elements.groupEnd = groupEnd;
|
|
344
|
+
elements.el.classList.toggle('pci-group-end', groupEnd);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const { text, cls = 'obj', target = null, swatch, code } = row.value;
|
|
348
|
+
const hasCode = typeof code === 'string';
|
|
349
|
+
const expandable = hasCode || !!row.value.expand;
|
|
350
|
+
const expanded = expandable && this._expanded.has(`${section.key}\0${row.key}`);
|
|
351
|
+
const caret = expandable ? `${expanded ? '▾' : '▸'} ` : '';
|
|
352
|
+
if (elements.caretEl.textContent !== caret) elements.caretEl.textContent = caret;
|
|
353
|
+
if (elements.text !== text) {
|
|
354
|
+
elements.text = text;
|
|
355
|
+
elements.valueEl.lastChild.textContent = text;
|
|
356
|
+
}
|
|
357
|
+
elements.el.classList.toggle('pci-expandable', expandable);
|
|
358
|
+
elements.select = row.value.select ?? null;
|
|
359
|
+
elements.el.classList.toggle('pci-selectable', !!elements.select);
|
|
360
|
+
elements.el.classList.toggle('pci-active', !!row.value.active);
|
|
361
|
+
this._renderCode(section, elements, hasCode ? code : null, expanded);
|
|
362
|
+
this._renderActions(elements, row.value.actions ?? null);
|
|
363
|
+
if (elements.cls !== cls) {
|
|
364
|
+
elements.valueEl.classList.remove(`pci-v-${elements.cls}`);
|
|
365
|
+
elements.valueEl.classList.add(`pci-v-${cls}`);
|
|
366
|
+
elements.cls = cls;
|
|
367
|
+
}
|
|
368
|
+
if (elements.target !== target) {
|
|
369
|
+
elements.target = target;
|
|
370
|
+
elements.valueEl.classList.toggle('pci-link', !!target);
|
|
371
|
+
}
|
|
372
|
+
if (swatch) {
|
|
373
|
+
if (!elements.swatchEl) {
|
|
374
|
+
elements.swatchEl = el('i', 'pci-swatch');
|
|
375
|
+
elements.valueEl.prepend(elements.swatchEl);
|
|
376
|
+
}
|
|
377
|
+
if (elements.swatchEl.style.background !== swatch) {
|
|
378
|
+
elements.swatchEl.style.background = swatch;
|
|
379
|
+
}
|
|
380
|
+
} else if (elements.swatchEl) {
|
|
381
|
+
elements.swatchEl.remove();
|
|
382
|
+
elements.swatchEl = null;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (elements.el !== cursor) {
|
|
386
|
+
section.rowsEl.insertBefore(elements.el, cursor);
|
|
387
|
+
} else {
|
|
388
|
+
cursor = cursor.nextSibling;
|
|
389
|
+
}
|
|
390
|
+
// the code block follows its row
|
|
391
|
+
if (elements.codeEl) {
|
|
392
|
+
if (elements.codeEl !== cursor) {
|
|
393
|
+
section.rowsEl.insertBefore(elements.codeEl, cursor);
|
|
394
|
+
} else {
|
|
395
|
+
cursor = cursor.nextSibling;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
for (const [key, elements] of section.rows) {
|
|
401
|
+
if (!seen.has(key)) {
|
|
402
|
+
elements.el.remove();
|
|
403
|
+
elements.codeEl?.remove();
|
|
404
|
+
section.rows.delete(key);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The rows to show for a list of model rows: each row, followed by the rows of whatever it
|
|
411
|
+
* expands into while it is open. Keys are prefixed by their parent so that a label repeated at
|
|
412
|
+
* two depths, such as the material of two mesh instances, stays distinct.
|
|
413
|
+
*
|
|
414
|
+
* @param {string} sectionKey - The key of the section being rendered.
|
|
415
|
+
* @param {PropertyRow[]} rows - The rows to flatten.
|
|
416
|
+
* @param {number} depth - The depth the rows sit at.
|
|
417
|
+
* @param {string} prefix - The key of the row they expand from.
|
|
418
|
+
* @returns {PropertyRow[]} The flattened rows.
|
|
419
|
+
* @private
|
|
420
|
+
*/
|
|
421
|
+
_flatten(sectionKey, rows, depth, prefix) {
|
|
422
|
+
const out = [];
|
|
423
|
+
for (let i = 0; i < rows.length; i++) {
|
|
424
|
+
const row = rows[i];
|
|
425
|
+
const key = prefix ? `${prefix}/${row.key}` : row.key;
|
|
426
|
+
const rowDepth = depth + (row.depth ?? 0);
|
|
427
|
+
out.push({ ...row, key, depth: rowDepth });
|
|
428
|
+
|
|
429
|
+
const expanded = row.value.expand && this._expanded.has(`${sectionKey}\0${key}`);
|
|
430
|
+
if (expanded && rowDepth < this._maxDepth) {
|
|
431
|
+
let children;
|
|
432
|
+
try {
|
|
433
|
+
children = row.value.expand();
|
|
434
|
+
} catch (e) {
|
|
435
|
+
children = [{ key: 'error', label: '', value: { text: `<${e?.message ?? e}>`, cls: 'err' } }];
|
|
436
|
+
}
|
|
437
|
+
out.push(...this._flatten(sectionKey, children, rowDepth + 1, key));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// close the collection under its last entry, which is the last row that entry opened
|
|
441
|
+
if (row.group && !rows[i + 1]?.group) {
|
|
442
|
+
out[out.length - 1].groupEnd = true;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return out;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Keeps the buttons of a row in step with its value. The buttons are rebuilt only when their text
|
|
450
|
+
* or state changes, and always run the actions of the latest refresh, whose closures hold the
|
|
451
|
+
* current state.
|
|
452
|
+
*
|
|
453
|
+
* @param {RowElements} elements - The row.
|
|
454
|
+
* @param {Described['actions']|null} actions - The actions, or null for a row without any.
|
|
455
|
+
* @private
|
|
456
|
+
*/
|
|
457
|
+
_renderActions(elements, actions) {
|
|
458
|
+
elements.actions = actions;
|
|
459
|
+
const signature = actions ? actions.map(action => `${action.text}:${action.disabled ? 1 : 0}`).join('|') : '';
|
|
460
|
+
if (elements.actionsSignature === signature) return;
|
|
461
|
+
elements.actionsSignature = signature;
|
|
462
|
+
elements.actionsEl?.remove();
|
|
463
|
+
elements.actionsEl = null;
|
|
464
|
+
if (!actions?.length) return;
|
|
465
|
+
|
|
466
|
+
const wrap = el('span', 'pci-actions');
|
|
467
|
+
actions.forEach((action, index) => {
|
|
468
|
+
const button = /** @type {HTMLButtonElement} */ (el('button', 'pci-action'));
|
|
469
|
+
button.textContent = action.text;
|
|
470
|
+
setTip(button, action.title ?? '');
|
|
471
|
+
button.disabled = !!action.disabled;
|
|
472
|
+
button.addEventListener('click', (e) => {
|
|
473
|
+
e.stopPropagation();
|
|
474
|
+
elements.actions?.[index]?.run();
|
|
475
|
+
this.refresh();
|
|
476
|
+
});
|
|
477
|
+
wrap.appendChild(button);
|
|
478
|
+
});
|
|
479
|
+
elements.actionsEl = wrap;
|
|
480
|
+
elements.el.appendChild(wrap);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Keeps the copy button and the expanded code block of a row in step with its value.
|
|
485
|
+
*
|
|
486
|
+
* @param {SectionElements} section - The section the row belongs to.
|
|
487
|
+
* @param {RowElements} elements - The row.
|
|
488
|
+
* @param {string|null} code - The code, or null for a row without any.
|
|
489
|
+
* @param {boolean} expanded - Whether the block is shown.
|
|
490
|
+
* @private
|
|
491
|
+
*/
|
|
492
|
+
_renderCode(section, elements, code, expanded) {
|
|
493
|
+
if (code === null) {
|
|
494
|
+
elements.copyEl?.remove();
|
|
495
|
+
elements.copyEl = null;
|
|
496
|
+
elements.codeEl?.remove();
|
|
497
|
+
elements.codeEl = null;
|
|
498
|
+
elements.code = '';
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (!elements.copyEl) {
|
|
503
|
+
elements.copyEl = el('button', 'pci-copy');
|
|
504
|
+
elements.copyEl.textContent = 'Copy';
|
|
505
|
+
setTip(elements.copyEl, 'Copy to the clipboard');
|
|
506
|
+
elements.copyEl.addEventListener('click', (e) => {
|
|
507
|
+
e.stopPropagation();
|
|
508
|
+
navigator.clipboard?.writeText(elements.code);
|
|
509
|
+
elements.copyEl.textContent = 'Copied';
|
|
510
|
+
setTimeout(() => {
|
|
511
|
+
if (elements.copyEl) elements.copyEl.textContent = 'Copy';
|
|
512
|
+
}, 1200);
|
|
513
|
+
});
|
|
514
|
+
elements.el.appendChild(elements.copyEl);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (!expanded) {
|
|
518
|
+
elements.codeEl?.remove();
|
|
519
|
+
elements.codeEl = null;
|
|
520
|
+
elements.code = code;
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (!elements.codeEl) {
|
|
525
|
+
elements.codeEl = el('pre', 'pci-code');
|
|
526
|
+
elements.code = '';
|
|
527
|
+
section.rowsEl.insertBefore(elements.codeEl, elements.el.nextSibling);
|
|
528
|
+
}
|
|
529
|
+
// indent the block with its row, so it reads as part of the same level
|
|
530
|
+
const indent = `${INDENT_BASE + elements.depth * INDENT_STEP}px`;
|
|
531
|
+
if (elements.codeEl.style.marginLeft !== indent) elements.codeEl.style.marginLeft = indent;
|
|
532
|
+
if (elements.code !== code || !elements.codeEl.firstChild) {
|
|
533
|
+
elements.code = code;
|
|
534
|
+
elements.codeEl.textContent = '';
|
|
535
|
+
for (const line of code.split('\n')) {
|
|
536
|
+
const lineEl = el('span', 'pci-code-line');
|
|
537
|
+
lineEl.textContent = line;
|
|
538
|
+
elements.codeEl.appendChild(lineEl);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* @param {PropertySection} section - The section.
|
|
545
|
+
* @returns {SectionElements} The new section elements.
|
|
546
|
+
* @private
|
|
547
|
+
*/
|
|
548
|
+
_createSection(section) {
|
|
549
|
+
const elements = {
|
|
550
|
+
el: el('div', 'pci-section'),
|
|
551
|
+
titleEl: el('div', 'pci-section-title'),
|
|
552
|
+
rowsEl: el('div', 'pci-rows'),
|
|
553
|
+
rows: new Map()
|
|
554
|
+
};
|
|
555
|
+
elements.el.append(elements.titleEl, elements.rowsEl);
|
|
556
|
+
if (section.collapsed) this._collapsed.add(section.key);
|
|
557
|
+
elements.titleEl.addEventListener('click', () => {
|
|
558
|
+
if (this._collapsed.has(section.key)) this._collapsed.delete(section.key);
|
|
559
|
+
else this._collapsed.add(section.key);
|
|
560
|
+
this.refresh();
|
|
561
|
+
});
|
|
562
|
+
this._sections.set(section.key, elements);
|
|
563
|
+
return elements;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* @param {SectionElements} section - The section the row belongs to.
|
|
568
|
+
* @param {PropertyRow} row - The row.
|
|
569
|
+
* @returns {RowElements} The new row elements.
|
|
570
|
+
* @private
|
|
571
|
+
*/
|
|
572
|
+
_createRow(section, row) {
|
|
573
|
+
const elements = {
|
|
574
|
+
el: el('div', 'pci-prop'),
|
|
575
|
+
labelEl: el('span', 'pci-label'),
|
|
576
|
+
valueEl: el('span', 'pci-value pci-v-obj'),
|
|
577
|
+
swatchEl: null,
|
|
578
|
+
text: '',
|
|
579
|
+
cls: 'obj',
|
|
580
|
+
target: null,
|
|
581
|
+
codeEl: null,
|
|
582
|
+
copyEl: null,
|
|
583
|
+
code: '',
|
|
584
|
+
depth: -1,
|
|
585
|
+
group: false,
|
|
586
|
+
groupEnd: false,
|
|
587
|
+
actionsEl: null,
|
|
588
|
+
actionsSignature: '',
|
|
589
|
+
actions: null,
|
|
590
|
+
caretEl: el('span', 'pci-caret'),
|
|
591
|
+
select: null
|
|
592
|
+
};
|
|
593
|
+
elements.valueEl.append(elements.caretEl, document.createTextNode(''));
|
|
594
|
+
elements.el.append(elements.labelEl, elements.valueEl);
|
|
595
|
+
// the whole row takes the click, so a choice can be made anywhere on it
|
|
596
|
+
elements.valueEl.addEventListener('pointerenter', () => {
|
|
597
|
+
if (elements.target) this.onHover?.(elements.valueEl, () => elements.target);
|
|
598
|
+
});
|
|
599
|
+
elements.valueEl.addEventListener('pointerleave', () => this.onHover?.(null, null));
|
|
600
|
+
elements.el.addEventListener('click', (e) => {
|
|
601
|
+
const expandable = elements.el.classList.contains('pci-expandable');
|
|
602
|
+
const toggle = () => {
|
|
603
|
+
const key = `${section.key}\0${row.key}`;
|
|
604
|
+
if (this._expanded.has(key)) this._expanded.delete(key);
|
|
605
|
+
else this._expanded.add(key);
|
|
606
|
+
this.refresh();
|
|
607
|
+
};
|
|
608
|
+
// the caret always opens; elsewhere a choice wins over a link, and a link over opening,
|
|
609
|
+
// which only the value reacts to
|
|
610
|
+
if (expandable && e.target === elements.caretEl) {
|
|
611
|
+
toggle();
|
|
612
|
+
} else if (elements.select) {
|
|
613
|
+
elements.select();
|
|
614
|
+
this.refresh();
|
|
615
|
+
} else if (elements.valueEl.contains(/** @type {Node} */ (e.target))) {
|
|
616
|
+
if (elements.target) {
|
|
617
|
+
this.onSelect?.(elements.target);
|
|
618
|
+
} else if (expandable) {
|
|
619
|
+
toggle();
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
section.rows.set(row.key, elements);
|
|
624
|
+
return elements;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
export { PropertyView };
|