@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
@@ -0,0 +1,692 @@
1
+ import {
2
+ BoundingBox, Color, Curve, CurveSet, GraphNode, Mat3, Mat4, OrientedBox, Quat, Tags, Vec2, Vec3, Vec4
3
+ } from 'playcanvas';
4
+
5
+ import { collectProperties, describeValue } from './describe.js';
6
+
7
+ /** @import { Described } from './describe.js' */
8
+ /** @import { ListRow } from './list-view.js' */
9
+ /** @import { PropertyRow, PropertySection } from './model.js' */
10
+
11
+ /**
12
+ * How far a copy follows the links of what it copies.
13
+ *
14
+ * @typedef {object} CopyOptions
15
+ * @ignore
16
+ * @property {number} depth - How many links to follow from the object copied: 0 copies it alone,
17
+ * and Infinity follows every link there is.
18
+ * @property {(object: object) => PropertySection[]|PropertyRow[]|null} [modelFor] - The model of
19
+ * an object reached through a link, as the panel shows it, or null for one it has no model for,
20
+ * which is copied from its public properties instead.
21
+ * @property {(object: object) => boolean} [opaque] - Whether an object is never followed, such as
22
+ * the app and its device, through which everything else could be reached.
23
+ * @property {number} [maxObjects] - How many objects are copied in full before the rest are only
24
+ * referenced, so that copying a huge scene cannot stall the page for long.
25
+ * @property {(object: object) => boolean} [exclude] - Whether a child node belongs to the panel
26
+ * rather than the app, such as the entity of its gizmo, and is left out with everything below it.
27
+ */
28
+
29
+ /**
30
+ * @typedef {object} CopyResult
31
+ * @ignore
32
+ * @property {*} json - The copy, ready to be stringified.
33
+ * @property {number} objects - The number of objects copied in full.
34
+ * @property {boolean} truncated - Whether the copy stopped at `maxObjects`.
35
+ */
36
+
37
+ // how many objects a copy holds in full by default
38
+ const MAX_OBJECTS = 50000;
39
+
40
+ // how many entries of a list a copy holds, and how many numbers of a typed array: a long one
41
+ // holds data such as vertices, which only its length and first values say anything about here
42
+ const MAX_ENTRIES = 10000;
43
+ const MAX_NUMBERS = 64;
44
+
45
+ // the properties of an object without a model that are left out, as every tab leaves them out:
46
+ // the device, and the objects of the graphics backend behind a resource
47
+ const SKIP_REFLECTED = ['device', 'impl'];
48
+
49
+ // the properties that tell an object apart, for a reference to one that is not copied in full
50
+ const IDENTITY = ['guid', 'id', 'name', 'scriptName', 'path'];
51
+
52
+ /**
53
+ * @param {*} value - Any object or function.
54
+ * @returns {string} The name of its class.
55
+ */
56
+ function typeName(value) {
57
+ if (typeof value === 'function') return 'Function';
58
+ return value?.constructor?.name || 'Object';
59
+ }
60
+
61
+ /**
62
+ * @param {*} n - A number, or a bigint from a typed array.
63
+ * @returns {number|string} The number, or its text where JSON has no number for it, such as NaN.
64
+ */
65
+ function num(n) {
66
+ return typeof n === 'number' && Number.isFinite(n) ? n : String(n);
67
+ }
68
+
69
+ /**
70
+ * @param {*} e - A thrown value.
71
+ * @returns {string} Its message, the way the panel shows a getter that throws.
72
+ */
73
+ function errorText(e) {
74
+ return `<${e?.message ?? e}>`;
75
+ }
76
+
77
+ /**
78
+ * @param {object} out - The object being filled.
79
+ * @param {string} key - The key wanted.
80
+ * @returns {string} The key, numbered when it is taken, as two sections can share a title.
81
+ */
82
+ function uniqueKey(out, key) {
83
+ if (!Object.hasOwn(out, key)) return key;
84
+ let i = 2;
85
+ while (Object.hasOwn(out, `${key} (${i})`)) i++;
86
+ return `${key} (${i})`;
87
+ }
88
+
89
+ /**
90
+ * @param {*} value - A value.
91
+ * @returns {boolean} Whether it is a plain object, such as a JSON attribute of a script.
92
+ */
93
+ function isPlain(value) {
94
+ const proto = Object.getPrototypeOf(value);
95
+ return proto === Object.prototype || proto === null;
96
+ }
97
+
98
+ /**
99
+ * Whether a value is an object of the browser rather than of the app, such as an element, a
100
+ * rendering context or a promise. These name their kind through `Symbol.toStringTag`, which the
101
+ * classes of the engine and of apps do not, and are summarized rather than followed, as reading
102
+ * their properties says little and some of them reach the whole page.
103
+ *
104
+ * @param {object} value - An object.
105
+ * @returns {boolean} Whether it is a browser object.
106
+ */
107
+ function isHostObject(value) {
108
+ try {
109
+ return typeof /** @type {any} */ (value)[Symbol.toStringTag] === 'string';
110
+ } catch {
111
+ return true;
112
+ }
113
+ }
114
+
115
+ /**
116
+ * @param {PropertySection[]|PropertyRow[]} model - A model.
117
+ * @returns {boolean} Whether it is a list of sections rather than of rows.
118
+ */
119
+ function isSections(model) {
120
+ return model.length > 0 && Array.isArray(/** @type {any} */ (model[0]).rows);
121
+ }
122
+
123
+ /**
124
+ * The object a value stands for: the value it was made from, or else the object it links to.
125
+ * Plain data, such as a vector or a list, is not an object of its own but part of its holder.
126
+ *
127
+ * @param {Described} described - A value of the panel.
128
+ * @returns {object|null} The object, or null when the value is plain data.
129
+ */
130
+ function objectOf(described) {
131
+ const value = 'raw' in described ? described.raw : described.target;
132
+ if (value === null || value === undefined) return null;
133
+ if (typeof value === 'function') return value;
134
+ if (typeof value !== 'object' || isPlain(value) || Array.isArray(value) || ArrayBuffer.isView(value)) return null;
135
+ if (value instanceof Set || value instanceof Map || value instanceof Tags) return null;
136
+ if (value instanceof Vec2 || value instanceof Vec3 || value instanceof Vec4 || value instanceof Quat || value instanceof Color) return null;
137
+ if (value instanceof Mat3 || value instanceof Mat4 || value instanceof BoundingBox || value instanceof OrientedBox) return null;
138
+ if (value instanceof Curve || value instanceof CurveSet || isHostObject(value)) return null;
139
+ return value;
140
+ }
141
+
142
+ /**
143
+ * Turns what the panel shows into JSON. Values are copied from what they were made from, so
144
+ * numbers keep their precision and lists their length. Objects reached through links are copied
145
+ * the way their own tab shows them, up to a number of links away; beyond that, and wherever an
146
+ * object comes up again, a reference names it instead.
147
+ *
148
+ * Links never lead back up: a node's parent and the lists of what uses an object are left out,
149
+ * and the scene is followed down through the children of nodes only, so a link to another node
150
+ * is a reference. The app's own objects, such as its device and scene, are references too, as
151
+ * everything else can be reached through them.
152
+ *
153
+ * @ignore
154
+ */
155
+ class JsonCopy {
156
+ /**
157
+ * @type {number}
158
+ * @private
159
+ */
160
+ _depth;
161
+
162
+ /**
163
+ * @type {(object: object) => PropertySection[]|PropertyRow[]|null}
164
+ * @private
165
+ */
166
+ _modelFor;
167
+
168
+ /**
169
+ * @type {(object: object) => boolean}
170
+ * @private
171
+ */
172
+ _opaque;
173
+
174
+ /**
175
+ * @type {number}
176
+ * @private
177
+ */
178
+ _maxObjects;
179
+
180
+ /**
181
+ * @type {(object: object) => boolean}
182
+ * @private
183
+ */
184
+ _exclude;
185
+
186
+ /**
187
+ * The objects copied in full so far, so that the next mention of one is a reference.
188
+ *
189
+ * @type {Set<object>}
190
+ * @private
191
+ */
192
+ _copied = new Set();
193
+
194
+ /**
195
+ * The plain objects and lists being copied, so that a cycle through them is cut.
196
+ *
197
+ * @type {Set<object>}
198
+ * @private
199
+ */
200
+ _open = new Set();
201
+
202
+ /**
203
+ * The number of objects copied in full.
204
+ *
205
+ * @type {number}
206
+ */
207
+ objects = 0;
208
+
209
+ /**
210
+ * Whether the copy stopped at its object limit.
211
+ *
212
+ * @type {boolean}
213
+ */
214
+ truncated = false;
215
+
216
+ /**
217
+ * @param {CopyOptions} options - How far to follow links.
218
+ */
219
+ constructor(options) {
220
+ this._depth = options.depth;
221
+ this._modelFor = options.modelFor ?? (() => null);
222
+ this._opaque = options.opaque ?? (() => false);
223
+ this._maxObjects = options.maxObjects ?? MAX_OBJECTS;
224
+ this._exclude = options.exclude ?? (() => false);
225
+ }
226
+
227
+ /**
228
+ * @param {*} json - The copy.
229
+ * @returns {CopyResult} The copy, with how much of it there is.
230
+ */
231
+ result(json) {
232
+ return { json, objects: this.objects, truncated: this.truncated };
233
+ }
234
+
235
+ /**
236
+ * Copies an object found as a value.
237
+ *
238
+ * @param {object} obj - The object.
239
+ * @param {number} level - The number of links followed to reach it.
240
+ * @param {PropertyRow[]} [rows] - The rows the panel opens it into in place, if it does.
241
+ * @returns {*} The copy.
242
+ */
243
+ link(obj, level, rows) {
244
+ if (level > 0 && (obj instanceof GraphNode || this._opaque(obj))) return this.reference(obj, '$stub');
245
+ return this.object(obj, level, rows);
246
+ }
247
+
248
+ /**
249
+ * Copies an object in full, or as a reference when it is out of reach or already copied.
250
+ *
251
+ * @param {object} obj - The object.
252
+ * @param {number} level - The number of links followed to reach it.
253
+ * @param {PropertySection[]|PropertyRow[]} [model] - What the panel shows for it, when that is
254
+ * known. Defaults to the model of its own tab.
255
+ * @returns {*} The copy.
256
+ */
257
+ object(obj, level, model) {
258
+ if (this._copied.has(obj)) return this.reference(obj, '$ref');
259
+ if (level > this._depth) return this.reference(obj, '$stub');
260
+ if (this.objects >= this._maxObjects) {
261
+ this.truncated = true;
262
+ return { ...this.reference(obj, '$stub'), $truncated: true };
263
+ }
264
+
265
+ const content = model ?? this._modelFor(obj);
266
+ // a function only has the properties every function has, so its name says it all
267
+ if (!content && typeof obj === 'function') return `ƒ ${obj.name || 'anonymous'}`;
268
+
269
+ this._copied.add(obj);
270
+ this.objects++;
271
+
272
+ const out = { $type: typeName(obj) };
273
+ if (!content) {
274
+ Object.assign(out, this.reflect(obj, level));
275
+ } else if (isSections(content)) {
276
+ Object.assign(out, this.sections(/** @type {PropertySection[]} */ (content), level));
277
+ } else {
278
+ Object.assign(out, this.rows(/** @type {PropertyRow[]} */ (content), level));
279
+ }
280
+ if (obj instanceof GraphNode) {
281
+ out.children = obj.children.filter(child => !this._exclude(child)).map(child => this.object(child, level + 1));
282
+ }
283
+ return out;
284
+ }
285
+
286
+ /**
287
+ * @param {object} obj - An object not copied in full.
288
+ * @param {'$ref'|'$stub'} flag - `$ref` for one copied in full elsewhere in the copy, `$stub`
289
+ * for one out of reach.
290
+ * @returns {object} What tells the object apart.
291
+ */
292
+ reference(obj, flag) {
293
+ const out = { $type: typeName(obj) };
294
+ for (const name of IDENTITY) {
295
+ let value;
296
+ try {
297
+ value = /** @type {any} */ (obj)[name];
298
+ } catch {
299
+ continue;
300
+ }
301
+ if (typeof value === 'string' || typeof value === 'number') out[name] = value;
302
+ }
303
+ // an object without a name of its own is told apart by its summary in the panel
304
+ if (Object.keys(out).length === 1) {
305
+ const summary = describeValue(obj, 1).text;
306
+ if (summary !== out.$type) out.$summary = summary;
307
+ }
308
+ out[flag] = true;
309
+ return out;
310
+ }
311
+
312
+ /**
313
+ * @param {PropertySection[]} sections - The sections of an object.
314
+ * @param {number} level - The number of links followed to reach the object.
315
+ * @returns {object} The sections by title, without those listing what uses the object.
316
+ */
317
+ sections(sections, level) {
318
+ const out = {};
319
+ for (const section of sections) {
320
+ if (section.backlink) continue;
321
+ out[uniqueKey(out, section.title)] = this.rows(section.rows, level);
322
+ }
323
+ return out;
324
+ }
325
+
326
+ /**
327
+ * @param {PropertyRow[]} rows - The rows of an object, or of a section of one.
328
+ * @param {number} level - The number of links followed to reach the object.
329
+ * @returns {object} The values by label.
330
+ */
331
+ rows(rows, level) {
332
+ const out = {};
333
+ for (const row of rows) {
334
+ // the entries of a collection follow its row, which copies them from its value
335
+ if (row.depth || row.value.backlink) continue;
336
+ out[uniqueKey(out, row.label || row.key)] = this.described(row.value, level);
337
+ }
338
+ return out;
339
+ }
340
+
341
+ /**
342
+ * @param {Described} described - A value of the panel.
343
+ * @param {number} level - The number of links followed to reach the object holding it.
344
+ * @returns {*} The copy.
345
+ */
346
+ described(described, level) {
347
+ const obj = objectOf(described);
348
+ if (described.expand) {
349
+ let rows;
350
+ try {
351
+ rows = described.expand();
352
+ } catch (e) {
353
+ return errorText(e);
354
+ }
355
+ // an object opened in place is a link like any other; anything else is a detail of its holder
356
+ return obj ? this.link(obj, level + 1, rows) : this.rows(rows, level);
357
+ }
358
+ if ('raw' in described) return this.value(described.raw, level);
359
+ if (described.items) return this.items(described.items, level);
360
+ if (obj) return this.link(obj, level + 1);
361
+ // source code only for the object copied, as the sources of everything it reaches would bury it
362
+ if (typeof described.code === 'string' && level <= 0) return described.code;
363
+ return described.text;
364
+ }
365
+
366
+ /**
367
+ * @param {Described[]} items - The entries of a collection the panel built, each with a label.
368
+ * @param {number} level - The number of links followed to reach the object holding it.
369
+ * @returns {*} The entries: a list when they are only numbered, or else by label, as the
370
+ * versions of a shader's source are.
371
+ */
372
+ items(items, level) {
373
+ if (items.every(item => !item.label || /^\[\d+\]$/.test(item.label))) {
374
+ return items.map(item => this.described(item, level));
375
+ }
376
+ const out = {};
377
+ for (const item of items) {
378
+ out[uniqueKey(out, item.label ?? '')] = this.described(item, level);
379
+ }
380
+ return out;
381
+ }
382
+
383
+ /**
384
+ * @param {*} value - A value read from an object.
385
+ * @param {number} level - The number of links followed to reach the object holding it.
386
+ * @returns {*} The copy.
387
+ */
388
+ value(value, level) {
389
+ if (value === null || value === undefined) return null;
390
+ switch (typeof value) {
391
+ case 'boolean':
392
+ case 'string':
393
+ return value;
394
+ case 'number':
395
+ case 'bigint':
396
+ return num(value);
397
+ case 'symbol':
398
+ return value.toString();
399
+ case 'function':
400
+ return this.link(value, level + 1);
401
+ }
402
+
403
+ if (value instanceof Vec2) return [num(value.x), num(value.y)];
404
+ if (value instanceof Vec3) return [num(value.x), num(value.y), num(value.z)];
405
+ if (value instanceof Vec4 || value instanceof Quat) return [num(value.x), num(value.y), num(value.z), num(value.w)];
406
+ if (value instanceof Color) return [num(value.r), num(value.g), num(value.b), num(value.a)];
407
+ if (value instanceof Mat3 || value instanceof Mat4) return Array.from(value.data, num);
408
+ if (value instanceof OrientedBox) {
409
+ return { halfExtents: this.value(value.halfExtents, level), worldTransform: this.value(value.worldTransform, level) };
410
+ }
411
+ if (value instanceof BoundingBox) {
412
+ return { center: this.value(value.center, level), halfExtents: this.value(value.halfExtents, level) };
413
+ }
414
+ if (value instanceof Curve) return { type: value.type, keys: value.keys.map(key => Array.from(key, num)) };
415
+ if (value instanceof CurveSet) return { type: value.type, curves: value.curves.map(curve => this.value(curve, level)) };
416
+ if (value instanceof Tags) return value.list();
417
+
418
+ if (value instanceof DataView || value instanceof ArrayBuffer) return { $type: typeName(value), byteLength: value.byteLength };
419
+ if (ArrayBuffer.isView(value)) {
420
+ const numbers = /** @type {any} */ (value);
421
+ if (numbers.length <= MAX_NUMBERS) return Array.from(numbers, num);
422
+ return { $type: typeName(value), length: numbers.length, values: Array.from(numbers.subarray(0, MAX_NUMBERS), num), $truncated: true };
423
+ }
424
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
425
+ if (value instanceof RegExp) return String(value);
426
+
427
+ if (Array.isArray(value) || value instanceof Set) return this.list(value, level);
428
+ if (value instanceof Map) return this.map(value, level);
429
+ if (isPlain(value)) return this.plain(value, level);
430
+ if (isHostObject(value)) return describeValue(value, 1).text;
431
+
432
+ return this.link(value, level + 1);
433
+ }
434
+
435
+ /**
436
+ * @param {any[]|Set<*>} values - A list.
437
+ * @param {number} level - The number of links followed to reach the object holding it.
438
+ * @returns {*} The entries, or the first of them when there are too many to hold.
439
+ */
440
+ list(values, level) {
441
+ if (this._open.has(values)) return '<circular>';
442
+ this._open.add(values);
443
+ const out = [];
444
+ for (const entry of values) {
445
+ if (out.length >= MAX_ENTRIES) break;
446
+ out.push(this.value(entry, level));
447
+ }
448
+ this._open.delete(values);
449
+
450
+ const size = Array.isArray(values) ? values.length : values.size;
451
+ return size > MAX_ENTRIES ? { $type: typeName(values), length: size, values: out, $truncated: true } : out;
452
+ }
453
+
454
+ /**
455
+ * @param {Map<*, *>} map - A map.
456
+ * @param {number} level - The number of links followed to reach the object holding it.
457
+ * @returns {object} The entries by key, the way the panel names the keys.
458
+ */
459
+ map(map, level) {
460
+ if (this._open.has(map)) return '<circular>';
461
+ this._open.add(map);
462
+ const out = {};
463
+ let count = 0;
464
+ for (const [key, entry] of map) {
465
+ if (count++ >= MAX_ENTRIES) {
466
+ out.$truncated = true;
467
+ break;
468
+ }
469
+ const name = typeof key === 'object' && key !== null ? describeValue(key, 1).text : String(key);
470
+ out[uniqueKey(out, name)] = this.value(entry, level);
471
+ }
472
+ this._open.delete(map);
473
+ return out;
474
+ }
475
+
476
+ /**
477
+ * @param {object} obj - A plain object, part of the object holding it.
478
+ * @param {number} level - The number of links followed to reach the object holding it.
479
+ * @returns {object} Its fields.
480
+ */
481
+ plain(obj, level) {
482
+ if (this._open.has(obj)) return '<circular>';
483
+ this._open.add(obj);
484
+ const out = {};
485
+ for (const key of Object.keys(obj)) {
486
+ let value;
487
+ try {
488
+ value = obj[key];
489
+ } catch (e) {
490
+ out[key] = errorText(e);
491
+ continue;
492
+ }
493
+ if (typeof value !== 'function') out[key] = this.value(value, level);
494
+ }
495
+ this._open.delete(obj);
496
+ return out;
497
+ }
498
+
499
+ /**
500
+ * @param {ListRow[]} rows - The rows of a list.
501
+ * @returns {*} The entries, see {@link JsonCopy#listEntry}, or for a list in groups, the
502
+ * groups by title, see {@link JsonCopy#group}.
503
+ */
504
+ listRows(rows) {
505
+ const first = rows.findIndex(row => row.header);
506
+ if (first < 0) return rows.map(row => this.listEntry(row));
507
+
508
+ const out = {};
509
+ // rows ahead of the first group belong to none
510
+ if (first > 0) out.entries = rows.slice(0, first).map(row => this.listEntry(row));
511
+ rows.forEach((row, index) => {
512
+ if (row.header) out[uniqueKey(out, row.name)] = this.group(rows, index);
513
+ });
514
+ return out;
515
+ }
516
+
517
+ /**
518
+ * @param {ListRow[]} rows - The rows of a list in groups.
519
+ * @param {number} index - The index of the header of the group.
520
+ * @returns {object} The figures of the header, such as its count and size, and the entries of
521
+ * the group: its rows, followed by what the header says the group holds without listing it.
522
+ */
523
+ group(rows, index) {
524
+ const header = rows[index];
525
+ const out = this.figures(header, false);
526
+ const entries = [];
527
+ for (let i = index + 1; i < rows.length && !rows[i].header; i++) {
528
+ entries.push(this.listEntry(rows[i]));
529
+ }
530
+ for (const obj of header.contents?.() ?? []) {
531
+ entries.push(this.object(obj, 0));
532
+ }
533
+ out.entries = entries;
534
+ return out;
535
+ }
536
+
537
+ /**
538
+ * @param {ListRow} row - A row of a list.
539
+ * @returns {*} The object the row selects, copied as if it was selected and copied on its own,
540
+ * or the figures of a row that selects nothing.
541
+ */
542
+ listEntry(row) {
543
+ const item = row.item;
544
+ if ((typeof item === 'object' && item !== null) || typeof item === 'function') return this.object(item, 0);
545
+ return this.figures(row, true);
546
+ }
547
+
548
+ /**
549
+ * @param {ListRow} row - A row of a list.
550
+ * @param {boolean} named - Whether to include the cell naming the row, which a group is
551
+ * already keyed by.
552
+ * @returns {object} The text of its cells, keyed by the kind of each: its name, info, count,
553
+ * size and so on.
554
+ */
555
+ figures(row, named) {
556
+ const out = {};
557
+ row.cells.forEach((cell, index) => {
558
+ const value = cell.toggle ? !!cell.checked : cell.text;
559
+ if (value === '') return;
560
+ const key = cell.cls?.match(/\bpci-cell-([a-z]+)/)?.[1] ?? String(index);
561
+ if (key === 'name' && !named) return;
562
+ out[uniqueKey(out, key)] = value;
563
+ });
564
+ return out;
565
+ }
566
+
567
+ /**
568
+ * Copies an object the panel has no model for from its public properties, see
569
+ * {@link collectProperties}, other than its device and the objects of the graphics backend.
570
+ *
571
+ * @param {object} obj - The object.
572
+ * @param {number} level - The number of links followed to reach it.
573
+ * @returns {object} Its properties.
574
+ */
575
+ reflect(obj, level) {
576
+ const out = {};
577
+ for (const name of collectProperties(obj, [], SKIP_REFLECTED)) {
578
+ let value;
579
+ try {
580
+ value = /** @type {any} */ (obj)[name];
581
+ } catch (e) {
582
+ out[name] = errorText(e);
583
+ continue;
584
+ }
585
+ out[name] = this.value(value, level);
586
+ }
587
+ return out;
588
+ }
589
+ }
590
+
591
+ /**
592
+ * Copies the subject of the panel, which is where the links followed are counted from.
593
+ *
594
+ * @param {object} subject - The subject.
595
+ * @param {PropertySection[]} sections - What the panel shows for it.
596
+ * @param {CopyOptions} options - How far to follow links.
597
+ * @returns {CopyResult} The copy.
598
+ */
599
+ function copySubject(subject, sections, options) {
600
+ const copy = new JsonCopy(options);
601
+ return copy.result(copy.object(subject, 0, sections));
602
+ }
603
+
604
+ /**
605
+ * Copies a section of the subject, under its title.
606
+ *
607
+ * @param {PropertySection} section - The section.
608
+ * @param {CopyOptions} options - How far to follow links.
609
+ * @returns {CopyResult} The copy.
610
+ */
611
+ function copySection(section, options) {
612
+ const copy = new JsonCopy(options);
613
+ return copy.result({ [section.title]: copy.rows(section.rows, 0) });
614
+ }
615
+
616
+ /**
617
+ * Copies a list, such as everything a tab lists, under its name. See {@link JsonCopy#listRows}.
618
+ *
619
+ * @param {string} title - The name of the list.
620
+ * @param {ListRow[]} rows - Its rows, all of them, whatever its filter shows.
621
+ * @param {CopyOptions} options - How far to follow links from each entry.
622
+ * @returns {CopyResult} The copy.
623
+ */
624
+ function copyList(title, rows, options) {
625
+ const copy = new JsonCopy(options);
626
+ return copy.result({ [title]: copy.listRows(rows) });
627
+ }
628
+
629
+ /**
630
+ * Copies a group of a list, under its title. See {@link JsonCopy#group}.
631
+ *
632
+ * @param {ListRow[]} rows - The rows of the list.
633
+ * @param {string} key - The key of the group's header.
634
+ * @param {CopyOptions} options - How far to follow links from each entry.
635
+ * @returns {CopyResult} The copy, empty when the group is gone.
636
+ */
637
+ function copyGroup(rows, key, options) {
638
+ const copy = new JsonCopy(options);
639
+ const index = rows.findIndex(row => row.header && row.key === key);
640
+ return copy.result(index < 0 ? {} : { [rows[index].name]: copy.group(rows, index) });
641
+ }
642
+
643
+ /**
644
+ * Copies the figures of a row of a list that selects nothing, such as the size of a pool.
645
+ *
646
+ * @param {ListRow} row - The row.
647
+ * @returns {CopyResult} The copy.
648
+ */
649
+ function copyListRow(row) {
650
+ const copy = new JsonCopy({ depth: 0 });
651
+ return copy.result(copy.figures(row, true));
652
+ }
653
+
654
+ /**
655
+ * Copies a row. A row standing for an object copies the object, counting the links followed from
656
+ * it; a list copies its entries the same way, each as if it was copied on its own. Anything else
657
+ * is copied under its label.
658
+ *
659
+ * @param {PropertyRow} row - The row.
660
+ * @param {CopyOptions} options - How far to follow links.
661
+ * @returns {CopyResult} The copy.
662
+ */
663
+ function copyRow(row, options) {
664
+ const copy = new JsonCopy(options);
665
+ const json = copy.described(row.value, -1);
666
+ const isObject = !!objectOf(row.value) && typeof json === 'object' && json !== null && Object.hasOwn(json, '$type');
667
+ return copy.result(isObject ? json : { [row.label || row.key]: json });
668
+ }
669
+
670
+ /**
671
+ * Prints a copy as indented JSON, the way `JSON.stringify` does, except that a list of plain
672
+ * values stays on one line, so a vector reads as `[0, 1, 0]` rather than taking five lines.
673
+ *
674
+ * @param {*} json - The copy.
675
+ * @param {string} [indent] - The indentation of the line the value starts on.
676
+ * @returns {string} The text.
677
+ */
678
+ function stringifyCopy(json, indent = '') {
679
+ if (json === null || typeof json !== 'object') return JSON.stringify(json) ?? 'null';
680
+ const inner = `${indent} `;
681
+ if (Array.isArray(json)) {
682
+ if (json.every(value => value === null || typeof value !== 'object')) {
683
+ return `[${json.map(value => JSON.stringify(value) ?? 'null').join(', ')}]`;
684
+ }
685
+ return `[\n${json.map(value => inner + stringifyCopy(value, inner)).join(',\n')}\n${indent}]`;
686
+ }
687
+ const keys = Object.keys(json);
688
+ if (keys.length === 0) return '{}';
689
+ return `{\n${keys.map(key => `${inner}${JSON.stringify(key)}: ${stringifyCopy(json[key], inner)}`).join(',\n')}\n${indent}}`;
690
+ }
691
+
692
+ export { copyGroup, copyList, copyListRow, copyRow, copySection, copySubject, stringifyCopy };