@playcanvas/inspector 0.1.0 → 0.3.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/README.md +47 -26
- package/package.json +12 -6
- package/src/asset-view.js +24 -17
- package/src/camera-view.js +5 -3
- package/src/context-menu.js +231 -0
- package/src/copy-json.js +692 -0
- package/src/describe.js +51 -2
- package/src/frame-graph-view.js +1 -0
- package/src/hierarchy-view.js +29 -3
- package/src/inspector.js +406 -59
- package/src/list-view.js +118 -12
- package/src/material-view.js +7 -6
- package/src/memory-view.js +224 -66
- package/src/mesh-view.js +6 -5
- package/src/model.js +24 -2
- package/src/node-model.js +78 -9
- package/src/property-view.js +159 -5
- package/src/render-target-view.js +3 -3
- package/src/script-view.js +21 -12
- package/src/styles.js +196 -2
- package/src/texture-view.js +4 -4
- package/src/transform-tool.js +185 -0
- package/types/asset-usage.d.ts +2 -0
- package/types/asset-view.d.ts +17 -10
- package/types/camera-view.d.ts +12 -6
- package/types/context-menu.d.ts +90 -0
- package/types/copy-json.d.ts +69 -0
- package/types/describe.d.ts +3 -2
- package/types/entity-inspector-script.d.ts +2 -2
- package/types/frame-graph-view.d.ts +6 -2
- package/types/hierarchy-view.d.ts +25 -11
- package/types/input/fly-controller.d.ts +4 -4
- package/types/input/input.d.ts +2 -2
- package/types/input/orbit-controller.d.ts +6 -6
- package/types/input/pose.d.ts +9 -7
- package/types/inspector.d.ts +144 -22
- package/types/instance-survey.d.ts +3 -1
- package/types/list-view.d.ts +44 -3
- package/types/material-view.d.ts +13 -6
- package/types/memory-view.d.ts +37 -30
- package/types/mesh-view.d.ts +12 -5
- package/types/model.d.ts +21 -3
- package/types/node-model.d.ts +30 -6
- package/types/physics-debug.d.ts +9 -5
- package/types/physics-view.d.ts +8 -6
- package/types/picker/picker.d.ts +15 -6
- package/types/picker/render-pass-picker.d.ts +16 -10
- package/types/property-view.d.ts +41 -0
- package/types/render-target-view.d.ts +10 -7
- package/types/renderers/texture-renderer-shaders.d.ts +1 -1
- package/types/renderers/texture-renderer.d.ts +11 -5
- package/types/renderers/wire-renderer.d.ts +14 -5
- package/types/script-view.d.ts +10 -4
- package/types/shader-view.d.ts +10 -6
- package/types/texture-view.d.ts +8 -5
- package/types/transform-tool.d.ts +92 -0
- package/types/viewport-tools.d.ts +25 -18
package/README.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# PlayCanvas Inspector
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@playcanvas/inspector)
|
|
4
|
+
[](https://npmtrends.com/@playcanvas/inspector)
|
|
5
|
+
[](https://github.com/playcanvas/inspector/blob/main/LICENSE)
|
|
6
|
+
[](https://discord.gg/RSaMRzg)
|
|
7
|
+
[](https://www.reddit.com/r/PlayCanvas)
|
|
8
|
+
[](https://x.com/intent/follow?screen_name=playcanvas)
|
|
9
|
+
|
|
10
|
+
| [User Manual](https://developer.playcanvas.com) | [Blog](https://blog.playcanvas.com) | [Forum](https://forum.playcanvas.com) |
|
|
11
|
+
|
|
3
12
|
A debug panel for running [PlayCanvas](https://github.com/playcanvas/engine) apps. It shows the entity
|
|
4
13
|
hierarchy and every component's properties, and tabs for the cameras, assets, frame graph, render
|
|
5
14
|
targets, textures, meshes, materials, scripts, memory, shaders and physics of the app, with live
|
|
@@ -7,7 +16,10 @@ previews of textures and render targets, picking and flying in the view, and a d
|
|
|
7
16
|
that steps through the draw calls of a render pass.
|
|
8
17
|
|
|
9
18
|
The inspector is read-only, apart from toggling entities on and off, pausing and stepping the app,
|
|
10
|
-
and view-only overrides (render modes, wireframe, a flown camera) it removes when it
|
|
19
|
+
and view-only overrides (render modes, wireframe, a flown or orbited camera) it removes when it
|
|
20
|
+
closes.
|
|
21
|
+
|
|
22
|
+

|
|
11
23
|
|
|
12
24
|
## Install
|
|
13
25
|
|
|
@@ -42,11 +54,42 @@ entity.script.create(EntityInspector, { properties: { dock: 'left' } });
|
|
|
42
54
|
app.root.addChild(entity);
|
|
43
55
|
```
|
|
44
56
|
|
|
57
|
+
### Step through a frame
|
|
58
|
+
|
|
59
|
+
In the Frame graph tab, select a forward pass or one of its layer steps and turn on **Debug frame**.
|
|
60
|
+
The app pauses and the pass draws only up to the selected draw; the arrow keys step through the
|
|
61
|
+
draws one at a time, with each one outlined in the view and listed with its mesh and material. It
|
|
62
|
+
needs the debug build of the engine.
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
### Edit transforms
|
|
67
|
+
|
|
68
|
+
Click a position, rotation or the local scale in the Transform section of an entity to edit it as
|
|
69
|
+
three numbers separated by commas (one number for a uniform scale); Enter applies it and Escape
|
|
70
|
+
gives up. Or turn on **Move**, **Rotate** or **Scale** in the toolbar and drag the handles of the
|
|
71
|
+
gizmo drawn on the selected entity. A drag on a handle is kept from the app, while away from the
|
|
72
|
+
handles the app has the view as usual. Pick, Fly and Orbit are off while a gizmo is on; Esc or its
|
|
73
|
+
button turns it off. An entity with a physics body takes its body along.
|
|
74
|
+
|
|
75
|
+
### Copy as JSON
|
|
76
|
+
|
|
77
|
+
Right-click a row, a section title or an item of a list to copy it to the clipboard as JSON, with
|
|
78
|
+
numbers at full precision. Right-click the header of a group, such as Textures on the Memory tab,
|
|
79
|
+
to copy the group, or the name of a tab to copy everything it lists: the Hierarchy tab copies the
|
|
80
|
+
hierarchy from its root. **Copy as JSON** copies it on its own; **Copy N levels deep** also copies
|
|
81
|
+
what it links to, such as the children of an entity, its mesh instances, their materials and their
|
|
82
|
+
textures, up to N links away; **Copy all levels** follows every link, so copying the root copies
|
|
83
|
+
the whole hierarchy. A link that is not followed is named by `$type` and its name or id, and
|
|
84
|
+
marked `$stub`; an object already copied elsewhere in the copy is marked `$ref`. A node's parent
|
|
85
|
+
and the lists of what uses an object are left out, so a copy never climbs out of what was copied.
|
|
86
|
+
|
|
45
87
|
## Engine versions
|
|
46
88
|
|
|
47
89
|
Each inspector release names the engine versions it supports in its `playcanvas` peer dependency.
|
|
48
|
-
The inspector reads engine internals to show what it shows, so
|
|
49
|
-
|
|
90
|
+
The inspector reads engine internals to show what it shows, so a new engine release can need a new
|
|
91
|
+
inspector release. The range takes later 2.x releases but not prereleases of them, such as
|
|
92
|
+
`2.24.0-beta.1`, as npm does not match prereleases of other versions than the range names.
|
|
50
93
|
|
|
51
94
|
## The devtools hook
|
|
52
95
|
|
|
@@ -73,6 +116,7 @@ npm run lint
|
|
|
73
116
|
npm test
|
|
74
117
|
npm run build:types # writes the TypeScript declarations to types/
|
|
75
118
|
npm run test:types # compiles a TypeScript usage of the package against them
|
|
119
|
+
npm run publint # checks the package is publishable
|
|
76
120
|
```
|
|
77
121
|
|
|
78
122
|
The tests run against the `playcanvas` version in `devDependencies`, on the engine's null graphics
|
|
@@ -82,29 +126,6 @@ A few engine helpers the inspector draws with are copied into `src/renderers`, `
|
|
|
82
126
|
`src/input`, so the inspector does not depend on them being in the app's engine build. See
|
|
83
127
|
[COPIED_FROM.md](COPIED_FROM.md).
|
|
84
128
|
|
|
85
|
-
## Releasing
|
|
86
|
-
|
|
87
|
-
Releases are published to npm from GitHub Actions when a version tag is pushed. Only repository
|
|
88
|
-
admins and maintainers can push tags.
|
|
89
|
-
|
|
90
|
-
1. Update your `main` to the commit to release, with CI green.
|
|
91
|
-
2. Bump the version and tag it:
|
|
92
|
-
|
|
93
|
-
```sh
|
|
94
|
-
npm version minor # or major, or patch
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
3. Push the commit and the tag:
|
|
98
|
-
|
|
99
|
-
```sh
|
|
100
|
-
git push --follow-tags
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
The **Publish** workflow runs the lint and tests, publishes to npm with provenance, and creates a
|
|
104
|
-
GitHub release with generated notes.
|
|
105
|
-
|
|
106
|
-
A tag that does not match the `package.json` version fails the run without publishing.
|
|
107
|
-
|
|
108
129
|
## License
|
|
109
130
|
|
|
110
131
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@playcanvas/inspector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A debug panel for PlayCanvas apps: the entity hierarchy, assets, frame graph, render targets, textures, meshes, materials, scripts, cameras, memory, shaders and physics of a running app",
|
|
5
5
|
"author": "PlayCanvas <support@playcanvas.com>",
|
|
6
6
|
"homepage": "https://github.com/playcanvas/inspector#readme",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/playcanvas/inspector.git"
|
|
12
|
+
"url": "git+https://github.com/playcanvas/inspector.git"
|
|
13
13
|
},
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"keywords": [
|
|
@@ -36,18 +36,23 @@
|
|
|
36
36
|
"LICENSE"
|
|
37
37
|
],
|
|
38
38
|
"sideEffects": false,
|
|
39
|
-
"
|
|
40
|
-
"
|
|
39
|
+
"devEngines": {
|
|
40
|
+
"runtime": {
|
|
41
|
+
"name": "node",
|
|
42
|
+
"version": ">=22.19.0",
|
|
43
|
+
"onFail": "error"
|
|
44
|
+
}
|
|
41
45
|
},
|
|
42
46
|
"scripts": {
|
|
43
47
|
"lint": "eslint src test",
|
|
44
48
|
"lint:fix": "eslint --fix src test",
|
|
49
|
+
"publint": "publint",
|
|
45
50
|
"test": "mocha --recursive test --timeout 5000",
|
|
46
51
|
"build:types": "tsc",
|
|
47
52
|
"test:types": "tsc -p test/types"
|
|
48
53
|
},
|
|
49
54
|
"peerDependencies": {
|
|
50
|
-
"playcanvas": "^2.23.0
|
|
55
|
+
"playcanvas": "^2.23.0"
|
|
51
56
|
},
|
|
52
57
|
"devDependencies": {
|
|
53
58
|
"@playcanvas/eslint-config": "3.0.0-beta.8",
|
|
@@ -56,7 +61,8 @@
|
|
|
56
61
|
"globals": "17.12.0",
|
|
57
62
|
"jsdom": "30.1.0",
|
|
58
63
|
"mocha": "12.0.2",
|
|
59
|
-
"playcanvas": "2.23.0
|
|
64
|
+
"playcanvas": "2.23.0",
|
|
65
|
+
"publint": "0.3.24",
|
|
60
66
|
"typescript": "5.9.3"
|
|
61
67
|
}
|
|
62
68
|
}
|
package/src/asset-view.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Asset } from 'playcanvas';
|
|
2
2
|
|
|
3
3
|
import { collectProperties, describeValue } from './describe.js';
|
|
4
|
-
import { formatBytes, makeSection, push, read, reflectRows } from './model.js';
|
|
4
|
+
import { backlink, formatBytes, makeSection, makeUsageSection, push, read, reflectRows } from './model.js';
|
|
5
5
|
|
|
6
6
|
/** @import { AppBase } from 'playcanvas' */
|
|
7
7
|
/** @import { AssetRegistry } from 'playcanvas' */
|
|
@@ -103,12 +103,9 @@ function assetPropertyNames(component) {
|
|
|
103
103
|
*/
|
|
104
104
|
function describeAssetValue(value, registry) {
|
|
105
105
|
if (Array.isArray(value)) {
|
|
106
|
+
if (!value.length) return describeValue(value);
|
|
106
107
|
const items = value.map((entry, i) => ({ label: `[${i}]`, ...describeAssetValue(entry, registry) }));
|
|
107
|
-
return {
|
|
108
|
-
text: value.length ? `Array(${value.length}) of asset` : '[]',
|
|
109
|
-
cls: value.length ? 'obj' : 'null',
|
|
110
|
-
items: value.length ? items : undefined
|
|
111
|
-
};
|
|
108
|
+
return { text: `Array(${value.length}) of asset`, cls: 'obj', items };
|
|
112
109
|
}
|
|
113
110
|
if (typeof value === 'number') {
|
|
114
111
|
const asset = registry?.get(value) ?? null;
|
|
@@ -292,7 +289,8 @@ function assetRows(registry, sort) {
|
|
|
292
289
|
* into (linked), the components using it (linked), and every other public property.
|
|
293
290
|
*
|
|
294
291
|
* @param {Asset} asset - The asset.
|
|
295
|
-
* @param {{ app: AppBase }} ctx - The app, to find the asset's users
|
|
292
|
+
* @param {{ app: AppBase, backlinks?: boolean }} ctx - The app, to find the asset's users, and
|
|
293
|
+
* whether to find them at all, which a copy has no use for.
|
|
296
294
|
* @returns {PropertySection[]} The sections.
|
|
297
295
|
*/
|
|
298
296
|
function buildAssetModel(asset, ctx) {
|
|
@@ -305,7 +303,7 @@ function buildAssetModel(asset, ctx) {
|
|
|
305
303
|
push(general, 'type', { text: asset.type, cls: 'obj' });
|
|
306
304
|
const { parentOf, parts } = containerParts(ctx.app?.assets ?? null);
|
|
307
305
|
const container = parentOf.get(asset) ?? null;
|
|
308
|
-
if (container) push(general, 'container', describeValue(container));
|
|
306
|
+
if (container) push(general, 'container', backlink(describeValue(container)));
|
|
309
307
|
push(general, 'state', { text: stateName(asset), cls: asset.loaded ? 'bool' : 'null' });
|
|
310
308
|
push(general, 'preload', read(asset, 'preload'));
|
|
311
309
|
if (container) {
|
|
@@ -343,8 +341,23 @@ function buildAssetModel(asset, ctx) {
|
|
|
343
341
|
sections.push(contents);
|
|
344
342
|
}
|
|
345
343
|
|
|
346
|
-
|
|
347
|
-
|
|
344
|
+
if (ctx.backlinks !== false) sections.push(usersSection(asset, ctx.app));
|
|
345
|
+
|
|
346
|
+
const rest = makeSection('props', 'Properties');
|
|
347
|
+
reflectRows(rest, asset, [], SKIP_ASSET);
|
|
348
|
+
if (rest.rows.length) sections.push(rest);
|
|
349
|
+
|
|
350
|
+
return sections;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* @param {Asset} asset - The asset.
|
|
355
|
+
* @param {AppBase} app - The app, to find the asset's users.
|
|
356
|
+
* @returns {PropertySection} The section listing the components using the asset.
|
|
357
|
+
*/
|
|
358
|
+
function usersSection(asset, app) {
|
|
359
|
+
const users = assetUsers(app, asset);
|
|
360
|
+
const usage = makeUsageSection('users', 'Used by');
|
|
348
361
|
push(usage, 'components', {
|
|
349
362
|
text: users.length ? `${users.length} component${users.length === 1 ? '' : 's'}` : 'no component in the scene',
|
|
350
363
|
cls: users.length ? 'obj' : 'null',
|
|
@@ -355,13 +368,7 @@ function buildAssetModel(asset, ctx) {
|
|
|
355
368
|
target: user.entity
|
|
356
369
|
}))
|
|
357
370
|
});
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
const rest = makeSection('props', 'Properties');
|
|
361
|
-
reflectRows(rest, asset, [], SKIP_ASSET);
|
|
362
|
-
if (rest.rows.length) sections.push(rest);
|
|
363
|
-
|
|
364
|
-
return sections;
|
|
371
|
+
return usage;
|
|
365
372
|
}
|
|
366
373
|
|
|
367
374
|
export {
|
package/src/camera-view.js
CHANGED
|
@@ -17,6 +17,7 @@ import { RENDER_MODES, isScreenCamera } from './viewport-tools.js';
|
|
|
17
17
|
* @typedef {object} CameraActions
|
|
18
18
|
* @property {(camera: CameraComponent) => void} fly - Flies the camera.
|
|
19
19
|
* @property {CameraComponent|null} flying - The camera being flown, if any.
|
|
20
|
+
* @property {string|null} [unavailable] - Why no camera can be flown now, such as a gizmo being on.
|
|
20
21
|
* @ignore
|
|
21
22
|
*/
|
|
22
23
|
|
|
@@ -96,13 +97,14 @@ function buildCameraModel(camera, ctx) {
|
|
|
96
97
|
const general = makeSection('camera', `Camera "${entity.name}"`);
|
|
97
98
|
push(general, 'entity', describeValue(entity));
|
|
98
99
|
push(general, 'enabled', { text: String(camera.enabled && entity.enabled), cls: 'bool' });
|
|
100
|
+
const unavailable = cameraActions.unavailable ?? null;
|
|
99
101
|
push(general, 'fly', {
|
|
100
|
-
text: camera === cameraActions.flying ? 'flying now, Esc to stop' : 'fly it with the mouse and WASD',
|
|
102
|
+
text: camera === cameraActions.flying ? 'flying now, Esc to stop' : (unavailable ?? 'fly it with the mouse and WASD'),
|
|
101
103
|
cls: 'obj',
|
|
102
104
|
actions: [{
|
|
103
105
|
text: camera === cameraActions.flying ? 'Flying' : 'Fly',
|
|
104
|
-
title: 'Fly this camera: WASD and QE move, drag to look, the wheel sets the speed, Shift is faster, Esc stops',
|
|
105
|
-
disabled: camera === cameraActions.flying,
|
|
106
|
+
title: unavailable ?? 'Fly this camera: WASD and QE move, drag to look, the wheel sets the speed, Shift is faster, Esc stops',
|
|
107
|
+
disabled: camera === cameraActions.flying || !!unavailable,
|
|
106
108
|
run: () => cameraActions.fly(camera)
|
|
107
109
|
}]
|
|
108
110
|
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { setTip } from './tooltip.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An entry of a context menu.
|
|
5
|
+
*
|
|
6
|
+
* @typedef {object} MenuItem
|
|
7
|
+
* @ignore
|
|
8
|
+
* @property {() => string} text - The text, read again whenever the entry redraws.
|
|
9
|
+
* @property {() => void} run - Called when the entry is chosen, once the menu has closed.
|
|
10
|
+
* @property {string} [title] - The tooltip.
|
|
11
|
+
* @property {MenuButton[]} [buttons] - Small buttons at the end of the entry, which change the
|
|
12
|
+
* entry without choosing it or closing the menu.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} MenuButton
|
|
17
|
+
* @ignore
|
|
18
|
+
* @property {string} text - The text.
|
|
19
|
+
* @property {string} title - The tooltip.
|
|
20
|
+
* @property {() => boolean} disabled - Whether it is disabled, read again whenever it redraws.
|
|
21
|
+
* @property {() => void} run - Called when it is clicked.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// milliseconds a note stays up
|
|
25
|
+
const NOTE_TIME = 2500;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} tag - The element tag.
|
|
29
|
+
* @param {string} className - The class name.
|
|
30
|
+
* @param {string} [text] - The text content.
|
|
31
|
+
* @returns {HTMLElement} The element.
|
|
32
|
+
*/
|
|
33
|
+
function el(tag, className, text) {
|
|
34
|
+
const element = document.createElement(tag);
|
|
35
|
+
element.className = className;
|
|
36
|
+
if (text !== undefined) element.textContent = text;
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A menu opened where the pointer is, and a note shown briefly at the bottom of the panel, such as
|
|
42
|
+
* for what was copied. Both live in the panel, so they move with it into a popped-out window. The
|
|
43
|
+
* menu closes on a choice, a press anywhere outside it, Escape, scrolling and leaving the window.
|
|
44
|
+
*
|
|
45
|
+
* @ignore
|
|
46
|
+
*/
|
|
47
|
+
class ContextMenu {
|
|
48
|
+
/**
|
|
49
|
+
* @type {HTMLElement}
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
_panel;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @type {HTMLElement}
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
_menu;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @type {HTMLElement}
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
_note;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @type {number}
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
_noteTimer = 0;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The document the closing listeners are on while the menu is open.
|
|
74
|
+
*
|
|
75
|
+
* @type {Document|null}
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
_doc = null;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {HTMLElement} panel - The panel to show the menu and notes in.
|
|
82
|
+
*/
|
|
83
|
+
constructor(panel) {
|
|
84
|
+
this._panel = panel;
|
|
85
|
+
this._menu = el('div', 'pci-menu');
|
|
86
|
+
this._menu.setAttribute('role', 'menu');
|
|
87
|
+
this._note = el('div', 'pci-toast');
|
|
88
|
+
panel.append(this._menu, this._note);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Whether the menu is open.
|
|
93
|
+
*
|
|
94
|
+
* @type {boolean}
|
|
95
|
+
*/
|
|
96
|
+
get open() {
|
|
97
|
+
return this._doc !== null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Opens the menu at a point, in place of any menu already open.
|
|
102
|
+
*
|
|
103
|
+
* @param {number} x - The point, in client pixels.
|
|
104
|
+
* @param {number} y - The point, in client pixels.
|
|
105
|
+
* @param {string} title - What the menu acts on.
|
|
106
|
+
* @param {MenuItem[]} items - The entries.
|
|
107
|
+
*/
|
|
108
|
+
show(x, y, title, items) {
|
|
109
|
+
this.hide();
|
|
110
|
+
const menu = this._menu;
|
|
111
|
+
menu.textContent = '';
|
|
112
|
+
const titleEl = el('div', 'pci-menu-title', title);
|
|
113
|
+
setTip(titleEl, title);
|
|
114
|
+
menu.appendChild(titleEl);
|
|
115
|
+
for (const item of items) menu.appendChild(this._entry(item));
|
|
116
|
+
menu.style.display = 'block';
|
|
117
|
+
|
|
118
|
+
const view = this._panel.ownerDocument.defaultView ?? window;
|
|
119
|
+
const width = menu.offsetWidth;
|
|
120
|
+
const height = menu.offsetHeight;
|
|
121
|
+
menu.style.left = `${Math.max(4, Math.min(x, view.innerWidth - width - 4))}px`;
|
|
122
|
+
menu.style.top = `${Math.max(4, Math.min(y, view.innerHeight - height - 4))}px`;
|
|
123
|
+
|
|
124
|
+
const doc = this._panel.ownerDocument;
|
|
125
|
+
doc.addEventListener('pointerdown', this._onPointerDown, true);
|
|
126
|
+
doc.addEventListener('keydown', this._onKeyDown, true);
|
|
127
|
+
doc.addEventListener('wheel', this.hide, { capture: true, passive: true });
|
|
128
|
+
view.addEventListener('blur', this.hide);
|
|
129
|
+
this._doc = doc;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Closes the menu, if it is open.
|
|
134
|
+
*/
|
|
135
|
+
hide = () => {
|
|
136
|
+
const doc = this._doc;
|
|
137
|
+
if (!doc) return;
|
|
138
|
+
this._doc = null;
|
|
139
|
+
this._menu.style.display = 'none';
|
|
140
|
+
doc.removeEventListener('pointerdown', this._onPointerDown, true);
|
|
141
|
+
doc.removeEventListener('keydown', this._onKeyDown, true);
|
|
142
|
+
doc.removeEventListener('wheel', this.hide, { capture: true });
|
|
143
|
+
doc.defaultView?.removeEventListener('blur', this.hide);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Shows a note at the bottom of the panel for a moment.
|
|
148
|
+
*
|
|
149
|
+
* @param {string} text - The note.
|
|
150
|
+
*/
|
|
151
|
+
note(text) {
|
|
152
|
+
clearTimeout(this._noteTimer);
|
|
153
|
+
this._note.textContent = text;
|
|
154
|
+
this._note.style.display = 'block';
|
|
155
|
+
this._noteTimer = setTimeout(() => {
|
|
156
|
+
this._note.style.display = 'none';
|
|
157
|
+
}, NOTE_TIME);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Closes the menu and removes its elements.
|
|
162
|
+
*/
|
|
163
|
+
destroy() {
|
|
164
|
+
this.hide();
|
|
165
|
+
clearTimeout(this._noteTimer);
|
|
166
|
+
this._menu.remove();
|
|
167
|
+
this._note.remove();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @param {MenuItem} item - An entry.
|
|
172
|
+
* @returns {HTMLElement} Its element.
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
_entry(item) {
|
|
176
|
+
const entry = el('div', 'pci-menu-item');
|
|
177
|
+
entry.setAttribute('role', 'menuitem');
|
|
178
|
+
const label = el('span', 'pci-menu-label', item.text());
|
|
179
|
+
entry.appendChild(label);
|
|
180
|
+
if (item.title) setTip(entry, item.title);
|
|
181
|
+
|
|
182
|
+
/** @type {HTMLButtonElement[]} */
|
|
183
|
+
const buttons = [];
|
|
184
|
+
const redraw = () => {
|
|
185
|
+
label.textContent = item.text();
|
|
186
|
+
item.buttons?.forEach((button, i) => {
|
|
187
|
+
buttons[i].disabled = button.disabled();
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
if (item.buttons?.length) {
|
|
191
|
+
const wrap = el('span', 'pci-menu-buttons');
|
|
192
|
+
for (const button of item.buttons) {
|
|
193
|
+
const buttonEl = /** @type {HTMLButtonElement} */ (el('button', 'pci-menu-button', button.text));
|
|
194
|
+
setTip(buttonEl, button.title);
|
|
195
|
+
buttonEl.addEventListener('click', (e) => {
|
|
196
|
+
e.stopPropagation();
|
|
197
|
+
button.run();
|
|
198
|
+
redraw();
|
|
199
|
+
});
|
|
200
|
+
buttons.push(buttonEl);
|
|
201
|
+
wrap.appendChild(buttonEl);
|
|
202
|
+
}
|
|
203
|
+
entry.appendChild(wrap);
|
|
204
|
+
redraw();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
entry.addEventListener('click', () => {
|
|
208
|
+
this.hide();
|
|
209
|
+
item.run();
|
|
210
|
+
});
|
|
211
|
+
return entry;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {PointerEvent} e - The event.
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
_onPointerDown = (e) => {
|
|
219
|
+
if (!e.composedPath().includes(this._menu)) this.hide();
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {KeyboardEvent} e - The event.
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
_onKeyDown = (e) => {
|
|
227
|
+
if (e.key === 'Escape') this.hide();
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { ContextMenu };
|