@link-foundation/ui-designer 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 +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/drop.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Shared canvas/Outline drop rules use catalog child constraints. */
|
|
2
|
+
import { isLocked } from '@link-foundation/ui-devmode/model';
|
|
3
|
+
import { canContain } from './catalog.js';
|
|
4
|
+
import { property } from './geometry.js';
|
|
5
|
+
|
|
6
|
+
function targetNode(context, target) {
|
|
7
|
+
const row = target?.closest?.('[role=treeitem]');
|
|
8
|
+
if (row && context.outline.tree.contains(row)) {
|
|
9
|
+
return {
|
|
10
|
+
id: row.dataset.id,
|
|
11
|
+
node: row.querySelector('.outline-row') ?? row,
|
|
12
|
+
row: true,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (!context.canvas.contains(target)) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const id = context.handle.idFor(target);
|
|
19
|
+
return id ? { id, node: context.handle.nodeFor(id), row: false } : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isHorizontal(store, parent, row) {
|
|
23
|
+
return (
|
|
24
|
+
!row &&
|
|
25
|
+
['horizontal', 'row', 'row-reverse', 'grid'].includes(
|
|
26
|
+
property(store, parent, 'direction') ?? store.get(parent)?.kind
|
|
27
|
+
)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Decide the stack/grid insert slot or container; centre drops on leaves refuse. */
|
|
32
|
+
export function dropTarget(context, target, x, y, types) {
|
|
33
|
+
const hit = targetNode(context, target);
|
|
34
|
+
if (!hit) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const { store, catalog, policy } = context;
|
|
38
|
+
const { id, node, row } = hit;
|
|
39
|
+
const rect = node.getBoundingClientRect();
|
|
40
|
+
const parent = store.parentOf(id);
|
|
41
|
+
const horizontal = isHorizontal(store, parent, row);
|
|
42
|
+
const reverse =
|
|
43
|
+
!row &&
|
|
44
|
+
['row-reverse', 'column-reverse'].includes(
|
|
45
|
+
property(store, parent, 'direction')
|
|
46
|
+
);
|
|
47
|
+
const fraction = horizontal
|
|
48
|
+
? (x - rect.left) / rect.width
|
|
49
|
+
: (y - rect.top) / rect.height;
|
|
50
|
+
const edge = fraction < 0.25 ? 'before' : fraction > 0.75 ? 'after' : null;
|
|
51
|
+
const accepts = (candidate) =>
|
|
52
|
+
types.every((type) =>
|
|
53
|
+
canContain(catalog, store.get(candidate)?.kind, type)
|
|
54
|
+
);
|
|
55
|
+
const around = edge && parent !== null && accepts(parent);
|
|
56
|
+
const parentId = around ? parent : id;
|
|
57
|
+
const valid = !!(
|
|
58
|
+
(around || accepts(id)) &&
|
|
59
|
+
!isLocked(policy, parentId, store.parentOf)
|
|
60
|
+
);
|
|
61
|
+
return {
|
|
62
|
+
parentId,
|
|
63
|
+
index: around
|
|
64
|
+
? store.indexOf(id) + ((edge === 'after') !== reverse ? 1 : 0)
|
|
65
|
+
: store.children(id).length,
|
|
66
|
+
valid,
|
|
67
|
+
node,
|
|
68
|
+
rect,
|
|
69
|
+
horizontal,
|
|
70
|
+
edge: around ? edge : null,
|
|
71
|
+
free: !row && !around,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function ancestorOf(store, ancestor, id) {
|
|
76
|
+
for (let at = store.parentOf(id); at !== null; at = store.parentOf(at)) {
|
|
77
|
+
if (at === ancestor) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
package/src/editing.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** Shared editing paths for canvas, properties, Source and keyboard commands. */
|
|
2
|
+
import { isLocked } from '@link-foundation/ui-devmode/model';
|
|
3
|
+
import { insertElement } from './commands.js';
|
|
4
|
+
import { outlineOrder, updateProperties } from './panels.js';
|
|
5
|
+
|
|
6
|
+
export function validatePolicy(context, ops) {
|
|
7
|
+
const { store, policy } = context;
|
|
8
|
+
for (const op of ops) {
|
|
9
|
+
const affected = [op.id, op.parentId];
|
|
10
|
+
if (['remove', 'move'].includes(op.op) && store.has(op.id)) {
|
|
11
|
+
const descendants = (id) => {
|
|
12
|
+
affected.push(id);
|
|
13
|
+
store.children(id).forEach(descendants);
|
|
14
|
+
};
|
|
15
|
+
descendants(op.id);
|
|
16
|
+
}
|
|
17
|
+
for (const id of affected) {
|
|
18
|
+
if (id && store.has(id) && isLocked(policy, id, store.parentOf)) {
|
|
19
|
+
throw new Error(`${id} is locked`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const insertionParent = (context) =>
|
|
26
|
+
[...context.selection.ids].at(-1) ?? context.store.roots()[0] ?? null;
|
|
27
|
+
|
|
28
|
+
export function createEditing(context) {
|
|
29
|
+
const {
|
|
30
|
+
root,
|
|
31
|
+
store,
|
|
32
|
+
selection,
|
|
33
|
+
session,
|
|
34
|
+
policy,
|
|
35
|
+
source,
|
|
36
|
+
shellStore,
|
|
37
|
+
shell,
|
|
38
|
+
options,
|
|
39
|
+
report,
|
|
40
|
+
} = context;
|
|
41
|
+
function refresh() {
|
|
42
|
+
context.handle.flush();
|
|
43
|
+
selection.retain(outlineOrder(store));
|
|
44
|
+
context.outline.update();
|
|
45
|
+
if (!context.timeline?.hasSelection()) {
|
|
46
|
+
updateProperties(shell.nodeFor('properties'), context);
|
|
47
|
+
}
|
|
48
|
+
context.timeline?.update();
|
|
49
|
+
context.canvasController?.update();
|
|
50
|
+
shellStore.setProp('undo', 'disabled', [String(!store.canUndo())]);
|
|
51
|
+
shellStore.setProp('redo', 'disabled', [String(!store.canRedo())]);
|
|
52
|
+
if (!source.invalid()) {
|
|
53
|
+
source.setText(session.getText());
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function choose(id, event = {}) {
|
|
57
|
+
context.timeline?.clearSelection();
|
|
58
|
+
if (event.shiftKey && id) {
|
|
59
|
+
selection.range(id, outlineOrder(store));
|
|
60
|
+
} else if ((event.ctrlKey || event.metaKey) && id) {
|
|
61
|
+
selection.toggle(id);
|
|
62
|
+
} else {
|
|
63
|
+
selection.select(id);
|
|
64
|
+
}
|
|
65
|
+
refresh();
|
|
66
|
+
}
|
|
67
|
+
function run(action) {
|
|
68
|
+
if (!policy.writeLayers.length || context.mode() !== 'design') {
|
|
69
|
+
report('Editing is disabled');
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const result = session.run(action, 'Canvas edit', (ops) =>
|
|
74
|
+
validatePolicy(context, ops)
|
|
75
|
+
);
|
|
76
|
+
options.trace?.('canvas-commit', { tx: store.log().at(-1)?.tx });
|
|
77
|
+
report('Changes saved to draft');
|
|
78
|
+
return result;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
report(error.message);
|
|
81
|
+
options.trace?.('edit-refused', { message: error.message });
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function insert(type) {
|
|
86
|
+
const ids = run((s) =>
|
|
87
|
+
insertElement(s, {
|
|
88
|
+
parentId: insertionParent(context),
|
|
89
|
+
type,
|
|
90
|
+
catalog: context.catalog,
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
if (ids) {
|
|
94
|
+
selection.replace(ids);
|
|
95
|
+
refresh();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function setMode(mode) {
|
|
99
|
+
if (!['design', 'run'].includes(mode)) {
|
|
100
|
+
throw new Error('Unknown designer mode');
|
|
101
|
+
}
|
|
102
|
+
context.modeValue = mode;
|
|
103
|
+
root.dataset.mode = mode;
|
|
104
|
+
source.setReadOnly(mode === 'run' || !policy.writeLayers.length);
|
|
105
|
+
shellStore.setProp('mode', 'label', [mode === 'design' ? 'Run' : 'Design']);
|
|
106
|
+
context.canvasController.update();
|
|
107
|
+
report(`${mode === 'design' ? 'Design' : 'Run'} mode`);
|
|
108
|
+
}
|
|
109
|
+
function editSource(text) {
|
|
110
|
+
source.setText(text);
|
|
111
|
+
const result =
|
|
112
|
+
!policy.writeLayers.length || context.mode() !== 'design'
|
|
113
|
+
? { error: { line: 1, column: 1, message: 'Editing is disabled' } }
|
|
114
|
+
: session.editSource(text, (ops) => validatePolicy(context, ops));
|
|
115
|
+
source.setError(result.error);
|
|
116
|
+
if (result.error) {
|
|
117
|
+
options.trace?.('source-error', result.error);
|
|
118
|
+
} else {
|
|
119
|
+
source.setText(session.getText());
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
return { refresh, choose, run, insert, setMode, editSource };
|
|
124
|
+
}
|
package/src/geometry.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** Geometry is persisted as typed LiNo properties of the designer vocabulary. */
|
|
2
|
+
import { setProperty, selectedRoots } from './commands.js';
|
|
3
|
+
|
|
4
|
+
export const property = (store, id, name) =>
|
|
5
|
+
store.get(id)?.properties.findLast((p) => p.name === name)?.values[0];
|
|
6
|
+
export const coordinate = (store, id, axis) =>
|
|
7
|
+
Number(property(store, id, axis) ?? 0);
|
|
8
|
+
|
|
9
|
+
/** Apply geometry in both design and run modes; source stays the source of truth. */
|
|
10
|
+
export function renderGeometry(store, handle) {
|
|
11
|
+
const visit = (id) => {
|
|
12
|
+
const node = handle.nodeFor(id);
|
|
13
|
+
// The window renderer already applies its numeric geometry in CSS pixels.
|
|
14
|
+
if (node && store.get(id).kind !== 'window') {
|
|
15
|
+
const x = coordinate(store, id, 'x');
|
|
16
|
+
const y = coordinate(store, id, 'y');
|
|
17
|
+
node.style.transform = x || y ? `translate(${x}px, ${y}px)` : '';
|
|
18
|
+
for (const size of ['width', 'height']) {
|
|
19
|
+
node.style[size] = String(property(store, id, size) ?? '');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
store.children(id).forEach(visit);
|
|
23
|
+
};
|
|
24
|
+
store.roots().forEach(visit);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function nudgeElements(store, ids, dx, dy) {
|
|
28
|
+
return store.transaction('Nudge elements', () => {
|
|
29
|
+
const roots = selectedRoots(store, ids);
|
|
30
|
+
for (const id of roots) {
|
|
31
|
+
if (dx) {
|
|
32
|
+
setProperty(store, id, 'x', coordinate(store, id, 'x') + dx);
|
|
33
|
+
}
|
|
34
|
+
if (dy) {
|
|
35
|
+
setProperty(store, id, 'y', coordinate(store, id, 'y') + dy);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return roots;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function resizeElement(store, id, width, height) {
|
|
43
|
+
const unit = store.get(id).kind === 'window' ? '' : 'px';
|
|
44
|
+
return store.transaction('Resize element', () => {
|
|
45
|
+
setProperty(store, id, 'width', `${Math.max(1, Math.round(width))}${unit}`);
|
|
46
|
+
setProperty(
|
|
47
|
+
store,
|
|
48
|
+
id,
|
|
49
|
+
'height',
|
|
50
|
+
`${Math.max(1, Math.round(height))}${unit}`
|
|
51
|
+
);
|
|
52
|
+
return [id];
|
|
53
|
+
});
|
|
54
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export { mountDesigner } from './designer.js';
|
|
2
|
+
export { createCatalog, designerVocabulary, canContain } from './catalog.js';
|
|
3
|
+
export {
|
|
4
|
+
insertElement,
|
|
5
|
+
moveElements,
|
|
6
|
+
removeElements,
|
|
7
|
+
duplicateElements,
|
|
8
|
+
setProperty,
|
|
9
|
+
copyElements,
|
|
10
|
+
pasteElements,
|
|
11
|
+
} from './commands.js';
|
|
12
|
+
export { createSelection, marquee } from './selection.js';
|
|
13
|
+
export { computeSnap } from './snap.js';
|
|
14
|
+
export {
|
|
15
|
+
canvasToSource,
|
|
16
|
+
sourceToStore,
|
|
17
|
+
createDocumentSession,
|
|
18
|
+
} from './sync.js';
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
readTimeline,
|
|
22
|
+
moveKeyframe,
|
|
23
|
+
insertKeyframe,
|
|
24
|
+
deleteKeyframe,
|
|
25
|
+
moveEvent,
|
|
26
|
+
} from './timeline/model.js';
|
|
27
|
+
export { timeToX, xToTime, snapTime } from './timeline/scale.js';
|
|
28
|
+
export { timelineEdit, runTimelineCommand } from './timeline/commands.js';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Embedded LiNo shell; kept identical to designer.lino by a unit test. */
|
|
2
|
+
export const DESIGNER_LAYOUT =
|
|
3
|
+
"designer: document\n title 'Links UI designer'\n toolbar: stack\n direction horizontal\n gap 4px\n mode: button\n label Run\n on press\n designer mode\n undo: button\n label Undo\n on press\n designer undo\n redo: button\n label Redo\n on press\n designer redo\n duplicate: button\n label Duplicate\n on press\n designer duplicate\n delete: button\n label Delete\n on press\n designer delete\n copy: button\n label Copy\n on press\n designer copy\n cut: button\n label Cut\n on press\n designer cut\n paste: button\n label Paste\n on press\n designer paste\n open: button\n label Open\n on press\n designer open\n save: button\n label Save\n on press\n designer save\n comments: button\n label 'Comment on UI'\n on press\n designer comments\n copy-source: button\n label 'Copy source'\n on press\n designer copy-source\n workspace: split\n direction column\n label Workspace\n sizes 72 28\n min 35 12\n max 88 65\n remember-size workspace\n horizontal: split\n direction row\n label Panels\n sizes 22 48 30\n min 12 20 18\n max 45 70 50\n remember-size horizontal\n left-dock: dock\n label 'Toolbox and Outline'\n persist left-dock\n left-region: region\n side center\n panels toolbox outline\n toolbox: panel\n title Toolbox\n outline: panel\n title Outline\n center-dock: dock\n label Canvas\n persist center-dock\n center-region: region\n side center\n panels canvas\n canvas: panel\n title Canvas\n closable false\n right-dock: dock\n label Properties\n persist right-dock\n right-region: region\n side center\n panels properties\n properties: panel\n title Properties\n bottom-dock: dock\n label Source\n persist bottom-dock\n bottom-region: region\n side center\n panels source timeline\n source: panel\n title Source\n timeline: panel\n title Timeline\n";
|
package/src/panels.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/** The inspector's outline model and typed property grid reused inside designer docks. */
|
|
2
|
+
import {
|
|
3
|
+
buildOutline,
|
|
4
|
+
flattenOutline,
|
|
5
|
+
propertyRows,
|
|
6
|
+
isLocked,
|
|
7
|
+
} from '@link-foundation/ui-devmode/model';
|
|
8
|
+
import { renderProperties } from '@link-foundation/ui-devmode/views';
|
|
9
|
+
import { createConfig } from '@link-foundation/ui-config';
|
|
10
|
+
import { designerVocabulary } from './catalog.js';
|
|
11
|
+
import { element } from './dom.js';
|
|
12
|
+
import { setProperty } from './commands.js';
|
|
13
|
+
|
|
14
|
+
export const outlineOrder = (store) =>
|
|
15
|
+
flattenOutline(buildOutline(store)).map(({ node }) => node.id);
|
|
16
|
+
|
|
17
|
+
function horizontalNavigation(event, item, items, index, collapsed, update) {
|
|
18
|
+
let next = null;
|
|
19
|
+
if (event.key === 'ArrowRight') {
|
|
20
|
+
if (collapsed.has(item?.dataset.id)) {
|
|
21
|
+
collapsed.delete(item.dataset.id);
|
|
22
|
+
update();
|
|
23
|
+
} else {
|
|
24
|
+
next = items[index + 1];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (event.key === 'ArrowLeft') {
|
|
28
|
+
if (item?.getAttribute('aria-expanded') === 'true') {
|
|
29
|
+
collapsed.add(item.dataset.id);
|
|
30
|
+
update();
|
|
31
|
+
} else {
|
|
32
|
+
next = item?.parentElement.closest('[role=treeitem]');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return next;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Tree items have roving focus, expansion, selection and range/toggle paths. */
|
|
39
|
+
export function createOutline(panel, context) {
|
|
40
|
+
const { store, selection, choose, controller, document } = context;
|
|
41
|
+
const collapsed = new Set();
|
|
42
|
+
let focusId = null;
|
|
43
|
+
const tree = element(document, 'ul', {
|
|
44
|
+
role: 'tree',
|
|
45
|
+
'aria-label': 'Document outline',
|
|
46
|
+
'aria-multiselectable': true,
|
|
47
|
+
});
|
|
48
|
+
panel.append(tree);
|
|
49
|
+
tree.addEventListener(
|
|
50
|
+
'click',
|
|
51
|
+
(event) => {
|
|
52
|
+
const item = event.target.closest('[role=treeitem]');
|
|
53
|
+
if (!item) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (event.target.closest('[data-expand]')) {
|
|
57
|
+
if (collapsed.has(item.dataset.id)) {
|
|
58
|
+
collapsed.delete(item.dataset.id);
|
|
59
|
+
} else {
|
|
60
|
+
collapsed.add(item.dataset.id);
|
|
61
|
+
}
|
|
62
|
+
update();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
focusId = item.dataset.id;
|
|
66
|
+
choose(focusId, event);
|
|
67
|
+
[...tree.querySelectorAll('[role=treeitem]')]
|
|
68
|
+
.find((n) => n.dataset.id === focusId)
|
|
69
|
+
?.focus();
|
|
70
|
+
},
|
|
71
|
+
{ signal: controller.signal }
|
|
72
|
+
);
|
|
73
|
+
tree.addEventListener(
|
|
74
|
+
'keydown',
|
|
75
|
+
(event) => {
|
|
76
|
+
if (event.altKey || event.ctrlKey || event.metaKey) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const item = event.target.closest('[role=treeitem]');
|
|
80
|
+
const items = [...tree.querySelectorAll('[role=treeitem]')].filter(
|
|
81
|
+
(n) => !n.closest('[hidden]')
|
|
82
|
+
);
|
|
83
|
+
const index = items.indexOf(item);
|
|
84
|
+
let next = null;
|
|
85
|
+
if (event.key === 'ArrowDown') {
|
|
86
|
+
next = items[index + 1];
|
|
87
|
+
}
|
|
88
|
+
if (event.key === 'ArrowUp') {
|
|
89
|
+
next = items[index - 1];
|
|
90
|
+
}
|
|
91
|
+
if (event.key === 'Home') {
|
|
92
|
+
next = items[0];
|
|
93
|
+
}
|
|
94
|
+
if (event.key === 'End') {
|
|
95
|
+
next = items.at(-1);
|
|
96
|
+
}
|
|
97
|
+
next =
|
|
98
|
+
next ??
|
|
99
|
+
horizontalNavigation(event, item, items, index, collapsed, update);
|
|
100
|
+
if (event.key === ' ' || event.key === 'Enter') {
|
|
101
|
+
choose(item.dataset.id, event);
|
|
102
|
+
}
|
|
103
|
+
if (next) {
|
|
104
|
+
focusId = next.dataset.id;
|
|
105
|
+
choose(focusId, event);
|
|
106
|
+
[...tree.querySelectorAll('[role=treeitem]')]
|
|
107
|
+
.find((n) => n.dataset.id === focusId)
|
|
108
|
+
?.focus();
|
|
109
|
+
}
|
|
110
|
+
if (
|
|
111
|
+
[
|
|
112
|
+
'ArrowDown',
|
|
113
|
+
'ArrowUp',
|
|
114
|
+
'ArrowRight',
|
|
115
|
+
'ArrowLeft',
|
|
116
|
+
'Home',
|
|
117
|
+
'End',
|
|
118
|
+
' ',
|
|
119
|
+
'Enter',
|
|
120
|
+
].includes(event.key)
|
|
121
|
+
) {
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
{ signal: controller.signal }
|
|
126
|
+
);
|
|
127
|
+
function update() {
|
|
128
|
+
const active = tree.contains(document.activeElement)
|
|
129
|
+
? document.activeElement.dataset.id
|
|
130
|
+
: null;
|
|
131
|
+
tree.replaceChildren();
|
|
132
|
+
const build = (node, into, level) => {
|
|
133
|
+
const item = element(document, 'li', {
|
|
134
|
+
role: 'treeitem',
|
|
135
|
+
tabindex: -1,
|
|
136
|
+
'aria-selected': selection.ids.has(node.id),
|
|
137
|
+
'aria-level': level,
|
|
138
|
+
'data-id': node.id,
|
|
139
|
+
});
|
|
140
|
+
const row = element(document, 'span', { class: 'outline-row' });
|
|
141
|
+
if (node.children.length) {
|
|
142
|
+
item.setAttribute('aria-expanded', String(!collapsed.has(node.id)));
|
|
143
|
+
row.append(
|
|
144
|
+
element(
|
|
145
|
+
document,
|
|
146
|
+
'span',
|
|
147
|
+
{ 'data-expand': '', 'aria-hidden': true },
|
|
148
|
+
collapsed.has(node.id) ? '▸' : '▾'
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
row.append(element(document, 'span', {}, `${node.id}: ${node.type}`));
|
|
153
|
+
item.append(row);
|
|
154
|
+
if (node.children.length) {
|
|
155
|
+
const group = element(document, 'ul', { role: 'group' });
|
|
156
|
+
group.hidden = collapsed.has(node.id);
|
|
157
|
+
node.children.forEach((child) => build(child, group, level + 1));
|
|
158
|
+
item.append(group);
|
|
159
|
+
}
|
|
160
|
+
into.append(item);
|
|
161
|
+
};
|
|
162
|
+
buildOutline(store).forEach((node) => build(node, tree, 1));
|
|
163
|
+
const items = [...tree.querySelectorAll('[role=treeitem]')];
|
|
164
|
+
const focused =
|
|
165
|
+
items.find((n) => n.dataset.id === (active ?? focusId)) ?? items[0];
|
|
166
|
+
if (focused) {
|
|
167
|
+
focused.tabIndex = 0;
|
|
168
|
+
if (active) {
|
|
169
|
+
focused.focus();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
update();
|
|
174
|
+
return { update, tree };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const PROPERTY_MESSAGES = {
|
|
178
|
+
'properties.caption': 'Properties of {id}',
|
|
179
|
+
'properties.name': 'Property',
|
|
180
|
+
'properties.editor': 'Editor',
|
|
181
|
+
'properties.value': 'Value',
|
|
182
|
+
'properties.layer': 'Layer',
|
|
183
|
+
'properties.reset': 'Reset',
|
|
184
|
+
'properties.resetLabel': 'Reset {name}',
|
|
185
|
+
'properties.unitLabel': '{name} unit',
|
|
186
|
+
'properties.unset': 'Unset',
|
|
187
|
+
'properties.locked': '{id} is locked',
|
|
188
|
+
'properties.readOnly': 'Read only',
|
|
189
|
+
};
|
|
190
|
+
const t = (key, params = {}) =>
|
|
191
|
+
Object.entries(params).reduce(
|
|
192
|
+
(text, [name, value]) => text.replaceAll(`{${name}}`, value),
|
|
193
|
+
PROPERTY_MESSAGES[key] ?? key
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
/** One field change is one transaction, even when input emits several keystrokes. */
|
|
197
|
+
export function updateProperties(panel, context) {
|
|
198
|
+
const { store, selection, policy, run, report, document } = context;
|
|
199
|
+
const id = [...selection.ids].at(-1);
|
|
200
|
+
if (!id || !store.has(id)) {
|
|
201
|
+
panel.replaceChildren(
|
|
202
|
+
element(document, 'p', {}, 'Select an element to edit its properties.')
|
|
203
|
+
);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const config = createConfig({ document: store.toDocument() });
|
|
207
|
+
const rows = propertyRows(store.get(id), config, {
|
|
208
|
+
policy,
|
|
209
|
+
vocabulary: designerVocabulary(),
|
|
210
|
+
parentOf: store.parentOf,
|
|
211
|
+
});
|
|
212
|
+
const pending = new Map();
|
|
213
|
+
renderProperties(
|
|
214
|
+
panel,
|
|
215
|
+
{
|
|
216
|
+
id,
|
|
217
|
+
rows,
|
|
218
|
+
locked: isLocked(policy, id, store.parentOf),
|
|
219
|
+
writable: policy.writeLayers.length > 0,
|
|
220
|
+
tokenPaths: [],
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
t,
|
|
224
|
+
onEdit(row, value) {
|
|
225
|
+
pending.set(row.name, value);
|
|
226
|
+
return null;
|
|
227
|
+
},
|
|
228
|
+
onReset(row) {
|
|
229
|
+
run((s) => s.removeProp(id, row.name));
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
panel.onchange = () => {
|
|
234
|
+
for (const [name, value] of pending) {
|
|
235
|
+
try {
|
|
236
|
+
run((s) => setProperty(s, id, name, value));
|
|
237
|
+
} catch (error) {
|
|
238
|
+
report(error.message);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
pending.clear();
|
|
242
|
+
};
|
|
243
|
+
}
|
package/src/selection.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Selection has no DOM dependency. Ranges use the current outline order. */
|
|
2
|
+
export function createSelection(initial = []) {
|
|
3
|
+
let ids = new Set(initial);
|
|
4
|
+
let anchor = initial.at(-1) ?? null;
|
|
5
|
+
return {
|
|
6
|
+
get ids() {
|
|
7
|
+
return new Set(ids);
|
|
8
|
+
},
|
|
9
|
+
get anchor() {
|
|
10
|
+
return anchor;
|
|
11
|
+
},
|
|
12
|
+
select(id) {
|
|
13
|
+
ids = new Set(id === null ? [] : [id]);
|
|
14
|
+
anchor = id;
|
|
15
|
+
},
|
|
16
|
+
replace(next) {
|
|
17
|
+
ids = new Set(next);
|
|
18
|
+
anchor = [...ids].at(-1) ?? null;
|
|
19
|
+
},
|
|
20
|
+
toggle(id) {
|
|
21
|
+
if (ids.has(id)) {
|
|
22
|
+
ids.delete(id);
|
|
23
|
+
} else {
|
|
24
|
+
ids.add(id);
|
|
25
|
+
}
|
|
26
|
+
anchor = id;
|
|
27
|
+
},
|
|
28
|
+
range(id, order) {
|
|
29
|
+
const from = order.indexOf(anchor);
|
|
30
|
+
const to = order.indexOf(id);
|
|
31
|
+
if (from < 0 || to < 0) {
|
|
32
|
+
this.select(id);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
ids = new Set(order.slice(Math.min(from, to), Math.max(from, to) + 1));
|
|
36
|
+
},
|
|
37
|
+
retain(available) {
|
|
38
|
+
const allowed = new Set(available);
|
|
39
|
+
ids = new Set([...ids].filter((id) => allowed.has(id)));
|
|
40
|
+
if (!allowed.has(anchor)) {
|
|
41
|
+
anchor = [...ids].at(-1) ?? null;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function normalizedRect({ x, y, width, height }) {
|
|
48
|
+
return {
|
|
49
|
+
x: Math.min(x, x + width),
|
|
50
|
+
y: Math.min(y, y + height),
|
|
51
|
+
width: Math.abs(width),
|
|
52
|
+
height: Math.abs(height),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Intersecting rectangles; negative marquee dimensions work in every drag direction. */
|
|
57
|
+
export function marquee(rects, box) {
|
|
58
|
+
const b = normalizedRect(box);
|
|
59
|
+
return rects
|
|
60
|
+
.filter(
|
|
61
|
+
(r) =>
|
|
62
|
+
r.x <= b.x + b.width &&
|
|
63
|
+
r.x + r.width >= b.x &&
|
|
64
|
+
r.y <= b.y + b.height &&
|
|
65
|
+
r.y + r.height >= b.y
|
|
66
|
+
)
|
|
67
|
+
.map((r) => r.id);
|
|
68
|
+
}
|
package/src/snap.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Snap in CSS pixels. Ties resolve in sibling order, then leading/centre/trailing order. */
|
|
2
|
+
export function computeSnap(moving, others, { threshold = 6 } = {}) {
|
|
3
|
+
if (!Number.isFinite(threshold) || threshold < 0) {
|
|
4
|
+
throw new Error('Invalid snap threshold');
|
|
5
|
+
}
|
|
6
|
+
const result = { dx: 0, dy: 0, guides: [] };
|
|
7
|
+
for (const [axis, size, cross, span, delta] of [
|
|
8
|
+
['x', 'width', 'y', 'height', 'dx'],
|
|
9
|
+
['y', 'height', 'x', 'width', 'dy'],
|
|
10
|
+
]) {
|
|
11
|
+
let best = null;
|
|
12
|
+
for (const other of others) {
|
|
13
|
+
for (const fraction of [0, 0.5, 1]) {
|
|
14
|
+
const position = other[axis] + other[size] * fraction;
|
|
15
|
+
for (const own of [0, 0.5, 1]) {
|
|
16
|
+
const offset = position - moving[axis] - moving[size] * own;
|
|
17
|
+
if (
|
|
18
|
+
Math.abs(offset) <= threshold &&
|
|
19
|
+
(best === null || Math.abs(offset) < Math.abs(best.offset))
|
|
20
|
+
) {
|
|
21
|
+
best = {
|
|
22
|
+
offset,
|
|
23
|
+
guide: {
|
|
24
|
+
axis,
|
|
25
|
+
position,
|
|
26
|
+
from: Math.min(moving[cross], other[cross]),
|
|
27
|
+
to: Math.max(
|
|
28
|
+
moving[cross] + moving[span],
|
|
29
|
+
other[cross] + other[span]
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (best) {
|
|
38
|
+
result[delta] = best.offset;
|
|
39
|
+
result.guides.push(best.guide);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|