@link-foundation/ui-dom 0.0.0-stage → 0.1.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 +295 -2
- package/package.json +54 -4
- package/src/diagnostics.js +58 -0
- package/src/events.js +174 -0
- package/src/frame-fit.js +205 -0
- package/src/html.d.ts +4 -0
- package/src/html.js +245 -0
- package/src/index.d.ts +267 -0
- package/src/index.js +47 -0
- package/src/kinds/basic.js +189 -0
- package/src/kinds.js +151 -0
- package/src/model.js +84 -0
- package/src/patch.js +62 -0
- package/src/render.js +121 -0
- package/src/scheduler.js +50 -0
- package/src/styles.js +190 -0
- package/src/themes.js +147 -0
- package/src/tree.js +335 -0
package/src/tree.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rendered tree of a store, shared by `render` (a live DOM) and
|
|
3
|
+
* `renderToHTML` (a static document): one custom element per store
|
|
4
|
+
* element, its store ID in `data-link-id`, kept in a Map (ID → node) and a
|
|
5
|
+
* WeakMap (node → ID).
|
|
6
|
+
*
|
|
7
|
+
* Every store element gets a node, so `idFor(nodeFor(id)) === id` holds
|
|
8
|
+
* for all of them; the children of a kind without a children slot (a text
|
|
9
|
+
* with children) stay detached.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { themeFor } from './themes.js';
|
|
13
|
+
import { GENERATED_ID_PREFIX } from '@link-foundation/ui-store/memory';
|
|
14
|
+
import { diagnostic } from './diagnostics.js';
|
|
15
|
+
import { bindHandlers, handlersOf } from './events.js';
|
|
16
|
+
import { UNKNOWN_KIND } from './kinds/basic.js';
|
|
17
|
+
import { ensureTag, getKind } from './kinds.js';
|
|
18
|
+
import {
|
|
19
|
+
atom,
|
|
20
|
+
elementOf,
|
|
21
|
+
propNames,
|
|
22
|
+
vocabulary,
|
|
23
|
+
withProperties,
|
|
24
|
+
} from './model.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {import('./model.js').Element} Element
|
|
28
|
+
*
|
|
29
|
+
* @typedef {object} TreeOptions
|
|
30
|
+
* @property {Document} document
|
|
31
|
+
* @property {Node} container the parent of the root elements
|
|
32
|
+
* @property {Record<string, any>} options the render options
|
|
33
|
+
* @property {boolean} interactive bind `(on ...)` handlers
|
|
34
|
+
* @property {(d: object) => void} report
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/** Mirror the properties a kind lists in `attributes` (for CSS). */
|
|
38
|
+
function mirrorAttributes(node, spec, el, changed) {
|
|
39
|
+
for (const [name, type] of Object.entries({
|
|
40
|
+
material: 'string',
|
|
41
|
+
'text-style': 'string',
|
|
42
|
+
appearance: 'string',
|
|
43
|
+
...spec.attributes,
|
|
44
|
+
})) {
|
|
45
|
+
if (!changed.includes(name)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const value = atom(el, name);
|
|
49
|
+
if (type === 'boolean') {
|
|
50
|
+
node.toggleAttribute(name, value === 'true');
|
|
51
|
+
} else if (value === undefined) {
|
|
52
|
+
node.removeAttribute(name);
|
|
53
|
+
} else {
|
|
54
|
+
node.setAttribute(name, value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Run a kind's update; a failing renderer is reported, not thrown. */
|
|
60
|
+
function runUpdate(node, el, spec, changed, ctx) {
|
|
61
|
+
try {
|
|
62
|
+
spec.update?.(node, el, changed, ctx);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
ctx.diagnostic('kind-error', 'error', { error: String(error?.message) });
|
|
65
|
+
}
|
|
66
|
+
mirrorAttributes(node, spec, el, changed);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Create the node of an element (unknown kind or kind error → `ui-unknown`). */
|
|
70
|
+
function createNode(win, el, kinds, ctx) {
|
|
71
|
+
let spec = getKind(el.kind, kinds);
|
|
72
|
+
if (spec === undefined) {
|
|
73
|
+
ctx.diagnostic('unknown-kind', 'warning');
|
|
74
|
+
spec = UNKNOWN_KIND;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
ensureTag(win, spec.tag);
|
|
78
|
+
return { spec, node: spec.create(el, ctx) };
|
|
79
|
+
} catch (error) {
|
|
80
|
+
ctx.diagnostic('kind-error', 'error', { error: String(error?.message) });
|
|
81
|
+
ensureTag(win, UNKNOWN_KIND.tag);
|
|
82
|
+
return { spec: UNKNOWN_KIND, node: UNKNOWN_KIND.create(el, ctx) };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Where the children of a rendered node go; `null` when it takes none. */
|
|
87
|
+
function slotOf(node, spec) {
|
|
88
|
+
if (spec.childrenSlot === null) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return spec.childrenSlot?.(node) ?? node;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Put a node at a store index among the rendered nodes (`isRendered`) of
|
|
96
|
+
* the slot; with no slot the node is detached.
|
|
97
|
+
*/
|
|
98
|
+
function place(node, slot, index, isRendered) {
|
|
99
|
+
if (slot === null) {
|
|
100
|
+
node.remove();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const rendered = [...slot.children].filter(
|
|
104
|
+
(child) => child !== node && isRendered(child)
|
|
105
|
+
);
|
|
106
|
+
slot.insertBefore(node, rendered[index] ?? null);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The store ID of a node or of its closest rendered ancestor. */
|
|
110
|
+
function closestId(ids, node) {
|
|
111
|
+
for (let n = node; n !== null && n !== undefined; n = n.parentNode) {
|
|
112
|
+
const id = ids.get(n);
|
|
113
|
+
if (id !== undefined) {
|
|
114
|
+
return id;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Render every element of the store, roots first, in document order. */
|
|
121
|
+
function mountStore(store, insert) {
|
|
122
|
+
const visit = (id, parentId, index) => {
|
|
123
|
+
insert(store.get(id), parentId, index);
|
|
124
|
+
store.children(id).forEach((child, i) => visit(child, id, i));
|
|
125
|
+
};
|
|
126
|
+
store.roots().forEach((id, i) => visit(id, null, i));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The context a kind gets: the document, the render options, a diagnostic
|
|
131
|
+
* reporter, and the child elements in the mounted store.
|
|
132
|
+
*/
|
|
133
|
+
const kindContext = (document, options, report, mounted) => (id, kind) => ({
|
|
134
|
+
document,
|
|
135
|
+
options,
|
|
136
|
+
diagnostic(code, severity, at = {}) {
|
|
137
|
+
report(diagnostic(code, severity, { element: id, kind, ...at }));
|
|
138
|
+
},
|
|
139
|
+
theme: () =>
|
|
140
|
+
mounted.store === null ? null : themeFor(mounted.store, id, options),
|
|
141
|
+
children: () => {
|
|
142
|
+
const { store } = mounted;
|
|
143
|
+
return store === null
|
|
144
|
+
? []
|
|
145
|
+
: store.children(id).map((child) => elementOf(store.get(child)));
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/** Positional events carry values, while explicit properties stay intact. */
|
|
150
|
+
function updatePositional(state, refresh, id, positional) {
|
|
151
|
+
const entry = state.get(id);
|
|
152
|
+
if (entry === undefined) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
entry.el = elementOf({ ...entry.el, positional });
|
|
156
|
+
refresh(id, vocabulary().kinds.get(entry.el.kind)?.positional ?? []);
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @param {TreeOptions} tree
|
|
162
|
+
*/
|
|
163
|
+
export function createTree({
|
|
164
|
+
document,
|
|
165
|
+
container,
|
|
166
|
+
options,
|
|
167
|
+
interactive,
|
|
168
|
+
report,
|
|
169
|
+
}) {
|
|
170
|
+
const win = document.defaultView;
|
|
171
|
+
/** @type {Map<string, HTMLElement>} */
|
|
172
|
+
const nodes = new Map();
|
|
173
|
+
/** @type {WeakMap<Node, string>} */
|
|
174
|
+
const ids = new WeakMap();
|
|
175
|
+
/** @type {Map<string, {el: Element, spec: object, unbind?: () => void}>} */
|
|
176
|
+
const state = new Map();
|
|
177
|
+
/** The store of the last `mount` (for `ctx.children`). */
|
|
178
|
+
const mounted = { store: null };
|
|
179
|
+
const contextOf = kindContext(document, options, report, mounted);
|
|
180
|
+
|
|
181
|
+
const idFor = (node) => closestId(ids, node);
|
|
182
|
+
|
|
183
|
+
function bind(id) {
|
|
184
|
+
const entry = state.get(id);
|
|
185
|
+
entry.unbind?.();
|
|
186
|
+
entry.unbind = undefined;
|
|
187
|
+
const ctx = contextOf(id, entry.el.kind);
|
|
188
|
+
const handlers = handlersOf(entry.el, ctx.diagnostic, options.isAction);
|
|
189
|
+
if (!interactive || handlers.length === 0) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const node = nodes.get(id);
|
|
193
|
+
const target = entry.spec.eventTarget?.(node) ?? node;
|
|
194
|
+
entry.unbind = bindHandlers(node, target, handlers, {
|
|
195
|
+
idFor,
|
|
196
|
+
onAction: options.onAction,
|
|
197
|
+
diagnostic: ctx.diagnostic,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const refresh = (id, changed) => {
|
|
202
|
+
const entry = state.get(id);
|
|
203
|
+
runUpdate(
|
|
204
|
+
nodes.get(id),
|
|
205
|
+
entry.el,
|
|
206
|
+
entry.spec,
|
|
207
|
+
changed,
|
|
208
|
+
contextOf(id, entry.el.kind)
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const slotFor = (parentId) => {
|
|
213
|
+
if (parentId === null) {
|
|
214
|
+
return container;
|
|
215
|
+
}
|
|
216
|
+
const node = nodes.get(parentId);
|
|
217
|
+
return node === undefined ? null : slotOf(node, state.get(parentId).spec);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const put = (node, parentId, index) =>
|
|
221
|
+
place(node, slotFor(parentId), index, (child) => ids.has(child));
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Render one element (not its children) at `index` of `parentId`.
|
|
225
|
+
* @param {object} view a store element view
|
|
226
|
+
*/
|
|
227
|
+
function insert(view, parentId, index) {
|
|
228
|
+
const el = elementOf(view);
|
|
229
|
+
const { spec, node } = createNode(
|
|
230
|
+
win,
|
|
231
|
+
el,
|
|
232
|
+
options.kinds,
|
|
233
|
+
contextOf(el.id, el.kind)
|
|
234
|
+
);
|
|
235
|
+
node.setAttribute('data-link-id', el.id);
|
|
236
|
+
nodes.set(el.id, node);
|
|
237
|
+
ids.set(node, el.id);
|
|
238
|
+
state.set(el.id, { el, spec });
|
|
239
|
+
refresh(el.id, propNames(el));
|
|
240
|
+
bind(el.id);
|
|
241
|
+
put(node, parentId, index);
|
|
242
|
+
return node;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Apply new properties; `changed` are the names that changed. */
|
|
246
|
+
function update(id, properties, changed) {
|
|
247
|
+
const entry = state.get(id);
|
|
248
|
+
if (entry === undefined) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
entry.el = withProperties(entry.el, properties);
|
|
252
|
+
refresh(id, changed);
|
|
253
|
+
if (changed.includes('on')) {
|
|
254
|
+
bind(id);
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function move(id, parentId, index) {
|
|
260
|
+
const node = nodes.get(id);
|
|
261
|
+
if (node === undefined) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
put(node, parentId, index);
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Destroy one element's node (its children are removed before it). */
|
|
269
|
+
function remove(id) {
|
|
270
|
+
const node = nodes.get(id);
|
|
271
|
+
if (node === undefined) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
const entry = state.get(id);
|
|
275
|
+
entry.unbind?.();
|
|
276
|
+
const ctx = contextOf(id, entry.el.kind);
|
|
277
|
+
try {
|
|
278
|
+
entry.spec.destroy?.(node, ctx);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
ctx.diagnostic('kind-error', 'error', { error: String(error?.message) });
|
|
281
|
+
}
|
|
282
|
+
node.remove();
|
|
283
|
+
nodes.delete(id);
|
|
284
|
+
ids.delete(node);
|
|
285
|
+
state.delete(id);
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function rename(from, to) {
|
|
290
|
+
const node = nodes.get(from);
|
|
291
|
+
if (node === undefined || nodes.has(to)) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
const entry = state.get(from);
|
|
295
|
+
entry.el = Object.freeze({
|
|
296
|
+
...entry.el,
|
|
297
|
+
id: to,
|
|
298
|
+
// `rename(id, null)` makes the element anonymous under a generated ID.
|
|
299
|
+
name: to.startsWith(GENERATED_ID_PREFIX) ? null : to,
|
|
300
|
+
});
|
|
301
|
+
nodes.delete(from);
|
|
302
|
+
state.delete(from);
|
|
303
|
+
nodes.set(to, node);
|
|
304
|
+
state.set(to, entry);
|
|
305
|
+
ids.set(node, to);
|
|
306
|
+
node.setAttribute('data-link-id', to);
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Destroy every node, children before parents. */
|
|
311
|
+
function clear() {
|
|
312
|
+
for (const id of [...nodes.keys()].reverse()) {
|
|
313
|
+
remove(id);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
nodes,
|
|
319
|
+
insert,
|
|
320
|
+
update,
|
|
321
|
+
updatePositional: (id, values) =>
|
|
322
|
+
updatePositional(state, refresh, id, values),
|
|
323
|
+
move,
|
|
324
|
+
remove,
|
|
325
|
+
rename,
|
|
326
|
+
mount(store) {
|
|
327
|
+
mounted.store = store;
|
|
328
|
+
mountStore(store, insert);
|
|
329
|
+
},
|
|
330
|
+
clear,
|
|
331
|
+
nodeFor: (id) => nodes.get(id) ?? null,
|
|
332
|
+
idFor,
|
|
333
|
+
elementFor: (id) => state.get(id)?.el ?? null,
|
|
334
|
+
};
|
|
335
|
+
}
|