@link-foundation/ui-designer 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 +180 -2
- package/package.json +41 -4
- package/src/actions.js +195 -0
- package/src/canvas-view.js +171 -0
- package/src/canvas.js +317 -0
- package/src/catalog.js +81 -0
- package/src/commands.js +229 -0
- package/src/designer.css +316 -0
- package/src/designer.js +266 -0
- package/src/designer.lino +104 -0
- package/src/dom.js +22 -0
- package/src/drop.js +82 -0
- package/src/editing.js +124 -0
- package/src/geometry.js +54 -0
- package/src/index.js +28 -0
- package/src/layout-text.js +3 -0
- package/src/panels.js +243 -0
- package/src/selection.js +68 -0
- package/src/snap.js +43 -0
- package/src/source-panel.js +111 -0
- package/src/sync.js +284 -0
- package/src/timeline/commands.js +208 -0
- package/src/timeline/layout-text.js +3 -0
- package/src/timeline/model.js +128 -0
- package/src/timeline/preview.js +105 -0
- package/src/timeline/properties.js +86 -0
- package/src/timeline/scale.js +16 -0
- package/src/timeline/timeline.js +757 -0
- package/src/timeline/timeline.lino +23 -0
package/src/canvas.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/** Pointer capture commits a whole drag, resize or marquee as one transaction. */
|
|
2
|
+
import { isLocked } from '@link-foundation/ui-devmode/model';
|
|
3
|
+
import { insertElement, moveElements, selectedRoots } from './commands.js';
|
|
4
|
+
import { nudgeElements, resizeElement } from './geometry.js';
|
|
5
|
+
import { marquee, normalizedRect } from './selection.js';
|
|
6
|
+
import { computeSnap } from './snap.js';
|
|
7
|
+
import { listen } from './dom.js';
|
|
8
|
+
import { dropTarget, ancestorOf } from './drop.js';
|
|
9
|
+
import { createCanvasView } from './canvas-view.js';
|
|
10
|
+
export { dropTarget } from './drop.js';
|
|
11
|
+
|
|
12
|
+
function isBlankDocument(context, target, inCanvas, id) {
|
|
13
|
+
return (
|
|
14
|
+
inCanvas &&
|
|
15
|
+
context.store.get(id)?.kind === 'document' &&
|
|
16
|
+
target === context.handle.nodeFor(id)
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pointerTarget(context, target) {
|
|
21
|
+
const toolbox = target.closest('[data-component]');
|
|
22
|
+
const resize = target.closest('[data-resize]');
|
|
23
|
+
const row = target.closest('[role=treeitem]');
|
|
24
|
+
if (target.closest('[data-expand]')) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const inCanvas = context.canvas.contains(target);
|
|
28
|
+
if (!toolbox && !resize && !row && !inCanvas) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const id =
|
|
32
|
+
resize?.dataset.resize ??
|
|
33
|
+
row?.dataset.id ??
|
|
34
|
+
(inCanvas ? context.handle.idFor(target) : null);
|
|
35
|
+
const blank = !id || isBlankDocument(context, target, inCanvas, id);
|
|
36
|
+
const type = toolbox
|
|
37
|
+
? 'insert'
|
|
38
|
+
: resize
|
|
39
|
+
? 'resize'
|
|
40
|
+
: blank
|
|
41
|
+
? 'marquee'
|
|
42
|
+
: 'move';
|
|
43
|
+
return { toolbox, row, inCanvas, id, type };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function start(context, state, event) {
|
|
47
|
+
if (context.mode() !== 'design' || event.button !== 0) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const hit = pointerTarget(context, event.target);
|
|
51
|
+
if (!hit) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const { store, policy, selection, handle, choose, root, canvas } = context;
|
|
55
|
+
const { id, type, toolbox, row, inCanvas } = hit;
|
|
56
|
+
if (id && isLocked(policy, id, store.parentOf)) {
|
|
57
|
+
context.report(`${id} is locked`);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const g = {
|
|
61
|
+
pointerId: event.pointerId,
|
|
62
|
+
startX: event.clientX,
|
|
63
|
+
startY: event.clientY,
|
|
64
|
+
type,
|
|
65
|
+
component: toolbox?.dataset.component,
|
|
66
|
+
id,
|
|
67
|
+
event,
|
|
68
|
+
active: false,
|
|
69
|
+
drop: null,
|
|
70
|
+
rect: id ? handle.nodeFor(id)?.getBoundingClientRect() : null,
|
|
71
|
+
};
|
|
72
|
+
if (type === 'move' && !selection.ids.has(id)) {
|
|
73
|
+
choose(id, event);
|
|
74
|
+
g.selectedOnStart = true;
|
|
75
|
+
}
|
|
76
|
+
g.ids = selectedRoots(store, selection.ids);
|
|
77
|
+
state.gesture = g;
|
|
78
|
+
if (row) {
|
|
79
|
+
[...context.outline.tree.querySelectorAll('[role=treeitem]')]
|
|
80
|
+
.find((n) => n.dataset.id === id)
|
|
81
|
+
?.focus();
|
|
82
|
+
} else if (inCanvas) {
|
|
83
|
+
canvas.focus();
|
|
84
|
+
}
|
|
85
|
+
root.setPointerCapture(event.pointerId);
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function previewDrop(context, view, g, event, dx, dy) {
|
|
90
|
+
const { store, handle, document } = context;
|
|
91
|
+
const target = document.elementFromPoint(event.clientX, event.clientY);
|
|
92
|
+
// The toolbox and Outline share a dock. Activate a tab under an active
|
|
93
|
+
// drag so the pointer can reach a destination behind the other tab.
|
|
94
|
+
const tab = target?.closest('[role=tab]');
|
|
95
|
+
if (tab && tab.getAttribute('aria-selected') !== 'true') {
|
|
96
|
+
tab.click();
|
|
97
|
+
}
|
|
98
|
+
const types =
|
|
99
|
+
g.type === 'insert' ? [g.component] : g.ids.map((id) => store.get(id).kind);
|
|
100
|
+
const drop = dropTarget(context, target, event.clientX, event.clientY, types);
|
|
101
|
+
if (
|
|
102
|
+
drop &&
|
|
103
|
+
g.type === 'move' &&
|
|
104
|
+
g.ids.some(
|
|
105
|
+
(id) => id === drop.parentId || ancestorOf(store, id, drop.parentId)
|
|
106
|
+
)
|
|
107
|
+
) {
|
|
108
|
+
drop.valid = false;
|
|
109
|
+
}
|
|
110
|
+
g.drop = drop;
|
|
111
|
+
view.showDrop(drop);
|
|
112
|
+
if (g.type !== 'move' || !g.rect) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const parentId = store.parentOf(g.id);
|
|
116
|
+
const siblings = parentId === null ? store.roots() : store.children(parentId);
|
|
117
|
+
const others = siblings
|
|
118
|
+
.filter((id) => !g.ids.includes(id))
|
|
119
|
+
.map((id) => handle.nodeFor(id)?.getBoundingClientRect())
|
|
120
|
+
.filter(Boolean);
|
|
121
|
+
const moving = {
|
|
122
|
+
x: g.rect.x + dx,
|
|
123
|
+
y: g.rect.y + dy,
|
|
124
|
+
width: g.rect.width,
|
|
125
|
+
height: g.rect.height,
|
|
126
|
+
};
|
|
127
|
+
const snap = computeSnap(moving, others, {
|
|
128
|
+
threshold: context.snapThreshold,
|
|
129
|
+
});
|
|
130
|
+
g.delta = { dx: Math.round(dx + snap.dx), dy: Math.round(dy + snap.dy) };
|
|
131
|
+
view.showGuides(snap.guides);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function move(context, state, view, event) {
|
|
135
|
+
const g = state.gesture;
|
|
136
|
+
if (!g) {
|
|
137
|
+
view.hover(
|
|
138
|
+
context.mode() === 'design' && context.canvas.contains(event.target)
|
|
139
|
+
? context.handle.idFor(event.target)
|
|
140
|
+
: null
|
|
141
|
+
);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (g.pointerId !== event.pointerId) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const dx = event.clientX - g.startX;
|
|
148
|
+
const dy = event.clientY - g.startY;
|
|
149
|
+
if (!g.active && Math.hypot(dx, dy) < 4) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
g.active = true;
|
|
153
|
+
view.clear();
|
|
154
|
+
if (g.type === 'marquee') {
|
|
155
|
+
g.box = normalizedRect({ x: g.startX, y: g.startY, width: dx, height: dy });
|
|
156
|
+
view.drawBox(g.box, 'designer-marquee');
|
|
157
|
+
} else if (g.type === 'resize') {
|
|
158
|
+
g.size = {
|
|
159
|
+
width: Math.max(1, g.rect.width + dx),
|
|
160
|
+
height: Math.max(1, g.rect.height + dy),
|
|
161
|
+
};
|
|
162
|
+
view.drawBox({ x: g.rect.x, y: g.rect.y, ...g.size }, 'designer-marquee');
|
|
163
|
+
} else {
|
|
164
|
+
previewDrop(context, view, g, event, dx, dy);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function selectMarquee(context, g) {
|
|
169
|
+
const { canvas, handle, store, selection } = context;
|
|
170
|
+
const rects = [...canvas.querySelectorAll('[data-link-id]')].map((node) => {
|
|
171
|
+
const { x, y, width, height } = node.getBoundingClientRect();
|
|
172
|
+
return { id: handle.idFor(node), x, y, width, height };
|
|
173
|
+
});
|
|
174
|
+
const picked = marquee(rects, g.box).filter(
|
|
175
|
+
(id) => store.get(id).kind !== 'document'
|
|
176
|
+
);
|
|
177
|
+
const leaves = picked.filter(
|
|
178
|
+
(id) =>
|
|
179
|
+
!picked.some((other) => other !== id && ancestorOf(store, id, other))
|
|
180
|
+
);
|
|
181
|
+
selection.replace(
|
|
182
|
+
g.event.shiftKey || g.event.ctrlKey || g.event.metaKey
|
|
183
|
+
? [...selection.ids, ...leaves]
|
|
184
|
+
: leaves
|
|
185
|
+
);
|
|
186
|
+
context.refresh();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function commitGesture(context, g) {
|
|
190
|
+
const { store, run, selection, catalog, refresh } = context;
|
|
191
|
+
if (g.type === 'marquee') {
|
|
192
|
+
selectMarquee(context, g);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (g.type === 'resize') {
|
|
196
|
+
run((s) => resizeElement(s, g.id, g.size.width, g.size.height));
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!g.drop?.valid) {
|
|
200
|
+
context.report('Drop refused: this component cannot contain the selection');
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (g.type === 'insert') {
|
|
204
|
+
const ids = run((s) =>
|
|
205
|
+
insertElement(s, { ...g.drop, type: g.component, catalog })
|
|
206
|
+
);
|
|
207
|
+
if (ids) {
|
|
208
|
+
selection.replace(ids);
|
|
209
|
+
refresh();
|
|
210
|
+
}
|
|
211
|
+
} else if (
|
|
212
|
+
g.drop.free &&
|
|
213
|
+
g.ids.every((id) => store.parentOf(id) === g.drop.parentId)
|
|
214
|
+
) {
|
|
215
|
+
run((s) => nudgeElements(s, g.ids, g.delta.dx, g.delta.dy));
|
|
216
|
+
} else {
|
|
217
|
+
run((s) => moveElements(s, { ...g.drop, ids: g.ids, catalog }));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function end(context, state, view, event, cancel = false) {
|
|
222
|
+
const g = state.gesture;
|
|
223
|
+
if (!g || g.pointerId !== event.pointerId) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
state.gesture = null;
|
|
227
|
+
if (context.root.hasPointerCapture(event.pointerId)) {
|
|
228
|
+
context.root.releasePointerCapture(event.pointerId);
|
|
229
|
+
}
|
|
230
|
+
view.clearPreview();
|
|
231
|
+
if (cancel) {
|
|
232
|
+
view.update();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
state.ignoreClick = true;
|
|
236
|
+
if (g.active) {
|
|
237
|
+
commitGesture(context, g);
|
|
238
|
+
view.update();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (g.type === 'move' && !g.selectedOnStart) {
|
|
242
|
+
context.choose(g.id, g.event);
|
|
243
|
+
}
|
|
244
|
+
if (g.type === 'insert') {
|
|
245
|
+
context.insert(g.component);
|
|
246
|
+
}
|
|
247
|
+
if (g.type === 'marquee') {
|
|
248
|
+
context.choose(null);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function mountCanvas(context) {
|
|
253
|
+
const { root, window, canvas, controller } = context;
|
|
254
|
+
const view = createCanvasView(context);
|
|
255
|
+
const state = { gesture: null, ignoreClick: false };
|
|
256
|
+
const cancel = (event) => end(context, state, view, event, true);
|
|
257
|
+
listen(
|
|
258
|
+
root,
|
|
259
|
+
'pointerdown',
|
|
260
|
+
(event) => start(context, state, event),
|
|
261
|
+
controller,
|
|
262
|
+
true
|
|
263
|
+
);
|
|
264
|
+
listen(
|
|
265
|
+
root,
|
|
266
|
+
'pointermove',
|
|
267
|
+
(event) => move(context, state, view, event),
|
|
268
|
+
controller,
|
|
269
|
+
true
|
|
270
|
+
);
|
|
271
|
+
listen(
|
|
272
|
+
root,
|
|
273
|
+
'pointerup',
|
|
274
|
+
(event) => end(context, state, view, event),
|
|
275
|
+
controller,
|
|
276
|
+
true
|
|
277
|
+
);
|
|
278
|
+
listen(root, 'pointercancel', cancel, controller, true);
|
|
279
|
+
listen(root, 'lostpointercapture', cancel, controller, true);
|
|
280
|
+
listen(
|
|
281
|
+
root,
|
|
282
|
+
'click',
|
|
283
|
+
(event) => {
|
|
284
|
+
if (
|
|
285
|
+
context.mode() === 'design' &&
|
|
286
|
+
(canvas.contains(event.target) || state.ignoreClick)
|
|
287
|
+
) {
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
event.stopImmediatePropagation();
|
|
290
|
+
state.ignoreClick = false;
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
controller,
|
|
294
|
+
true
|
|
295
|
+
);
|
|
296
|
+
listen(window, 'scroll', view.update, controller, true);
|
|
297
|
+
listen(window, 'resize', view.update, controller);
|
|
298
|
+
listen(
|
|
299
|
+
root,
|
|
300
|
+
'keydown',
|
|
301
|
+
(event) => {
|
|
302
|
+
if (event.key === 'Escape' && state.gesture) {
|
|
303
|
+
cancel({ pointerId: state.gesture.pointerId });
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
controller
|
|
307
|
+
);
|
|
308
|
+
return {
|
|
309
|
+
update: view.update,
|
|
310
|
+
destroy() {
|
|
311
|
+
if (state.gesture) {
|
|
312
|
+
cancel({ pointerId: state.gesture.pointerId });
|
|
313
|
+
}
|
|
314
|
+
view.destroy();
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
}
|
package/src/catalog.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** Component metadata shared by the toolbox and every drop validation. */
|
|
2
|
+
import { defaultVocabulary, loadVocabulary } from '@link-foundation/ui-core';
|
|
3
|
+
import { controlKindNames } from '@link-foundation/ui-components/controls';
|
|
4
|
+
import { layoutVocabulary } from '@link-foundation/ui-components/layout';
|
|
5
|
+
|
|
6
|
+
/** The controls of #34 plus the document, basic visual and layout kinds. */
|
|
7
|
+
export function createCatalog(vocabulary = designerVocabulary()) {
|
|
8
|
+
const names = new Set([
|
|
9
|
+
'document',
|
|
10
|
+
'text',
|
|
11
|
+
'image',
|
|
12
|
+
...controlKindNames(),
|
|
13
|
+
'stack',
|
|
14
|
+
'grid',
|
|
15
|
+
'split',
|
|
16
|
+
'tab-group',
|
|
17
|
+
'panel',
|
|
18
|
+
'dock',
|
|
19
|
+
'region',
|
|
20
|
+
'window',
|
|
21
|
+
'app-shell',
|
|
22
|
+
]);
|
|
23
|
+
return new Map(
|
|
24
|
+
[...names].map((type) => {
|
|
25
|
+
const spec =
|
|
26
|
+
vocabulary.kinds.get(type) ?? defaultVocabulary().kinds.get(type);
|
|
27
|
+
return [
|
|
28
|
+
type,
|
|
29
|
+
{
|
|
30
|
+
...spec,
|
|
31
|
+
type,
|
|
32
|
+
name: type
|
|
33
|
+
.split('-')
|
|
34
|
+
.map((word) => word[0].toUpperCase() + word.slice(1))
|
|
35
|
+
.join(' '),
|
|
36
|
+
icon: spec?.children === 'none' ? '□' : '▤',
|
|
37
|
+
defaults:
|
|
38
|
+
type === 'text'
|
|
39
|
+
? { value: 'New text' }
|
|
40
|
+
: type === 'button'
|
|
41
|
+
? { label: 'Button' }
|
|
42
|
+
: {},
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Explicit designer geometry extension; see README and spec/open-questions.md. */
|
|
50
|
+
export function designerVocabulary() {
|
|
51
|
+
const base = layoutVocabulary();
|
|
52
|
+
const geometry = loadVocabulary(`geometry: kind
|
|
53
|
+
property x number
|
|
54
|
+
property y number
|
|
55
|
+
property width dimension
|
|
56
|
+
property height dimension
|
|
57
|
+
`).kinds.get('geometry').properties;
|
|
58
|
+
return {
|
|
59
|
+
...base,
|
|
60
|
+
actions: new Map([...base.actions, ['designer', []]]),
|
|
61
|
+
kinds: new Map(
|
|
62
|
+
[...base.kinds].map(([name, spec]) => [
|
|
63
|
+
name,
|
|
64
|
+
{ ...spec, properties: new Map([...geometry, ...spec.properties]) },
|
|
65
|
+
])
|
|
66
|
+
),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Unknown entries are refused. `children` may be any, none, or allowed types. */
|
|
71
|
+
export function canContain(catalog, parentType, childType) {
|
|
72
|
+
const entries = catalog ?? createCatalog();
|
|
73
|
+
const entry =
|
|
74
|
+
entries instanceof Map ? entries.get(parentType) : entries[parentType];
|
|
75
|
+
const rule = entry?.children ?? entry?.allowedChildren;
|
|
76
|
+
return (
|
|
77
|
+
rule === 'any' ||
|
|
78
|
+
rule === true ||
|
|
79
|
+
(Array.isArray(rule) && rule.includes(childType))
|
|
80
|
+
);
|
|
81
|
+
}
|
package/src/commands.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/** Visual editing commands. Nested commands join the caller's transaction. */
|
|
2
|
+
import { format, parseOrThrow, valueMatches } from '@link-foundation/ui-core';
|
|
3
|
+
import { canContain, createCatalog, designerVocabulary } from './catalog.js';
|
|
4
|
+
export { canContain };
|
|
5
|
+
|
|
6
|
+
export const childrenOf = (store, parentId) =>
|
|
7
|
+
parentId === null ? store.roots() : store.children(parentId);
|
|
8
|
+
export const rawValues = (value) =>
|
|
9
|
+
Array.isArray(value) ? value : [String(value)];
|
|
10
|
+
|
|
11
|
+
/** Selected roots in document order: a selected ancestor subsumes its children. */
|
|
12
|
+
export function selectedRoots(store, ids) {
|
|
13
|
+
const selected = new Set(ids);
|
|
14
|
+
const result = [];
|
|
15
|
+
const visit = (id) => {
|
|
16
|
+
if (selected.has(id)) {
|
|
17
|
+
result.push(id);
|
|
18
|
+
} else {
|
|
19
|
+
store.children(id).forEach(visit);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
store.roots().forEach(visit);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A full node suitable for store insertion and ui-core formatting. */
|
|
27
|
+
export function subtree(store, id) {
|
|
28
|
+
const view = store.get(id);
|
|
29
|
+
if (!view) {
|
|
30
|
+
throw new Error(`Unknown element ${id}`);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
id: view.name,
|
|
34
|
+
kind: view.kind,
|
|
35
|
+
positional: [...view.positional],
|
|
36
|
+
properties: globalThis.structuredClone(view.properties),
|
|
37
|
+
unknown: globalThis.structuredClone(view.unknown),
|
|
38
|
+
children: store.children(id).map((child) => subtree(store, child)),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function freshId(store, type, reserved = new Set()) {
|
|
43
|
+
let n = 1;
|
|
44
|
+
while (store.has(`${type}-${n}`) || reserved.has(`${type}-${n}`)) {
|
|
45
|
+
n++;
|
|
46
|
+
}
|
|
47
|
+
const id = `${type}-${n}`;
|
|
48
|
+
reserved.add(id);
|
|
49
|
+
return id;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function checkDrop(store, parentId, type, catalog) {
|
|
53
|
+
if (
|
|
54
|
+
parentId !== null &&
|
|
55
|
+
!canContain(catalog, store.get(parentId)?.kind, type)
|
|
56
|
+
) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`${store.get(parentId)?.kind ?? parentId} cannot contain ${type}`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Insert one catalog component. Returns the new ID in a one-element array. */
|
|
64
|
+
export function insertElement(
|
|
65
|
+
store,
|
|
66
|
+
{ parentId = null, index, type, props = {}, catalog = createCatalog() }
|
|
67
|
+
) {
|
|
68
|
+
const entry = catalog.get(type);
|
|
69
|
+
if (!entry) {
|
|
70
|
+
throw new Error(`Unknown component ${type}`);
|
|
71
|
+
}
|
|
72
|
+
checkDrop(store, parentId, type, catalog);
|
|
73
|
+
return store.transaction('Insert element', () => {
|
|
74
|
+
const id = store.insert(parentId, index, {
|
|
75
|
+
id: freshId(store, type),
|
|
76
|
+
kind: type,
|
|
77
|
+
});
|
|
78
|
+
for (const [name, value] of Object.entries({
|
|
79
|
+
...entry.defaults,
|
|
80
|
+
...props,
|
|
81
|
+
})) {
|
|
82
|
+
setProperty(store, id, name, value, catalog);
|
|
83
|
+
}
|
|
84
|
+
return [id];
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Index is the drop slot before removing selected elements from that list. */
|
|
89
|
+
export function moveElements(
|
|
90
|
+
store,
|
|
91
|
+
{ ids, parentId = null, index, catalog = createCatalog() }
|
|
92
|
+
) {
|
|
93
|
+
const roots = selectedRoots(store, ids);
|
|
94
|
+
for (const id of roots) {
|
|
95
|
+
checkDrop(store, parentId, store.get(id).kind, catalog);
|
|
96
|
+
for (let at = parentId; at !== null; at = store.parentOf(at)) {
|
|
97
|
+
if (at === id) {
|
|
98
|
+
throw new Error('Cannot move an element into itself or a descendant');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const siblings = childrenOf(store, parentId);
|
|
103
|
+
const slot = index ?? siblings.length;
|
|
104
|
+
const adjusted =
|
|
105
|
+
slot -
|
|
106
|
+
roots.filter(
|
|
107
|
+
(id) => store.parentOf(id) === parentId && store.indexOf(id) < slot
|
|
108
|
+
).length;
|
|
109
|
+
return store.transaction('Move elements', () => {
|
|
110
|
+
// Park at the end first so a multi-move works in either direction.
|
|
111
|
+
for (const id of roots) {
|
|
112
|
+
store.move(id, parentId);
|
|
113
|
+
}
|
|
114
|
+
roots.forEach((id, offset) => store.move(id, parentId, adjusted + offset));
|
|
115
|
+
return roots;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function removeElements(store, ids) {
|
|
120
|
+
return store.transaction('Delete elements', () => {
|
|
121
|
+
const roots = selectedRoots(store, ids);
|
|
122
|
+
roots.forEach((id) => store.remove(id));
|
|
123
|
+
return roots;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** IDs within a copied fragment, including reference-typed property values, remap together. */
|
|
128
|
+
function cloneNodes(store, nodes, catalog) {
|
|
129
|
+
const names = new Map();
|
|
130
|
+
const reserved = new Set();
|
|
131
|
+
const visit = (node) => {
|
|
132
|
+
const old = node.id;
|
|
133
|
+
node.id = freshId(store, node.kind, reserved);
|
|
134
|
+
if (old) {
|
|
135
|
+
names.set(old, node.id);
|
|
136
|
+
}
|
|
137
|
+
node.children.forEach(visit);
|
|
138
|
+
};
|
|
139
|
+
nodes.forEach(visit);
|
|
140
|
+
const refs = new Set(['element-ref', 'element-ref-or-url']);
|
|
141
|
+
const rewrite = (node) => {
|
|
142
|
+
for (const prop of node.properties) {
|
|
143
|
+
const type = catalog.get(node.kind)?.properties?.get(prop.name)?.type;
|
|
144
|
+
if (refs.has(type)) {
|
|
145
|
+
prop.values = prop.values.map((v) => names.get(v) ?? v);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
node.children.forEach(rewrite);
|
|
149
|
+
};
|
|
150
|
+
nodes.forEach(rewrite);
|
|
151
|
+
return nodes;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function duplicateElements(store, ids, catalog = createCatalog()) {
|
|
155
|
+
const roots = selectedRoots(store, ids);
|
|
156
|
+
const nodes = cloneNodes(
|
|
157
|
+
store,
|
|
158
|
+
roots.map((id) => subtree(store, id)),
|
|
159
|
+
catalog
|
|
160
|
+
);
|
|
161
|
+
return store.transaction('Duplicate elements', () =>
|
|
162
|
+
roots.map((id, i) =>
|
|
163
|
+
store.insert(store.parentOf(id), store.indexOf(id) + 1, nodes[i])
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function setProperty(store, id, name, value, catalog = createCatalog()) {
|
|
169
|
+
const entry = catalog.get(store.get(id)?.kind);
|
|
170
|
+
const values = rawValues(value);
|
|
171
|
+
const view = store.get(id);
|
|
172
|
+
const positional =
|
|
173
|
+
name === view?.kind ||
|
|
174
|
+
(entry?.positional?.includes(name) &&
|
|
175
|
+
!view.properties.some((prop) => prop.name === name));
|
|
176
|
+
const type = entry?.properties?.get(
|
|
177
|
+
positional ? entry.positional[0] : name
|
|
178
|
+
)?.type;
|
|
179
|
+
if (type && values.some((v) => !valueMatches(type, v))) {
|
|
180
|
+
throw new Error(`Invalid ${name}`);
|
|
181
|
+
}
|
|
182
|
+
return store.transaction('Set property', () => {
|
|
183
|
+
if (positional) {
|
|
184
|
+
store.setPositional(id, values);
|
|
185
|
+
} else {
|
|
186
|
+
store.setProp(id, name, values);
|
|
187
|
+
}
|
|
188
|
+
return [id];
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function copyElements(store, ids) {
|
|
193
|
+
return format({
|
|
194
|
+
roots: selectedRoots(store, ids).map((id) => subtree(store, id)),
|
|
195
|
+
unknown: [],
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function pasteElements(
|
|
200
|
+
store,
|
|
201
|
+
{ parentId = null, index, text, catalog = createCatalog() }
|
|
202
|
+
) {
|
|
203
|
+
const document = parseOrThrow(text, { vocabulary: designerVocabulary() });
|
|
204
|
+
if (!document.roots.length) {
|
|
205
|
+
throw new Error('Clipboard has no elements');
|
|
206
|
+
}
|
|
207
|
+
const validate = (node, parentType) => {
|
|
208
|
+
if (!catalog.has(node.kind)) {
|
|
209
|
+
throw new Error(`Unknown component ${node.kind}`);
|
|
210
|
+
}
|
|
211
|
+
if (parentType !== null && !canContain(catalog, parentType, node.kind)) {
|
|
212
|
+
throw new Error(`${parentType} cannot contain ${node.kind}`);
|
|
213
|
+
}
|
|
214
|
+
node.children.forEach((child) => validate(child, node.kind));
|
|
215
|
+
};
|
|
216
|
+
document.roots.forEach((node) =>
|
|
217
|
+
validate(node, store.get(parentId)?.kind ?? null)
|
|
218
|
+
);
|
|
219
|
+
const nodes = cloneNodes(store, document.roots, catalog);
|
|
220
|
+
return store.transaction('Paste elements', () =>
|
|
221
|
+
nodes.map((node, offset) =>
|
|
222
|
+
store.insert(
|
|
223
|
+
parentId,
|
|
224
|
+
(index ?? childrenOf(store, parentId).length - offset) + offset,
|
|
225
|
+
node
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
);
|
|
229
|
+
}
|