@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.
Files changed (46) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +353 -2
  3. package/package.json +78 -4
  4. package/src/activation.js +235 -0
  5. package/src/adapter.js +92 -0
  6. package/src/agent-instruction.d.ts +6 -0
  7. package/src/agent-instruction.js +17 -0
  8. package/src/agent-instruction.txt +8 -0
  9. package/src/apply-shared.js +135 -0
  10. package/src/comments/commands.js +77 -0
  11. package/src/comments/geometry.js +65 -0
  12. package/src/comments/index.d.ts +93 -0
  13. package/src/comments/index.js +205 -0
  14. package/src/comments/model.js +359 -0
  15. package/src/comments/picking.js +154 -0
  16. package/src/comments/style.js +36 -0
  17. package/src/comments/view.js +250 -0
  18. package/src/comments/voice.js +64 -0
  19. package/src/defaults-dialog.js +120 -0
  20. package/src/details.js +159 -0
  21. package/src/editors.js +194 -0
  22. package/src/export.js +50 -0
  23. package/src/foreign/adapter.js +166 -0
  24. package/src/foreign/apply.js +89 -0
  25. package/src/foreign/entry.js +173 -0
  26. package/src/foreign/index.d.ts +32 -0
  27. package/src/foreign/injected.js +6 -0
  28. package/src/foreign/persist.js +54 -0
  29. package/src/foreign/properties.js +126 -0
  30. package/src/foreign/runtime.js +67 -0
  31. package/src/index.d.ts +204 -0
  32. package/src/index.js +201 -0
  33. package/src/inspector-css.js +106 -0
  34. package/src/inspector.js +342 -0
  35. package/src/inspector.lino +50 -0
  36. package/src/messages.js +113 -0
  37. package/src/model.d.ts +101 -0
  38. package/src/model.js +473 -0
  39. package/src/overlay.js +91 -0
  40. package/src/policy.js +222 -0
  41. package/src/read-asset.browser.js +8 -0
  42. package/src/read-asset.js +10 -0
  43. package/src/session.js +555 -0
  44. package/src/share.js +23 -0
  45. package/src/validate.js +170 -0
  46. package/src/views.js +328 -0
package/src/overlay.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * The highlight overlay: a box over the hovered element and one over the
3
+ * selected element, each labelled `id: type`. The boxes live in a shadow
4
+ * root of a fixed host above the page, take no pointer events, follow the
5
+ * element on scroll, resize and re-render, and do not animate when the user
6
+ * prefers reduced motion.
7
+ */
8
+
9
+ const OVERLAY_CSS = `
10
+ :host { all: initial; position: fixed; inset: 0; pointer-events: none;
11
+ z-index: 2147483646; }
12
+ .box { position: fixed; box-sizing: border-box; pointer-events: none;
13
+ border: 2px solid #1a56db; background: rgb(26 86 219 / 0.08);
14
+ transition: top 80ms ease-out, left 80ms ease-out, width 80ms ease-out,
15
+ height 80ms ease-out; }
16
+ .box[hidden] { display: none; }
17
+ .box.selected { border-color: #b3261e; background: transparent;
18
+ border-style: dashed; }
19
+ .label { position: absolute; left: -2px; bottom: 100%; padding: 1px 6px;
20
+ font: 600 12px/18px system-ui, sans-serif; white-space: nowrap;
21
+ color: #fff; background: #1a56db; border-radius: 3px 3px 0 0; }
22
+ .box.selected .label { background: #b3261e; }
23
+ .box.below .label { bottom: auto; top: 100%; border-radius: 0 0 3px 3px; }
24
+ @media (prefers-reduced-motion: reduce) { .box { transition: none; } }
25
+ @media (forced-colors: active) { .box { border-color: Highlight; }
26
+ .label { forced-color-adjust: none; } }
27
+ `;
28
+
29
+ /**
30
+ * Create the overlay.
31
+ * @param {{ document: Document, label: (id: string, type: string) => string }} options
32
+ * @returns {{ host: HTMLElement, show: (which: 'hover'|'selected',
33
+ * target: { node: Element, id: string, type: string }|null) => void,
34
+ * update: () => void, destroy: () => void }}
35
+ */
36
+ export function createOverlay({ document, label, container = document.body }) {
37
+ const host = document.createElement('ui-devmode-overlay');
38
+ host.setAttribute('aria-hidden', 'true');
39
+ host.dataset.uiDevmode = 'overlay';
40
+ const shadow = host.attachShadow({ mode: 'open' });
41
+ const style = document.createElement('style');
42
+ style.textContent = OVERLAY_CSS;
43
+ shadow.append(style);
44
+ const boxes = {};
45
+ const targets = { hover: null, selected: null };
46
+ for (const which of ['selected', 'hover']) {
47
+ const box = document.createElement('div');
48
+ box.className = which === 'hover' ? 'box hover' : 'box selected';
49
+ box.dataset.box = which;
50
+ box.hidden = true;
51
+ const text = document.createElement('span');
52
+ text.className = 'label';
53
+ box.append(text);
54
+ shadow.append(box);
55
+ boxes[which] = box;
56
+ }
57
+ container.append(host);
58
+
59
+ const place = (which) => {
60
+ const box = boxes[which];
61
+ const target = targets[which];
62
+ if (!target || !target.node.isConnected) {
63
+ box.hidden = true;
64
+ return;
65
+ }
66
+ const rect = target.node.getBoundingClientRect();
67
+ box.hidden = false;
68
+ box.style.top = `${rect.top}px`;
69
+ box.style.left = `${rect.left}px`;
70
+ box.style.width = `${rect.width}px`;
71
+ box.style.height = `${rect.height}px`;
72
+ // The label sits above the box, or inside the top when there is no room.
73
+ box.classList.toggle('below', rect.top < 20);
74
+ box.firstChild.textContent = label(target.id, target.type);
75
+ };
76
+
77
+ return {
78
+ host,
79
+ show(which, target) {
80
+ targets[which] = target;
81
+ place(which);
82
+ },
83
+ update() {
84
+ place('selected');
85
+ place('hover');
86
+ },
87
+ destroy() {
88
+ host.remove();
89
+ },
90
+ };
91
+ }
package/src/policy.js ADDED
@@ -0,0 +1,222 @@
1
+ /**
2
+ * The dev mode policy of a site: the `dev-mode` link of the site
3
+ * configuration. It can turn dev mode off, restrict its entry points, the
4
+ * configuration layers an edit may write and the elements that stay
5
+ * read-only.
6
+ *
7
+ * ```lino
8
+ * (site-dev-mode: dev-mode
9
+ * (enabled true)
10
+ * (entry shortcut url-parameter element)
11
+ * (shortcut 'Alt+Shift+D')
12
+ * (write-layers session user)
13
+ * (locked-elements payment-form))
14
+ * ```
15
+ *
16
+ * The text is read with ui-core `parse` (the official `links-notation`
17
+ * parser); this module never parses Links Notation itself.
18
+ */
19
+
20
+ import { parse } from '@link-foundation/ui-core';
21
+
22
+ /** The kind of the policy link. */
23
+ export const POLICY_KIND = 'dev-mode';
24
+
25
+ /** The entry points of dev mode, in the order they are documented. */
26
+ export const ENTRY_POINTS = Object.freeze([
27
+ 'shortcut',
28
+ 'url-parameter',
29
+ 'element',
30
+ ]);
31
+
32
+ /** The layers dev mode may write, lowest first (ui-config `EDITABLE_LAYERS`). */
33
+ export const WRITABLE_LAYERS = Object.freeze(['user', 'session']);
34
+
35
+ /** The shortcut when the policy names none. */
36
+ export const DEFAULT_SHORTCUT = 'Alt+Shift+D';
37
+
38
+ /** The configuration layers dev mode never writes. */
39
+ const READ_ONLY_LAYERS = new Set(['defaults', 'theme', 'site']);
40
+
41
+ /**
42
+ * @typedef {object} PolicyWarning
43
+ * @property {string} code `unknown-key`, `unknown-entry`, `unknown-layer`,
44
+ * `read-only-layer`, `bad-value` or `duplicate-policy`
45
+ * @property {'warning'} severity
46
+ * @property {string} message
47
+ * @property {Record<string, unknown>} at
48
+ *
49
+ * @typedef {object} DevModePolicy
50
+ * @property {boolean} enabled
51
+ * @property {ReadonlySet<string>} entry entry points that may open dev mode
52
+ * @property {string} shortcut the key combination, `Alt+Shift+D` by default
53
+ * @property {readonly ('user'|'session')[]} writeLayers lowest first
54
+ * @property {ReadonlySet<string>} lockedElements read-only element ids
55
+ * @property {ReadonlySet<string>} comfortKeys protected property or token keys
56
+ * @property {string|undefined} repository optional GitHub owner/repo
57
+ * @property {string|undefined} siteConfig committed site configuration path
58
+ * @property {readonly PolicyWarning[]} warnings
59
+ */
60
+
61
+ const warning = (code, message, at = {}) =>
62
+ Object.freeze({ code, severity: 'warning', message, at: Object.freeze(at) });
63
+
64
+ /** The ui-core nodes of the input, depth first. */
65
+ function nodesOf(input) {
66
+ if (input === undefined || input === null) {
67
+ return [];
68
+ }
69
+ const roots =
70
+ typeof input === 'string'
71
+ ? parse(input).roots
72
+ : Array.isArray(input)
73
+ ? input
74
+ : (input.roots ?? [input]);
75
+ const nodes = [];
76
+ const visit = (node) => {
77
+ nodes.push(node);
78
+ (node.children ?? []).forEach(visit);
79
+ };
80
+ roots.forEach(visit);
81
+ return nodes;
82
+ }
83
+
84
+ /**
85
+ * The sublinks of a policy node as `[name, values]`: the links ui-core keeps
86
+ * as `unknown` (the vocabulary has no `dev-mode` kind) and, should the kind
87
+ * be declared, its properties.
88
+ */
89
+ const entriesOf = (node) => [
90
+ ...(node.unknown ?? [])
91
+ .filter((raw) => Array.isArray(raw) && typeof raw[0] === 'string')
92
+ .map(([name, ...values]) => [name, values]),
93
+ ...(node.properties ?? []).map((p) => [p.name, p.values]),
94
+ ];
95
+
96
+ const atoms = (values) => values.filter((v) => typeof v === 'string');
97
+
98
+ const READERS = {
99
+ repository(policy, values) {
100
+ policy.repository = atoms(values)[0];
101
+ },
102
+ 'site-config'(policy, values) {
103
+ policy.siteConfig = atoms(values)[0];
104
+ },
105
+ 'comfort-keys'(policy, values) {
106
+ policy.comfortKeys = new Set(atoms(values));
107
+ },
108
+ enabled(policy, values, report) {
109
+ const [value] = values;
110
+ if (values.length !== 1 || (value !== 'true' && value !== 'false')) {
111
+ report('bad-value', '(enabled …) takes true or false', {
112
+ key: 'enabled',
113
+ });
114
+ return;
115
+ }
116
+ policy.enabled = value === 'true';
117
+ },
118
+ entry(policy, values, report) {
119
+ policy.entry = new Set();
120
+ for (const value of atoms(values)) {
121
+ if (ENTRY_POINTS.includes(value)) {
122
+ policy.entry.add(value);
123
+ } else {
124
+ report('unknown-entry', `Unknown entry point "${value}"`, { value });
125
+ }
126
+ }
127
+ },
128
+ shortcut(policy, values, report) {
129
+ if (values.length !== 1 || typeof values[0] !== 'string') {
130
+ report('bad-value', "(shortcut …) takes one key such as 'Alt+Shift+D'", {
131
+ key: 'shortcut',
132
+ });
133
+ return;
134
+ }
135
+ policy.shortcut = values[0];
136
+ },
137
+ 'write-layers'(policy, values, report) {
138
+ const layers = new Set();
139
+ for (const value of atoms(values)) {
140
+ if (WRITABLE_LAYERS.includes(value)) {
141
+ layers.add(value);
142
+ } else if (READ_ONLY_LAYERS.has(value)) {
143
+ report(
144
+ 'read-only-layer',
145
+ `Dev mode never writes the ${value} layer; it is ignored`,
146
+ { value }
147
+ );
148
+ } else {
149
+ report('unknown-layer', `Unknown layer "${value}"`, { value });
150
+ }
151
+ }
152
+ policy.writeLayers = WRITABLE_LAYERS.filter((layer) => layers.has(layer));
153
+ },
154
+ 'locked-elements'(policy, values) {
155
+ policy.lockedElements = new Set(atoms(values));
156
+ },
157
+ };
158
+
159
+ /**
160
+ * The default policy: enabled, every entry point, `Alt+Shift+D`, writes to
161
+ * `session` and `user`, nothing locked.
162
+ * @returns {DevModePolicy}
163
+ */
164
+ export const defaultPolicy = () =>
165
+ freezePolicy({
166
+ enabled: true,
167
+ entry: new Set(ENTRY_POINTS),
168
+ shortcut: DEFAULT_SHORTCUT,
169
+ writeLayers: [...WRITABLE_LAYERS],
170
+ lockedElements: new Set(),
171
+ comfortKeys: new Set(),
172
+ repository: undefined,
173
+ siteConfig: undefined,
174
+ warnings: [],
175
+ });
176
+
177
+ function freezePolicy(policy) {
178
+ return Object.freeze({
179
+ ...policy,
180
+ entry: Object.freeze(policy.entry),
181
+ writeLayers: Object.freeze([...policy.writeLayers]),
182
+ lockedElements: Object.freeze(policy.lockedElements),
183
+ comfortKeys: Object.freeze(policy.comfortKeys),
184
+ warnings: Object.freeze([...policy.warnings]),
185
+ });
186
+ }
187
+
188
+ /**
189
+ * Read the dev mode policy from the site configuration.
190
+ *
191
+ * `links` is Links Notation text, a ui-core document (`parse`), or a list of
192
+ * ui-core nodes. The first element of kind `dev-mode` (at any depth) is
193
+ * the policy; without one the defaults apply. Unknown keys, entry points
194
+ * and layers are warnings, never errors.
195
+ * @param {string|{roots: readonly object[]}|readonly object[]|null} [links]
196
+ * @returns {DevModePolicy}
197
+ */
198
+ export function parseDevModePolicy(links) {
199
+ const policy = { ...defaultPolicy() };
200
+ policy.warnings = [];
201
+ const report = (code, message, at) =>
202
+ policy.warnings.push(warning(code, message, at));
203
+ const found = nodesOf(links).filter((node) => node.kind === POLICY_KIND);
204
+ if (found.length > 1) {
205
+ report(
206
+ 'duplicate-policy',
207
+ `Only the first ${POLICY_KIND} link is read; "${found[1].id}" is ignored`,
208
+ { element: found[1].id }
209
+ );
210
+ }
211
+ for (const [name, values] of found.length > 0 ? entriesOf(found[0]) : []) {
212
+ const read = Object.hasOwn(READERS, name) ? READERS[name] : null;
213
+ if (read === null) {
214
+ report('unknown-key', `Unknown dev mode key "${name}"; it is ignored`, {
215
+ key: name,
216
+ });
217
+ } else {
218
+ read(policy, values, report);
219
+ }
220
+ }
221
+ return freezePolicy(policy);
222
+ }
@@ -0,0 +1,8 @@
1
+ /** Read emitted or inlined browser assets without importing filesystem modules. */
2
+ export async function readAsset(url) {
3
+ const response = await globalThis.fetch(url);
4
+ if (!response.ok) {
5
+ throw new Error(`Could not load ${url}: ${response.status}`);
6
+ }
7
+ return response.text();
8
+ }
@@ -0,0 +1,10 @@
1
+ import { readAsset as fetchAsset } from './read-asset.browser.js';
2
+
3
+ /** Native ESM can read package assets from disk; HTTP URLs still use fetch. */
4
+ export async function readAsset(url) {
5
+ if (url.protocol === 'file:') {
6
+ const { readFile } = await import('node:fs/promises');
7
+ return readFile(url, 'utf8');
8
+ }
9
+ return fetchAsset(url);
10
+ }