@zsherman/canvas 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/CHANGELOG.md +7 -0
- package/LICENSE +373 -0
- package/README.md +15 -2
- package/THIRD_PARTY_NOTICES.md +117 -0
- package/dist/dom/canvas/assets.js +89 -0
- package/dist/dom/canvas/board-store.js +56 -0
- package/dist/dom/canvas/canvas-actions.js +78 -0
- package/dist/dom/canvas/canvas-board.js +557 -0
- package/dist/dom/canvas/canvas-board.styles.js +110 -0
- package/dist/dom/canvas/canvas-chrome.js +1167 -0
- package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
- package/dist/dom/canvas/canvas-comments.js +950 -0
- package/dist/dom/canvas/canvas-comments.styles.js +231 -0
- package/dist/dom/canvas/canvas-context.js +196 -0
- package/dist/dom/canvas/canvas-controller.js +697 -0
- package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
- package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
- package/dist/dom/canvas/canvas-menus.js +276 -0
- package/dist/dom/canvas/canvas-menus.styles.js +7 -0
- package/dist/dom/canvas/canvas-style-bar.js +930 -0
- package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
- package/dist/dom/canvas/canvas-surface.js +1353 -0
- package/dist/dom/canvas/canvas-surface.styles.js +345 -0
- package/dist/dom/canvas/canvas-tool-rail.js +406 -0
- package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
- package/dist/dom/canvas/comments.js +58 -0
- package/dist/dom/canvas/equality.js +13 -0
- package/dist/dom/canvas/export-png.js +496 -0
- package/dist/dom/canvas/gesture-machine.js +91 -0
- package/dist/dom/canvas/handlers.js +18 -0
- package/dist/dom/canvas/index.js +9 -0
- package/dist/dom/canvas/input.js +85 -0
- package/dist/dom/canvas/instant.js +28 -0
- package/dist/dom/canvas/kernel.js +39 -0
- package/dist/dom/canvas/motion.js +686 -0
- package/dist/dom/canvas/palette.js +165 -0
- package/dist/dom/canvas/random-id.js +15 -0
- package/dist/dom/canvas/scene-renderer.js +559 -0
- package/dist/dom/canvas/scene.styles.js +7 -0
- package/dist/dom/canvas/surface-model.js +324 -0
- package/dist/dom/canvas/uploads.js +294 -0
- package/dist/dom/index.js +9 -0
- package/dist/dom/kernel/actions.js +455 -0
- package/dist/dom/kernel/board.js +856 -0
- package/dist/dom/kernel/claims.js +40 -0
- package/dist/dom/kernel/clipboard.js +30 -0
- package/dist/dom/kernel/constants.js +210 -0
- package/dist/dom/kernel/contract.js +0 -0
- package/dist/dom/kernel/decode.js +85 -0
- package/dist/dom/kernel/editor.js +2077 -0
- package/dist/dom/kernel/errors.js +82 -0
- package/dist/dom/kernel/fonts/inter.js +7924 -0
- package/dist/dom/kernel/fractional-index.js +54 -0
- package/dist/dom/kernel/geometry.js +123 -0
- package/dist/dom/kernel/hit-test.js +156 -0
- package/dist/dom/kernel/index.js +22 -0
- package/dist/dom/kernel/inspect.js +116 -0
- package/dist/dom/kernel/kernel.js +251 -0
- package/dist/dom/kernel/outline.js +64 -0
- package/dist/dom/kernel/plan.js +1370 -0
- package/dist/dom/kernel/rev.js +173 -0
- package/dist/dom/kernel/scene.js +580 -0
- package/dist/dom/kernel/schema.js +1109 -0
- package/dist/dom/kernel/snap.js +258 -0
- package/dist/dom/kernel/svg.js +148 -0
- package/dist/dom/kernel/text.js +142 -0
- package/dist/solid/canvas/assets.jsx +89 -0
- package/dist/solid/canvas/board-store.jsx +56 -0
- package/dist/solid/canvas/canvas-actions.jsx +78 -0
- package/dist/solid/canvas/canvas-board.jsx +326 -0
- package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
- package/dist/solid/canvas/canvas-chrome.jsx +612 -0
- package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
- package/dist/solid/canvas/canvas-comments.jsx +435 -0
- package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
- package/dist/solid/canvas/canvas-context.jsx +174 -0
- package/dist/solid/canvas/canvas-controller.jsx +697 -0
- package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
- package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
- package/dist/solid/canvas/canvas-menus.jsx +223 -0
- package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
- package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
- package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
- package/dist/solid/canvas/canvas-surface.jsx +681 -0
- package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
- package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
- package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
- package/dist/solid/canvas/comments.jsx +58 -0
- package/dist/solid/canvas/equality.jsx +13 -0
- package/dist/solid/canvas/export-png.jsx +496 -0
- package/dist/solid/canvas/gesture-machine.jsx +91 -0
- package/dist/solid/canvas/handlers.jsx +18 -0
- package/dist/solid/canvas/index.jsx +9 -0
- package/dist/solid/canvas/input.jsx +85 -0
- package/dist/solid/canvas/instant.jsx +28 -0
- package/dist/solid/canvas/kernel.jsx +39 -0
- package/dist/solid/canvas/motion.jsx +686 -0
- package/dist/solid/canvas/palette.jsx +165 -0
- package/dist/solid/canvas/random-id.jsx +15 -0
- package/dist/solid/canvas/scene-renderer.jsx +559 -0
- package/dist/solid/canvas/scene.styles.jsx +7 -0
- package/dist/solid/canvas/surface-model.jsx +324 -0
- package/dist/solid/canvas/uploads.jsx +294 -0
- package/dist/solid/index.jsx +9 -0
- package/dist/solid/kernel/actions.jsx +455 -0
- package/dist/solid/kernel/board.jsx +856 -0
- package/dist/solid/kernel/claims.jsx +40 -0
- package/dist/solid/kernel/clipboard.jsx +30 -0
- package/dist/solid/kernel/constants.jsx +210 -0
- package/dist/solid/kernel/contract.jsx +0 -0
- package/dist/solid/kernel/decode.jsx +85 -0
- package/dist/solid/kernel/editor.jsx +2077 -0
- package/dist/solid/kernel/errors.jsx +82 -0
- package/dist/solid/kernel/fonts/inter.jsx +7924 -0
- package/dist/solid/kernel/fractional-index.jsx +54 -0
- package/dist/solid/kernel/geometry.jsx +123 -0
- package/dist/solid/kernel/hit-test.jsx +156 -0
- package/dist/solid/kernel/index.jsx +22 -0
- package/dist/solid/kernel/inspect.jsx +116 -0
- package/dist/solid/kernel/kernel.jsx +251 -0
- package/dist/solid/kernel/outline.jsx +64 -0
- package/dist/solid/kernel/plan.jsx +1370 -0
- package/dist/solid/kernel/rev.jsx +173 -0
- package/dist/solid/kernel/scene.jsx +580 -0
- package/dist/solid/kernel/schema.jsx +1109 -0
- package/dist/solid/kernel/snap.jsx +258 -0
- package/dist/solid/kernel/svg.jsx +148 -0
- package/dist/solid/kernel/text.jsx +142 -0
- package/dist/styles.css +1 -0
- package/dist/types/canvas/assets.d.ts +111 -0
- package/dist/types/canvas/board-store.d.ts +86 -0
- package/dist/types/canvas/canvas-actions.d.ts +21 -0
- package/dist/types/canvas/canvas-board.d.ts +53 -0
- package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
- package/dist/types/canvas/canvas-chrome.d.ts +109 -0
- package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
- package/dist/types/canvas/canvas-comments.d.ts +20 -0
- package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
- package/dist/types/canvas/canvas-context.d.ts +104 -0
- package/dist/types/canvas/canvas-controller.d.ts +159 -0
- package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
- package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
- package/dist/types/canvas/canvas-menus.d.ts +21 -0
- package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
- package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
- package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
- package/dist/types/canvas/canvas-surface.d.ts +15 -0
- package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
- package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
- package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
- package/dist/types/canvas/comments.d.ts +106 -0
- package/dist/types/canvas/equality.d.ts +4 -0
- package/dist/types/canvas/export-png.d.ts +77 -0
- package/dist/types/canvas/gesture-machine.d.ts +46 -0
- package/dist/types/canvas/handlers.d.ts +7 -0
- package/dist/types/canvas/index.d.ts +18 -0
- package/dist/types/canvas/input.d.ts +63 -0
- package/dist/types/canvas/instant.d.ts +13 -0
- package/dist/types/canvas/kernel.d.ts +27 -0
- package/dist/types/canvas/motion.d.ts +72 -0
- package/dist/types/canvas/palette.d.ts +46 -0
- package/dist/types/canvas/random-id.d.ts +6 -0
- package/dist/types/canvas/scene-renderer.d.ts +27 -0
- package/dist/types/canvas/scene.styles.d.ts +4 -0
- package/dist/types/canvas/surface-model.d.ts +113 -0
- package/dist/types/canvas/uploads.d.ts +58 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/kernel/actions.d.ts +19 -0
- package/dist/types/kernel/board.d.ts +137 -0
- package/dist/types/kernel/claims.d.ts +29 -0
- package/dist/types/kernel/clipboard.d.ts +10 -0
- package/dist/types/kernel/constants.d.ts +195 -0
- package/dist/types/kernel/contract.d.ts +1533 -0
- package/dist/types/kernel/decode.d.ts +60 -0
- package/dist/types/kernel/editor.d.ts +12 -0
- package/dist/types/kernel/errors.d.ts +11 -0
- package/dist/types/kernel/fractional-index.d.ts +7 -0
- package/dist/types/kernel/geometry.d.ts +42 -0
- package/dist/types/kernel/hit-test.d.ts +16 -0
- package/dist/types/kernel/index.d.ts +8 -0
- package/dist/types/kernel/inspect.d.ts +6 -0
- package/dist/types/kernel/kernel.d.ts +6 -0
- package/dist/types/kernel/outline.d.ts +4 -0
- package/dist/types/kernel/plan.d.ts +20 -0
- package/dist/types/kernel/rev.d.ts +21 -0
- package/dist/types/kernel/scene.d.ts +16 -0
- package/dist/types/kernel/schema.d.ts +3436 -0
- package/dist/types/kernel/snap.d.ts +41 -0
- package/dist/types/kernel/svg.d.ts +3 -0
- package/dist/types/kernel/text.d.ts +21 -0
- package/docs/api.json +207 -0
- package/docs/canvas.md +444 -0
- package/docs/kernel.md +108 -0
- package/docs/llms.txt +41 -0
- package/docs/search-index.json +52 -0
- package/package.json +55 -4
|
@@ -0,0 +1,1370 @@
|
|
|
1
|
+
import { DEFAULTS, HISTORY, INK_TOLERANCE, LIMITS } from "./constants.jsx";
|
|
2
|
+
import { affectedArrows, applyChanges, applyProposal, bounds, boundsOf, compareShapes, currentField, diff, fitText, history, inkGeometry, jsonEqual, ordered, patchShape, patchedFields, pendingProposals, pickField, recordBytes, refreshRoutes, route, routeEnd, routeStart, sortedIds, toMap, trimHistory, withRoute } from "./board.jsx";
|
|
3
|
+
import { claimants, sameConnection } from "./claims.jsx";
|
|
4
|
+
import { claimed, historyExpired, invalid, locked, shapeNotFound, tooLarge, transactionNotFound } from "./errors.jsx";
|
|
5
|
+
import { keyAfter, keyBefore, keyBetween } from "./fractional-index.jsx";
|
|
6
|
+
import { overlaps, round, roundPoint } from "./geometry.jsx";
|
|
7
|
+
// src/kernel/plan.ts
|
|
8
|
+
/**
|
|
9
|
+
* Planning: commands in, row changes out. Planning reads the board and never
|
|
10
|
+
* writes it; the host stores `plan.changes` and `plan.record` in one write.
|
|
11
|
+
*/
|
|
12
|
+
/** A shape map that records the ids written to it, so each command refreshes and checks only those. */
|
|
13
|
+
var Rows = class extends Map {
|
|
14
|
+
dirty = /* @__PURE__ */ new Set();
|
|
15
|
+
set(id, shape) {
|
|
16
|
+
this.dirty.add(id);
|
|
17
|
+
return super.set(id, shape);
|
|
18
|
+
}
|
|
19
|
+
delete(id) {
|
|
20
|
+
this.dirty.add(id);
|
|
21
|
+
return super.delete(id);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* `<prefix>-<transaction>-<n>`. When that is longer than an id may be, the
|
|
26
|
+
* transaction id keeps its last characters, where ids such as
|
|
27
|
+
* `<session>-<seq>` differ, and the id is exactly 64 characters.
|
|
28
|
+
*/
|
|
29
|
+
const generate = (ctx, prefix) => {
|
|
30
|
+
ctx.next += 1;
|
|
31
|
+
const tail = `-${ctx.next}`;
|
|
32
|
+
const room = 64 - prefix.length - 1 - tail.length;
|
|
33
|
+
return `${prefix}-${ctx.transaction.id.slice(-room)}${tail}`;
|
|
34
|
+
};
|
|
35
|
+
/** A generated group id no shape on the board has. */
|
|
36
|
+
const newGroup = (ctx) => {
|
|
37
|
+
ctx.groups ??= new Set([...ctx.shapes.values()].flatMap((s) => s.group === null ? [] : [s.group]));
|
|
38
|
+
let id = generate(ctx, "group");
|
|
39
|
+
while (ctx.groups.has(id)) id = generate(ctx, "group");
|
|
40
|
+
ctx.groups.add(id);
|
|
41
|
+
return id;
|
|
42
|
+
};
|
|
43
|
+
const kindPrefix = (kind) => kind.toLowerCase();
|
|
44
|
+
const article = (kind) => /^[AEIOU]/.test(kind) ? "an" : "a";
|
|
45
|
+
const topZ = (shapes) => {
|
|
46
|
+
let top = null;
|
|
47
|
+
for (const shape of shapes.values()) if (top === null || shape.z > top) top = shape.z;
|
|
48
|
+
return top;
|
|
49
|
+
};
|
|
50
|
+
const bottomZ = (shapes) => {
|
|
51
|
+
let bottom = null;
|
|
52
|
+
for (const shape of shapes.values()) if (bottom === null || shape.z < bottom) bottom = shape.z;
|
|
53
|
+
return bottom;
|
|
54
|
+
};
|
|
55
|
+
/** Stores a shape and stamps it with the actor when it changed. */
|
|
56
|
+
const put = (ctx, shape) => {
|
|
57
|
+
const before = ctx.shapes.get(shape.id);
|
|
58
|
+
if (before !== void 0 && jsonEqual(before, shape)) return;
|
|
59
|
+
ctx.shapes.set(shape.id, {
|
|
60
|
+
...shape,
|
|
61
|
+
updatedBy: ctx.actor
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
const findShape = (ctx, id, path) => {
|
|
65
|
+
const shape = ctx.shapes.get(id);
|
|
66
|
+
return shape === void 0 ? { error: shapeNotFound(path, id, ctx.shapes.keys()) } : { shape };
|
|
67
|
+
};
|
|
68
|
+
/** A shape a command may change: it exists, nobody else claims it, and it is unlocked. */
|
|
69
|
+
const editable = (ctx, id, path, options = {}) => {
|
|
70
|
+
const found = findShape(ctx, id, path);
|
|
71
|
+
if ("error" in found) return found;
|
|
72
|
+
const by = ctx.claims.get(id);
|
|
73
|
+
if (by !== void 0) return { error: claimed(path, id, by, ctx.actor) };
|
|
74
|
+
if (found.shape.locked && options.ignoreLock !== true) return { error: locked(path, id) };
|
|
75
|
+
return found;
|
|
76
|
+
};
|
|
77
|
+
const editableAll = (ctx, ids, path, options = {}) => {
|
|
78
|
+
const seen = /* @__PURE__ */ new Set();
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const [index, id] of ids.entries()) {
|
|
81
|
+
if (seen.has(id)) continue;
|
|
82
|
+
seen.add(id);
|
|
83
|
+
const found = editable(ctx, id, [...path, index], options);
|
|
84
|
+
if ("error" in found) return found;
|
|
85
|
+
out.push(found.shape);
|
|
86
|
+
}
|
|
87
|
+
const min = options.min ?? 1;
|
|
88
|
+
if (out.length < min) return { error: invalid(path, `${min === 1 ? "at least one shape id" : `at least ${min} different shape ids`}`, "List the ids of shapes on the board; locked shapes do not count.") };
|
|
89
|
+
return { shapes: out };
|
|
90
|
+
};
|
|
91
|
+
const moveAnchor = (anchor, dx, dy) => anchor._tag === "Point" ? {
|
|
92
|
+
_tag: "Point",
|
|
93
|
+
x: round(anchor.x + dx),
|
|
94
|
+
y: round(anchor.y + dy)
|
|
95
|
+
} : anchor;
|
|
96
|
+
/** The shape moved by (dx, dy). A connector moves its free ends; bound ends follow their shapes. */
|
|
97
|
+
const translate = (shape, dx, dy) => {
|
|
98
|
+
if (shape.kind === "Arrow") return {
|
|
99
|
+
...shape,
|
|
100
|
+
x: round(shape.x + dx),
|
|
101
|
+
y: round(shape.y + dy),
|
|
102
|
+
from: moveAnchor(shape.from, dx, dy),
|
|
103
|
+
to: moveAnchor(shape.to, dx, dy)
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
...shape,
|
|
107
|
+
x: round(shape.x + dx),
|
|
108
|
+
y: round(shape.y + dy)
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
/** Maps a coordinate from box `a` to box `b` along one axis. */
|
|
112
|
+
const remap = (value, aStart, aSize, bStart, bSize) => aSize === 0 ? bStart + (value - aStart) : bStart + (value - aStart) * bSize / aSize;
|
|
113
|
+
const scaleInk = (ink, w, h) => {
|
|
114
|
+
const sx = ink.w === 0 ? 1 : w / ink.w;
|
|
115
|
+
const sy = ink.h === 0 ? 1 : h / ink.h;
|
|
116
|
+
return {
|
|
117
|
+
...ink,
|
|
118
|
+
w: ink.w === 0 ? 0 : round(w),
|
|
119
|
+
h: ink.h === 0 ? 0 : round(h),
|
|
120
|
+
points: ink.points.map((p) => ({
|
|
121
|
+
x: round(p.x * sx),
|
|
122
|
+
y: round(p.y * sy)
|
|
123
|
+
}))
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
/** The shape scaled from box `from` to box `to`. */
|
|
127
|
+
const scale = (fonts, shape, from, to) => {
|
|
128
|
+
const mapX = (x) => round(remap(x, from.x, from.w, to.x, to.w));
|
|
129
|
+
const mapY = (y) => round(remap(y, from.y, from.h, to.y, to.h));
|
|
130
|
+
const sx = from.w === 0 ? 1 : to.w / from.w;
|
|
131
|
+
const sy = from.h === 0 ? 1 : to.h / from.h;
|
|
132
|
+
switch (shape.kind) {
|
|
133
|
+
case "Arrow": {
|
|
134
|
+
const end = (anchor) => anchor._tag === "Point" ? {
|
|
135
|
+
_tag: "Point",
|
|
136
|
+
x: mapX(anchor.x),
|
|
137
|
+
y: mapY(anchor.y)
|
|
138
|
+
} : anchor;
|
|
139
|
+
return {
|
|
140
|
+
...shape,
|
|
141
|
+
from: end(shape.from),
|
|
142
|
+
to: end(shape.to)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
case "Ink": return {
|
|
146
|
+
...scaleInk(shape, shape.w * sx, shape.h * sy),
|
|
147
|
+
x: mapX(shape.x),
|
|
148
|
+
y: mapY(shape.y)
|
|
149
|
+
};
|
|
150
|
+
case "Text": return fitText(fonts, {
|
|
151
|
+
...shape,
|
|
152
|
+
x: mapX(shape.x),
|
|
153
|
+
y: mapY(shape.y),
|
|
154
|
+
w: round(Math.max(shape.w * sx, 16)),
|
|
155
|
+
style: {
|
|
156
|
+
...shape.style,
|
|
157
|
+
width: "fixed"
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
case "Image":
|
|
161
|
+
case "Timestamp": return {
|
|
162
|
+
...shape,
|
|
163
|
+
x: mapX(shape.x),
|
|
164
|
+
y: mapY(shape.y),
|
|
165
|
+
w: round(Math.max(shape.w * sx, 16)),
|
|
166
|
+
h: round(Math.max(shape.h * sy, 16))
|
|
167
|
+
};
|
|
168
|
+
case "Sticky":
|
|
169
|
+
case "Box": return fitText(fonts, {
|
|
170
|
+
...shape,
|
|
171
|
+
x: mapX(shape.x),
|
|
172
|
+
y: mapY(shape.y),
|
|
173
|
+
w: round(Math.max(shape.w * sx, 16)),
|
|
174
|
+
h: round(Math.max(shape.h * sy, 16))
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Where a box of size (w, h) goes: `At` puts its top-left corner at the
|
|
180
|
+
* point; `Beside` centers it on a side of a shape, `gap` away, sliding
|
|
181
|
+
* further out past shapes it would overlap. Without a placement it goes to
|
|
182
|
+
* the right of everything on the board, top-aligned.
|
|
183
|
+
*/
|
|
184
|
+
const place = (ctx, w, h, placement, exclude, path) => {
|
|
185
|
+
const others = [...ctx.shapes.values()].filter((shape) => !exclude.has(shape.id) && shape.kind !== "Arrow");
|
|
186
|
+
if (placement === void 0) {
|
|
187
|
+
const content = boundsOf(ctx.shapes, others.map((shape) => shape.id));
|
|
188
|
+
return content === null ? {
|
|
189
|
+
x: 0,
|
|
190
|
+
y: 0
|
|
191
|
+
} : {
|
|
192
|
+
x: round(content.x + content.w + DEFAULTS.gap),
|
|
193
|
+
y: round(content.y)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (placement._tag === "At") return {
|
|
197
|
+
x: round(placement.x),
|
|
198
|
+
y: round(placement.y)
|
|
199
|
+
};
|
|
200
|
+
const found = findShape(ctx, placement.shape, [...path, "shape"]);
|
|
201
|
+
if ("error" in found) return found;
|
|
202
|
+
const r = bounds(ctx.shapes, found.shape);
|
|
203
|
+
const gap = placement.gap ?? DEFAULTS.gap;
|
|
204
|
+
let x;
|
|
205
|
+
let y;
|
|
206
|
+
switch (placement.side) {
|
|
207
|
+
case "right":
|
|
208
|
+
x = r.x + r.w + gap;
|
|
209
|
+
y = r.y + (r.h - h) / 2;
|
|
210
|
+
break;
|
|
211
|
+
case "left":
|
|
212
|
+
x = r.x - gap - w;
|
|
213
|
+
y = r.y + (r.h - h) / 2;
|
|
214
|
+
break;
|
|
215
|
+
case "bottom":
|
|
216
|
+
x = r.x + (r.w - w) / 2;
|
|
217
|
+
y = r.y + r.h + gap;
|
|
218
|
+
break;
|
|
219
|
+
case "top":
|
|
220
|
+
x = r.x + (r.w - w) / 2;
|
|
221
|
+
y = r.y - gap - h;
|
|
222
|
+
}
|
|
223
|
+
const across = placement.side === "right" || placement.side === "left";
|
|
224
|
+
const out = placement.side === "right" || placement.side === "bottom";
|
|
225
|
+
const length = across ? w : h;
|
|
226
|
+
const lo = across ? y : x;
|
|
227
|
+
const hi = lo + (across ? h : w);
|
|
228
|
+
const lanes = [];
|
|
229
|
+
for (const shape of others) {
|
|
230
|
+
if (shape.id === found.shape.id) continue;
|
|
231
|
+
const o = bounds(ctx.shapes, shape);
|
|
232
|
+
const [a, b, c, d] = across ? [
|
|
233
|
+
o.x,
|
|
234
|
+
o.x + o.w,
|
|
235
|
+
o.y,
|
|
236
|
+
o.y + o.h
|
|
237
|
+
] : [
|
|
238
|
+
o.y,
|
|
239
|
+
o.y + o.h,
|
|
240
|
+
o.x,
|
|
241
|
+
o.x + o.w
|
|
242
|
+
];
|
|
243
|
+
if (c < hi && lo < d) lanes.push({
|
|
244
|
+
a,
|
|
245
|
+
b
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
lanes.sort((p, q) => out ? p.a - q.a : q.b - p.b);
|
|
249
|
+
let s = across ? x : y;
|
|
250
|
+
let i = 0;
|
|
251
|
+
for (let reach = 0; reach !== null;) {
|
|
252
|
+
reach = null;
|
|
253
|
+
for (; i < lanes.length; i++) {
|
|
254
|
+
const lane = lanes[i];
|
|
255
|
+
if (lane === void 0 || (out ? lane.a >= s + length : lane.b <= s)) break;
|
|
256
|
+
if (out ? lane.b > s : lane.a < s + length) reach = out ? Math.max(reach ?? lane.b, lane.b) : Math.min(reach ?? lane.a, lane.a);
|
|
257
|
+
}
|
|
258
|
+
if (reach !== null) s = out ? reach + gap : reach - gap - length;
|
|
259
|
+
}
|
|
260
|
+
if (across) x = s;
|
|
261
|
+
else y = s;
|
|
262
|
+
return {
|
|
263
|
+
x: round(x),
|
|
264
|
+
y: round(y)
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
const stickyStyle = (input, base) => ({
|
|
268
|
+
color: input.color ?? base.color,
|
|
269
|
+
size: input.size ?? base.size,
|
|
270
|
+
align: input.align ?? base.align
|
|
271
|
+
});
|
|
272
|
+
const boxStyle = (input, base) => ({
|
|
273
|
+
color: input.color ?? base.color,
|
|
274
|
+
form: input.form ?? base.form,
|
|
275
|
+
size: input.size ?? base.size,
|
|
276
|
+
align: input.align ?? base.align,
|
|
277
|
+
fill: input.fill ?? base.fill,
|
|
278
|
+
dash: input.dash ?? base.dash
|
|
279
|
+
});
|
|
280
|
+
const textStyle = (input, base) => ({
|
|
281
|
+
color: input.color ?? base.color,
|
|
282
|
+
size: input.size ?? base.size,
|
|
283
|
+
align: input.align ?? base.align,
|
|
284
|
+
width: base.width
|
|
285
|
+
});
|
|
286
|
+
const arrowStyle = (input, base) => ({
|
|
287
|
+
color: input.color ?? base.color,
|
|
288
|
+
start: input.start ?? base.start,
|
|
289
|
+
end: input.end ?? base.end,
|
|
290
|
+
route: input.route ?? base.route,
|
|
291
|
+
dash: input.dash ?? base.dash
|
|
292
|
+
});
|
|
293
|
+
const inkStyle = (input, base) => ({
|
|
294
|
+
color: input.color ?? base.color,
|
|
295
|
+
size: input.size ?? base.size,
|
|
296
|
+
brush: input.brush ?? base.brush
|
|
297
|
+
});
|
|
298
|
+
const checkAnchor = (ctx, anchor, self, path) => {
|
|
299
|
+
if (anchor._tag === "Point") return null;
|
|
300
|
+
if (anchor.id === self) return invalid([...path, "id"], "another shape's id", "A connector cannot attach to itself.");
|
|
301
|
+
const target = ctx.shapes.get(anchor.id);
|
|
302
|
+
if (target === void 0) return shapeNotFound([...path, "id"], anchor.id, ctx.shapes.keys());
|
|
303
|
+
if (target.kind === "Arrow") return invalid([...path, "id"], "the id of a shape that is not a connector", "Connectors attach to stickies, boxes, text and ink.");
|
|
304
|
+
return null;
|
|
305
|
+
};
|
|
306
|
+
const newId = (ctx, wanted, prefix, path) => {
|
|
307
|
+
if (wanted === void 0) {
|
|
308
|
+
let id = generate(ctx, prefix);
|
|
309
|
+
while (ctx.shapes.has(id)) id = generate(ctx, prefix);
|
|
310
|
+
return id;
|
|
311
|
+
}
|
|
312
|
+
if (ctx.shapes.has(wanted)) return { error: invalid(path, "an id no shape has yet", `${JSON.stringify(wanted)} is taken; leave id out to get a fresh one.`) };
|
|
313
|
+
return ctx.original.has(wanted) ? { error: invalid(path, "an id no shape had when the transaction began", `${JSON.stringify(wanted)} is deleted in this transaction; create it in the next one, or leave id out.`) } : wanted;
|
|
314
|
+
};
|
|
315
|
+
const create = (ctx, input, placement, path) => {
|
|
316
|
+
const id = newId(ctx, input.id, kindPrefix(input.kind), [
|
|
317
|
+
...path,
|
|
318
|
+
"shape",
|
|
319
|
+
"id"
|
|
320
|
+
]);
|
|
321
|
+
if (typeof id !== "string") return id.error;
|
|
322
|
+
const row = {
|
|
323
|
+
id,
|
|
324
|
+
z: keyAfter(topZ(ctx.shapes)),
|
|
325
|
+
group: null,
|
|
326
|
+
locked: false,
|
|
327
|
+
createdBy: ctx.actor,
|
|
328
|
+
updatedBy: ctx.actor
|
|
329
|
+
};
|
|
330
|
+
const empty = /* @__PURE__ */ new Set();
|
|
331
|
+
switch (input.kind) {
|
|
332
|
+
case "Arrow": {
|
|
333
|
+
if (placement !== void 0) return invalid([...path, "place"], "left out for a connector", "A connector's ends place it.");
|
|
334
|
+
const fromFailure = checkAnchor(ctx, input.from, id, [
|
|
335
|
+
...path,
|
|
336
|
+
"shape",
|
|
337
|
+
"from"
|
|
338
|
+
]);
|
|
339
|
+
if (fromFailure !== null) return fromFailure;
|
|
340
|
+
const toFailure = checkAnchor(ctx, input.to, id, [
|
|
341
|
+
...path,
|
|
342
|
+
"shape",
|
|
343
|
+
"to"
|
|
344
|
+
]);
|
|
345
|
+
if (toFailure !== null) return toFailure;
|
|
346
|
+
const draft = {
|
|
347
|
+
...row,
|
|
348
|
+
kind: "Arrow",
|
|
349
|
+
x: 0,
|
|
350
|
+
y: 0,
|
|
351
|
+
w: 0,
|
|
352
|
+
h: 0,
|
|
353
|
+
from: input.from,
|
|
354
|
+
to: input.to,
|
|
355
|
+
style: arrowStyle(input, DEFAULTS.arrow)
|
|
356
|
+
};
|
|
357
|
+
ctx.shapes.set(id, withRoute(ctx.shapes, draft));
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
case "Ink": {
|
|
361
|
+
if (input.points.length === 0) return invalid([
|
|
362
|
+
...path,
|
|
363
|
+
"shape",
|
|
364
|
+
"points"
|
|
365
|
+
], "at least one point", "Send the stroke's samples in board coordinates.");
|
|
366
|
+
const geometry = inkGeometry(input.points, INK_TOLERANCE);
|
|
367
|
+
const at = placement === void 0 ? {
|
|
368
|
+
x: geometry.x,
|
|
369
|
+
y: geometry.y
|
|
370
|
+
} : place(ctx, geometry.w, geometry.h, placement, empty, [...path, "place"]);
|
|
371
|
+
if ("error" in at) return at.error;
|
|
372
|
+
ctx.shapes.set(id, {
|
|
373
|
+
...row,
|
|
374
|
+
...geometry,
|
|
375
|
+
kind: "Ink",
|
|
376
|
+
x: at.x,
|
|
377
|
+
y: at.y,
|
|
378
|
+
style: inkStyle(input, DEFAULTS.ink)
|
|
379
|
+
});
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
case "Text": {
|
|
383
|
+
const sized = fitText(ctx.fonts, {
|
|
384
|
+
...row,
|
|
385
|
+
kind: "Text",
|
|
386
|
+
x: 0,
|
|
387
|
+
y: 0,
|
|
388
|
+
w: input.w ?? 0,
|
|
389
|
+
h: 0,
|
|
390
|
+
text: input.text,
|
|
391
|
+
style: textStyle(input, {
|
|
392
|
+
...DEFAULTS.text,
|
|
393
|
+
width: input.w === void 0 ? "auto" : "fixed"
|
|
394
|
+
})
|
|
395
|
+
});
|
|
396
|
+
const at = place(ctx, sized.w, sized.h, placement, empty, [...path, "place"]);
|
|
397
|
+
if ("error" in at) return at.error;
|
|
398
|
+
ctx.shapes.set(id, {
|
|
399
|
+
...sized,
|
|
400
|
+
x: at.x,
|
|
401
|
+
y: at.y
|
|
402
|
+
});
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
case "Sticky": {
|
|
406
|
+
const sized = fitText(ctx.fonts, {
|
|
407
|
+
...row,
|
|
408
|
+
kind: "Sticky",
|
|
409
|
+
x: 0,
|
|
410
|
+
y: 0,
|
|
411
|
+
w: round(Math.max(input.w ?? DEFAULTS.sticky.w, 16)),
|
|
412
|
+
h: round(Math.max(input.h ?? DEFAULTS.sticky.h, 16)),
|
|
413
|
+
text: input.text ?? "",
|
|
414
|
+
style: stickyStyle(input, DEFAULTS.sticky)
|
|
415
|
+
});
|
|
416
|
+
const at = place(ctx, sized.w, sized.h, placement, empty, [...path, "place"]);
|
|
417
|
+
if ("error" in at) return at.error;
|
|
418
|
+
ctx.shapes.set(id, {
|
|
419
|
+
...sized,
|
|
420
|
+
x: at.x,
|
|
421
|
+
y: at.y
|
|
422
|
+
});
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
case "Box": {
|
|
426
|
+
const sized = fitText(ctx.fonts, {
|
|
427
|
+
...row,
|
|
428
|
+
kind: "Box",
|
|
429
|
+
x: 0,
|
|
430
|
+
y: 0,
|
|
431
|
+
w: round(Math.max(input.w ?? DEFAULTS.box.w, 16)),
|
|
432
|
+
h: round(Math.max(input.h ?? DEFAULTS.box.h, 16)),
|
|
433
|
+
text: input.text ?? "",
|
|
434
|
+
style: boxStyle(input, DEFAULTS.box)
|
|
435
|
+
});
|
|
436
|
+
const at = place(ctx, sized.w, sized.h, placement, empty, [...path, "place"]);
|
|
437
|
+
if ("error" in at) return at.error;
|
|
438
|
+
ctx.shapes.set(id, {
|
|
439
|
+
...sized,
|
|
440
|
+
x: at.x,
|
|
441
|
+
y: at.y
|
|
442
|
+
});
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
case "Image": {
|
|
446
|
+
const w = round(Math.max(input.w, 16));
|
|
447
|
+
const h = round(Math.max(input.h, 16));
|
|
448
|
+
const at = place(ctx, w, h, placement, empty, [...path, "place"]);
|
|
449
|
+
if ("error" in at) return at.error;
|
|
450
|
+
ctx.shapes.set(id, {
|
|
451
|
+
...row,
|
|
452
|
+
kind: "Image",
|
|
453
|
+
x: at.x,
|
|
454
|
+
y: at.y,
|
|
455
|
+
w,
|
|
456
|
+
h,
|
|
457
|
+
asset: input.asset,
|
|
458
|
+
alt: input.alt ?? ""
|
|
459
|
+
});
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
case "Timestamp": {
|
|
463
|
+
const w = round(Math.max(input.w ?? DEFAULTS.timestamp.w, 16));
|
|
464
|
+
const h = round(Math.max(input.h ?? DEFAULTS.timestamp.h, 16));
|
|
465
|
+
const at = place(ctx, w, h, placement, empty, [...path, "place"]);
|
|
466
|
+
if ("error" in at) return at.error;
|
|
467
|
+
ctx.shapes.set(id, {
|
|
468
|
+
...row,
|
|
469
|
+
kind: "Timestamp",
|
|
470
|
+
x: at.x,
|
|
471
|
+
y: at.y,
|
|
472
|
+
w,
|
|
473
|
+
h,
|
|
474
|
+
at: input.at
|
|
475
|
+
});
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
const PATCH_KEYS = {
|
|
481
|
+
Arrow: [
|
|
482
|
+
"color",
|
|
483
|
+
"start",
|
|
484
|
+
"end",
|
|
485
|
+
"route",
|
|
486
|
+
"dash",
|
|
487
|
+
"from",
|
|
488
|
+
"to"
|
|
489
|
+
],
|
|
490
|
+
Box: [
|
|
491
|
+
"x",
|
|
492
|
+
"y",
|
|
493
|
+
"w",
|
|
494
|
+
"h",
|
|
495
|
+
"text",
|
|
496
|
+
"color",
|
|
497
|
+
"size",
|
|
498
|
+
"align",
|
|
499
|
+
"form",
|
|
500
|
+
"fill",
|
|
501
|
+
"dash"
|
|
502
|
+
],
|
|
503
|
+
Ink: [
|
|
504
|
+
"x",
|
|
505
|
+
"y",
|
|
506
|
+
"w",
|
|
507
|
+
"h",
|
|
508
|
+
"color",
|
|
509
|
+
"size",
|
|
510
|
+
"brush"
|
|
511
|
+
],
|
|
512
|
+
Sticky: [
|
|
513
|
+
"x",
|
|
514
|
+
"y",
|
|
515
|
+
"w",
|
|
516
|
+
"h",
|
|
517
|
+
"text",
|
|
518
|
+
"color",
|
|
519
|
+
"size",
|
|
520
|
+
"align"
|
|
521
|
+
],
|
|
522
|
+
Text: [
|
|
523
|
+
"x",
|
|
524
|
+
"y",
|
|
525
|
+
"w",
|
|
526
|
+
"text",
|
|
527
|
+
"color",
|
|
528
|
+
"size",
|
|
529
|
+
"align"
|
|
530
|
+
],
|
|
531
|
+
Image: [
|
|
532
|
+
"x",
|
|
533
|
+
"y",
|
|
534
|
+
"w",
|
|
535
|
+
"h",
|
|
536
|
+
"asset",
|
|
537
|
+
"alt"
|
|
538
|
+
],
|
|
539
|
+
Timestamp: [
|
|
540
|
+
"x",
|
|
541
|
+
"y",
|
|
542
|
+
"w",
|
|
543
|
+
"h"
|
|
544
|
+
]
|
|
545
|
+
};
|
|
546
|
+
const update = (ctx, id, patch, path) => {
|
|
547
|
+
const found = editable(ctx, id, [...path, "id"]);
|
|
548
|
+
if ("error" in found) return found.error;
|
|
549
|
+
const shape = found.shape;
|
|
550
|
+
const allowed = PATCH_KEYS[shape.kind];
|
|
551
|
+
for (const key of Object.keys(patch)) if (!allowed.includes(key)) return invalid([
|
|
552
|
+
...path,
|
|
553
|
+
"patch",
|
|
554
|
+
key
|
|
555
|
+
], `left out for ${article(shape.kind)} ${shape.kind}`, `${article(shape.kind) === "a" ? "A" : "An"} ${shape.kind} takes ${allowed.join(", ")}.`);
|
|
556
|
+
let next;
|
|
557
|
+
switch (shape.kind) {
|
|
558
|
+
case "Arrow":
|
|
559
|
+
for (const end of ["from", "to"]) {
|
|
560
|
+
const anchor = patch[end];
|
|
561
|
+
if (anchor === void 0) continue;
|
|
562
|
+
const failure = checkAnchor(ctx, anchor, id, [
|
|
563
|
+
...path,
|
|
564
|
+
"patch",
|
|
565
|
+
end
|
|
566
|
+
]);
|
|
567
|
+
if (failure !== null) return failure;
|
|
568
|
+
}
|
|
569
|
+
next = withRoute(ctx.shapes, {
|
|
570
|
+
...shape,
|
|
571
|
+
from: patch.from ?? shape.from,
|
|
572
|
+
to: patch.to ?? shape.to,
|
|
573
|
+
style: arrowStyle(patch, shape.style)
|
|
574
|
+
});
|
|
575
|
+
break;
|
|
576
|
+
case "Ink":
|
|
577
|
+
next = {
|
|
578
|
+
...scaleInk(shape, patch.w ?? shape.w, patch.h ?? shape.h),
|
|
579
|
+
x: round(patch.x ?? shape.x),
|
|
580
|
+
y: round(patch.y ?? shape.y),
|
|
581
|
+
style: inkStyle(patch, shape.style)
|
|
582
|
+
};
|
|
583
|
+
break;
|
|
584
|
+
case "Text":
|
|
585
|
+
next = fitText(ctx.fonts, {
|
|
586
|
+
...shape,
|
|
587
|
+
x: round(patch.x ?? shape.x),
|
|
588
|
+
y: round(patch.y ?? shape.y),
|
|
589
|
+
w: patch.w === void 0 ? shape.w : round(Math.max(patch.w, 16)),
|
|
590
|
+
text: patch.text ?? shape.text,
|
|
591
|
+
style: textStyle(patch, {
|
|
592
|
+
...shape.style,
|
|
593
|
+
width: patch.w === void 0 ? shape.style.width : "fixed"
|
|
594
|
+
})
|
|
595
|
+
});
|
|
596
|
+
break;
|
|
597
|
+
case "Sticky":
|
|
598
|
+
next = fitText(ctx.fonts, {
|
|
599
|
+
...shape,
|
|
600
|
+
x: round(patch.x ?? shape.x),
|
|
601
|
+
y: round(patch.y ?? shape.y),
|
|
602
|
+
w: round(Math.max(patch.w ?? shape.w, 16)),
|
|
603
|
+
h: round(Math.max(patch.h ?? shape.h, 16)),
|
|
604
|
+
text: patch.text ?? shape.text,
|
|
605
|
+
style: stickyStyle(patch, shape.style)
|
|
606
|
+
});
|
|
607
|
+
break;
|
|
608
|
+
case "Box":
|
|
609
|
+
next = fitText(ctx.fonts, {
|
|
610
|
+
...shape,
|
|
611
|
+
x: round(patch.x ?? shape.x),
|
|
612
|
+
y: round(patch.y ?? shape.y),
|
|
613
|
+
w: round(Math.max(patch.w ?? shape.w, 16)),
|
|
614
|
+
h: round(Math.max(patch.h ?? shape.h, 16)),
|
|
615
|
+
text: patch.text ?? shape.text,
|
|
616
|
+
style: boxStyle(patch, shape.style)
|
|
617
|
+
});
|
|
618
|
+
break;
|
|
619
|
+
case "Image":
|
|
620
|
+
next = {
|
|
621
|
+
...shape,
|
|
622
|
+
x: round(patch.x ?? shape.x),
|
|
623
|
+
y: round(patch.y ?? shape.y),
|
|
624
|
+
w: round(Math.max(patch.w ?? shape.w, 16)),
|
|
625
|
+
h: round(Math.max(patch.h ?? shape.h, 16)),
|
|
626
|
+
asset: patch.asset ?? shape.asset,
|
|
627
|
+
alt: patch.alt ?? shape.alt
|
|
628
|
+
};
|
|
629
|
+
break;
|
|
630
|
+
case "Timestamp": next = {
|
|
631
|
+
...shape,
|
|
632
|
+
x: round(patch.x ?? shape.x),
|
|
633
|
+
y: round(patch.y ?? shape.y),
|
|
634
|
+
w: round(Math.max(patch.w ?? shape.w, 16)),
|
|
635
|
+
h: round(Math.max(patch.h ?? shape.h, 16))
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
put(ctx, next);
|
|
639
|
+
return null;
|
|
640
|
+
};
|
|
641
|
+
const translateAll = (ctx, shapes, dx, dy) => {
|
|
642
|
+
if (dx === 0 && dy === 0) return;
|
|
643
|
+
for (const shape of shapes) put(ctx, translate(shape, dx, dy));
|
|
644
|
+
};
|
|
645
|
+
const move = (ctx, ids, placement, path) => {
|
|
646
|
+
const found = editableAll(ctx, ids, [...path, "ids"]);
|
|
647
|
+
if ("error" in found) return found.error;
|
|
648
|
+
const box = boundsOf(ctx.shapes, found.shapes.map((shape) => shape.id));
|
|
649
|
+
if (box === null) return null;
|
|
650
|
+
if (placement._tag === "Beside" && ids.includes(placement.shape)) return invalid([
|
|
651
|
+
...path,
|
|
652
|
+
"place",
|
|
653
|
+
"shape"
|
|
654
|
+
], "a shape that is not being moved", "Place the shapes beside one that stays put.");
|
|
655
|
+
const at = place(ctx, box.w, box.h, placement, new Set(ids), [...path, "place"]);
|
|
656
|
+
if ("error" in at) return at.error;
|
|
657
|
+
translateAll(ctx, found.shapes, at.x - box.x, at.y - box.y);
|
|
658
|
+
return null;
|
|
659
|
+
};
|
|
660
|
+
const resize = (ctx, ids, to, path) => {
|
|
661
|
+
const found = editableAll(ctx, ids, [...path, "ids"]);
|
|
662
|
+
if ("error" in found) return found.error;
|
|
663
|
+
const from = boundsOf(ctx.shapes, found.shapes.map((shape) => shape.id));
|
|
664
|
+
if (from === null) return null;
|
|
665
|
+
for (const shape of found.shapes) put(ctx, scale(ctx.fonts, shape, from, to));
|
|
666
|
+
return null;
|
|
667
|
+
};
|
|
668
|
+
/** Connectors bound to removed shapes keep their last end points. */
|
|
669
|
+
const detach = (ctx, removed) => {
|
|
670
|
+
for (const shape of [...ctx.shapes.values()]) {
|
|
671
|
+
if (shape.kind !== "Arrow") continue;
|
|
672
|
+
const fromGone = shape.from._tag === "Shape" && removed.has(shape.from.id);
|
|
673
|
+
const toGone = shape.to._tag === "Shape" && removed.has(shape.to.id);
|
|
674
|
+
if (!fromGone && !toGone) continue;
|
|
675
|
+
const r = route(ctx.shapes, shape);
|
|
676
|
+
const start = routeStart(r);
|
|
677
|
+
const end = routeEnd(r);
|
|
678
|
+
put(ctx, {
|
|
679
|
+
...shape,
|
|
680
|
+
from: fromGone ? {
|
|
681
|
+
_tag: "Point",
|
|
682
|
+
x: start.x,
|
|
683
|
+
y: start.y
|
|
684
|
+
} : shape.from,
|
|
685
|
+
to: toGone ? {
|
|
686
|
+
_tag: "Point",
|
|
687
|
+
x: end.x,
|
|
688
|
+
y: end.y
|
|
689
|
+
} : shape.to
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
const remove = (ctx, ids, path) => {
|
|
694
|
+
const found = editableAll(ctx, ids, [...path, "ids"]);
|
|
695
|
+
if ("error" in found) return found.error;
|
|
696
|
+
const removed = new Set(found.shapes.map((shape) => shape.id));
|
|
697
|
+
detach(ctx, removed);
|
|
698
|
+
for (const id of removed) ctx.shapes.delete(id);
|
|
699
|
+
return null;
|
|
700
|
+
};
|
|
701
|
+
const reorder = (ctx, ids, to, path) => {
|
|
702
|
+
const found = editableAll(ctx, ids, [...path, "ids"]);
|
|
703
|
+
if ("error" in found) return found.error;
|
|
704
|
+
const moving = new Set(found.shapes.map((shape) => shape.id));
|
|
705
|
+
const selected = [...found.shapes].sort(compareShapes);
|
|
706
|
+
if (to === "front") {
|
|
707
|
+
let key = topZ(ctx.shapes);
|
|
708
|
+
for (const shape of selected) {
|
|
709
|
+
key = keyAfter(key);
|
|
710
|
+
put(ctx, {
|
|
711
|
+
...shape,
|
|
712
|
+
z: key
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
if (to === "back") {
|
|
718
|
+
let key = bottomZ(ctx.shapes);
|
|
719
|
+
for (const shape of [...selected].reverse()) {
|
|
720
|
+
key = keyBefore(key);
|
|
721
|
+
put(ctx, {
|
|
722
|
+
...shape,
|
|
723
|
+
z: key
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
const forward = to === "forward";
|
|
729
|
+
const order = [...ordered(ctx.shapes)];
|
|
730
|
+
const boxes = new Map(order.map((shape) => [shape.id, bounds(ctx.shapes, shape)]));
|
|
731
|
+
const at = (shape) => {
|
|
732
|
+
let lo = 0;
|
|
733
|
+
let hi = order.length;
|
|
734
|
+
while (lo < hi) {
|
|
735
|
+
const mid = lo + hi >> 1;
|
|
736
|
+
const other = order[mid];
|
|
737
|
+
if (other !== void 0 && compareShapes(other, shape) < 0) lo = mid + 1;
|
|
738
|
+
else hi = mid;
|
|
739
|
+
}
|
|
740
|
+
return lo;
|
|
741
|
+
};
|
|
742
|
+
const step = forward ? 1 : -1;
|
|
743
|
+
for (const shape of forward ? [...selected].reverse() : selected) {
|
|
744
|
+
const latest = ctx.shapes.get(shape.id) ?? shape;
|
|
745
|
+
const index = at(latest);
|
|
746
|
+
const box = boxes.get(shape.id);
|
|
747
|
+
let target = -1;
|
|
748
|
+
for (let i = index + step; box !== void 0 && i >= 0 && i < order.length; i += step) {
|
|
749
|
+
const other = order[i];
|
|
750
|
+
const b = other === void 0 ? void 0 : boxes.get(other.id);
|
|
751
|
+
if (other === void 0 || b === void 0 || moving.has(other.id)) continue;
|
|
752
|
+
if (overlaps(b, box)) {
|
|
753
|
+
target = i;
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
const pivot = order[target];
|
|
758
|
+
if (pivot === void 0) continue;
|
|
759
|
+
const tied = [];
|
|
760
|
+
let past = target + step;
|
|
761
|
+
for (; order[past]?.z === pivot.z; past += step) tied.push(past);
|
|
762
|
+
const bound = order[past]?.z ?? null;
|
|
763
|
+
const beyondOf = (key) => bound === null ? forward ? keyAfter(key) : keyBefore(key) : forward ? keyBetween(key, bound) : keyBetween(bound, key);
|
|
764
|
+
const z = beyondOf(pivot.z);
|
|
765
|
+
let last = z;
|
|
766
|
+
for (const i of tied) {
|
|
767
|
+
const other = order[i];
|
|
768
|
+
if (other === void 0) continue;
|
|
769
|
+
last = beyondOf(last);
|
|
770
|
+
order[i] = {
|
|
771
|
+
...other,
|
|
772
|
+
z: last
|
|
773
|
+
};
|
|
774
|
+
ctx.shapes.set(other.id, {
|
|
775
|
+
...ctx.shapes.get(other.id) ?? other,
|
|
776
|
+
z: last
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
put(ctx, {
|
|
780
|
+
...latest,
|
|
781
|
+
z
|
|
782
|
+
});
|
|
783
|
+
const moved = ctx.shapes.get(shape.id) ?? latest;
|
|
784
|
+
order.splice(index, 1);
|
|
785
|
+
order.splice(at(moved), 0, moved);
|
|
786
|
+
}
|
|
787
|
+
return null;
|
|
788
|
+
};
|
|
789
|
+
const align = (ctx, ids, to, path) => {
|
|
790
|
+
const found = editableAll(ctx, ids, [...path, "ids"], { min: 2 });
|
|
791
|
+
if ("error" in found) return found.error;
|
|
792
|
+
const all = boundsOf(ctx.shapes, found.shapes.map((shape) => shape.id));
|
|
793
|
+
if (all === null) return null;
|
|
794
|
+
for (const shape of found.shapes) {
|
|
795
|
+
const b = bounds(ctx.shapes, shape);
|
|
796
|
+
const dx = to === "left" ? all.x - b.x : to === "right" ? all.x + all.w - (b.x + b.w) : to === "center" ? all.x + all.w / 2 - (b.x + b.w / 2) : 0;
|
|
797
|
+
const dy = to === "top" ? all.y - b.y : to === "bottom" ? all.y + all.h - (b.y + b.h) : to === "middle" ? all.y + all.h / 2 - (b.y + b.h / 2) : 0;
|
|
798
|
+
translateAll(ctx, [ctx.shapes.get(shape.id) ?? shape], dx, dy);
|
|
799
|
+
}
|
|
800
|
+
return null;
|
|
801
|
+
};
|
|
802
|
+
const distribute = (ctx, ids, axis, path) => {
|
|
803
|
+
const found = editableAll(ctx, ids, [...path, "ids"], { min: 3 });
|
|
804
|
+
if ("error" in found) return found.error;
|
|
805
|
+
const horizontal = axis === "horizontal";
|
|
806
|
+
const items = found.shapes.map((shape) => ({
|
|
807
|
+
shape,
|
|
808
|
+
box: bounds(ctx.shapes, shape)
|
|
809
|
+
})).sort((a, b) => {
|
|
810
|
+
return (horizontal ? a.box.x + a.box.w / 2 : a.box.y + a.box.h / 2) - (horizontal ? b.box.x + b.box.w / 2 : b.box.y + b.box.h / 2) || compareShapes(a.shape, b.shape);
|
|
811
|
+
});
|
|
812
|
+
const first = items[0];
|
|
813
|
+
const last = items[items.length - 1];
|
|
814
|
+
if (first === void 0 || last === void 0) return null;
|
|
815
|
+
const start = horizontal ? first.box.x : first.box.y;
|
|
816
|
+
const end = horizontal ? last.box.x + last.box.w : last.box.y + last.box.h;
|
|
817
|
+
const total = items.reduce((sum, item) => sum + (horizontal ? item.box.w : item.box.h), 0);
|
|
818
|
+
const gap = (end - start - total) / (items.length - 1);
|
|
819
|
+
let cursor = start;
|
|
820
|
+
for (const item of items) {
|
|
821
|
+
const at = horizontal ? item.box.x : item.box.y;
|
|
822
|
+
const delta = round(cursor - at);
|
|
823
|
+
translateAll(ctx, [ctx.shapes.get(item.shape.id) ?? item.shape], horizontal ? delta : 0, horizontal ? 0 : delta);
|
|
824
|
+
cursor += (horizontal ? item.box.w : item.box.h) + gap;
|
|
825
|
+
}
|
|
826
|
+
return null;
|
|
827
|
+
};
|
|
828
|
+
/**
|
|
829
|
+
* `source` copied by (dx, dy), its connector ends bound to the copies in
|
|
830
|
+
* `ids` (original id → copy id); an end bound to a shape not copied stays
|
|
831
|
+
* bound only when the copy does not move, and is a free point otherwise.
|
|
832
|
+
*/
|
|
833
|
+
const copyShape = (shapes, source, ids, dx, dy) => {
|
|
834
|
+
const moved = {
|
|
835
|
+
...source,
|
|
836
|
+
x: round(source.x + dx),
|
|
837
|
+
y: round(source.y + dy)
|
|
838
|
+
};
|
|
839
|
+
if (moved.kind !== "Arrow") return moved;
|
|
840
|
+
const rebind = (anchor, fallback) => {
|
|
841
|
+
if (anchor._tag === "Point") return {
|
|
842
|
+
_tag: "Point",
|
|
843
|
+
x: round(anchor.x + dx),
|
|
844
|
+
y: round(anchor.y + dy)
|
|
845
|
+
};
|
|
846
|
+
const copy = ids.get(anchor.id);
|
|
847
|
+
if (copy !== void 0) return {
|
|
848
|
+
...anchor,
|
|
849
|
+
id: copy
|
|
850
|
+
};
|
|
851
|
+
const target = shapes.get(anchor.id);
|
|
852
|
+
return target !== void 0 && target.kind !== "Arrow" && dx === 0 && dy === 0 ? anchor : {
|
|
853
|
+
_tag: "Point",
|
|
854
|
+
x: round(fallback.x + dx),
|
|
855
|
+
y: round(fallback.y + dy)
|
|
856
|
+
};
|
|
857
|
+
};
|
|
858
|
+
return {
|
|
859
|
+
...moved,
|
|
860
|
+
from: rebind(moved.from, {
|
|
861
|
+
x: source.x,
|
|
862
|
+
y: source.y
|
|
863
|
+
}),
|
|
864
|
+
to: rebind(moved.to, {
|
|
865
|
+
x: source.x + source.w,
|
|
866
|
+
y: source.y + source.h
|
|
867
|
+
})
|
|
868
|
+
};
|
|
869
|
+
};
|
|
870
|
+
/**
|
|
871
|
+
* Inserts copies of `sources`, offset by (dx, dy), on top of the board. Ids
|
|
872
|
+
* come from `as` in order or are generated. Connector ends bound to a copied
|
|
873
|
+
* shape bind to its copy; ends bound to a shape that is not on the board
|
|
874
|
+
* become free. Groups copied whole get a new group id.
|
|
875
|
+
*/
|
|
876
|
+
const insertCopies = (ctx, sources, dx, dy, as, path) => {
|
|
877
|
+
if (as !== void 0 && as.length !== sources.length) return invalid([...path, "as"], `${sources.length} ids, one per copied shape`, "Pass one fresh id per copy, or leave as out.");
|
|
878
|
+
const ids = /* @__PURE__ */ new Map();
|
|
879
|
+
const taken = /* @__PURE__ */ new Set();
|
|
880
|
+
for (const [index, source] of sources.entries()) {
|
|
881
|
+
const wanted = as?.[index];
|
|
882
|
+
if (wanted !== void 0 && taken.has(wanted)) return invalid([
|
|
883
|
+
...path,
|
|
884
|
+
"as",
|
|
885
|
+
index
|
|
886
|
+
], "an id not used earlier in as", "Give every copy its own id.");
|
|
887
|
+
const id = newId(ctx, wanted, kindPrefix(source.kind), [
|
|
888
|
+
...path,
|
|
889
|
+
"as",
|
|
890
|
+
index
|
|
891
|
+
]);
|
|
892
|
+
if (typeof id !== "string") return id.error;
|
|
893
|
+
taken.add(id);
|
|
894
|
+
ids.set(source.id, id);
|
|
895
|
+
}
|
|
896
|
+
const groups = /* @__PURE__ */ new Map();
|
|
897
|
+
for (const source of sources) if (source.group !== null && !groups.has(source.group)) groups.set(source.group, newGroup(ctx));
|
|
898
|
+
let z = topZ(ctx.shapes);
|
|
899
|
+
const copies = [];
|
|
900
|
+
for (const source of [...sources].sort(compareShapes)) {
|
|
901
|
+
z = keyAfter(z);
|
|
902
|
+
const row = {
|
|
903
|
+
id: ids.get(source.id) ?? source.id,
|
|
904
|
+
z,
|
|
905
|
+
group: source.group === null ? null : groups.get(source.group) ?? null,
|
|
906
|
+
locked: false,
|
|
907
|
+
createdBy: ctx.actor,
|
|
908
|
+
updatedBy: ctx.actor
|
|
909
|
+
};
|
|
910
|
+
copies.push({
|
|
911
|
+
...copyShape(ctx.shapes, source, ids, dx, dy),
|
|
912
|
+
...row
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
for (const copy of copies) ctx.shapes.set(copy.id, copy);
|
|
916
|
+
refreshRoutes(ctx.shapes, copies.filter((copy) => copy.kind === "Arrow").map((copy) => copy.id));
|
|
917
|
+
return null;
|
|
918
|
+
};
|
|
919
|
+
const duplicate = (ctx, command, path) => {
|
|
920
|
+
const sources = [];
|
|
921
|
+
const seen = /* @__PURE__ */ new Set();
|
|
922
|
+
for (const [index, id] of command.ids.entries()) {
|
|
923
|
+
if (seen.has(id)) continue;
|
|
924
|
+
seen.add(id);
|
|
925
|
+
const found = findShape(ctx, id, [
|
|
926
|
+
...path,
|
|
927
|
+
"ids",
|
|
928
|
+
index
|
|
929
|
+
]);
|
|
930
|
+
if ("error" in found) return found.error;
|
|
931
|
+
sources.push(found.shape);
|
|
932
|
+
}
|
|
933
|
+
if (sources.length === 0) return invalid([...path, "ids"], "at least one shape id", "List the ids of shapes on the board.");
|
|
934
|
+
return insertCopies(ctx, sources, command.dx ?? DEFAULTS.duplicate, command.dy ?? DEFAULTS.duplicate, command.as, path);
|
|
935
|
+
};
|
|
936
|
+
const paste = (ctx, clipboard, placement, as, path) => {
|
|
937
|
+
if (clipboard.shapes.length === 0) return invalid([
|
|
938
|
+
...path,
|
|
939
|
+
"clipboard",
|
|
940
|
+
"shapes"
|
|
941
|
+
], "at least one shape", "Paste a clipboard copied from a non-empty selection.");
|
|
942
|
+
const unique = /* @__PURE__ */ new Set();
|
|
943
|
+
for (const [index, shape] of clipboard.shapes.entries()) {
|
|
944
|
+
if (unique.has(shape.id)) return invalid([
|
|
945
|
+
...path,
|
|
946
|
+
"clipboard",
|
|
947
|
+
"shapes",
|
|
948
|
+
index,
|
|
949
|
+
"id"
|
|
950
|
+
], "an id not used earlier in the clipboard", "Paste the clipboard exactly as Copy produced it.");
|
|
951
|
+
unique.add(shape.id);
|
|
952
|
+
}
|
|
953
|
+
const local = toMap(clipboard.shapes);
|
|
954
|
+
const box = boundsOf(local, local.keys());
|
|
955
|
+
if (box === null) return null;
|
|
956
|
+
let dx = 0;
|
|
957
|
+
let dy = 0;
|
|
958
|
+
if (placement !== void 0) {
|
|
959
|
+
const at = place(ctx, box.w, box.h, placement, /* @__PURE__ */ new Set(), [...path, "place"]);
|
|
960
|
+
if ("error" in at) return at.error;
|
|
961
|
+
dx = at.x - box.x;
|
|
962
|
+
dy = at.y - box.y;
|
|
963
|
+
}
|
|
964
|
+
return insertCopies(ctx, clipboard.shapes, dx, dy, as, path);
|
|
965
|
+
};
|
|
966
|
+
const group = (ctx, ids, wanted, path) => {
|
|
967
|
+
const found = editableAll(ctx, ids, [...path, "ids"], { min: 2 });
|
|
968
|
+
if ("error" in found) return found.error;
|
|
969
|
+
const id = wanted ?? newGroup(ctx);
|
|
970
|
+
for (const shape of found.shapes) put(ctx, {
|
|
971
|
+
...shape,
|
|
972
|
+
group: id
|
|
973
|
+
});
|
|
974
|
+
return null;
|
|
975
|
+
};
|
|
976
|
+
const ungroup = (ctx, ids, path) => {
|
|
977
|
+
const found = editableAll(ctx, ids, [...path, "ids"]);
|
|
978
|
+
if ("error" in found) return found.error;
|
|
979
|
+
for (const shape of found.shapes) put(ctx, {
|
|
980
|
+
...shape,
|
|
981
|
+
group: null
|
|
982
|
+
});
|
|
983
|
+
return null;
|
|
984
|
+
};
|
|
985
|
+
const lock = (ctx, ids, value, path) => {
|
|
986
|
+
const found = editableAll(ctx, ids, [...path, "ids"], { ignoreLock: true });
|
|
987
|
+
if ("error" in found) return found.error;
|
|
988
|
+
for (const shape of found.shapes) put(ctx, {
|
|
989
|
+
...shape,
|
|
990
|
+
locked: value
|
|
991
|
+
});
|
|
992
|
+
return null;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Why `target` has no record: expired when the board has dropped older
|
|
996
|
+
* records (its head is past what it keeps), else not found.
|
|
997
|
+
*/
|
|
998
|
+
const missingRecord = (board, h, target, path) => board.head > board.transactions.length ? historyExpired([...path, "transaction"], target, board.transactions[0]?.seq ?? board.head + 1) : transactionNotFound([...path, "transaction"], target, h.records.keys());
|
|
999
|
+
const accept = (ctx, target, path) => {
|
|
1000
|
+
const h = history(ctx.board);
|
|
1001
|
+
const record = h.records.get(target);
|
|
1002
|
+
if (record === void 0) return missingRecord(ctx.board, h, target, path);
|
|
1003
|
+
if (record.mode !== "propose") return invalid([...path, "transaction"], "the id of a proposal", `${JSON.stringify(target)} was committed, not proposed.`);
|
|
1004
|
+
const resolved = h.resolvedBy.get(target) ?? (ctx.accepts.includes(target) || ctx.reverts.includes(target) ? {
|
|
1005
|
+
by: ctx.transaction.id,
|
|
1006
|
+
as: "accepted"
|
|
1007
|
+
} : void 0);
|
|
1008
|
+
if (resolved !== void 0) return invalid([...path, "transaction"], "a pending proposal", `${JSON.stringify(target)} was already ${resolved.as} by ${JSON.stringify(resolved.by)}.`);
|
|
1009
|
+
for (const change of record.changes) {
|
|
1010
|
+
const id = change.op === "set" ? change.shape.id : change.id;
|
|
1011
|
+
const by = ctx.claims.get(id);
|
|
1012
|
+
if (by !== void 0 && ctx.shapes.has(id)) return claimed([...path, "transaction"], id, by, ctx.actor);
|
|
1013
|
+
if (ctx.shapes.get(id)?.locked === true && !(change.op === "patch" && (change.fields.locked === false || patchedFields(change.fields).every((f) => f === "locked" || f === "updatedBy")))) return locked([...path, "transaction"], id);
|
|
1014
|
+
}
|
|
1015
|
+
const clash = applyProposal(ctx.shapes, record);
|
|
1016
|
+
if (clash !== null) return invalid([...path, "transaction"], "a proposal whose new shapes' ids are free", `${JSON.stringify(clash)} now exists; revert the proposal and propose again.`);
|
|
1017
|
+
ctx.accepts.push(target);
|
|
1018
|
+
return null;
|
|
1019
|
+
};
|
|
1020
|
+
/**
|
|
1021
|
+
* Shapes a revert of `record` leaves as they are, as a fresh edit would:
|
|
1022
|
+
* those `held` (claimed by another connection) and those locked, unless the
|
|
1023
|
+
* revert only locks or unlocks them.
|
|
1024
|
+
*/
|
|
1025
|
+
const revertSkips = (shapes, record, held) => {
|
|
1026
|
+
const skips = /* @__PURE__ */ new Set();
|
|
1027
|
+
for (const [index, change] of record.changes.entries()) {
|
|
1028
|
+
const id = change.op === "set" ? change.shape.id : change.id;
|
|
1029
|
+
const current = shapes.get(id);
|
|
1030
|
+
if (current === void 0) continue;
|
|
1031
|
+
const inverse = record.inverse[index];
|
|
1032
|
+
const lockOnly = inverse?.op === "patch" && (inverse.fields.locked === false || patchedFields(inverse.fields).every((f) => f === "locked" || f === "updatedBy"));
|
|
1033
|
+
if (held(id) || current.locked && !lockOnly) skips.add(id);
|
|
1034
|
+
}
|
|
1035
|
+
return skips;
|
|
1036
|
+
};
|
|
1037
|
+
/**
|
|
1038
|
+
* Restores what `record` changed, field by field, where nothing has changed
|
|
1039
|
+
* it since, except on `skips`. Returns whether it restored anything.
|
|
1040
|
+
*/
|
|
1041
|
+
const undoRecord = (ctx, record, skips) => {
|
|
1042
|
+
let restored = false;
|
|
1043
|
+
for (const [index, change] of record.changes.entries()) {
|
|
1044
|
+
const inverse = record.inverse[index];
|
|
1045
|
+
if (change.op === "set") {
|
|
1046
|
+
const current = ctx.shapes.get(change.shape.id);
|
|
1047
|
+
if (current !== void 0 && !skips.has(current.id) && jsonEqual(current, change.shape)) {
|
|
1048
|
+
ctx.shapes.delete(change.shape.id);
|
|
1049
|
+
restored = true;
|
|
1050
|
+
}
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
if (change.op === "remove") {
|
|
1054
|
+
if (inverse?.op === "set" && !ctx.shapes.has(change.id)) {
|
|
1055
|
+
ctx.shapes.set(change.id, inverse.shape);
|
|
1056
|
+
restored = true;
|
|
1057
|
+
}
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
const current = ctx.shapes.get(change.id);
|
|
1061
|
+
if (current === void 0 || skips.has(change.id) || inverse?.op !== "patch") continue;
|
|
1062
|
+
let restore = {};
|
|
1063
|
+
for (const field of patchedFields(change.fields)) {
|
|
1064
|
+
if (field === "updatedBy") continue;
|
|
1065
|
+
const written = pickField(change.fields, field);
|
|
1066
|
+
const old = pickField(inverse.fields, field);
|
|
1067
|
+
if (written !== null && old !== null && jsonEqual(currentField(current, field), written)) restore = {
|
|
1068
|
+
...restore,
|
|
1069
|
+
...old
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
if (Object.keys(restore).length === 0) continue;
|
|
1073
|
+
const next = patchShape(current, restore);
|
|
1074
|
+
if (next === null) continue;
|
|
1075
|
+
put(ctx, next.kind === "Sticky" || next.kind === "Box" || next.kind === "Text" ? fitText(ctx.fonts, next) : next);
|
|
1076
|
+
restored = true;
|
|
1077
|
+
}
|
|
1078
|
+
return restored;
|
|
1079
|
+
};
|
|
1080
|
+
const revert = (ctx, target, path) => {
|
|
1081
|
+
const h = history(ctx.board);
|
|
1082
|
+
const record = h.records.get(target);
|
|
1083
|
+
if (record === void 0) return missingRecord(ctx.board, h, target, path);
|
|
1084
|
+
const resolved = h.resolvedBy.get(target);
|
|
1085
|
+
if (resolved !== void 0 && resolved.as === "reverted") return invalid([...path, "transaction"], "a transaction that was not reverted", `${JSON.stringify(target)} was already reverted by ${JSON.stringify(resolved.by)}; revert that transaction to redo it.`);
|
|
1086
|
+
if (ctx.reverts.includes(target) || ctx.accepts.includes(target)) return invalid([...path, "transaction"], "a transaction this transaction does not already resolve", "Name each transaction once per Accept or Revert.");
|
|
1087
|
+
if (record.mode === "propose") {
|
|
1088
|
+
if (resolved !== void 0) return invalid([...path, "transaction"], "a pending proposal", `${JSON.stringify(target)} was accepted by ${JSON.stringify(resolved.by)}; revert that transaction instead.`);
|
|
1089
|
+
ctx.reverts.push(target);
|
|
1090
|
+
return null;
|
|
1091
|
+
}
|
|
1092
|
+
const skips = revertSkips(ctx.shapes, record, (id) => ctx.claims.has(id));
|
|
1093
|
+
if (!undoRecord(ctx, record, skips)) for (const id of skips) {
|
|
1094
|
+
const by = ctx.claims.get(id);
|
|
1095
|
+
return by === void 0 ? locked([...path, "transaction"], id) : claimed([...path, "transaction"], id, by, ctx.actor);
|
|
1096
|
+
}
|
|
1097
|
+
ctx.reverts.push(target);
|
|
1098
|
+
return null;
|
|
1099
|
+
};
|
|
1100
|
+
const run = (ctx, command, path) => {
|
|
1101
|
+
switch (command._tag) {
|
|
1102
|
+
case "Create": return create(ctx, command.shape, command.place, path);
|
|
1103
|
+
case "Update": return update(ctx, command.id, command.patch, path);
|
|
1104
|
+
case "Move": return move(ctx, command.ids, command.place, path);
|
|
1105
|
+
case "Resize": return resize(ctx, command.ids, command.box, path);
|
|
1106
|
+
case "Delete": return remove(ctx, command.ids, path);
|
|
1107
|
+
case "Reorder": return reorder(ctx, command.ids, command.to, path);
|
|
1108
|
+
case "Align": return align(ctx, command.ids, command.to, path);
|
|
1109
|
+
case "Distribute": return distribute(ctx, command.ids, command.axis, path);
|
|
1110
|
+
case "Duplicate": return duplicate(ctx, command, path);
|
|
1111
|
+
case "Paste": return paste(ctx, command.clipboard, command.place, command.as, path);
|
|
1112
|
+
case "Group": return group(ctx, command.ids, command.group, path);
|
|
1113
|
+
case "Ungroup": return ungroup(ctx, command.ids, path);
|
|
1114
|
+
case "Lock": return lock(ctx, command.ids, command.locked, path);
|
|
1115
|
+
case "Accept": return accept(ctx, command.transaction, path);
|
|
1116
|
+
case "Revert": return revert(ctx, command.transaction, path);
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
const tidyAnchor = (anchor) => anchor._tag === "Point" ? {
|
|
1120
|
+
...anchor,
|
|
1121
|
+
x: round(anchor.x),
|
|
1122
|
+
y: round(anchor.y)
|
|
1123
|
+
} : anchor.at === null ? anchor : {
|
|
1124
|
+
...anchor,
|
|
1125
|
+
at: roundPoint(anchor.at)
|
|
1126
|
+
};
|
|
1127
|
+
/** The shape with every number rounded, the one way the kernel stores numbers. */
|
|
1128
|
+
const tidy = (shape) => {
|
|
1129
|
+
const box = {
|
|
1130
|
+
x: round(shape.x),
|
|
1131
|
+
y: round(shape.y),
|
|
1132
|
+
w: round(shape.w),
|
|
1133
|
+
h: round(shape.h)
|
|
1134
|
+
};
|
|
1135
|
+
switch (shape.kind) {
|
|
1136
|
+
case "Arrow": return {
|
|
1137
|
+
...shape,
|
|
1138
|
+
...box,
|
|
1139
|
+
from: tidyAnchor(shape.from),
|
|
1140
|
+
to: tidyAnchor(shape.to)
|
|
1141
|
+
};
|
|
1142
|
+
case "Ink": return {
|
|
1143
|
+
...shape,
|
|
1144
|
+
...box,
|
|
1145
|
+
points: shape.points.map(roundPoint)
|
|
1146
|
+
};
|
|
1147
|
+
default: return {
|
|
1148
|
+
...shape,
|
|
1149
|
+
...box
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
};
|
|
1153
|
+
/** The first number of `shape` past `LIMITS`: its field, absolute value and limit. */
|
|
1154
|
+
const outside = (shape) => {
|
|
1155
|
+
const checks = [
|
|
1156
|
+
[
|
|
1157
|
+
"x",
|
|
1158
|
+
shape.x,
|
|
1159
|
+
LIMITS.coordinate
|
|
1160
|
+
],
|
|
1161
|
+
[
|
|
1162
|
+
"y",
|
|
1163
|
+
shape.y,
|
|
1164
|
+
LIMITS.coordinate
|
|
1165
|
+
],
|
|
1166
|
+
[
|
|
1167
|
+
"w",
|
|
1168
|
+
shape.w,
|
|
1169
|
+
LIMITS.size
|
|
1170
|
+
],
|
|
1171
|
+
[
|
|
1172
|
+
"h",
|
|
1173
|
+
shape.h,
|
|
1174
|
+
LIMITS.size
|
|
1175
|
+
]
|
|
1176
|
+
];
|
|
1177
|
+
if (shape.kind === "Arrow") {
|
|
1178
|
+
for (const [name, end] of [["from", shape.from], ["to", shape.to]]) if (end._tag === "Point") checks.push([
|
|
1179
|
+
`${name}.x`,
|
|
1180
|
+
end.x,
|
|
1181
|
+
LIMITS.coordinate
|
|
1182
|
+
], [
|
|
1183
|
+
`${name}.y`,
|
|
1184
|
+
end.y,
|
|
1185
|
+
LIMITS.coordinate
|
|
1186
|
+
]);
|
|
1187
|
+
}
|
|
1188
|
+
for (const [field, value, limit] of checks) if (Math.abs(value) > limit) return [
|
|
1189
|
+
field,
|
|
1190
|
+
Math.abs(value),
|
|
1191
|
+
limit
|
|
1192
|
+
];
|
|
1193
|
+
return null;
|
|
1194
|
+
};
|
|
1195
|
+
/**
|
|
1196
|
+
* Gives every key longer than `Z_KEY` and its neighbours fresh keys, in draw
|
|
1197
|
+
* order: the smallest window around it, grown by doubling, whose fresh keys
|
|
1198
|
+
* (bisected evenly between the keys just outside it) are at most half
|
|
1199
|
+
* `Z_KEY` long. The whole board always qualifies. Draw order is unchanged.
|
|
1200
|
+
*/
|
|
1201
|
+
const rekey = (shapes) => {
|
|
1202
|
+
let long = false;
|
|
1203
|
+
for (const shape of shapes.values()) long ||= shape.z.length > 32;
|
|
1204
|
+
if (!long) return;
|
|
1205
|
+
const order = [...ordered(shapes)];
|
|
1206
|
+
const spread = (a, b, count, out) => {
|
|
1207
|
+
if (count === 0) return;
|
|
1208
|
+
const mid = keyBetween(a, b);
|
|
1209
|
+
const left = count - 1 >> 1;
|
|
1210
|
+
spread(a, mid, left, out);
|
|
1211
|
+
out.push(mid);
|
|
1212
|
+
spread(mid, b, count - 1 - left, out);
|
|
1213
|
+
};
|
|
1214
|
+
for (let i = 0; i < order.length; i++) {
|
|
1215
|
+
if ((order[i]?.z.length ?? 0) <= 32) continue;
|
|
1216
|
+
for (let reach = 0;; reach = reach * 2 || 1) {
|
|
1217
|
+
const lo = Math.max(0, i - reach);
|
|
1218
|
+
const hi = Math.min(order.length - 1, i + reach);
|
|
1219
|
+
const before = order[lo - 1]?.z ?? null;
|
|
1220
|
+
const after = order[hi + 1]?.z ?? null;
|
|
1221
|
+
if (before !== null && after !== null && before >= after) continue;
|
|
1222
|
+
const keys = [];
|
|
1223
|
+
spread(before, after, hi - lo + 1, keys);
|
|
1224
|
+
if (keys.some((key) => key.length > 16) && (lo > 0 || hi < order.length - 1)) continue;
|
|
1225
|
+
keys.forEach((z, n) => {
|
|
1226
|
+
const shape = order[lo + n];
|
|
1227
|
+
if (shape === void 0) return;
|
|
1228
|
+
order[lo + n] = {
|
|
1229
|
+
...shape,
|
|
1230
|
+
z
|
|
1231
|
+
};
|
|
1232
|
+
shapes.set(shape.id, {
|
|
1233
|
+
...shape,
|
|
1234
|
+
z
|
|
1235
|
+
});
|
|
1236
|
+
});
|
|
1237
|
+
i = hi;
|
|
1238
|
+
break;
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
const planTransaction = (fonts, board, transaction, actor, presence, connection) => {
|
|
1243
|
+
if (board.head >= Number.MAX_SAFE_INTEGER) return { error: tooLarge(["board", "head"], "the board's head", Number.MAX_SAFE_INTEGER - 1, board.head, "Copy the shapes to a new board; this one has used every sequence number.") };
|
|
1244
|
+
let named = 0;
|
|
1245
|
+
for (const c of transaction.commands) named += ("ids" in c ? c.ids.length : 0) + ("as" in c ? c.as?.length ?? 0 : 0) + (c._tag === "Paste" ? c.clipboard.shapes.length : 0) + (c._tag === "Create" && c.shape.kind === "Ink" ? c.shape.points.length : 0);
|
|
1246
|
+
if (named > LIMITS.named) return { error: tooLarge(["transaction", "commands"], "the number of ids, pasted shapes and ink points its commands name", LIMITS.named, named) };
|
|
1247
|
+
if (board.transactions.some((record) => record.id === transaction.id)) return { error: invalid(["transaction", "id"], "an id no transaction on the board has", "Use a fresh transaction id for every transaction.") };
|
|
1248
|
+
if (transaction.mode === "propose") {
|
|
1249
|
+
const index = transaction.commands.findIndex((c) => c._tag === "Accept" || c._tag === "Revert");
|
|
1250
|
+
if (index !== -1) return { error: invalid([
|
|
1251
|
+
"transaction",
|
|
1252
|
+
"commands",
|
|
1253
|
+
index
|
|
1254
|
+
], "a command other than Accept or Revert in a proposal", "Accept and Revert run in commit mode.") };
|
|
1255
|
+
}
|
|
1256
|
+
const self = connection === null ? null : {
|
|
1257
|
+
actor,
|
|
1258
|
+
connection
|
|
1259
|
+
};
|
|
1260
|
+
const claims = /* @__PURE__ */ new Map();
|
|
1261
|
+
for (const [id, list] of claimants(presence, self, null)) {
|
|
1262
|
+
if (list[0] === void 0 || sameConnection(list[0], self)) continue;
|
|
1263
|
+
const others = list.filter((c) => !sameConnection(c, self)).map((c) => c.actor);
|
|
1264
|
+
claims.set(id, sortedIds([...new Set(others)]));
|
|
1265
|
+
}
|
|
1266
|
+
const original = toMap(board.shapes);
|
|
1267
|
+
const shapes = new Rows();
|
|
1268
|
+
for (const shape of board.shapes) shapes.set(shape.id, shape);
|
|
1269
|
+
const ctx = {
|
|
1270
|
+
fonts,
|
|
1271
|
+
actor,
|
|
1272
|
+
transaction,
|
|
1273
|
+
board,
|
|
1274
|
+
original,
|
|
1275
|
+
shapes,
|
|
1276
|
+
claims,
|
|
1277
|
+
accepts: [],
|
|
1278
|
+
reverts: [],
|
|
1279
|
+
next: 0,
|
|
1280
|
+
groups: null
|
|
1281
|
+
};
|
|
1282
|
+
for (const [index, command] of transaction.commands.entries()) {
|
|
1283
|
+
const path = [
|
|
1284
|
+
"transaction",
|
|
1285
|
+
"commands",
|
|
1286
|
+
index
|
|
1287
|
+
];
|
|
1288
|
+
shapes.dirty.clear();
|
|
1289
|
+
const failure = run(ctx, command, path);
|
|
1290
|
+
if (failure !== null) return { error: failure };
|
|
1291
|
+
const written = new Set(shapes.dirty);
|
|
1292
|
+
for (const id of written) {
|
|
1293
|
+
const shape = shapes.get(id);
|
|
1294
|
+
const tidied = shape === void 0 ? shape : tidy(shape);
|
|
1295
|
+
if (shape !== void 0 && tidied !== void 0 && !jsonEqual(shape, tidied)) shapes.set(id, tidied);
|
|
1296
|
+
}
|
|
1297
|
+
refreshRoutes(shapes, affectedArrows(shapes, written));
|
|
1298
|
+
for (const id of shapes.dirty) {
|
|
1299
|
+
const shape = shapes.get(id);
|
|
1300
|
+
const bad = shape === void 0 ? null : outside(shape);
|
|
1301
|
+
if (bad !== null) return { error: tooLarge(path, `the ${bad[0]} of ${JSON.stringify(id)} after it`, bad[2], bad[1], `Keep shapes within ${LIMITS.coordinate} of the origin and at most ${LIMITS.size} across, and a connector's ends within ${LIMITS.size} of each other: place them nearer, or make them smaller.`) };
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
if (ctx.shapes.size > LIMITS.shapes) return { error: tooLarge(["transaction"], "the number of shapes on the board after it", LIMITS.shapes, ctx.shapes.size) };
|
|
1305
|
+
if (transaction.mode === "commit") rekey(shapes);
|
|
1306
|
+
for (const shape of shapes.values()) if (shape.z.length > LIMITS.z && original.get(shape.id)?.z !== shape.z) return { error: tooLarge(["transaction"], `the length of the draw-order key of ${JSON.stringify(shape.id)}`, LIMITS.z, shape.z.length, "Commit the change instead: a commit re-keys draw order.") };
|
|
1307
|
+
const { changes, inverse } = diff(original, shapes);
|
|
1308
|
+
if (changes.length > LIMITS.shapes) return { error: tooLarge(["transaction"], "the number of shapes it changes", LIMITS.shapes, changes.length) };
|
|
1309
|
+
const record = {
|
|
1310
|
+
id: transaction.id,
|
|
1311
|
+
seq: board.head + 1,
|
|
1312
|
+
author: {
|
|
1313
|
+
principal: actor,
|
|
1314
|
+
via: transaction.via ?? null
|
|
1315
|
+
},
|
|
1316
|
+
reason: transaction.reason ?? null,
|
|
1317
|
+
mode: transaction.mode,
|
|
1318
|
+
changes,
|
|
1319
|
+
inverse,
|
|
1320
|
+
shapes: changes.map((change) => change.op === "set" ? change.shape.id : change.id),
|
|
1321
|
+
accepts: ctx.accepts,
|
|
1322
|
+
reverts: ctx.reverts
|
|
1323
|
+
};
|
|
1324
|
+
const size = recordBytes(record);
|
|
1325
|
+
if (size > HISTORY.recordBytes) return { error: tooLarge(["transaction"], "its record in bytes", HISTORY.recordBytes, size) };
|
|
1326
|
+
if (transaction.mode === "propose") {
|
|
1327
|
+
const pending = pendingProposals(board, history(board));
|
|
1328
|
+
if (pending.length + 1 > HISTORY.proposals) return { error: tooLarge(["transaction", "mode"], "the number of pending proposals", HISTORY.proposals, pending.length + 1, "Accept or reject pending proposals first.") };
|
|
1329
|
+
const pendingBytes = pending.reduce((sum, p) => sum + recordBytes(p), size);
|
|
1330
|
+
if (pendingBytes > HISTORY.proposalBytes) return { error: tooLarge(["transaction", "mode"], "the bytes of pending proposals", HISTORY.proposalBytes, pendingBytes, "Accept or reject pending proposals first, or propose less at once.") };
|
|
1331
|
+
}
|
|
1332
|
+
return transaction.mode === "commit" ? { ok: {
|
|
1333
|
+
changes,
|
|
1334
|
+
inverse,
|
|
1335
|
+
record
|
|
1336
|
+
} } : { ok: {
|
|
1337
|
+
changes: [],
|
|
1338
|
+
inverse: [],
|
|
1339
|
+
record
|
|
1340
|
+
} };
|
|
1341
|
+
};
|
|
1342
|
+
const applyPlan = (board, plan) => {
|
|
1343
|
+
if (plan.record.seq !== board.head + 1) return { error: invalid([
|
|
1344
|
+
"plan",
|
|
1345
|
+
"record",
|
|
1346
|
+
"seq"
|
|
1347
|
+
], `${board.head + 1}, one past the board's head`, "Plan against the current board and apply that plan once.") };
|
|
1348
|
+
if (board.transactions.some((record) => record.id === plan.record.id)) return { error: invalid([
|
|
1349
|
+
"plan",
|
|
1350
|
+
"record",
|
|
1351
|
+
"id"
|
|
1352
|
+
], "an id no transaction on the board has", "Apply each plan once.") };
|
|
1353
|
+
const shapes = toMap(board.shapes);
|
|
1354
|
+
const failure = applyChanges(shapes, plan.changes);
|
|
1355
|
+
if (failure !== null) return { error: failure._tag === "Missing" ? shapeNotFound([
|
|
1356
|
+
"plan",
|
|
1357
|
+
"changes",
|
|
1358
|
+
failure.index
|
|
1359
|
+
], failure.id, shapes.keys()) : invalid([
|
|
1360
|
+
"plan",
|
|
1361
|
+
"changes",
|
|
1362
|
+
failure.index
|
|
1363
|
+
], `a patch that fits shape ${JSON.stringify(failure.id)}`, "Plan against the current board and apply that plan.") };
|
|
1364
|
+
return { ok: {
|
|
1365
|
+
shapes: [...shapes.values()].sort(compareShapes),
|
|
1366
|
+
head: plan.record.seq,
|
|
1367
|
+
transactions: trimHistory([...board.transactions, plan.record])
|
|
1368
|
+
} };
|
|
1369
|
+
};
|
|
1370
|
+
export { applyPlan, copyShape, planTransaction, revertSkips, scale, translate };
|