@playcanvas/inspector 0.2.0 → 0.4.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.
Files changed (49) hide show
  1. package/README.md +147 -5
  2. package/package.json +1 -1
  3. package/src/asset-view.js +24 -17
  4. package/src/camera-view.js +5 -3
  5. package/src/context-menu.js +231 -0
  6. package/src/copy-json.js +692 -0
  7. package/src/describe.js +20 -1
  8. package/src/entity-inspector-script.js +10 -0
  9. package/src/frame-graph-view.js +41 -9
  10. package/src/hierarchy-view.js +29 -3
  11. package/src/inspector.js +958 -80
  12. package/src/list-view.js +118 -12
  13. package/src/material-view.js +7 -6
  14. package/src/memory-view.js +224 -66
  15. package/src/mesh-view.js +6 -5
  16. package/src/model.js +24 -2
  17. package/src/node-model.js +81 -11
  18. package/src/perf-capture.js +2034 -0
  19. package/src/perf-chart.js +3989 -0
  20. package/src/perf-copy.js +367 -0
  21. package/src/perf-view.js +1224 -0
  22. package/src/property-view.js +162 -5
  23. package/src/render-target-view.js +3 -3
  24. package/src/script-view.js +21 -12
  25. package/src/styles.js +374 -2
  26. package/src/texture-view.js +4 -4
  27. package/src/transform-tool.js +185 -0
  28. package/types/asset-view.d.ts +3 -1
  29. package/types/camera-view.d.ts +4 -0
  30. package/types/context-menu.d.ts +90 -0
  31. package/types/copy-json.d.ts +69 -0
  32. package/types/describe.d.ts +1 -1
  33. package/types/entity-inspector-script.d.ts +8 -0
  34. package/types/frame-graph-view.d.ts +7 -0
  35. package/types/hierarchy-view.d.ts +13 -0
  36. package/types/inspector.d.ts +339 -25
  37. package/types/list-view.d.ts +44 -3
  38. package/types/material-view.d.ts +3 -1
  39. package/types/memory-view.d.ts +24 -25
  40. package/types/mesh-view.d.ts +3 -1
  41. package/types/model.d.ts +16 -1
  42. package/types/node-model.d.ts +17 -1
  43. package/types/perf-capture.d.ts +871 -0
  44. package/types/perf-chart.d.ts +1407 -0
  45. package/types/perf-copy.d.ts +61 -0
  46. package/types/perf-view.d.ts +231 -0
  47. package/types/property-view.d.ts +41 -0
  48. package/types/script-view.d.ts +3 -1
  49. package/types/transform-tool.d.ts +92 -0
@@ -1,9 +1,24 @@
1
+ import { copyRow, copySection, copySubject } from './copy-json.js';
2
+ import { describeValue } from './describe.js';
1
3
  import { INDENT_COLORS } from './styles.js';
2
4
  import { setTip } from './tooltip.js';
3
5
 
6
+ /** @import { CopyOptions, CopyResult } from './copy-json.js' */
4
7
  /** @import { Described } from './describe.js' */
5
8
  /** @import { PropertyRow, PropertySection } from './model.js' */
6
9
 
10
+ /**
11
+ * Something a context menu of the panel can copy.
12
+ *
13
+ * @typedef {object} CopyTarget
14
+ * @ignore
15
+ * @property {string} title - What is copied, as the menu names it.
16
+ * @property {(options: CopyOptions) => CopyResult} copy - Makes the copy.
17
+ * @property {boolean} [plain] - Whether it has no links to follow, so the menu offers no deeper
18
+ * copies, as the figures of the Performance tab.
19
+ * @property {() => string} [csv] - Makes the copy as CSV, for a series of frames.
20
+ */
21
+
7
22
  /**
8
23
  * @typedef {object} RowElements
9
24
  * @ignore
@@ -25,6 +40,9 @@ import { setTip } from './tooltip.js';
25
40
  * @property {Described['actions']|null} actions - The actions the buttons run, kept current each refresh.
26
41
  * @property {HTMLElement} caretEl - The caret of a row that expands, its own click target.
27
42
  * @property {(() => void)|null} select - What clicking the row chooses, kept current each refresh.
43
+ * @property {PropertyRow|null} row - The row last rendered, which a copy of it is made from.
44
+ * @property {Described['edit']|null} edit - The edit of the value, kept current each refresh.
45
+ * @property {HTMLInputElement|null} editEl - The text field the value is edited in, while it is.
28
46
  */
29
47
 
30
48
  /**
@@ -34,8 +52,12 @@ import { setTip } from './tooltip.js';
34
52
  * @property {HTMLElement} titleEl - The clickable title.
35
53
  * @property {HTMLElement} rowsEl - The container the rows live in.
36
54
  * @property {Map<string, RowElements>} rows - The rows by key.
55
+ * @property {PropertySection|null} section - The section last rendered, which a copy of it is made from.
37
56
  */
38
57
 
58
+ // the tooltip of an editable value
59
+ const EDIT_TIP = 'Click to edit. Enter applies, Esc cancels';
60
+
39
61
  // where the first indent guide sits, and how far apart the levels are, in CSS pixels
40
62
  const INDENT_BASE = 8;
41
63
  const INDENT_STEP = 14;
@@ -186,6 +208,14 @@ class PropertyView {
186
208
  */
187
209
  _emptyEl = null;
188
210
 
211
+ /**
212
+ * The rows and sections by the elements showing them, to find what a menu was opened on.
213
+ *
214
+ * @type {WeakMap<Element, RowElements|SectionElements>}
215
+ * @private
216
+ */
217
+ _byElement = new WeakMap();
218
+
189
219
  /**
190
220
  * @param {HTMLElement} container - The element to render into.
191
221
  * @param {(target: *) => void} onSelect - The value link callback.
@@ -252,6 +282,45 @@ class PropertyView {
252
282
  return top ?? 0;
253
283
  }
254
284
 
285
+ /**
286
+ * What a copy made from an element of the panel copies: the row or the section title the
287
+ * element is in, and anywhere else the subject itself.
288
+ *
289
+ * @param {Node} node - The element, such as the target of a context menu event.
290
+ * @returns {CopyTarget|null} What to copy, or null when the element is not in the panel or
291
+ * nothing is shown.
292
+ */
293
+ copyTargetAt(node) {
294
+ if (!this.container.contains(node)) return null;
295
+ // the node type rather than instanceof, as the element may come from another window's realm
296
+ let element = node.nodeType === 1 ? /** @type {Element} */ (node) : node.parentElement;
297
+ for (; element && element !== this.container; element = element.parentElement) {
298
+ const found = this._byElement.get(element);
299
+ if (!found) continue;
300
+ if ('row' in found && found.row) {
301
+ const row = found.row;
302
+ const text = row.value.text;
303
+ return { title: row.label ? `${row.label}: ${text}` : text, copy: options => copyRow(row, options) };
304
+ }
305
+ if ('section' in found && found.section) {
306
+ const section = found.section;
307
+ return { title: section.title, copy: options => copySection(section, options) };
308
+ }
309
+ }
310
+ return this.subjectCopyTarget();
311
+ }
312
+
313
+ /**
314
+ * @returns {CopyTarget|null} A copy of the subject, rebuilt when it is made so it reads the
315
+ * live object, or null when nothing is shown.
316
+ */
317
+ subjectCopyTarget() {
318
+ const subject = this._subject;
319
+ const buildModel = this._buildModel;
320
+ if (!subject || !buildModel) return null;
321
+ return { title: describeValue(subject, 1).text, copy: options => copySubject(subject, buildModel(subject), options) };
322
+ }
323
+
255
324
  /**
256
325
  * Re-reads every shown property and updates the text that changed.
257
326
  */
@@ -280,6 +349,7 @@ class PropertyView {
280
349
 
281
350
  for (const section of model) {
282
351
  const elements = this._sections.get(section.key) ?? this._createSection(section);
352
+ elements.section = section;
283
353
  seen.add(section.key);
284
354
 
285
355
  if (elements.titleEl.textContent !== section.title) {
@@ -317,6 +387,7 @@ class PropertyView {
317
387
 
318
388
  for (const row of this._flatten(section.key, rows, 0, '')) {
319
389
  const elements = section.rows.get(row.key) ?? this._createRow(section, row);
390
+ elements.row = row;
320
391
  seen.add(row.key);
321
392
 
322
393
  if (elements.labelEl.textContent !== row.label) {
@@ -350,7 +421,13 @@ class PropertyView {
350
421
  const expanded = expandable && this._expanded.has(`${section.key}\0${row.key}`);
351
422
  const caret = expandable ? `${expanded ? '▾' : '▸'} ` : '';
352
423
  if (elements.caretEl.textContent !== caret) elements.caretEl.textContent = caret;
353
- if (elements.text !== text) {
424
+ elements.edit = row.value.edit ?? null;
425
+ elements.el.classList.toggle('pci-editable', !!elements.edit);
426
+ const tip = elements.edit ? EDIT_TIP : '';
427
+ if ((elements.valueEl.dataset.tip ?? '') !== tip) setTip(elements.valueEl, tip);
428
+ if (elements.editEl && !elements.edit) this._endEdit(elements);
429
+ // the text field being typed in is left alone until it is done with
430
+ if (elements.text !== text && !elements.editEl) {
354
431
  elements.text = text;
355
432
  elements.valueEl.lastChild.textContent = text;
356
433
  }
@@ -523,6 +600,7 @@ class PropertyView {
523
600
 
524
601
  if (!elements.codeEl) {
525
602
  elements.codeEl = el('pre', 'pci-code');
603
+ this._byElement.set(elements.codeEl, elements);
526
604
  elements.code = '';
527
605
  section.rowsEl.insertBefore(elements.codeEl, elements.el.nextSibling);
528
606
  }
@@ -550,9 +628,11 @@ class PropertyView {
550
628
  el: el('div', 'pci-section'),
551
629
  titleEl: el('div', 'pci-section-title'),
552
630
  rowsEl: el('div', 'pci-rows'),
553
- rows: new Map()
631
+ rows: new Map(),
632
+ section: null
554
633
  };
555
634
  elements.el.append(elements.titleEl, elements.rowsEl);
635
+ this._byElement.set(elements.titleEl, elements);
556
636
  if (section.collapsed) this._collapsed.add(section.key);
557
637
  elements.titleEl.addEventListener('click', () => {
558
638
  if (this._collapsed.has(section.key)) this._collapsed.delete(section.key);
@@ -588,8 +668,12 @@ class PropertyView {
588
668
  actionsSignature: '',
589
669
  actions: null,
590
670
  caretEl: el('span', 'pci-caret'),
591
- select: null
671
+ select: null,
672
+ row: null,
673
+ edit: null,
674
+ editEl: null
592
675
  };
676
+ this._byElement.set(elements.el, elements);
593
677
  elements.valueEl.append(elements.caretEl, document.createTextNode(''));
594
678
  elements.el.append(elements.labelEl, elements.valueEl);
595
679
  // the whole row takes the click, so a choice can be made anywhere on it
@@ -598,6 +682,8 @@ class PropertyView {
598
682
  });
599
683
  elements.valueEl.addEventListener('pointerleave', () => this.onHover?.(null, null));
600
684
  elements.el.addEventListener('click', (e) => {
685
+ // a click in the open text field is the field's own
686
+ if (elements.editEl) return;
601
687
  const expandable = elements.el.classList.contains('pci-expandable');
602
688
  const toggle = () => {
603
689
  const key = `${section.key}\0${row.key}`;
@@ -606,8 +692,10 @@ class PropertyView {
606
692
  this.refresh();
607
693
  };
608
694
  // 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) {
695
+ // which only the value reacts to. An editable value opens its text field
696
+ if (elements.edit && elements.valueEl.contains(/** @type {Node} */ (e.target))) {
697
+ this._startEdit(elements);
698
+ } else if (expandable && e.target === elements.caretEl) {
611
699
  toggle();
612
700
  } else if (elements.select) {
613
701
  elements.select();
@@ -623,6 +711,75 @@ class PropertyView {
623
711
  section.rows.set(row.key, elements);
624
712
  return elements;
625
713
  }
714
+
715
+ /**
716
+ * Opens the text field of an editable value. Enter hands the text to the edit and closes the
717
+ * field once the edit takes it, or marks the text as not taken; leaving the field does the
718
+ * same, and gives up on text that is not taken; Escape closes it without a change.
719
+ *
720
+ * @param {RowElements} elements - The row.
721
+ * @private
722
+ */
723
+ _startEdit(elements) {
724
+ const start = elements.edit?.text;
725
+ if (start === undefined) return;
726
+
727
+ const input = /** @type {HTMLInputElement} */ (el('input', 'pci-edit'));
728
+ input.type = 'text';
729
+ input.spellcheck = false;
730
+ input.value = start;
731
+ elements.valueEl.lastChild.textContent = '';
732
+ elements.text = '';
733
+ elements.valueEl.appendChild(input);
734
+ elements.editEl = input;
735
+ elements.el.classList.add('pci-editing');
736
+
737
+ /**
738
+ * @param {boolean} commit - Whether to hand the text to the edit.
739
+ * @returns {boolean} Whether the field closed.
740
+ */
741
+ const finish = (commit) => {
742
+ if (elements.editEl !== input) return true;
743
+ // an untouched field changes nothing, as its text is only what the value read when it opened
744
+ if (commit && input.value !== start && !elements.edit?.apply(input.value)) {
745
+ input.classList.add('pci-invalid');
746
+ return false;
747
+ }
748
+ this._endEdit(elements);
749
+ this.refresh();
750
+ return true;
751
+ };
752
+ input.addEventListener('keydown', (e) => {
753
+ if (e.key === 'Enter') {
754
+ e.preventDefault();
755
+ finish(true);
756
+ } else if (e.key === 'Escape') {
757
+ e.preventDefault();
758
+ e.stopPropagation();
759
+ finish(false);
760
+ }
761
+ });
762
+ input.addEventListener('input', () => input.classList.remove('pci-invalid'));
763
+ input.addEventListener('blur', () => {
764
+ if (!finish(true)) finish(false);
765
+ });
766
+ input.focus();
767
+ input.select();
768
+ }
769
+
770
+ /**
771
+ * Closes the text field of a value, which shows its text again on the next refresh.
772
+ *
773
+ * @param {RowElements} elements - The row.
774
+ * @private
775
+ */
776
+ _endEdit(elements) {
777
+ const input = elements.editEl;
778
+ if (!input) return;
779
+ elements.editEl = null;
780
+ elements.el.classList.remove('pci-editing');
781
+ input.remove();
782
+ }
626
783
  }
627
784
 
628
785
  export { PropertyView };
@@ -3,7 +3,7 @@ import {
3
3
  } from 'playcanvas';
4
4
 
5
5
  import { describeValue } from './describe.js';
6
- import { attachmentsText, formatName, makeSection, passDisplayName, push, read, reflectRows } from './model.js';
6
+ import { attachmentsText, backlink, formatName, makeSection, passDisplayName, push, read, reflectRows } from './model.js';
7
7
 
8
8
  /** @import { GraphicsDevice } from 'playcanvas' */
9
9
  /** @import { RenderTarget } from 'playcanvas' */
@@ -141,11 +141,11 @@ function buildRenderTargetModel(rt, ctx) {
141
141
  push(general, 'initialized', read(rt, 'initialized'));
142
142
 
143
143
  const uses = frame?.usage.get(rt) ?? [];
144
- push(general, 'used by', {
144
+ push(general, 'used by', backlink({
145
145
  text: uses.length ? `${uses.length} pass${uses.length === 1 ? '' : 'es'} this frame` : 'no pass this frame',
146
146
  cls: uses.length ? 'obj' : 'null',
147
147
  items: uses.map(entry => ({ label: `#${entry.index}`, text: passDisplayName(entry.pass), cls: 'ref', target: entry.pass }))
148
- });
148
+ }));
149
149
  sections.push(general);
150
150
 
151
151
  const attachments = makeSection('attachments', 'Attachments');
@@ -2,7 +2,7 @@ import { Script, ScriptType } from 'playcanvas';
2
2
 
3
3
  import { describeValue } from './describe.js';
4
4
  import { nodeLabel } from './memory-view.js';
5
- import { makeSection, push } from './model.js';
5
+ import { makeSection, makeUsageSection, push } from './model.js';
6
6
  import { scriptInstanceRows } from './node-model.js';
7
7
 
8
8
  /** @import { AppBase } from 'playcanvas' */
@@ -228,7 +228,8 @@ function scriptListRows(survey) {
228
228
  * using it, each instance opening in place into its attributes, and its source.
229
229
  *
230
230
  * @param {Function} cls - The script class.
231
- * @param {{ app: AppBase }} ctx - The app.
231
+ * @param {{ app: AppBase, backlinks?: boolean }} ctx - The app, and whether to list the entities
232
+ * using the class, which a copy has no use for.
232
233
  * @returns {PropertySection[]} The sections.
233
234
  */
234
235
  function buildScriptModel(cls, ctx) {
@@ -257,8 +258,24 @@ function buildScriptModel(cls, ctx) {
257
258
  });
258
259
  }
259
260
  sections.push(general);
261
+ if (ctx.backlinks !== false) sections.push(instancesSection(instances));
260
262
 
261
- const usage = makeSection('instances', 'Used by');
263
+ const source = scriptSource(cls, name);
264
+ const code = makeSection('source', 'Source');
265
+ const lines = source.split('\n').length;
266
+ push(code, 'source', { text: `${lines} line${lines === 1 ? '' : 's'}`, cls: 'num', code: source });
267
+ sections.push(code);
268
+
269
+ return sections;
270
+ }
271
+
272
+ /**
273
+ * @param {{ script: ScriptType|Script, entity: Entity }[]} instances - The instances of a script
274
+ * class, with the entities they are on.
275
+ * @returns {PropertySection} The section listing them, each opening in place into its attributes.
276
+ */
277
+ function instancesSection(instances) {
278
+ const usage = makeUsageSection('instances', 'Used by');
262
279
  const running = instances.filter(({ script, entity }) => isRunning(script, entity)).length;
263
280
  push(usage, 'instances', {
264
281
  text: instances.length ? `${instances.length} instance${instances.length === 1 ? '' : 's'}, ${running} running` : 'none',
@@ -274,15 +291,7 @@ function buildScriptModel(cls, ctx) {
274
291
  if (instances.length > MAX_INSTANCES) {
275
292
  usage.rows.push({ key: 'more', label: '', value: { text: `… ${instances.length - MAX_INSTANCES} more`, cls: 'null' }, depth: 1 });
276
293
  }
277
- sections.push(usage);
278
-
279
- const source = scriptSource(cls, name);
280
- const code = makeSection('source', 'Source');
281
- const lines = source.split('\n').length;
282
- push(code, 'source', { text: `${lines} line${lines === 1 ? '' : 's'}`, cls: 'num', code: source });
283
- sections.push(code);
284
-
285
- return sections;
294
+ return usage;
286
295
  }
287
296
 
288
297
  export { buildScriptModel, isScriptClass, scriptListRows, surveyScripts };