@link-foundation/ui-devmode 0.0.0-stage → 0.0.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 +24 -0
- package/README.md +353 -2
- package/package.json +78 -4
- package/src/activation.js +235 -0
- package/src/adapter.js +92 -0
- package/src/agent-instruction.d.ts +6 -0
- package/src/agent-instruction.js +17 -0
- package/src/agent-instruction.txt +8 -0
- package/src/apply-shared.js +135 -0
- package/src/comments/commands.js +77 -0
- package/src/comments/geometry.js +65 -0
- package/src/comments/index.d.ts +93 -0
- package/src/comments/index.js +205 -0
- package/src/comments/model.js +359 -0
- package/src/comments/picking.js +154 -0
- package/src/comments/style.js +36 -0
- package/src/comments/view.js +250 -0
- package/src/comments/voice.js +64 -0
- package/src/defaults-dialog.js +120 -0
- package/src/details.js +159 -0
- package/src/editors.js +194 -0
- package/src/export.js +50 -0
- package/src/foreign/adapter.js +166 -0
- package/src/foreign/apply.js +89 -0
- package/src/foreign/entry.js +173 -0
- package/src/foreign/index.d.ts +32 -0
- package/src/foreign/injected.js +6 -0
- package/src/foreign/persist.js +54 -0
- package/src/foreign/properties.js +126 -0
- package/src/foreign/runtime.js +67 -0
- package/src/index.d.ts +204 -0
- package/src/index.js +201 -0
- package/src/inspector-css.js +106 -0
- package/src/inspector.js +342 -0
- package/src/inspector.lino +50 -0
- package/src/messages.js +113 -0
- package/src/model.d.ts +101 -0
- package/src/model.js +473 -0
- package/src/overlay.js +91 -0
- package/src/policy.js +222 -0
- package/src/read-asset.browser.js +8 -0
- package/src/read-asset.js +10 -0
- package/src/session.js +555 -0
- package/src/share.js +23 -0
- package/src/validate.js +170 -0
- package/src/views.js +328 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @link-foundation/ui-devmode: the development/configuration mode of the
|
|
3
|
+
* framework, a runtime inspector over any page rendered by ui-dom.
|
|
4
|
+
*
|
|
5
|
+
* This entry module is small on purpose: it reads the site policy and
|
|
6
|
+
* installs the entry points (shortcut, `?ui-dev`, `<ui-dev-mode>`). The
|
|
7
|
+
* overlay, the inspector and the model are loaded with a dynamic `import()`
|
|
8
|
+
* only when dev mode opens; with `(enabled false)` they are never requested.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { installActivation } from './activation.js';
|
|
12
|
+
import { translator } from './messages.js';
|
|
13
|
+
import { defaultPolicy, parseDevModePolicy } from './policy.js';
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
ELEMENT_NAME,
|
|
17
|
+
installActivation,
|
|
18
|
+
isEditableTarget,
|
|
19
|
+
matchesShortcut,
|
|
20
|
+
parseShortcut,
|
|
21
|
+
REQUEST_EVENT,
|
|
22
|
+
shouldActivate,
|
|
23
|
+
URL_PARAMETER,
|
|
24
|
+
} from './activation.js';
|
|
25
|
+
export { MESSAGES, message, translator } from './messages.js';
|
|
26
|
+
export {
|
|
27
|
+
DEFAULT_SHORTCUT,
|
|
28
|
+
defaultPolicy,
|
|
29
|
+
ENTRY_POINTS,
|
|
30
|
+
parseDevModePolicy,
|
|
31
|
+
POLICY_KIND,
|
|
32
|
+
WRITABLE_LAYERS,
|
|
33
|
+
} from './policy.js';
|
|
34
|
+
|
|
35
|
+
/** The name of the global handle of an open dev mode. */
|
|
36
|
+
export const GLOBAL_NAME = 'linkUiDevMode';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The policy of `mountDevMode`: a policy object, Links Notation text, a
|
|
40
|
+
* ui-core document or nodes; the defaults without one.
|
|
41
|
+
*/
|
|
42
|
+
function readPolicy(policy) {
|
|
43
|
+
if (policy === undefined || policy === null) {
|
|
44
|
+
return defaultPolicy();
|
|
45
|
+
}
|
|
46
|
+
if (typeof policy === 'object' && policy.entry instanceof Set) {
|
|
47
|
+
return policy;
|
|
48
|
+
}
|
|
49
|
+
return parseDevModePolicy(policy);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Mount dev mode over a page rendered by ui-dom.
|
|
54
|
+
*
|
|
55
|
+
* Open, it highlights the element under the pointer (`id: type`), selects
|
|
56
|
+
* it on click without delivering the click to the app, and shows the
|
|
57
|
+
* inspector docked to the right: Outline, Properties (typed editors that
|
|
58
|
+
* write the session layer), Styles, Tokens, Animations and Source. While it
|
|
59
|
+
* is open, `globalThis.linkUiDevMode` is `{ select, getSelection,
|
|
60
|
+
* setProperty, close }`.
|
|
61
|
+
*
|
|
62
|
+
* @param {{ root: Element, config: object,
|
|
63
|
+
* policy?: string|object|import('./policy.js').DevModePolicy,
|
|
64
|
+
* store?: object, handle?: { nodeFor?: (id: string) => Element|null },
|
|
65
|
+
* styles?: string|object[], tokens?: string|Map<string, object>,
|
|
66
|
+
* theme?: string, animations?: string|object[], vocabulary?: object,
|
|
67
|
+
* messages?: Record<string, string>, layout?: string,
|
|
68
|
+
* commentOptions?: object,
|
|
69
|
+
* window?: Window & typeof globalThis,
|
|
70
|
+
* onOpen?: () => void, onClose?: () => void }} options
|
|
71
|
+
* `root` is the container ui-dom rendered into, `config` the ui-config
|
|
72
|
+
* configuration of the page (bound to its store), `store` and `handle`
|
|
73
|
+
* what `mountDocument` returned
|
|
74
|
+
* @returns {{ policy: import('./policy.js').DevModePolicy,
|
|
75
|
+
* readonly isOpen: boolean, open: () => Promise<boolean>,
|
|
76
|
+
* close: () => void, toggle: () => Promise<boolean>,
|
|
77
|
+
* select: (id: string|null) => Promise<string|null>,
|
|
78
|
+
* destroy: () => void }}
|
|
79
|
+
*/
|
|
80
|
+
export function mountDevMode(options) {
|
|
81
|
+
const { root, config } = options;
|
|
82
|
+
const policy = readPolicy(options.policy);
|
|
83
|
+
const window = options.window ?? root.ownerDocument.defaultView;
|
|
84
|
+
const t = translator(options.messages);
|
|
85
|
+
let session = null;
|
|
86
|
+
let opening = null;
|
|
87
|
+
let destroyed = false;
|
|
88
|
+
|
|
89
|
+
const publish = (value) => {
|
|
90
|
+
if (value) {
|
|
91
|
+
globalThis[GLOBAL_NAME] = Object.freeze({
|
|
92
|
+
select: (id) => value.select(id),
|
|
93
|
+
getSelection: () => value.getSelection(),
|
|
94
|
+
setProperty: (id, name, propertyValue) =>
|
|
95
|
+
value.setProperty(id, name, propertyValue),
|
|
96
|
+
close: () => controller.close(),
|
|
97
|
+
});
|
|
98
|
+
} else if (globalThis[GLOBAL_NAME]?.close) {
|
|
99
|
+
delete globalThis[GLOBAL_NAME];
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const start = async () => {
|
|
104
|
+
const { openSession } = await import('./session.js');
|
|
105
|
+
if (destroyed) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return openSession({
|
|
109
|
+
...options,
|
|
110
|
+
window,
|
|
111
|
+
root,
|
|
112
|
+
config,
|
|
113
|
+
policy,
|
|
114
|
+
t,
|
|
115
|
+
onClose() {
|
|
116
|
+
session = null;
|
|
117
|
+
publish(null);
|
|
118
|
+
options.onClose?.();
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const controller = {
|
|
124
|
+
policy,
|
|
125
|
+
get isOpen() {
|
|
126
|
+
return session !== null;
|
|
127
|
+
},
|
|
128
|
+
/** Open dev mode (loads the inspector the first time). */
|
|
129
|
+
async open() {
|
|
130
|
+
if (!policy.enabled || destroyed) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (session) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
if (opening === null) {
|
|
137
|
+
const ticket = start().then((value) => {
|
|
138
|
+
if (opening !== ticket) {
|
|
139
|
+
// Closed (or destroyed) while the inspector was loading.
|
|
140
|
+
value?.close();
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
opening = null;
|
|
144
|
+
session = value;
|
|
145
|
+
if (value) {
|
|
146
|
+
publish(value);
|
|
147
|
+
options.onOpen?.();
|
|
148
|
+
}
|
|
149
|
+
return value;
|
|
150
|
+
});
|
|
151
|
+
opening = ticket;
|
|
152
|
+
}
|
|
153
|
+
return (await opening) !== null;
|
|
154
|
+
},
|
|
155
|
+
/** Close dev mode; the session and user layers stay as they are. */
|
|
156
|
+
close() {
|
|
157
|
+
opening = null;
|
|
158
|
+
session?.close();
|
|
159
|
+
},
|
|
160
|
+
/** Close dev mode when it is open, else open it. */
|
|
161
|
+
toggle() {
|
|
162
|
+
if (session || opening) {
|
|
163
|
+
controller.close();
|
|
164
|
+
return Promise.resolve(false);
|
|
165
|
+
}
|
|
166
|
+
return controller.open();
|
|
167
|
+
},
|
|
168
|
+
/** Open dev mode and select an element (`null` clears the selection). */
|
|
169
|
+
async select(id) {
|
|
170
|
+
return (await controller.open()) ? session.select(id) : null;
|
|
171
|
+
},
|
|
172
|
+
/** Close dev mode and remove its entry points. */
|
|
173
|
+
destroy() {
|
|
174
|
+
controller.close();
|
|
175
|
+
destroyed = true;
|
|
176
|
+
uninstall();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const uninstall = installActivation({
|
|
181
|
+
window,
|
|
182
|
+
policy,
|
|
183
|
+
onActivate: () => {
|
|
184
|
+
controller.open();
|
|
185
|
+
},
|
|
186
|
+
onToggle: () => {
|
|
187
|
+
controller.toggle();
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
return controller;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { exportOverrides } from './export.js';
|
|
194
|
+
export {
|
|
195
|
+
encodeShareHash,
|
|
196
|
+
decodeShareHash,
|
|
197
|
+
shareUrl,
|
|
198
|
+
URL_BUDGET,
|
|
199
|
+
} from './share.js';
|
|
200
|
+
export { validateOverrides } from './validate.js';
|
|
201
|
+
export { applySharedOverrides, BACKUP_KEY } from './apply-shared.js';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stylesheet of the inspector shadow root. Colours meet WCAG 2.2 AA in
|
|
3
|
+
* light and dark schemes; nothing animates when the user prefers reduced
|
|
4
|
+
* motion.
|
|
5
|
+
*/
|
|
6
|
+
export const INSPECTOR_CSS = `
|
|
7
|
+
:host {
|
|
8
|
+
all: initial;
|
|
9
|
+
position: fixed; top: 0; right: 0; bottom: 0; z-index: 2147483647;
|
|
10
|
+
width: var(--devmode-width, 420px); max-width: 100vw;
|
|
11
|
+
display: flex;
|
|
12
|
+
color-scheme: light dark;
|
|
13
|
+
--bg: #ffffff; --fg: #1d1b20; --muted: #49454f; --line: #c4c7c5;
|
|
14
|
+
--accent: #1a56db; --accent-fg: #ffffff; --soft: #eef2fb;
|
|
15
|
+
--changed: #fff3e0; --changed-line: #8a4b00; --error: #b3261e;
|
|
16
|
+
font: 13px/1.45 system-ui, -apple-system, 'Segoe UI', sans-serif;
|
|
17
|
+
color: var(--fg); background: var(--bg);
|
|
18
|
+
box-shadow: -2px 0 8px rgb(0 0 0 / 0.2);
|
|
19
|
+
}
|
|
20
|
+
@media (prefers-color-scheme: dark) {
|
|
21
|
+
:host {
|
|
22
|
+
--bg: #1d1b20; --fg: #e6e1e5; --muted: #cac4d0; --line: #49454f;
|
|
23
|
+
--accent: #a8c7fa; --accent-fg: #062e6f; --soft: #2b2930;
|
|
24
|
+
--changed: #3a2a12; --changed-line: #ffb870; --error: #f2b8b5;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
* { box-sizing: border-box; }
|
|
28
|
+
[hidden] { display: none !important; }
|
|
29
|
+
.splitter { flex: none; width: 6px; cursor: col-resize;
|
|
30
|
+
background: var(--line); }
|
|
31
|
+
.splitter:hover, .splitter:focus-visible { background: var(--accent); }
|
|
32
|
+
.frame { flex: 1; min-width: 0; overflow: auto; }
|
|
33
|
+
ui-devmode-panel { display: flex; flex-direction: column; min-height: 100%;
|
|
34
|
+
padding: 8px 12px; gap: 8px; }
|
|
35
|
+
ui-devmode-heading { display: block; font-size: 15px; font-weight: 700; }
|
|
36
|
+
ui-devmode-toolbar { display: flex; flex-wrap: wrap; gap: 6px; }
|
|
37
|
+
ui-devmode-status { display: block; color: var(--muted); min-height: 1.45em; }
|
|
38
|
+
ui-devmode-status[data-role="selection"] { color: var(--fg); font-weight: 600; }
|
|
39
|
+
ui-devmode-tabs { display: flex; flex-direction: column; gap: 6px; flex: 1; }
|
|
40
|
+
button, select, input { font: inherit; color: inherit; }
|
|
41
|
+
button { border: 1px solid var(--line); background: var(--soft);
|
|
42
|
+
border-radius: 6px; padding: 3px 10px; cursor: pointer; min-height: 28px; }
|
|
43
|
+
button:disabled { cursor: default; opacity: 0.65; }
|
|
44
|
+
button[aria-pressed="true"] { background: var(--accent);
|
|
45
|
+
color: var(--accent-fg); border-color: var(--accent); }
|
|
46
|
+
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
|
|
47
|
+
[role="tablist"] { display: flex; flex-wrap: wrap; gap: 2px;
|
|
48
|
+
border-bottom: 1px solid var(--line); }
|
|
49
|
+
[role="tab"] { border: 0; border-radius: 6px 6px 0 0; background: none;
|
|
50
|
+
border-bottom: 3px solid transparent; padding: 4px 8px; }
|
|
51
|
+
[role="tab"][aria-selected="true"] { border-bottom-color: var(--accent);
|
|
52
|
+
font-weight: 700; }
|
|
53
|
+
ui-devmode-tab { display: block; padding: 2px; }
|
|
54
|
+
ul[role="tree"], ul[role="group"] { list-style: none; margin: 0;
|
|
55
|
+
padding: 0; }
|
|
56
|
+
ul[role="group"] { padding-left: 16px; }
|
|
57
|
+
[role="treeitem"] > .row { display: flex; gap: 6px; align-items: baseline;
|
|
58
|
+
padding: 2px 6px; border-radius: 4px; cursor: pointer; }
|
|
59
|
+
[role="treeitem"][aria-selected="true"] > .row { background: var(--accent);
|
|
60
|
+
color: var(--accent-fg); }
|
|
61
|
+
[role="treeitem"]:focus { outline: none; }
|
|
62
|
+
[role="treeitem"]:focus-visible > .row { outline: 2px solid var(--accent);
|
|
63
|
+
outline-offset: -2px; }
|
|
64
|
+
.toggle { width: 1em; display: inline-block; text-align: center; }
|
|
65
|
+
.kind { opacity: 0.85; font-style: italic; }
|
|
66
|
+
table { width: 100%; border-collapse: collapse; }
|
|
67
|
+
caption { text-align: left; font-weight: 700; padding: 4px 0; }
|
|
68
|
+
th, td { text-align: left; vertical-align: middle; padding: 3px 4px;
|
|
69
|
+
border-bottom: 1px solid var(--line); }
|
|
70
|
+
thead th { font-size: 12px; color: var(--muted); }
|
|
71
|
+
tr.is-changed { background: var(--changed);
|
|
72
|
+
box-shadow: inset 3px 0 0 var(--changed-line); }
|
|
73
|
+
tr.is-locked th, tr.is-locked .resolved { color: var(--muted); }
|
|
74
|
+
td.resolved { font-family: ui-monospace, monospace; word-break: break-word; }
|
|
75
|
+
.layer { font-size: 12px; padding: 0 6px; border-radius: 8px;
|
|
76
|
+
border: 1px solid var(--line); white-space: nowrap; }
|
|
77
|
+
tr.is-changed .layer { border-color: var(--changed-line); font-weight: 700; }
|
|
78
|
+
.editor { display: flex; gap: 4px; align-items: center; }
|
|
79
|
+
.editor input[type="text"], .editor input[type="number"], .editor select {
|
|
80
|
+
width: 100%; min-width: 0; padding: 2px 4px; border-radius: 4px;
|
|
81
|
+
border: 1px solid var(--muted); background: var(--bg); }
|
|
82
|
+
.editor input[type="number"] { width: 6em; }
|
|
83
|
+
.editor input[type="checkbox"] { width: 24px; height: 24px; margin: 0;
|
|
84
|
+
accent-color: var(--accent); }
|
|
85
|
+
.editor input[type="color"] { width: 3em; height: 26px; padding: 0;
|
|
86
|
+
border: 1px solid var(--muted); background: var(--bg); }
|
|
87
|
+
[aria-invalid="true"] { border-color: var(--error) !important;
|
|
88
|
+
outline: 1px solid var(--error); }
|
|
89
|
+
.swatch { display: inline-block; width: 1em; height: 1em;
|
|
90
|
+
vertical-align: -2px; border: 1px solid var(--muted); margin-right: 4px; }
|
|
91
|
+
.note { color: var(--muted); margin: 4px 0; }
|
|
92
|
+
dl { margin: 4px 0 10px; display: grid; grid-template-columns: auto 1fr;
|
|
93
|
+
gap: 2px 10px; }
|
|
94
|
+
dt { font-weight: 600; }
|
|
95
|
+
dd { margin: 0; font-family: ui-monospace, monospace; }
|
|
96
|
+
h3 { font-size: 13px; margin: 8px 0 2px; }
|
|
97
|
+
pre { margin: 0; padding: 8px; background: var(--soft); border-radius: 6px;
|
|
98
|
+
white-space: pre-wrap; font: 12px/1.5 ui-monospace, monospace; }
|
|
99
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
100
|
+
button, [role="tab"] { transition: background-color 120ms ease-out; }
|
|
101
|
+
}
|
|
102
|
+
@media (forced-colors: active) {
|
|
103
|
+
[role="treeitem"][aria-selected="true"] > .row { outline: 2px solid Highlight; }
|
|
104
|
+
tr.is-changed { outline: 1px solid Highlight; }
|
|
105
|
+
}
|
|
106
|
+
`;
|
package/src/inspector.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/* global URL */
|
|
2
|
+
import { readAsset } from './read-asset.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The inspector panel: a host docked to the right of the page, resizable,
|
|
6
|
+
* whose shadow root holds the layout of `inspector.lino` rendered by ui-dom
|
|
7
|
+
* with the dev mode kinds below. Every text comes from the message table.
|
|
8
|
+
*
|
|
9
|
+
* The panel knows nothing of the page: `session.js` fills its views and
|
|
10
|
+
* reacts to its commands.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { mountDocument } from '@link-foundation/ui-dom';
|
|
14
|
+
import { INSPECTOR_CSS } from './inspector-css.js';
|
|
15
|
+
|
|
16
|
+
/** The width of the panel, in CSS pixels. */
|
|
17
|
+
export const WIDTH = Object.freeze({ initial: 420, min: 280, step: 16 });
|
|
18
|
+
|
|
19
|
+
const kind = (properties, children = 'any') => ({
|
|
20
|
+
positional: [],
|
|
21
|
+
properties: new Map(
|
|
22
|
+
properties.map((name) => [name, { type: 'string', multi: false }])
|
|
23
|
+
),
|
|
24
|
+
children,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The vocabulary of `inspector.lino`: the dev mode kinds and their
|
|
29
|
+
* properties (all text, message keys or names).
|
|
30
|
+
*/
|
|
31
|
+
export const LAYOUT_VOCABULARY = Object.freeze({
|
|
32
|
+
version: null,
|
|
33
|
+
kinds: new Map([
|
|
34
|
+
['devmode-panel', kind(['label'])],
|
|
35
|
+
['devmode-heading', kind(['label'], 'none')],
|
|
36
|
+
['devmode-toolbar', kind(['label'])],
|
|
37
|
+
['devmode-action', kind(['label', 'command', 'toggle'], 'none')],
|
|
38
|
+
['devmode-status', kind(['label', 'role'], 'none')],
|
|
39
|
+
['devmode-tabs', kind(['label'])],
|
|
40
|
+
['devmode-tab', kind(['label', 'view'], 'none')],
|
|
41
|
+
]),
|
|
42
|
+
actions: new Map(),
|
|
43
|
+
multiValued: new Set(),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const text = (el, name) => {
|
|
47
|
+
const value = el.props.get(name)?.[0];
|
|
48
|
+
return typeof value === 'string' ? value : undefined;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** The translated `label` of a layout element. */
|
|
52
|
+
const label = (el, ctx) => ctx.options.t(text(el, 'label') ?? '');
|
|
53
|
+
|
|
54
|
+
const make = (ctx, tag, attributes = {}) => {
|
|
55
|
+
const node = ctx.document.createElement(tag);
|
|
56
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
57
|
+
node.setAttribute(name, value);
|
|
58
|
+
}
|
|
59
|
+
return node;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** The ui-dom kinds of the layout (render option `kinds`). */
|
|
63
|
+
export const LAYOUT_KINDS = Object.freeze({
|
|
64
|
+
'devmode-panel': {
|
|
65
|
+
tag: 'ui-devmode-panel',
|
|
66
|
+
create: (el, ctx) =>
|
|
67
|
+
make(ctx, 'ui-devmode-panel', {
|
|
68
|
+
role: 'complementary',
|
|
69
|
+
'aria-label': label(el, ctx),
|
|
70
|
+
}),
|
|
71
|
+
},
|
|
72
|
+
'devmode-heading': {
|
|
73
|
+
tag: 'ui-devmode-heading',
|
|
74
|
+
childrenSlot: null,
|
|
75
|
+
create(el, ctx) {
|
|
76
|
+
const node = make(ctx, 'ui-devmode-heading', {
|
|
77
|
+
role: 'heading',
|
|
78
|
+
'aria-level': '2',
|
|
79
|
+
});
|
|
80
|
+
node.textContent = label(el, ctx);
|
|
81
|
+
return node;
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
'devmode-toolbar': {
|
|
85
|
+
tag: 'ui-devmode-toolbar',
|
|
86
|
+
create: (el, ctx) =>
|
|
87
|
+
make(ctx, 'ui-devmode-toolbar', {
|
|
88
|
+
role: 'group',
|
|
89
|
+
'aria-label': label(el, ctx),
|
|
90
|
+
}),
|
|
91
|
+
},
|
|
92
|
+
'devmode-action': {
|
|
93
|
+
tag: 'ui-devmode-action',
|
|
94
|
+
childrenSlot: null,
|
|
95
|
+
create(el, ctx) {
|
|
96
|
+
const node = make(ctx, 'ui-devmode-action');
|
|
97
|
+
const button = make(ctx, 'button', {
|
|
98
|
+
type: 'button',
|
|
99
|
+
'data-command': text(el, 'command') ?? '',
|
|
100
|
+
});
|
|
101
|
+
if (text(el, 'toggle') === 'true') {
|
|
102
|
+
button.setAttribute('aria-pressed', 'false');
|
|
103
|
+
}
|
|
104
|
+
button.textContent = label(el, ctx);
|
|
105
|
+
node.append(button);
|
|
106
|
+
return node;
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
'devmode-status': {
|
|
110
|
+
tag: 'ui-devmode-status',
|
|
111
|
+
childrenSlot: null,
|
|
112
|
+
create(el, ctx) {
|
|
113
|
+
const role = text(el, 'role');
|
|
114
|
+
const node = make(ctx, 'ui-devmode-status', { 'data-role': role ?? '' });
|
|
115
|
+
if (role === 'live') {
|
|
116
|
+
node.setAttribute('role', 'status');
|
|
117
|
+
}
|
|
118
|
+
node.textContent = text(el, 'label') ? label(el, ctx) : '';
|
|
119
|
+
return node;
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
'devmode-tabs': {
|
|
123
|
+
tag: 'ui-devmode-tabs',
|
|
124
|
+
create(el, ctx) {
|
|
125
|
+
const node = make(ctx, 'ui-devmode-tabs');
|
|
126
|
+
const list = make(ctx, 'div', {
|
|
127
|
+
role: 'tablist',
|
|
128
|
+
'aria-label': label(el, ctx),
|
|
129
|
+
});
|
|
130
|
+
const panels = make(ctx, 'div', { class: 'panels' });
|
|
131
|
+
node.append(list, panels);
|
|
132
|
+
return node;
|
|
133
|
+
},
|
|
134
|
+
childrenSlot: (node) => node.querySelector('.panels'),
|
|
135
|
+
},
|
|
136
|
+
'devmode-tab': {
|
|
137
|
+
tag: 'ui-devmode-tab',
|
|
138
|
+
childrenSlot: null,
|
|
139
|
+
create: (el, ctx) =>
|
|
140
|
+
make(ctx, 'ui-devmode-tab', {
|
|
141
|
+
role: 'tabpanel',
|
|
142
|
+
tabindex: '0',
|
|
143
|
+
'data-view': text(el, 'view') ?? '',
|
|
144
|
+
'data-label': label(el, ctx),
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Read `inspector.lino` next to this module: with `fetch` in a browser,
|
|
151
|
+
* from the file system in Node.js.
|
|
152
|
+
* @returns {Promise<string>}
|
|
153
|
+
*/
|
|
154
|
+
export async function loadLayout() {
|
|
155
|
+
return await readAsset(new URL('./inspector.lino', import.meta.url));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Make the tab buttons of the panels and wire the WAI-ARIA tabs keys. */
|
|
159
|
+
function buildTabs(shadow, onChange) {
|
|
160
|
+
const list = shadow.querySelector('[role="tablist"]');
|
|
161
|
+
const panels = [...shadow.querySelectorAll('[role="tabpanel"]')];
|
|
162
|
+
const doc = shadow.ownerDocument;
|
|
163
|
+
const tabs = panels.map((panel) => {
|
|
164
|
+
const view = panel.dataset.view;
|
|
165
|
+
const tab = doc.createElement('button');
|
|
166
|
+
tab.type = 'button';
|
|
167
|
+
tab.setAttribute('role', 'tab');
|
|
168
|
+
tab.id = `devmode-tab-${view}`;
|
|
169
|
+
tab.dataset.view = view;
|
|
170
|
+
tab.textContent = panel.dataset.label;
|
|
171
|
+
panel.id = `devmode-panel-${view}`;
|
|
172
|
+
tab.setAttribute('aria-controls', panel.id);
|
|
173
|
+
panel.setAttribute('aria-labelledby', tab.id);
|
|
174
|
+
list.append(tab);
|
|
175
|
+
return tab;
|
|
176
|
+
});
|
|
177
|
+
const select = (view, focus = false) => {
|
|
178
|
+
tabs.forEach((tab, i) => {
|
|
179
|
+
const on = tab.dataset.view === view;
|
|
180
|
+
tab.setAttribute('aria-selected', String(on));
|
|
181
|
+
tab.tabIndex = on ? 0 : -1;
|
|
182
|
+
panels[i].hidden = !on;
|
|
183
|
+
if (on && focus) {
|
|
184
|
+
tab.focus();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
onChange(view);
|
|
188
|
+
};
|
|
189
|
+
list.addEventListener('click', (event) => {
|
|
190
|
+
const tab = event.target.closest?.('[role="tab"]');
|
|
191
|
+
if (tab) {
|
|
192
|
+
select(tab.dataset.view);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
list.addEventListener('keydown', (event) => {
|
|
196
|
+
const at = tabs.indexOf(event.target);
|
|
197
|
+
const moves = {
|
|
198
|
+
ArrowRight: at + 1,
|
|
199
|
+
ArrowLeft: at - 1,
|
|
200
|
+
Home: 0,
|
|
201
|
+
End: tabs.length - 1,
|
|
202
|
+
};
|
|
203
|
+
if (at < 0 || !(event.key in moves)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
const next = (moves[event.key] + tabs.length) % tabs.length;
|
|
208
|
+
select(tabs[next].dataset.view, true);
|
|
209
|
+
});
|
|
210
|
+
return { select, panels };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The WAI-ARIA window splitter on the left edge of the panel. */
|
|
214
|
+
function buildSplitter(host, shadow, t, window) {
|
|
215
|
+
const doc = shadow.ownerDocument;
|
|
216
|
+
const splitter = doc.createElement('div');
|
|
217
|
+
splitter.className = 'splitter';
|
|
218
|
+
splitter.tabIndex = 0;
|
|
219
|
+
splitter.setAttribute('role', 'separator');
|
|
220
|
+
splitter.setAttribute('aria-orientation', 'vertical');
|
|
221
|
+
splitter.setAttribute('aria-label', t('resize.label'));
|
|
222
|
+
splitter.setAttribute('aria-valuemin', String(WIDTH.min));
|
|
223
|
+
const max = () => Math.max(WIDTH.min, Math.round(window.innerWidth * 0.9));
|
|
224
|
+
const setWidth = (width) => {
|
|
225
|
+
const clamped = Math.round(Math.min(max(), Math.max(WIDTH.min, width)));
|
|
226
|
+
host.style.setProperty('--devmode-width', `${clamped}px`);
|
|
227
|
+
splitter.setAttribute('aria-valuemax', String(max()));
|
|
228
|
+
splitter.setAttribute('aria-valuenow', String(clamped));
|
|
229
|
+
return clamped;
|
|
230
|
+
};
|
|
231
|
+
let width = setWidth(WIDTH.initial);
|
|
232
|
+
splitter.addEventListener('keydown', (event) => {
|
|
233
|
+
const keys = {
|
|
234
|
+
ArrowLeft: width + WIDTH.step,
|
|
235
|
+
ArrowRight: width - WIDTH.step,
|
|
236
|
+
Home: max(),
|
|
237
|
+
End: WIDTH.min,
|
|
238
|
+
};
|
|
239
|
+
if (event.key in keys) {
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
width = setWidth(keys[event.key]);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
splitter.addEventListener('pointerdown', (event) => {
|
|
245
|
+
event.preventDefault();
|
|
246
|
+
splitter.setPointerCapture?.(event.pointerId);
|
|
247
|
+
const move = (e) => {
|
|
248
|
+
width = setWidth(window.innerWidth - e.clientX);
|
|
249
|
+
};
|
|
250
|
+
const up = () => {
|
|
251
|
+
splitter.removeEventListener('pointermove', move);
|
|
252
|
+
splitter.removeEventListener('pointerup', up);
|
|
253
|
+
};
|
|
254
|
+
splitter.addEventListener('pointermove', move);
|
|
255
|
+
splitter.addEventListener('pointerup', up);
|
|
256
|
+
});
|
|
257
|
+
return { splitter, setWidth: (w) => (width = setWidth(w)) };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Create the inspector panel and append it to the page.
|
|
262
|
+
* @param {{ window: Window & typeof globalThis, layout: string,
|
|
263
|
+
* t: (key: string, params?: object) => string,
|
|
264
|
+
* onCommand: (command: string) => void,
|
|
265
|
+
* onView?: (view: string) => void }} options
|
|
266
|
+
*/
|
|
267
|
+
export function createInspector({
|
|
268
|
+
window,
|
|
269
|
+
layout,
|
|
270
|
+
t,
|
|
271
|
+
onCommand,
|
|
272
|
+
onView,
|
|
273
|
+
container = window.document.body,
|
|
274
|
+
}) {
|
|
275
|
+
const doc = window.document;
|
|
276
|
+
const host = doc.createElement('ui-devmode-inspector');
|
|
277
|
+
host.dataset.uiDevmode = 'inspector';
|
|
278
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
279
|
+
const { splitter, setWidth } = buildSplitter(host, shadow, t, window);
|
|
280
|
+
const frame = doc.createElement('div');
|
|
281
|
+
frame.className = 'frame';
|
|
282
|
+
shadow.append(splitter, frame);
|
|
283
|
+
container.append(host);
|
|
284
|
+
const mounted = mountDocument(frame, layout, {
|
|
285
|
+
parseOptions: { vocabulary: LAYOUT_VOCABULARY },
|
|
286
|
+
kinds: LAYOUT_KINDS,
|
|
287
|
+
t,
|
|
288
|
+
baseCss: false,
|
|
289
|
+
css: INSPECTOR_CSS,
|
|
290
|
+
});
|
|
291
|
+
mounted.handle.flush?.();
|
|
292
|
+
|
|
293
|
+
let view = 'outline';
|
|
294
|
+
const tabs = buildTabs(shadow, (next) => {
|
|
295
|
+
view = next;
|
|
296
|
+
onView?.(next);
|
|
297
|
+
});
|
|
298
|
+
const button = (command) =>
|
|
299
|
+
shadow.querySelector(`button[data-command="${command}"]`);
|
|
300
|
+
shadow.addEventListener('click', (event) => {
|
|
301
|
+
const target = event.target.closest?.('button[data-command]');
|
|
302
|
+
if (target && !target.disabled) {
|
|
303
|
+
onCommand(target.dataset.command);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
const status = shadow.querySelector('[data-role="live"]');
|
|
307
|
+
const selection = shadow.querySelector('[data-role="selection"]');
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
host,
|
|
311
|
+
shadow,
|
|
312
|
+
/** The panel element of a view (`outline`, `properties`, …). */
|
|
313
|
+
panel: (name) =>
|
|
314
|
+
shadow.querySelector(`[role="tabpanel"][data-view="${name}"]`),
|
|
315
|
+
get view() {
|
|
316
|
+
return view;
|
|
317
|
+
},
|
|
318
|
+
showView: (name, focus = false) => tabs.select(name, focus),
|
|
319
|
+
setPressed(command, pressed) {
|
|
320
|
+
button(command)?.setAttribute('aria-pressed', String(pressed));
|
|
321
|
+
},
|
|
322
|
+
setDisabled(command, disabled) {
|
|
323
|
+
const target = button(command);
|
|
324
|
+
if (target) {
|
|
325
|
+
target.disabled = disabled;
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
setSelection(textContent) {
|
|
329
|
+
selection.textContent = textContent;
|
|
330
|
+
},
|
|
331
|
+
announce(textContent) {
|
|
332
|
+
status.textContent = textContent;
|
|
333
|
+
},
|
|
334
|
+
setWidth,
|
|
335
|
+
/** Whether an event happened inside the panel. */
|
|
336
|
+
contains: (event) => event.composedPath?.().includes(host) ?? false,
|
|
337
|
+
destroy() {
|
|
338
|
+
mounted.handle.destroy();
|
|
339
|
+
host.remove();
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
devmode-inspector: devmode-panel
|
|
2
|
+
label inspector.label
|
|
3
|
+
devmode-title: devmode-heading
|
|
4
|
+
label inspector.title
|
|
5
|
+
devmode-actions: devmode-toolbar
|
|
6
|
+
label toolbar.label
|
|
7
|
+
devmode-pick: devmode-action
|
|
8
|
+
label toolbar.pick
|
|
9
|
+
command pick
|
|
10
|
+
toggle true
|
|
11
|
+
devmode-save: devmode-action
|
|
12
|
+
label toolbar.save
|
|
13
|
+
command save
|
|
14
|
+
devmode-discard: devmode-action
|
|
15
|
+
label toolbar.discard
|
|
16
|
+
command discard
|
|
17
|
+
devmode-defaults: devmode-action
|
|
18
|
+
label toolbar.defaults
|
|
19
|
+
command defaults
|
|
20
|
+
devmode-comments: devmode-action
|
|
21
|
+
label toolbar.comments
|
|
22
|
+
command comments
|
|
23
|
+
devmode-close: devmode-action
|
|
24
|
+
label toolbar.close
|
|
25
|
+
command close
|
|
26
|
+
devmode-selection: devmode-status
|
|
27
|
+
label selection.none
|
|
28
|
+
role selection
|
|
29
|
+
devmode-views: devmode-tabs
|
|
30
|
+
label tabs.label
|
|
31
|
+
devmode-outline: devmode-tab
|
|
32
|
+
label tab.outline
|
|
33
|
+
view outline
|
|
34
|
+
devmode-properties: devmode-tab
|
|
35
|
+
label tab.properties
|
|
36
|
+
view properties
|
|
37
|
+
devmode-styles: devmode-tab
|
|
38
|
+
label tab.styles
|
|
39
|
+
view styles
|
|
40
|
+
devmode-tokens: devmode-tab
|
|
41
|
+
label tab.tokens
|
|
42
|
+
view tokens
|
|
43
|
+
devmode-animations: devmode-tab
|
|
44
|
+
label tab.animations
|
|
45
|
+
view animations
|
|
46
|
+
devmode-source: devmode-tab
|
|
47
|
+
label tab.source
|
|
48
|
+
view source
|
|
49
|
+
devmode-status: devmode-status
|
|
50
|
+
role live
|