@zsherman/canvas 0.0.0-stage → 0.1.1
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 +24 -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 +1354 -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 +60 -4
|
@@ -0,0 +1,2077 @@
|
|
|
1
|
+
import { TOOL_OF, actionsForKey, changesSelection, isEnabled, labelOf } from "./actions.jsx";
|
|
2
|
+
import { bounds, boundsOf, hasText, jsonEqual, toMap } from "./board.jsx";
|
|
3
|
+
import { DEFAULTS, INTERACTION, LIMITS, ZOOM } from "./constants.jsx";
|
|
4
|
+
import { claimants, claimsOf, sameConnection } from "./claims.jsx";
|
|
5
|
+
import { invalid } from "./errors.jsx";
|
|
6
|
+
import { boxFromPoints, center, clamp, distance, round, roundBox, roundPoint } from "./geometry.jsx";
|
|
7
|
+
import { hitTestIndex, hitsShape, resizable, touchedBySegment } from "./hit-test.jsx";
|
|
8
|
+
import { describeShape, inspectShapes } from "./inspect.jsx";
|
|
9
|
+
import { planTransaction, revertSkips } from "./plan.jsx";
|
|
10
|
+
import { activityOf, createdId, draftKind, newId, nextTransaction } from "./scene.jsx";
|
|
11
|
+
import { boxValues, snap, snapTargets } from "./snap.jsx";
|
|
12
|
+
// src/kernel/editor.ts
|
|
13
|
+
/**
|
|
14
|
+
* The editor machine: one viewer's tools, gestures, selection, view and undo
|
|
15
|
+
* history. `step` takes the state, the board and one input and returns the
|
|
16
|
+
* next state, what the host must do (apply a transaction, publish presence,
|
|
17
|
+
* move the view, fill the clipboard, announce), whether the input was used,
|
|
18
|
+
* and the pointer style.
|
|
19
|
+
*
|
|
20
|
+
* Pointer points are board coordinates; wheel and pan deltas are screen
|
|
21
|
+
* pixels. The editor sends transactions with ids `<session>-<seq>` and keeps
|
|
22
|
+
* undo and redo stacks of its own transactions only, so undo never reverts
|
|
23
|
+
* someone else's work. Undo is a `Revert`; redo reverts that revert.
|
|
24
|
+
*/
|
|
25
|
+
const NO_MODIFIERS = {
|
|
26
|
+
shift: false,
|
|
27
|
+
alt: false,
|
|
28
|
+
meta: false
|
|
29
|
+
};
|
|
30
|
+
const NO_INSET = {
|
|
31
|
+
top: 0,
|
|
32
|
+
right: 0,
|
|
33
|
+
bottom: 0,
|
|
34
|
+
left: 0
|
|
35
|
+
};
|
|
36
|
+
const initialState = (session, view) => ({
|
|
37
|
+
session,
|
|
38
|
+
seq: 0,
|
|
39
|
+
tool: "select",
|
|
40
|
+
spaceTool: null,
|
|
41
|
+
style: {
|
|
42
|
+
color: null,
|
|
43
|
+
size: null,
|
|
44
|
+
align: null,
|
|
45
|
+
fill: null,
|
|
46
|
+
dash: null,
|
|
47
|
+
brush: null,
|
|
48
|
+
start: null,
|
|
49
|
+
end: null,
|
|
50
|
+
route: null
|
|
51
|
+
},
|
|
52
|
+
grid: null,
|
|
53
|
+
readOnly: false,
|
|
54
|
+
selection: [],
|
|
55
|
+
view,
|
|
56
|
+
inset: NO_INSET,
|
|
57
|
+
following: null,
|
|
58
|
+
pointer: null,
|
|
59
|
+
pointerKind: "mouse",
|
|
60
|
+
modifiers: NO_MODIFIERS,
|
|
61
|
+
hover: { _tag: "Nothing" },
|
|
62
|
+
undo: [],
|
|
63
|
+
redo: [],
|
|
64
|
+
mode: { _tag: "Idle" },
|
|
65
|
+
uploads: [],
|
|
66
|
+
chat: null
|
|
67
|
+
});
|
|
68
|
+
const set = (ctx, patch) => {
|
|
69
|
+
ctx.state = {
|
|
70
|
+
...ctx.state,
|
|
71
|
+
...patch
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const announce = (ctx, text) => {
|
|
75
|
+
ctx.actions.push({
|
|
76
|
+
_tag: "Announce",
|
|
77
|
+
text
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const tidyView = (view) => ({
|
|
81
|
+
x: round(view.x),
|
|
82
|
+
y: round(view.y),
|
|
83
|
+
w: round(view.w),
|
|
84
|
+
h: round(view.h),
|
|
85
|
+
zoom: Math.round(view.zoom * 1e4) / 1e4,
|
|
86
|
+
viewer: view.viewer
|
|
87
|
+
});
|
|
88
|
+
const setView = (ctx, view, animate) => {
|
|
89
|
+
const next = tidyView(view);
|
|
90
|
+
set(ctx, {
|
|
91
|
+
view: next,
|
|
92
|
+
following: null
|
|
93
|
+
});
|
|
94
|
+
ctx.actions.push({
|
|
95
|
+
_tag: "Viewport",
|
|
96
|
+
view: next,
|
|
97
|
+
animate
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
/** Announces and returns true when another connection holds one of `ids`. */
|
|
101
|
+
const refuse = (ctx, ids) => {
|
|
102
|
+
for (const id of ids) {
|
|
103
|
+
const why = ctx.held.get(id);
|
|
104
|
+
if (why !== void 0) {
|
|
105
|
+
announce(ctx, why);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Sends a transaction as this editor. Undoable transactions go on the undo
|
|
113
|
+
* stack and clear redo. Returns the transaction id, or null in read-only mode.
|
|
114
|
+
*/
|
|
115
|
+
const send = (ctx, commands, options = {}) => {
|
|
116
|
+
if (ctx.state.readOnly || commands.length === 0) return null;
|
|
117
|
+
if (refuse(ctx, commands.flatMap((c) => c._tag === "Update" ? [c.id] : "ids" in c && c._tag !== "Duplicate" ? c.ids : []))) return null;
|
|
118
|
+
const id = options.id ?? nextTransaction(ctx.state);
|
|
119
|
+
const reason = options.reason ?? null;
|
|
120
|
+
const transaction = {
|
|
121
|
+
id,
|
|
122
|
+
commands,
|
|
123
|
+
mode: "commit",
|
|
124
|
+
...reason === null ? {} : { reason }
|
|
125
|
+
};
|
|
126
|
+
const tooLarge = () => {
|
|
127
|
+
if (commands.reduce((sum, c) => sum + ("ids" in c ? c.ids.length : c._tag === "Paste" ? c.clipboard.shapes.length : 1), 0) < 2 || ctx.actions.some((a) => a._tag === "Apply")) return false;
|
|
128
|
+
const planned = planTransaction(ctx.fonts, ctx.board, transaction, selfOf(ctx.state).actor, ctx.presence, ctx.state.session);
|
|
129
|
+
return "error" in planned && planned.error._tag === "TooLarge";
|
|
130
|
+
};
|
|
131
|
+
if (commands.length > LIMITS.commands || tooLarge()) {
|
|
132
|
+
announce(ctx, "Can't change that many shapes at once: select fewer and try again");
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
ctx.actions.push({
|
|
136
|
+
_tag: "Apply",
|
|
137
|
+
transaction
|
|
138
|
+
});
|
|
139
|
+
if (options.id === void 0) set(ctx, { seq: ctx.state.seq + 1 });
|
|
140
|
+
if ((options.history ?? "push") === "push") set(ctx, {
|
|
141
|
+
undo: pushEntry(ctx.state.undo, {
|
|
142
|
+
transaction: id,
|
|
143
|
+
restores: null
|
|
144
|
+
}),
|
|
145
|
+
redo: []
|
|
146
|
+
});
|
|
147
|
+
return id;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Takes the next transaction id for a gesture's drop, which presence names
|
|
151
|
+
* while the gesture runs. Nothing else is sent as it, and it is not reused
|
|
152
|
+
* when the gesture is cancelled or its drop changes nothing.
|
|
153
|
+
*/
|
|
154
|
+
const reserve = (ctx) => {
|
|
155
|
+
const id = nextTransaction(ctx.state);
|
|
156
|
+
set(ctx, { seq: ctx.state.seq + 1 });
|
|
157
|
+
return id;
|
|
158
|
+
};
|
|
159
|
+
const pushEntry = (stack, entry) => [...stack, entry].slice(-100);
|
|
160
|
+
const groupCache = /* @__PURE__ */ new WeakMap();
|
|
161
|
+
const groups = (index) => {
|
|
162
|
+
const cached = groupCache.get(index);
|
|
163
|
+
if (cached !== void 0) return cached;
|
|
164
|
+
const out = /* @__PURE__ */ new Map();
|
|
165
|
+
for (const shape of index.ordered) {
|
|
166
|
+
if (shape.group === null) continue;
|
|
167
|
+
const members = out.get(shape.group) ?? [];
|
|
168
|
+
members.push(shape.id);
|
|
169
|
+
out.set(shape.group, members);
|
|
170
|
+
}
|
|
171
|
+
groupCache.set(index, out);
|
|
172
|
+
return out;
|
|
173
|
+
};
|
|
174
|
+
/** Ids with their whole groups, first appearance order, unknown ids dropped. */
|
|
175
|
+
const expand = (index, ids) => {
|
|
176
|
+
const out = [];
|
|
177
|
+
const seen = /* @__PURE__ */ new Set();
|
|
178
|
+
const add = (id) => {
|
|
179
|
+
if (seen.has(id) || !index.shapes.has(id)) return;
|
|
180
|
+
seen.add(id);
|
|
181
|
+
out.push(id);
|
|
182
|
+
};
|
|
183
|
+
for (const id of ids) {
|
|
184
|
+
const shape = index.shapes.get(id);
|
|
185
|
+
if (shape === void 0) continue;
|
|
186
|
+
if (shape.group === null) add(id);
|
|
187
|
+
else for (const member of groups(index).get(shape.group) ?? [id]) add(member);
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
};
|
|
191
|
+
const existing = (index, ids) => {
|
|
192
|
+
const out = [];
|
|
193
|
+
for (const id of ids) if (index.shapes.has(id) && !out.includes(id)) out.push(id);
|
|
194
|
+
return out;
|
|
195
|
+
};
|
|
196
|
+
const unlocked = (index, ids) => ids.filter((id) => index.shapes.get(id)?.locked === false);
|
|
197
|
+
const announceSelection = (ctx) => {
|
|
198
|
+
const ids = ctx.state.selection;
|
|
199
|
+
const only = ids.length === 1 ? ctx.index.shapes.get(ids[0] ?? "") : void 0;
|
|
200
|
+
announce(ctx, only !== void 0 ? describeShape(ctx.index, only) : ids.length === 0 ? "Nothing selected" : `${ids.length} shapes selected`);
|
|
201
|
+
};
|
|
202
|
+
/** Shapes in reading order: top to bottom, then left to right. */
|
|
203
|
+
const readingOrder = (index) => index.ordered.map((shape) => ({
|
|
204
|
+
id: shape.id,
|
|
205
|
+
b: bounds(index.shapes, shape)
|
|
206
|
+
})).sort((a, b) => a.b.y - b.b.y || a.b.x - b.b.x || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)).map((entry) => entry.id);
|
|
207
|
+
/** This editor's connection: the viewer (none sorts first) and the session. */
|
|
208
|
+
const selfOf = (state) => ({
|
|
209
|
+
actor: state.view.viewer ?? "",
|
|
210
|
+
connection: state.session
|
|
211
|
+
});
|
|
212
|
+
/**
|
|
213
|
+
* Shapes another connection holds (see `claims.ts`), each with the sentence
|
|
214
|
+
* that says so. This editor's claims are its mode's, so when it and another
|
|
215
|
+
* connection both claim a shape (each started before seeing the other),
|
|
216
|
+
* exactly one side stops.
|
|
217
|
+
*/
|
|
218
|
+
const heldBy = (state, presence) => {
|
|
219
|
+
const self = selfOf(state);
|
|
220
|
+
const held = /* @__PURE__ */ new Map();
|
|
221
|
+
for (const [id, [holder]] of claimants(presence, self, claimsOf(state.mode))) if (holder !== void 0 && !sameConnection(holder, self)) held.set(id, holder.actor === self.actor ? "You're editing this in another tab" : `${holder.name} is editing this`);
|
|
222
|
+
return held;
|
|
223
|
+
};
|
|
224
|
+
const presenceOf = (state, index) => ({
|
|
225
|
+
cursor: state.pointer === null ? null : {
|
|
226
|
+
point: state.pointer,
|
|
227
|
+
arriveInMs: 0
|
|
228
|
+
},
|
|
229
|
+
selection: state.selection,
|
|
230
|
+
viewport: {
|
|
231
|
+
x: state.view.x,
|
|
232
|
+
y: state.view.y,
|
|
233
|
+
w: state.view.w,
|
|
234
|
+
h: state.view.h
|
|
235
|
+
},
|
|
236
|
+
following: state.following,
|
|
237
|
+
activity: activityOf(state, index.shapes),
|
|
238
|
+
claims: claimsOf(state.mode),
|
|
239
|
+
uploads: state.uploads,
|
|
240
|
+
chat: state.chat
|
|
241
|
+
});
|
|
242
|
+
const RESIZE_CURSOR = {
|
|
243
|
+
n: "ns-resize",
|
|
244
|
+
s: "ns-resize",
|
|
245
|
+
e: "ew-resize",
|
|
246
|
+
w: "ew-resize",
|
|
247
|
+
nw: "nwse-resize",
|
|
248
|
+
se: "nwse-resize",
|
|
249
|
+
ne: "nesw-resize",
|
|
250
|
+
sw: "nesw-resize"
|
|
251
|
+
};
|
|
252
|
+
const TOOL_CURSOR = {
|
|
253
|
+
select: "default",
|
|
254
|
+
hand: "grab",
|
|
255
|
+
rectangle: "crosshair",
|
|
256
|
+
ellipse: "crosshair",
|
|
257
|
+
sticky: "crosshair",
|
|
258
|
+
text: "text",
|
|
259
|
+
arrow: "crosshair",
|
|
260
|
+
pen: "pen",
|
|
261
|
+
eraser: "eraser",
|
|
262
|
+
comment: "crosshair",
|
|
263
|
+
timestamp: "crosshair"
|
|
264
|
+
};
|
|
265
|
+
const cursorOf = (state, index, held) => {
|
|
266
|
+
const mode = state.mode;
|
|
267
|
+
switch (mode._tag) {
|
|
268
|
+
case "Panning": return "grabbing";
|
|
269
|
+
case "Dragging": return "move";
|
|
270
|
+
case "Resizing": return RESIZE_CURSOR[mode.handle];
|
|
271
|
+
case "Creating":
|
|
272
|
+
case "Connecting":
|
|
273
|
+
case "Marquee":
|
|
274
|
+
case "Placing": return "crosshair";
|
|
275
|
+
case "Inking": return "pen";
|
|
276
|
+
case "Erasing": return "eraser";
|
|
277
|
+
case "EditingText": return "default";
|
|
278
|
+
}
|
|
279
|
+
if (state.tool !== "select") return TOOL_CURSOR[state.tool];
|
|
280
|
+
const hover = state.hover;
|
|
281
|
+
switch (hover._tag) {
|
|
282
|
+
case "Handle": return RESIZE_CURSOR[hover.handle];
|
|
283
|
+
case "End": return "pointer";
|
|
284
|
+
case "Shape": {
|
|
285
|
+
const shape = index.shapes.get(hover.id);
|
|
286
|
+
if (shape === void 0 || state.readOnly) return "default";
|
|
287
|
+
if (held.has(shape.id)) return "not-allowed";
|
|
288
|
+
return state.selection.includes(hover.id) && !shape.locked ? "move" : "default";
|
|
289
|
+
}
|
|
290
|
+
case "Nothing": return "default";
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
const screenSize = (view) => ({
|
|
294
|
+
w: view.w * view.zoom,
|
|
295
|
+
h: view.h * view.zoom
|
|
296
|
+
});
|
|
297
|
+
/** The view at `zoom` keeping the board point `at` in place on screen. */
|
|
298
|
+
const zoomAt = (view, at, zoom) => {
|
|
299
|
+
const z = clamp(zoom, ZOOM.min, ZOOM.max);
|
|
300
|
+
const ratio = view.zoom / z;
|
|
301
|
+
return {
|
|
302
|
+
x: at.x - (at.x - view.x) * ratio,
|
|
303
|
+
y: at.y - (at.y - view.y) * ratio,
|
|
304
|
+
w: view.w * ratio,
|
|
305
|
+
h: view.h * ratio,
|
|
306
|
+
zoom: z,
|
|
307
|
+
viewer: view.viewer
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
/** The part of the screen the host's chrome leaves clear, in screen pixels from the view's top left. */
|
|
311
|
+
const clearArea = (view, inset) => {
|
|
312
|
+
const screen = screenSize(view);
|
|
313
|
+
const w = screen.w - inset.left - inset.right;
|
|
314
|
+
const h = screen.h - inset.top - inset.bottom;
|
|
315
|
+
return w > 0 && h > 0 ? {
|
|
316
|
+
x: inset.left,
|
|
317
|
+
y: inset.top,
|
|
318
|
+
w,
|
|
319
|
+
h
|
|
320
|
+
} : {
|
|
321
|
+
x: 0,
|
|
322
|
+
y: 0,
|
|
323
|
+
...screen
|
|
324
|
+
};
|
|
325
|
+
};
|
|
326
|
+
/** The board point at the middle of the clear area. */
|
|
327
|
+
const clearCenter = (view, inset) => {
|
|
328
|
+
const area = clearArea(view, inset);
|
|
329
|
+
return {
|
|
330
|
+
x: view.x + (area.x + area.w / 2) / view.zoom,
|
|
331
|
+
y: view.y + (area.y + area.h / 2) / view.zoom
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
/**
|
|
335
|
+
* The largest zoom, up to `maxZoom`, at which `box` and a margin fit the
|
|
336
|
+
* clear area, with `box` centred in it. The margin shrinks on a small
|
|
337
|
+
* screen, a twelfth of its shorter side, so a side panel is not mostly margin.
|
|
338
|
+
*/
|
|
339
|
+
const fitView = (view, inset, box, maxZoom) => {
|
|
340
|
+
const screen = screenSize(view);
|
|
341
|
+
const area = clearArea(view, inset);
|
|
342
|
+
const margin = Math.min(INTERACTION.fitMargin, Math.min(area.w, area.h) / 12);
|
|
343
|
+
const zx = (area.w - 2 * margin) / Math.max(box.w, 1);
|
|
344
|
+
const zy = (area.h - 2 * margin) / Math.max(box.h, 1);
|
|
345
|
+
const zoom = clamp(Math.min(zx, zy), ZOOM.min, maxZoom);
|
|
346
|
+
const w = screen.w / zoom;
|
|
347
|
+
const h = screen.h / zoom;
|
|
348
|
+
const c = center(box);
|
|
349
|
+
return {
|
|
350
|
+
x: c.x - (area.x + area.w / 2) / zoom,
|
|
351
|
+
y: c.y - (area.y + area.h / 2) / zoom,
|
|
352
|
+
w,
|
|
353
|
+
h,
|
|
354
|
+
zoom,
|
|
355
|
+
viewer: view.viewer
|
|
356
|
+
};
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* One scale for both sides: at most `DEFAULTS.image` on the long side and
|
|
360
|
+
* the natural size, unless the short side would fall below `MIN_SIZE`, which
|
|
361
|
+
* it is scaled up to (the long side stays within `LIMITS.size`).
|
|
362
|
+
*/
|
|
363
|
+
const imageSize = (w, h) => {
|
|
364
|
+
let k = Math.min(1, DEFAULTS.image / Math.max(w, h));
|
|
365
|
+
k = Math.min(Math.max(k, 16 / Math.min(w, h)), LIMITS.size / Math.max(w, h));
|
|
366
|
+
return {
|
|
367
|
+
w: round(Math.max(w * k, 16)),
|
|
368
|
+
h: round(Math.max(h * k, 16))
|
|
369
|
+
};
|
|
370
|
+
};
|
|
371
|
+
/** Boxes of `sizes` in rows of about the square root of their count, `DEFAULTS.gap` apart, centred on `at`. */
|
|
372
|
+
const layOut = (sizes, at) => {
|
|
373
|
+
const gap = DEFAULTS.gap;
|
|
374
|
+
const columns = Math.ceil(Math.sqrt(sizes.length));
|
|
375
|
+
const rows = [];
|
|
376
|
+
for (let i = 0; i < sizes.length; i += columns) rows.push(sizes.slice(i, i + columns));
|
|
377
|
+
const width = (row) => row.reduce((sum, size) => sum + size.w + gap, -gap);
|
|
378
|
+
const height = (row) => Math.max(...row.map((size) => size.h));
|
|
379
|
+
let y = at.y - rows.reduce((sum, row) => sum + height(row) + gap, -gap) / 2;
|
|
380
|
+
const boxes = [];
|
|
381
|
+
for (const row of rows) {
|
|
382
|
+
let x = at.x - width(row) / 2;
|
|
383
|
+
for (const size of row) {
|
|
384
|
+
boxes.push({
|
|
385
|
+
x: round(x),
|
|
386
|
+
y: round(y),
|
|
387
|
+
...size
|
|
388
|
+
});
|
|
389
|
+
x += size.w + gap;
|
|
390
|
+
}
|
|
391
|
+
y += height(row) + gap;
|
|
392
|
+
}
|
|
393
|
+
return boxes;
|
|
394
|
+
};
|
|
395
|
+
const beginUpload = (ctx, at, pending) => {
|
|
396
|
+
const known = new Set(ctx.state.uploads.map((upload) => upload.id));
|
|
397
|
+
const fresh = [];
|
|
398
|
+
for (const upload of pending) {
|
|
399
|
+
if (known.has(upload.id)) continue;
|
|
400
|
+
known.add(upload.id);
|
|
401
|
+
fresh.push(upload);
|
|
402
|
+
}
|
|
403
|
+
const boxes = layOut(fresh.map((upload) => upload.naturalW === null || upload.naturalH === null ? imageSize(DEFAULTS.image, DEFAULTS.image * 3 / 4) : imageSize(upload.naturalW, upload.naturalH)), at);
|
|
404
|
+
const added = fresh.map((upload, i) => ({
|
|
405
|
+
id: upload.id,
|
|
406
|
+
box: boxes[i] ?? {
|
|
407
|
+
...at,
|
|
408
|
+
w: 0,
|
|
409
|
+
h: 0
|
|
410
|
+
},
|
|
411
|
+
name: upload.name
|
|
412
|
+
}));
|
|
413
|
+
set(ctx, { uploads: [...ctx.state.uploads, ...added].slice(0, LIMITS.uploads) });
|
|
414
|
+
};
|
|
415
|
+
const insertImages = (ctx, at, images) => {
|
|
416
|
+
const placeholder = (upload) => ctx.state.uploads.find((pending) => pending.id === upload)?.box;
|
|
417
|
+
const sizes = images.map((image) => imageSize(image.naturalW, image.naturalH));
|
|
418
|
+
const loose = layOut(sizes.filter((_, i) => placeholder(images[i]?.upload ?? null) === void 0), at);
|
|
419
|
+
let next = 0;
|
|
420
|
+
const ids = [];
|
|
421
|
+
const commands = images.map((image, i) => {
|
|
422
|
+
const size = sizes[i] ?? {
|
|
423
|
+
w: 16,
|
|
424
|
+
h: 16
|
|
425
|
+
};
|
|
426
|
+
const held = placeholder(image.upload);
|
|
427
|
+
const box = held === void 0 ? loose[next++] : void 0;
|
|
428
|
+
const c = held === void 0 ? null : center(held);
|
|
429
|
+
const id = newId(ctx.state, "Image", i + 1);
|
|
430
|
+
ids.push(id);
|
|
431
|
+
return {
|
|
432
|
+
_tag: "Create",
|
|
433
|
+
shape: {
|
|
434
|
+
kind: "Image",
|
|
435
|
+
id,
|
|
436
|
+
asset: image.asset,
|
|
437
|
+
alt: image.alt,
|
|
438
|
+
...size
|
|
439
|
+
},
|
|
440
|
+
place: {
|
|
441
|
+
_tag: "At",
|
|
442
|
+
x: round(c === null ? box?.x ?? at.x : c.x - size.w / 2),
|
|
443
|
+
y: round(c === null ? box?.y ?? at.y : c.y - size.h / 2)
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
});
|
|
447
|
+
if (send(ctx, commands) === null) return;
|
|
448
|
+
const used = new Set(images.map((image) => image.upload));
|
|
449
|
+
set(ctx, {
|
|
450
|
+
selection: ids,
|
|
451
|
+
tool: "select",
|
|
452
|
+
uploads: ctx.state.uploads.filter((upload) => !used.has(upload.id))
|
|
453
|
+
});
|
|
454
|
+
announce(ctx, ids.length === 1 ? "Inserted 1 image" : `Inserted ${ids.length} images`);
|
|
455
|
+
};
|
|
456
|
+
/**
|
|
457
|
+
* The follower's view of a leader's viewport: the largest zoom within
|
|
458
|
+
* `ZOOM` at which `box` fits the clear area, with no margin, centred.
|
|
459
|
+
*/
|
|
460
|
+
const followView = (view, inset, box) => {
|
|
461
|
+
const screen = screenSize(view);
|
|
462
|
+
const area = clearArea(view, inset);
|
|
463
|
+
const zoom = clamp(Math.min(area.w / Math.max(box.w, 1), area.h / Math.max(box.h, 1)), ZOOM.min, ZOOM.max);
|
|
464
|
+
const c = center(box);
|
|
465
|
+
return {
|
|
466
|
+
x: c.x - (area.x + area.w / 2) / zoom,
|
|
467
|
+
y: c.y - (area.y + area.h / 2) / zoom,
|
|
468
|
+
w: screen.w / zoom,
|
|
469
|
+
h: screen.h / zoom,
|
|
470
|
+
zoom,
|
|
471
|
+
viewer: view.viewer
|
|
472
|
+
};
|
|
473
|
+
};
|
|
474
|
+
const stepZoom = (current, direction) => {
|
|
475
|
+
if (direction === "in") return ZOOM.steps.find((z) => z > current + 1e-9) ?? ZOOM.max;
|
|
476
|
+
return [...ZOOM.steps].reverse().find((z) => z < current - 1e-9) ?? ZOOM.min;
|
|
477
|
+
};
|
|
478
|
+
const zoomTo = (ctx, to) => {
|
|
479
|
+
const view = ctx.state.view;
|
|
480
|
+
const inset = ctx.state.inset;
|
|
481
|
+
const middle = clearCenter(view, inset);
|
|
482
|
+
switch (to) {
|
|
483
|
+
case "in":
|
|
484
|
+
case "out":
|
|
485
|
+
setView(ctx, zoomAt(view, middle, stepZoom(view.zoom, to)), true);
|
|
486
|
+
return;
|
|
487
|
+
case "reset":
|
|
488
|
+
setView(ctx, zoomAt(view, middle, 1), true);
|
|
489
|
+
return;
|
|
490
|
+
case "fit": {
|
|
491
|
+
const box = boundsOf(ctx.index.shapes, ctx.index.shapes.keys());
|
|
492
|
+
if (box !== null) setView(ctx, fitView(view, inset, box, 1), true);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
case "selection": {
|
|
496
|
+
const box = boundsOf(ctx.index.shapes, ctx.state.selection);
|
|
497
|
+
if (box !== null) setView(ctx, fitView(view, inset, box, ZOOM.max), true);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
const opt = (key, value) => value === null ? {} : { [key]: value };
|
|
503
|
+
const shapeInput = (draft, id, style, size) => {
|
|
504
|
+
const common = {
|
|
505
|
+
id,
|
|
506
|
+
...opt("color", style.color),
|
|
507
|
+
...opt("size", style.size),
|
|
508
|
+
...opt("align", style.align)
|
|
509
|
+
};
|
|
510
|
+
switch (draft) {
|
|
511
|
+
case "sticky": return {
|
|
512
|
+
kind: "Sticky",
|
|
513
|
+
...common,
|
|
514
|
+
...size === null ? {} : size
|
|
515
|
+
};
|
|
516
|
+
case "rectangle":
|
|
517
|
+
case "ellipse": return {
|
|
518
|
+
kind: "Box",
|
|
519
|
+
...common,
|
|
520
|
+
form: draft,
|
|
521
|
+
...opt("fill", style.fill),
|
|
522
|
+
...opt("dash", style.dash),
|
|
523
|
+
...size === null ? {} : size
|
|
524
|
+
};
|
|
525
|
+
case "text": return {
|
|
526
|
+
kind: "Text",
|
|
527
|
+
...common,
|
|
528
|
+
text: "",
|
|
529
|
+
...size === null ? {} : { w: size.w }
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const DRAFT_OF = {
|
|
534
|
+
rectangle: "rectangle",
|
|
535
|
+
ellipse: "ellipse",
|
|
536
|
+
sticky: "sticky",
|
|
537
|
+
text: "text"
|
|
538
|
+
};
|
|
539
|
+
const defaultSize = (draft) => draft === "sticky" ? {
|
|
540
|
+
w: DEFAULTS.sticky.w,
|
|
541
|
+
h: DEFAULTS.sticky.h
|
|
542
|
+
} : draft === "text" ? {
|
|
543
|
+
w: 0,
|
|
544
|
+
h: 0
|
|
545
|
+
} : {
|
|
546
|
+
w: DEFAULTS.box.w,
|
|
547
|
+
h: DEFAULTS.box.h
|
|
548
|
+
};
|
|
549
|
+
/** Creates a shape and selects it; sticky notes, boxes and text open for typing when `edit`. */
|
|
550
|
+
const createDraft = (ctx, draft, box, at, edit) => {
|
|
551
|
+
const id = newId(ctx.state, draftKind(draft));
|
|
552
|
+
const size = box === null ? null : {
|
|
553
|
+
w: round(box.w),
|
|
554
|
+
h: round(box.h)
|
|
555
|
+
};
|
|
556
|
+
const place = box === null ? draft === "text" ? {
|
|
557
|
+
_tag: "At",
|
|
558
|
+
x: round(at.x),
|
|
559
|
+
y: round(at.y)
|
|
560
|
+
} : {
|
|
561
|
+
_tag: "At",
|
|
562
|
+
x: round(at.x - defaultSize(draft).w / 2),
|
|
563
|
+
y: round(at.y - defaultSize(draft).h / 2)
|
|
564
|
+
} : {
|
|
565
|
+
_tag: "At",
|
|
566
|
+
x: round(box.x),
|
|
567
|
+
y: round(box.y)
|
|
568
|
+
};
|
|
569
|
+
const sent = send(ctx, [{
|
|
570
|
+
_tag: "Create",
|
|
571
|
+
shape: shapeInput(draft, id, ctx.state.style, size),
|
|
572
|
+
place
|
|
573
|
+
}]);
|
|
574
|
+
if (sent === null) return;
|
|
575
|
+
set(ctx, {
|
|
576
|
+
selection: [id],
|
|
577
|
+
tool: "select",
|
|
578
|
+
mode: edit ? {
|
|
579
|
+
_tag: "EditingText",
|
|
580
|
+
id,
|
|
581
|
+
draft: "",
|
|
582
|
+
original: "",
|
|
583
|
+
created: sent
|
|
584
|
+
} : { _tag: "Idle" }
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
/** Ends text editing, sending the text if it changed. */
|
|
588
|
+
const finishEditing = (ctx) => {
|
|
589
|
+
const mode = ctx.state.mode;
|
|
590
|
+
if (mode._tag !== "EditingText") return;
|
|
591
|
+
set(ctx, { mode: { _tag: "Idle" } });
|
|
592
|
+
const shape = ctx.index.shapes.get(mode.id);
|
|
593
|
+
if (shape === void 0 || !hasText(shape)) return;
|
|
594
|
+
if (mode.draft === "" && shape.kind === "Text") {
|
|
595
|
+
if (mode.created !== null && ctx.state.undo.at(-1)?.transaction === mode.created) {
|
|
596
|
+
send(ctx, [{
|
|
597
|
+
_tag: "Revert",
|
|
598
|
+
transaction: mode.created
|
|
599
|
+
}], { history: "none" });
|
|
600
|
+
set(ctx, { undo: ctx.state.undo.slice(0, -1) });
|
|
601
|
+
} else send(ctx, [{
|
|
602
|
+
_tag: "Delete",
|
|
603
|
+
ids: [mode.id]
|
|
604
|
+
}]);
|
|
605
|
+
set(ctx, { selection: ctx.state.selection.filter((id) => id !== mode.id) });
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
if (mode.draft !== shape.text) send(ctx, [{
|
|
609
|
+
_tag: "Update",
|
|
610
|
+
id: mode.id,
|
|
611
|
+
patch: { text: mode.draft }
|
|
612
|
+
}]);
|
|
613
|
+
};
|
|
614
|
+
const startEditing = (ctx, id) => {
|
|
615
|
+
const shape = ctx.index.shapes.get(id);
|
|
616
|
+
if (shape === void 0 || !hasText(shape) || shape.locked || ctx.state.readOnly || refuse(ctx, [id])) return false;
|
|
617
|
+
set(ctx, {
|
|
618
|
+
selection: [id],
|
|
619
|
+
mode: {
|
|
620
|
+
_tag: "EditingText",
|
|
621
|
+
id,
|
|
622
|
+
draft: shape.text,
|
|
623
|
+
original: shape.text,
|
|
624
|
+
created: null
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
return true;
|
|
628
|
+
};
|
|
629
|
+
const probeHit = (ctx, p, pointer) => hitTestIndex(ctx.index, {
|
|
630
|
+
point: p,
|
|
631
|
+
zoom: ctx.state.view.zoom,
|
|
632
|
+
selection: ctx.state.readOnly ? [] : ctx.state.selection,
|
|
633
|
+
pointer
|
|
634
|
+
});
|
|
635
|
+
/** Where a connector end lands at `p`: on the top shape under it, else free. */
|
|
636
|
+
const anchorAt = (ctx, p, exclude) => {
|
|
637
|
+
const tolerance = INTERACTION.hitTolerance[ctx.state.pointerKind] / ctx.state.view.zoom;
|
|
638
|
+
for (let i = ctx.index.ordered.length - 1; i >= 0; i--) {
|
|
639
|
+
const shape = ctx.index.ordered[i];
|
|
640
|
+
if (shape === void 0 || shape.kind === "Arrow" || shape.id === exclude) continue;
|
|
641
|
+
if (hitsShape(ctx.index.shapes, shape, p, tolerance, true)) return {
|
|
642
|
+
_tag: "Shape",
|
|
643
|
+
id: shape.id,
|
|
644
|
+
at: null
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
return {
|
|
648
|
+
_tag: "Point",
|
|
649
|
+
x: round(p.x),
|
|
650
|
+
y: round(p.y)
|
|
651
|
+
};
|
|
652
|
+
};
|
|
653
|
+
const anchorId = (anchor) => anchor._tag === "Shape" ? anchor.id : null;
|
|
654
|
+
const pastThreshold = (ctx, a, b) => distance(a, b) * ctx.state.view.zoom > INTERACTION.dragThreshold;
|
|
655
|
+
const snapReach = (ctx) => INTERACTION.snap / ctx.state.view.zoom;
|
|
656
|
+
const pointerDown = (ctx, input) => {
|
|
657
|
+
finishEditing(ctx);
|
|
658
|
+
const p = input.point;
|
|
659
|
+
set(ctx, {
|
|
660
|
+
pointer: p,
|
|
661
|
+
pointerKind: input.pointer,
|
|
662
|
+
modifiers: input.modifiers
|
|
663
|
+
});
|
|
664
|
+
ctx.handled = true;
|
|
665
|
+
if (input.button === "secondary") {
|
|
666
|
+
const hit = probeHit(ctx, p, input.pointer);
|
|
667
|
+
if (hit._tag === "Shape" && !ctx.state.selection.includes(hit.id)) set(ctx, { selection: expand(ctx.index, [hit.id]) });
|
|
668
|
+
else if (hit._tag === "Nothing") set(ctx, { selection: [] });
|
|
669
|
+
ctx.handled = false;
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
if (input.button === "middle" || ctx.state.tool === "hand") {
|
|
673
|
+
set(ctx, { mode: {
|
|
674
|
+
_tag: "Panning",
|
|
675
|
+
origin: p,
|
|
676
|
+
start: ctx.state.view
|
|
677
|
+
} });
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
const tool = ctx.state.readOnly ? "select" : ctx.state.tool;
|
|
681
|
+
const draft = DRAFT_OF[tool];
|
|
682
|
+
if (draft !== void 0) {
|
|
683
|
+
set(ctx, { mode: {
|
|
684
|
+
_tag: "Creating",
|
|
685
|
+
draft,
|
|
686
|
+
origin: p,
|
|
687
|
+
box: {
|
|
688
|
+
x: p.x,
|
|
689
|
+
y: p.y,
|
|
690
|
+
w: 0,
|
|
691
|
+
h: 0
|
|
692
|
+
},
|
|
693
|
+
dragged: false,
|
|
694
|
+
guides: []
|
|
695
|
+
} });
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
switch (tool) {
|
|
699
|
+
case "comment":
|
|
700
|
+
case "timestamp":
|
|
701
|
+
set(ctx, { mode: {
|
|
702
|
+
_tag: "Placing",
|
|
703
|
+
tool,
|
|
704
|
+
origin: p
|
|
705
|
+
} });
|
|
706
|
+
return;
|
|
707
|
+
case "arrow":
|
|
708
|
+
set(ctx, {
|
|
709
|
+
mode: {
|
|
710
|
+
_tag: "Connecting",
|
|
711
|
+
origin: p,
|
|
712
|
+
arrow: null,
|
|
713
|
+
from: anchorAt(ctx, p, null),
|
|
714
|
+
to: {
|
|
715
|
+
_tag: "Point",
|
|
716
|
+
x: round(p.x),
|
|
717
|
+
y: round(p.y)
|
|
718
|
+
},
|
|
719
|
+
moving: "to"
|
|
720
|
+
},
|
|
721
|
+
selection: []
|
|
722
|
+
});
|
|
723
|
+
return;
|
|
724
|
+
case "pen":
|
|
725
|
+
set(ctx, {
|
|
726
|
+
mode: {
|
|
727
|
+
_tag: "Inking",
|
|
728
|
+
points: [roundPoint(p)]
|
|
729
|
+
},
|
|
730
|
+
selection: []
|
|
731
|
+
});
|
|
732
|
+
return;
|
|
733
|
+
case "eraser":
|
|
734
|
+
set(ctx, {
|
|
735
|
+
mode: {
|
|
736
|
+
_tag: "Erasing",
|
|
737
|
+
ids: touchedBySegment(ctx.index, p, p, 0, input.pointer, ctx.state.view.zoom),
|
|
738
|
+
trail: [roundPoint(p)]
|
|
739
|
+
},
|
|
740
|
+
selection: []
|
|
741
|
+
});
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
const hit = probeHit(ctx, p, input.pointer);
|
|
745
|
+
switch (hit._tag) {
|
|
746
|
+
case "Handle": {
|
|
747
|
+
const ids = resizable(ctx.index.shapes, ctx.state.selection);
|
|
748
|
+
const start = boundsOf(ctx.index.shapes, ids);
|
|
749
|
+
if (start === null || refuse(ctx, ids)) return;
|
|
750
|
+
set(ctx, { mode: {
|
|
751
|
+
_tag: "Resizing",
|
|
752
|
+
ids,
|
|
753
|
+
handle: hit.handle,
|
|
754
|
+
origin: p,
|
|
755
|
+
start,
|
|
756
|
+
box: start,
|
|
757
|
+
guides: [],
|
|
758
|
+
transaction: reserve(ctx)
|
|
759
|
+
} });
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
case "End": {
|
|
763
|
+
const arrow = ctx.index.shapes.get(hit.id);
|
|
764
|
+
if (arrow?.kind !== "Arrow" || refuse(ctx, [arrow.id])) return;
|
|
765
|
+
set(ctx, { mode: {
|
|
766
|
+
_tag: "Connecting",
|
|
767
|
+
origin: p,
|
|
768
|
+
arrow: arrow.id,
|
|
769
|
+
from: arrow.from,
|
|
770
|
+
to: arrow.to,
|
|
771
|
+
moving: hit.end
|
|
772
|
+
} });
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
case "Shape": {
|
|
776
|
+
const wasSelected = ctx.state.selection.includes(hit.id);
|
|
777
|
+
const additive = input.modifiers.shift;
|
|
778
|
+
if (!wasSelected) {
|
|
779
|
+
const group = expand(ctx.index, [hit.id]);
|
|
780
|
+
set(ctx, { selection: additive ? expand(ctx.index, [...ctx.state.selection, ...group]) : group });
|
|
781
|
+
}
|
|
782
|
+
set(ctx, { mode: {
|
|
783
|
+
_tag: "Pressing",
|
|
784
|
+
origin: p,
|
|
785
|
+
target: hit,
|
|
786
|
+
additive,
|
|
787
|
+
wasSelected
|
|
788
|
+
} });
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
case "Nothing":
|
|
792
|
+
set(ctx, { mode: {
|
|
793
|
+
_tag: "Pressing",
|
|
794
|
+
origin: p,
|
|
795
|
+
target: hit,
|
|
796
|
+
additive: input.modifiers.shift,
|
|
797
|
+
wasSelected: false
|
|
798
|
+
} });
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
const dragTo = (ctx, mode, p) => {
|
|
803
|
+
const m = ctx.state.modifiers;
|
|
804
|
+
let dx = p.x - mode.origin.x;
|
|
805
|
+
let dy = p.y - mode.origin.y;
|
|
806
|
+
if (m.shift) {
|
|
807
|
+
if (Math.abs(dx) > Math.abs(dy)) dy = 0;
|
|
808
|
+
else dx = 0;
|
|
809
|
+
}
|
|
810
|
+
const start = boundsOf(ctx.index.shapes, mode.ids);
|
|
811
|
+
let guides = [];
|
|
812
|
+
if (start !== null && !m.meta) {
|
|
813
|
+
const moved = {
|
|
814
|
+
...start,
|
|
815
|
+
x: start.x + dx,
|
|
816
|
+
y: start.y + dy
|
|
817
|
+
};
|
|
818
|
+
const values = boxValues(moved);
|
|
819
|
+
const targets = snapTargets(ctx.index, new Set(mode.copy ? [] : mode.ids), ctx.state.view);
|
|
820
|
+
const snapped = snap(targets, moved, m.shift && dx === 0 ? [] : values.xs, m.shift && dy === 0 ? [] : values.ys, snapReach(ctx), ctx.state.grid, true);
|
|
821
|
+
dx += snapped.dx;
|
|
822
|
+
dy += snapped.dy;
|
|
823
|
+
guides = snapped.guides({
|
|
824
|
+
...start,
|
|
825
|
+
x: start.x + dx,
|
|
826
|
+
y: start.y + dy
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
return {
|
|
830
|
+
...mode,
|
|
831
|
+
dx: round(dx),
|
|
832
|
+
dy: round(dy),
|
|
833
|
+
copy: m.alt,
|
|
834
|
+
guides
|
|
835
|
+
};
|
|
836
|
+
};
|
|
837
|
+
const resizeTo = (ctx, mode, p) => {
|
|
838
|
+
const m = ctx.state.modifiers;
|
|
839
|
+
const s = mode.start;
|
|
840
|
+
const h = mode.handle;
|
|
841
|
+
const movesW = h.includes("w");
|
|
842
|
+
const movesE = h.includes("e");
|
|
843
|
+
const movesN = h.includes("n");
|
|
844
|
+
const movesS = h.includes("s");
|
|
845
|
+
const dx = p.x - mode.origin.x;
|
|
846
|
+
const dy = p.y - mode.origin.y;
|
|
847
|
+
let left = s.x + (movesW ? dx : 0);
|
|
848
|
+
let right = s.x + s.w + (movesE ? dx : 0);
|
|
849
|
+
let top = s.y + (movesN ? dy : 0);
|
|
850
|
+
let bottom = s.y + s.h + (movesS ? dy : 0);
|
|
851
|
+
let guides = [];
|
|
852
|
+
if (!m.meta && !m.shift) {
|
|
853
|
+
const targets = snapTargets(ctx.index, new Set(mode.ids), ctx.state.view);
|
|
854
|
+
const xs = movesW ? [left] : movesE ? [right] : [];
|
|
855
|
+
const ys = movesN ? [top] : movesS ? [bottom] : [];
|
|
856
|
+
const moving = boxFromPoints({
|
|
857
|
+
x: left,
|
|
858
|
+
y: top
|
|
859
|
+
}, {
|
|
860
|
+
x: right,
|
|
861
|
+
y: bottom
|
|
862
|
+
});
|
|
863
|
+
const snapped = snap(targets, moving, xs, ys, snapReach(ctx), ctx.state.grid, false);
|
|
864
|
+
if (movesW) left += snapped.dx;
|
|
865
|
+
if (movesE) right += snapped.dx;
|
|
866
|
+
if (movesN) top += snapped.dy;
|
|
867
|
+
if (movesS) bottom += snapped.dy;
|
|
868
|
+
guides = snapped.guides(boxFromPoints({
|
|
869
|
+
x: left,
|
|
870
|
+
y: top
|
|
871
|
+
}, {
|
|
872
|
+
x: right,
|
|
873
|
+
y: bottom
|
|
874
|
+
}));
|
|
875
|
+
}
|
|
876
|
+
const images = mode.ids.some((id) => ctx.index.shapes.get(id)?.kind === "Image");
|
|
877
|
+
if (m.shift !== images && s.w > 0 && s.h > 0) {
|
|
878
|
+
const sx = (right - left) / s.w;
|
|
879
|
+
const sy = (bottom - top) / s.h;
|
|
880
|
+
const k = (movesW || movesE) && (movesN || movesS) ? Math.max(sx, sy) : movesW || movesE ? sx : sy;
|
|
881
|
+
const w = s.w * k;
|
|
882
|
+
const hh = s.h * k;
|
|
883
|
+
if (movesW) left = right - w;
|
|
884
|
+
else if (movesE) right = left + w;
|
|
885
|
+
else {
|
|
886
|
+
left = s.x + s.w / 2 - w / 2;
|
|
887
|
+
right = left + w;
|
|
888
|
+
}
|
|
889
|
+
if (movesN) top = bottom - hh;
|
|
890
|
+
else if (movesS) bottom = top + hh;
|
|
891
|
+
else {
|
|
892
|
+
top = s.y + s.h / 2 - hh / 2;
|
|
893
|
+
bottom = top + hh;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (m.alt) {
|
|
897
|
+
const cx = s.x + s.w / 2;
|
|
898
|
+
const cy = s.y + s.h / 2;
|
|
899
|
+
if (movesW) right = 2 * cx - left;
|
|
900
|
+
if (movesE) left = 2 * cx - right;
|
|
901
|
+
if (movesN) bottom = 2 * cy - top;
|
|
902
|
+
if (movesS) top = 2 * cy - bottom;
|
|
903
|
+
}
|
|
904
|
+
if (right - left < 16) {
|
|
905
|
+
if (movesW && !m.alt) left = right - 16;
|
|
906
|
+
else if (movesE && !m.alt) right = left + 16;
|
|
907
|
+
else {
|
|
908
|
+
const cx = (left + right) / 2;
|
|
909
|
+
left = cx - 8;
|
|
910
|
+
right = cx + 8;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
if (bottom - top < 16) {
|
|
914
|
+
if (movesN && !m.alt) top = bottom - 16;
|
|
915
|
+
else if (movesS && !m.alt) bottom = top + 16;
|
|
916
|
+
else {
|
|
917
|
+
const cy = (top + bottom) / 2;
|
|
918
|
+
top = cy - 8;
|
|
919
|
+
bottom = cy + 8;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
return {
|
|
923
|
+
...mode,
|
|
924
|
+
box: roundBox({
|
|
925
|
+
x: left,
|
|
926
|
+
y: top,
|
|
927
|
+
w: right - left,
|
|
928
|
+
h: bottom - top
|
|
929
|
+
}),
|
|
930
|
+
guides
|
|
931
|
+
};
|
|
932
|
+
};
|
|
933
|
+
const createTo = (ctx, mode, p) => {
|
|
934
|
+
const m = ctx.state.modifiers;
|
|
935
|
+
let corner = p;
|
|
936
|
+
let guides = [];
|
|
937
|
+
if (!m.meta) {
|
|
938
|
+
const targets = snapTargets(ctx.index, /* @__PURE__ */ new Set(), ctx.state.view);
|
|
939
|
+
const snapped = snap(targets, {
|
|
940
|
+
x: p.x,
|
|
941
|
+
y: p.y,
|
|
942
|
+
w: 0,
|
|
943
|
+
h: 0
|
|
944
|
+
}, [p.x], [p.y], snapReach(ctx), ctx.state.grid, false);
|
|
945
|
+
corner = {
|
|
946
|
+
x: p.x + snapped.dx,
|
|
947
|
+
y: p.y + snapped.dy
|
|
948
|
+
};
|
|
949
|
+
guides = snapped.guides(boxFromPoints(mode.origin, corner));
|
|
950
|
+
}
|
|
951
|
+
if (m.shift) {
|
|
952
|
+
const side = Math.max(Math.abs(corner.x - mode.origin.x), Math.abs(corner.y - mode.origin.y));
|
|
953
|
+
corner = {
|
|
954
|
+
x: mode.origin.x + (corner.x < mode.origin.x ? -side : side),
|
|
955
|
+
y: mode.origin.y + (corner.y < mode.origin.y ? -side : side)
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
return {
|
|
959
|
+
...mode,
|
|
960
|
+
box: roundBox(boxFromPoints(mode.origin, corner)),
|
|
961
|
+
dragged: true,
|
|
962
|
+
guides
|
|
963
|
+
};
|
|
964
|
+
};
|
|
965
|
+
const marqueeSelection = (ctx, base, box) => {
|
|
966
|
+
const hits = [];
|
|
967
|
+
for (const shape of ctx.index.ordered) {
|
|
968
|
+
const b = bounds(ctx.index.shapes, shape);
|
|
969
|
+
if (b.x < box.x + box.w && b.x + b.w > box.x && b.y < box.y + box.h && b.y + b.h > box.y) hits.push(shape.id);
|
|
970
|
+
}
|
|
971
|
+
return expand(ctx.index, [...base, ...hits]);
|
|
972
|
+
};
|
|
973
|
+
const pointerMove = (ctx, p, samples, pointer) => {
|
|
974
|
+
set(ctx, {
|
|
975
|
+
pointer: p,
|
|
976
|
+
pointerKind: pointer
|
|
977
|
+
});
|
|
978
|
+
const mode = ctx.state.mode;
|
|
979
|
+
switch (mode._tag) {
|
|
980
|
+
case "Idle":
|
|
981
|
+
case "EditingText":
|
|
982
|
+
if (ctx.state.tool === "select" || ctx.state.readOnly) set(ctx, { hover: probeHit(ctx, p, pointer) });
|
|
983
|
+
return;
|
|
984
|
+
case "Pressing": {
|
|
985
|
+
if (!pastThreshold(ctx, mode.origin, p)) return;
|
|
986
|
+
if (mode.target._tag === "Nothing") {
|
|
987
|
+
const base = mode.additive ? ctx.state.selection : [];
|
|
988
|
+
set(ctx, { mode: {
|
|
989
|
+
_tag: "Marquee",
|
|
990
|
+
origin: mode.origin,
|
|
991
|
+
point: p,
|
|
992
|
+
base
|
|
993
|
+
} });
|
|
994
|
+
set(ctx, { selection: marqueeSelection(ctx, base, boxFromPoints(mode.origin, p)) });
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
if (ctx.state.readOnly) return;
|
|
998
|
+
const ids = unlocked(ctx.index, ctx.state.selection);
|
|
999
|
+
if (ids.length === 0) return;
|
|
1000
|
+
if (refuse(ctx, ids)) {
|
|
1001
|
+
set(ctx, { mode: { _tag: "Idle" } });
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
set(ctx, {
|
|
1005
|
+
mode: dragTo(ctx, {
|
|
1006
|
+
_tag: "Dragging",
|
|
1007
|
+
origin: mode.origin,
|
|
1008
|
+
ids,
|
|
1009
|
+
dx: 0,
|
|
1010
|
+
dy: 0,
|
|
1011
|
+
copy: false,
|
|
1012
|
+
guides: [],
|
|
1013
|
+
transaction: reserve(ctx)
|
|
1014
|
+
}, p),
|
|
1015
|
+
hover: { _tag: "Nothing" }
|
|
1016
|
+
});
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
case "Dragging":
|
|
1020
|
+
set(ctx, { mode: dragTo(ctx, mode, p) });
|
|
1021
|
+
return;
|
|
1022
|
+
case "Resizing":
|
|
1023
|
+
set(ctx, { mode: resizeTo(ctx, mode, p) });
|
|
1024
|
+
return;
|
|
1025
|
+
case "Creating":
|
|
1026
|
+
if (!mode.dragged && !pastThreshold(ctx, mode.origin, p)) return;
|
|
1027
|
+
set(ctx, { mode: createTo(ctx, mode, p) });
|
|
1028
|
+
return;
|
|
1029
|
+
case "Placing":
|
|
1030
|
+
if (pastThreshold(ctx, mode.origin, p)) set(ctx, { mode: { _tag: "Idle" } });
|
|
1031
|
+
return;
|
|
1032
|
+
case "Marquee":
|
|
1033
|
+
set(ctx, {
|
|
1034
|
+
mode: {
|
|
1035
|
+
...mode,
|
|
1036
|
+
point: p
|
|
1037
|
+
},
|
|
1038
|
+
selection: marqueeSelection(ctx, mode.base, boxFromPoints(mode.origin, p))
|
|
1039
|
+
});
|
|
1040
|
+
return;
|
|
1041
|
+
case "Connecting": {
|
|
1042
|
+
const other = mode.moving === "to" ? mode.from : mode.to;
|
|
1043
|
+
const end = anchorAt(ctx, p, anchorId(other));
|
|
1044
|
+
set(ctx, { mode: mode.moving === "to" ? {
|
|
1045
|
+
...mode,
|
|
1046
|
+
to: end
|
|
1047
|
+
} : {
|
|
1048
|
+
...mode,
|
|
1049
|
+
from: end
|
|
1050
|
+
} });
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
case "Inking": {
|
|
1054
|
+
const points = [...mode.points];
|
|
1055
|
+
const spacing = INTERACTION.inkSpacing / ctx.state.view.zoom;
|
|
1056
|
+
for (const sample of [...samples, p]) {
|
|
1057
|
+
const last = points[points.length - 1];
|
|
1058
|
+
if (points.length >= LIMITS.points) break;
|
|
1059
|
+
if (last === void 0 || distance(last, sample) >= spacing) points.push(roundPoint(sample));
|
|
1060
|
+
}
|
|
1061
|
+
set(ctx, { mode: {
|
|
1062
|
+
_tag: "Inking",
|
|
1063
|
+
points
|
|
1064
|
+
} });
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
case "Erasing": {
|
|
1068
|
+
const ids = [...mode.ids];
|
|
1069
|
+
const trail = [...mode.trail];
|
|
1070
|
+
for (const sample of [...samples, p]) {
|
|
1071
|
+
const last = trail[trail.length - 1] ?? sample;
|
|
1072
|
+
for (const id of touchedBySegment(ctx.index, last, sample, 0, pointer, ctx.state.view.zoom)) if (!ids.includes(id)) ids.push(id);
|
|
1073
|
+
trail.push(roundPoint(sample));
|
|
1074
|
+
}
|
|
1075
|
+
set(ctx, { mode: {
|
|
1076
|
+
_tag: "Erasing",
|
|
1077
|
+
ids,
|
|
1078
|
+
trail: trail.slice(-INTERACTION.eraserTrail)
|
|
1079
|
+
} });
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
case "Panning": {
|
|
1083
|
+
const start = mode.start;
|
|
1084
|
+
const view = ctx.state.view;
|
|
1085
|
+
const sx = (p.x - view.x) * view.zoom;
|
|
1086
|
+
const sy = (p.y - view.y) * view.zoom;
|
|
1087
|
+
const ox = (mode.origin.x - start.x) * start.zoom;
|
|
1088
|
+
const oy = (mode.origin.y - start.y) * start.zoom;
|
|
1089
|
+
setView(ctx, {
|
|
1090
|
+
...view,
|
|
1091
|
+
x: start.x - (sx - ox) / view.zoom,
|
|
1092
|
+
y: start.y - (sy - oy) / view.zoom
|
|
1093
|
+
}, false);
|
|
1094
|
+
set(ctx, { pointer: {
|
|
1095
|
+
x: round(ctx.state.view.x + sx / view.zoom),
|
|
1096
|
+
y: round(ctx.state.view.y + sy / view.zoom)
|
|
1097
|
+
} });
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
const pointerUp = (ctx, p) => {
|
|
1103
|
+
const mode = ctx.state.mode;
|
|
1104
|
+
set(ctx, {
|
|
1105
|
+
mode: { _tag: "Idle" },
|
|
1106
|
+
pointer: p
|
|
1107
|
+
});
|
|
1108
|
+
switch (mode._tag) {
|
|
1109
|
+
case "Idle":
|
|
1110
|
+
case "EditingText":
|
|
1111
|
+
set(ctx, { mode });
|
|
1112
|
+
return;
|
|
1113
|
+
case "Panning": return;
|
|
1114
|
+
case "Placing": {
|
|
1115
|
+
const hit = hitTestIndex(ctx.index, {
|
|
1116
|
+
point: mode.origin,
|
|
1117
|
+
zoom: ctx.state.view.zoom,
|
|
1118
|
+
selection: [],
|
|
1119
|
+
pointer: ctx.state.pointerKind
|
|
1120
|
+
});
|
|
1121
|
+
ctx.actions.push({
|
|
1122
|
+
_tag: "Place",
|
|
1123
|
+
tool: mode.tool,
|
|
1124
|
+
at: roundPoint(mode.origin),
|
|
1125
|
+
shape: hit._tag === "Shape" ? hit.id : null
|
|
1126
|
+
});
|
|
1127
|
+
if (mode.tool === "timestamp") set(ctx, { tool: "select" });
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
case "Pressing":
|
|
1131
|
+
if (mode.target._tag === "Shape") {
|
|
1132
|
+
const group = expand(ctx.index, [mode.target.id]);
|
|
1133
|
+
if (mode.additive && mode.wasSelected) set(ctx, { selection: ctx.state.selection.filter((id) => !group.includes(id)) });
|
|
1134
|
+
else if (!mode.additive && mode.wasSelected && ctx.state.selection.length > group.length) set(ctx, { selection: group });
|
|
1135
|
+
} else if (!mode.additive) set(ctx, { selection: [] });
|
|
1136
|
+
return;
|
|
1137
|
+
case "Marquee": return;
|
|
1138
|
+
case "Dragging": {
|
|
1139
|
+
if (mode.dx === 0 && mode.dy === 0) return;
|
|
1140
|
+
if (mode.copy) {
|
|
1141
|
+
const as = mode.ids.map((id, i) => createdId(mode.transaction, ctx.index.shapes.get(id)?.kind ?? "Sticky", i + 1));
|
|
1142
|
+
if (send(ctx, [{
|
|
1143
|
+
_tag: "Duplicate",
|
|
1144
|
+
ids: mode.ids,
|
|
1145
|
+
dx: mode.dx,
|
|
1146
|
+
dy: mode.dy,
|
|
1147
|
+
as
|
|
1148
|
+
}], { id: mode.transaction }) !== null) set(ctx, { selection: as });
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
const start = boundsOf(ctx.index.shapes, mode.ids);
|
|
1152
|
+
if (start === null) return;
|
|
1153
|
+
send(ctx, [{
|
|
1154
|
+
_tag: "Move",
|
|
1155
|
+
ids: mode.ids,
|
|
1156
|
+
place: {
|
|
1157
|
+
_tag: "At",
|
|
1158
|
+
x: round(start.x + mode.dx),
|
|
1159
|
+
y: round(start.y + mode.dy)
|
|
1160
|
+
}
|
|
1161
|
+
}], { id: mode.transaction });
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
case "Resizing":
|
|
1165
|
+
if (!jsonEqual(mode.box, mode.start)) send(ctx, [{
|
|
1166
|
+
_tag: "Resize",
|
|
1167
|
+
ids: mode.ids,
|
|
1168
|
+
box: mode.box
|
|
1169
|
+
}], { id: mode.transaction });
|
|
1170
|
+
return;
|
|
1171
|
+
case "Creating": {
|
|
1172
|
+
const box = mode.dragged ? {
|
|
1173
|
+
...mode.box,
|
|
1174
|
+
w: Math.max(mode.box.w, 16),
|
|
1175
|
+
h: Math.max(mode.box.h, 16)
|
|
1176
|
+
} : null;
|
|
1177
|
+
createDraft(ctx, mode.draft, box, mode.origin, mode.draft === "sticky" || mode.draft === "text");
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
1180
|
+
case "Connecting": {
|
|
1181
|
+
if (mode.arrow === null) {
|
|
1182
|
+
if (!pastThreshold(ctx, mode.origin, p)) return;
|
|
1183
|
+
const id = newId(ctx.state, "Arrow");
|
|
1184
|
+
const style = ctx.state.style;
|
|
1185
|
+
const shape = {
|
|
1186
|
+
kind: "Arrow",
|
|
1187
|
+
id,
|
|
1188
|
+
from: mode.from,
|
|
1189
|
+
to: mode.to,
|
|
1190
|
+
...opt("color", style.color),
|
|
1191
|
+
...opt("start", style.start),
|
|
1192
|
+
...opt("end", style.end),
|
|
1193
|
+
...opt("route", style.route),
|
|
1194
|
+
...opt("dash", style.dash)
|
|
1195
|
+
};
|
|
1196
|
+
if (send(ctx, [{
|
|
1197
|
+
_tag: "Create",
|
|
1198
|
+
shape
|
|
1199
|
+
}]) !== null) set(ctx, {
|
|
1200
|
+
selection: [id],
|
|
1201
|
+
tool: "select"
|
|
1202
|
+
});
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
const arrow = ctx.index.shapes.get(mode.arrow);
|
|
1206
|
+
if (arrow?.kind !== "Arrow") return;
|
|
1207
|
+
const patch = mode.moving === "to" ? { to: mode.to } : { from: mode.from };
|
|
1208
|
+
if (!jsonEqual(mode.moving === "to" ? arrow.to : arrow.from, mode.moving === "to" ? mode.to : mode.from)) send(ctx, [{
|
|
1209
|
+
_tag: "Update",
|
|
1210
|
+
id: arrow.id,
|
|
1211
|
+
patch
|
|
1212
|
+
}]);
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
case "Inking": {
|
|
1216
|
+
const style = ctx.state.style;
|
|
1217
|
+
send(ctx, [{
|
|
1218
|
+
_tag: "Create",
|
|
1219
|
+
shape: {
|
|
1220
|
+
kind: "Ink",
|
|
1221
|
+
id: newId(ctx.state, "Ink"),
|
|
1222
|
+
points: mode.points,
|
|
1223
|
+
...opt("color", style.color),
|
|
1224
|
+
...opt("size", style.size),
|
|
1225
|
+
...opt("brush", style.brush)
|
|
1226
|
+
}
|
|
1227
|
+
}]);
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
case "Erasing": {
|
|
1231
|
+
const ids = unlocked(ctx.index, existing(ctx.index, mode.ids));
|
|
1232
|
+
if (ids.length > 0) {
|
|
1233
|
+
if (send(ctx, [{
|
|
1234
|
+
_tag: "Delete",
|
|
1235
|
+
ids
|
|
1236
|
+
}]) !== null) announce(ctx, ids.length === 1 ? "Erased 1 shape" : `Erased ${ids.length} shapes`);
|
|
1237
|
+
}
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
/** Drops a gesture without sending anything. */
|
|
1243
|
+
const cancelGesture = (ctx) => {
|
|
1244
|
+
const mode = ctx.state.mode;
|
|
1245
|
+
switch (mode._tag) {
|
|
1246
|
+
case "Idle": return false;
|
|
1247
|
+
case "EditingText":
|
|
1248
|
+
finishEditing(ctx);
|
|
1249
|
+
return true;
|
|
1250
|
+
case "Marquee":
|
|
1251
|
+
set(ctx, {
|
|
1252
|
+
mode: { _tag: "Idle" },
|
|
1253
|
+
selection: mode.base
|
|
1254
|
+
});
|
|
1255
|
+
return true;
|
|
1256
|
+
case "Panning":
|
|
1257
|
+
set(ctx, { mode: { _tag: "Idle" } });
|
|
1258
|
+
setView(ctx, mode.start, false);
|
|
1259
|
+
return true;
|
|
1260
|
+
default:
|
|
1261
|
+
set(ctx, { mode: { _tag: "Idle" } });
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
const gestureActive = (mode) => mode._tag !== "Idle" && mode._tag !== "EditingText";
|
|
1266
|
+
/** Re-runs the gesture at the last pointer after modifiers change. */
|
|
1267
|
+
const refreshGesture = (ctx) => {
|
|
1268
|
+
const p = ctx.state.pointer;
|
|
1269
|
+
if (p !== null && gestureActive(ctx.state.mode) && ctx.state.mode._tag !== "Inking" && ctx.state.mode._tag !== "Erasing") pointerMove(ctx, p, [], ctx.state.pointerKind);
|
|
1270
|
+
};
|
|
1271
|
+
const NUDGE = {
|
|
1272
|
+
"nudge-left": {
|
|
1273
|
+
dx: -INTERACTION.nudge,
|
|
1274
|
+
dy: 0
|
|
1275
|
+
},
|
|
1276
|
+
"nudge-right": {
|
|
1277
|
+
dx: INTERACTION.nudge,
|
|
1278
|
+
dy: 0
|
|
1279
|
+
},
|
|
1280
|
+
"nudge-up": {
|
|
1281
|
+
dx: 0,
|
|
1282
|
+
dy: -INTERACTION.nudge
|
|
1283
|
+
},
|
|
1284
|
+
"nudge-down": {
|
|
1285
|
+
dx: 0,
|
|
1286
|
+
dy: INTERACTION.nudge
|
|
1287
|
+
},
|
|
1288
|
+
"nudge-left-large": {
|
|
1289
|
+
dx: -INTERACTION.nudgeLarge,
|
|
1290
|
+
dy: 0
|
|
1291
|
+
},
|
|
1292
|
+
"nudge-right-large": {
|
|
1293
|
+
dx: INTERACTION.nudgeLarge,
|
|
1294
|
+
dy: 0
|
|
1295
|
+
},
|
|
1296
|
+
"nudge-up-large": {
|
|
1297
|
+
dx: 0,
|
|
1298
|
+
dy: -INTERACTION.nudgeLarge
|
|
1299
|
+
},
|
|
1300
|
+
"nudge-down-large": {
|
|
1301
|
+
dx: 0,
|
|
1302
|
+
dy: INTERACTION.nudgeLarge
|
|
1303
|
+
}
|
|
1304
|
+
};
|
|
1305
|
+
const ALIGN = {
|
|
1306
|
+
"align-left": "left",
|
|
1307
|
+
"align-center": "center",
|
|
1308
|
+
"align-right": "right",
|
|
1309
|
+
"align-top": "top",
|
|
1310
|
+
"align-middle": "middle",
|
|
1311
|
+
"align-bottom": "bottom"
|
|
1312
|
+
};
|
|
1313
|
+
const REORDER = {
|
|
1314
|
+
"bring-forward": "forward",
|
|
1315
|
+
"send-backward": "backward",
|
|
1316
|
+
"bring-to-front": "front",
|
|
1317
|
+
"send-to-back": "back"
|
|
1318
|
+
};
|
|
1319
|
+
const clipboardOf = (ctx) => {
|
|
1320
|
+
const ids = new Set(ctx.state.selection);
|
|
1321
|
+
const shapes = ctx.index.ordered.filter((shape) => ids.has(shape.id));
|
|
1322
|
+
if (shapes.length === 0) return null;
|
|
1323
|
+
const text = shapes.filter(hasText).map((shape) => shape.text).filter((t) => t !== "").join("\n");
|
|
1324
|
+
return {
|
|
1325
|
+
clipboard: {
|
|
1326
|
+
kind: "hyper-canvas",
|
|
1327
|
+
version: 1,
|
|
1328
|
+
shapes
|
|
1329
|
+
},
|
|
1330
|
+
text
|
|
1331
|
+
};
|
|
1332
|
+
};
|
|
1333
|
+
const undoOrRedo = (ctx, which) => {
|
|
1334
|
+
const known = new Set(ctx.board.transactions.map((record) => record.id));
|
|
1335
|
+
for (const a of ctx.actions) if (a._tag === "Apply") known.add(a.transaction.id);
|
|
1336
|
+
const stack = which === "undo" ? ctx.state.undo : ctx.state.redo;
|
|
1337
|
+
const entry = stack[stack.length - 1];
|
|
1338
|
+
if (entry === void 0) return;
|
|
1339
|
+
if (!known.has(entry.transaction)) {
|
|
1340
|
+
const kept = stack.filter((e) => known.has(e.transaction));
|
|
1341
|
+
set(ctx, which === "undo" ? { undo: kept } : { redo: kept });
|
|
1342
|
+
announce(ctx, which === "undo" ? "Can't undo: that change is too old" : "Can't redo: that change is too old");
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
const record = ctx.board.transactions.find((r) => r.id === entry.transaction);
|
|
1346
|
+
const skips = record === void 0 ? /* @__PURE__ */ new Set() : revertSkips(ctx.index.shapes, record, (sid) => ctx.held.has(sid));
|
|
1347
|
+
const nothingLeft = () => {
|
|
1348
|
+
if (record === void 0 || skips.size === 0) return false;
|
|
1349
|
+
if (record.changes.every((c) => skips.has(c.op === "set" ? c.shape.id : c.id))) return true;
|
|
1350
|
+
if (ctx.actions.some((a) => a._tag === "Apply")) return false;
|
|
1351
|
+
const planned = planTransaction(ctx.fonts, ctx.board, {
|
|
1352
|
+
id: "undo-check",
|
|
1353
|
+
commands: [{
|
|
1354
|
+
_tag: "Revert",
|
|
1355
|
+
transaction: record.id
|
|
1356
|
+
}],
|
|
1357
|
+
mode: "commit"
|
|
1358
|
+
}, selfOf(ctx.state).actor, ctx.presence, ctx.state.session);
|
|
1359
|
+
return "error" in planned && (planned.error._tag === "Locked" || planned.error._tag === "Claimed");
|
|
1360
|
+
};
|
|
1361
|
+
if (nothingLeft()) {
|
|
1362
|
+
const [first = ""] = skips;
|
|
1363
|
+
announce(ctx, `Can't ${which === "undo" ? "undo" : "redo"}: ${ctx.held.get(first) ?? "a shape it changed is locked"}`);
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
const id = send(ctx, [{
|
|
1367
|
+
_tag: "Revert",
|
|
1368
|
+
transaction: entry.transaction
|
|
1369
|
+
}], { history: "none" });
|
|
1370
|
+
if (id === null) return;
|
|
1371
|
+
const rest = stack.slice(0, -1);
|
|
1372
|
+
const moved = {
|
|
1373
|
+
transaction: id,
|
|
1374
|
+
restores: entry.transaction
|
|
1375
|
+
};
|
|
1376
|
+
set(ctx, which === "undo" ? {
|
|
1377
|
+
undo: rest,
|
|
1378
|
+
redo: pushEntry(ctx.state.redo, moved)
|
|
1379
|
+
} : {
|
|
1380
|
+
redo: rest,
|
|
1381
|
+
undo: pushEntry(ctx.state.undo, moved)
|
|
1382
|
+
});
|
|
1383
|
+
const done = which === "undo" ? "Undone" : "Redone";
|
|
1384
|
+
announce(ctx, skips.size === 0 ? done : skips.size === 1 ? `${done}, except 1 shape that is locked or being edited` : `${done}, except ${skips.size} shapes that are locked or being edited`);
|
|
1385
|
+
};
|
|
1386
|
+
const cycleSelection = (ctx, direction) => {
|
|
1387
|
+
const order = readingOrder(ctx.index);
|
|
1388
|
+
if (order.length === 0) return;
|
|
1389
|
+
const current = ctx.state.selection;
|
|
1390
|
+
const positions = current.map((id) => order.indexOf(id)).filter((i) => i >= 0);
|
|
1391
|
+
let i = positions.length === 0 ? direction === 1 ? -1 : order.length : direction === 1 ? Math.max(...positions) : Math.min(...positions);
|
|
1392
|
+
for (let n = 0; n < order.length; n++) {
|
|
1393
|
+
i = (i + direction + order.length) % order.length;
|
|
1394
|
+
const id = order[i];
|
|
1395
|
+
if (id !== void 0 && !current.includes(id)) {
|
|
1396
|
+
set(ctx, { selection: expand(ctx.index, [id]) });
|
|
1397
|
+
break;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
announceSelection(ctx);
|
|
1401
|
+
};
|
|
1402
|
+
/**
|
|
1403
|
+
* Space held: the Hand tool until it is released. Key repeats are used and
|
|
1404
|
+
* ignored; during another gesture Space only stops the page from scrolling.
|
|
1405
|
+
*/
|
|
1406
|
+
const holdSpace = (ctx) => {
|
|
1407
|
+
const { mode, spaceTool, tool } = ctx.state;
|
|
1408
|
+
if (spaceTool !== null) return true;
|
|
1409
|
+
if (mode._tag !== "Idle") return gestureActive(mode);
|
|
1410
|
+
set(ctx, {
|
|
1411
|
+
spaceTool: tool,
|
|
1412
|
+
tool: "hand",
|
|
1413
|
+
hover: { _tag: "Nothing" }
|
|
1414
|
+
});
|
|
1415
|
+
return true;
|
|
1416
|
+
};
|
|
1417
|
+
/** Space released: back to the held tool. A pan under way finishes on pointer up. */
|
|
1418
|
+
const releaseSpace = (ctx) => {
|
|
1419
|
+
const held = ctx.state.spaceTool;
|
|
1420
|
+
if (held === null) return;
|
|
1421
|
+
set(ctx, {
|
|
1422
|
+
tool: held,
|
|
1423
|
+
spaceTool: null
|
|
1424
|
+
});
|
|
1425
|
+
};
|
|
1426
|
+
const runAction = (ctx, id) => {
|
|
1427
|
+
const can = inspectShapes(ctx.index, ctx.state.selection).can;
|
|
1428
|
+
if (!isEnabled(id, ctx.state, ctx.index, can)) return false;
|
|
1429
|
+
if (changesSelection(id) && refuse(ctx, ctx.state.selection)) return true;
|
|
1430
|
+
const tool = TOOL_OF[id];
|
|
1431
|
+
if (tool !== void 0) {
|
|
1432
|
+
finishEditing(ctx);
|
|
1433
|
+
cancelGesture(ctx);
|
|
1434
|
+
set(ctx, {
|
|
1435
|
+
tool,
|
|
1436
|
+
spaceTool: null,
|
|
1437
|
+
mode: { _tag: "Idle" },
|
|
1438
|
+
hover: { _tag: "Nothing" }
|
|
1439
|
+
});
|
|
1440
|
+
announce(ctx, `${labelOf(id)} tool`);
|
|
1441
|
+
return true;
|
|
1442
|
+
}
|
|
1443
|
+
const selection = ctx.state.selection;
|
|
1444
|
+
const nudge = NUDGE[id];
|
|
1445
|
+
if (nudge !== void 0) {
|
|
1446
|
+
const box = boundsOf(ctx.index.shapes, selection);
|
|
1447
|
+
if (box === null) return false;
|
|
1448
|
+
send(ctx, [{
|
|
1449
|
+
_tag: "Move",
|
|
1450
|
+
ids: selection,
|
|
1451
|
+
place: {
|
|
1452
|
+
_tag: "At",
|
|
1453
|
+
x: round(box.x + nudge.dx),
|
|
1454
|
+
y: round(box.y + nudge.dy)
|
|
1455
|
+
}
|
|
1456
|
+
}]);
|
|
1457
|
+
return true;
|
|
1458
|
+
}
|
|
1459
|
+
const align = ALIGN[id];
|
|
1460
|
+
if (align !== void 0) {
|
|
1461
|
+
if (send(ctx, [{
|
|
1462
|
+
_tag: "Align",
|
|
1463
|
+
ids: selection,
|
|
1464
|
+
to: align
|
|
1465
|
+
}]) !== null) announce(ctx, labelOf(id));
|
|
1466
|
+
return true;
|
|
1467
|
+
}
|
|
1468
|
+
const reorder = REORDER[id];
|
|
1469
|
+
if (reorder !== void 0) {
|
|
1470
|
+
if (send(ctx, [{
|
|
1471
|
+
_tag: "Reorder",
|
|
1472
|
+
ids: selection,
|
|
1473
|
+
to: reorder
|
|
1474
|
+
}]) !== null) announce(ctx, labelOf(id));
|
|
1475
|
+
return true;
|
|
1476
|
+
}
|
|
1477
|
+
switch (id) {
|
|
1478
|
+
case "cancel":
|
|
1479
|
+
if (cancelGesture(ctx)) return true;
|
|
1480
|
+
if (selection.length > 0) {
|
|
1481
|
+
set(ctx, { selection: [] });
|
|
1482
|
+
announceSelection(ctx);
|
|
1483
|
+
} else {
|
|
1484
|
+
set(ctx, {
|
|
1485
|
+
tool: "select",
|
|
1486
|
+
spaceTool: null
|
|
1487
|
+
});
|
|
1488
|
+
announce(ctx, "Select tool");
|
|
1489
|
+
}
|
|
1490
|
+
return true;
|
|
1491
|
+
case "delete":
|
|
1492
|
+
if (send(ctx, [{
|
|
1493
|
+
_tag: "Delete",
|
|
1494
|
+
ids: selection
|
|
1495
|
+
}]) === null) return true;
|
|
1496
|
+
set(ctx, { selection: [] });
|
|
1497
|
+
announce(ctx, selection.length === 1 ? "Deleted 1 shape" : `Deleted ${selection.length} shapes`);
|
|
1498
|
+
return true;
|
|
1499
|
+
case "select-all":
|
|
1500
|
+
finishEditing(ctx);
|
|
1501
|
+
set(ctx, {
|
|
1502
|
+
selection: ctx.index.ordered.map((shape) => shape.id),
|
|
1503
|
+
tool: "select"
|
|
1504
|
+
});
|
|
1505
|
+
announceSelection(ctx);
|
|
1506
|
+
return true;
|
|
1507
|
+
case "select-next":
|
|
1508
|
+
cycleSelection(ctx, 1);
|
|
1509
|
+
return true;
|
|
1510
|
+
case "select-previous":
|
|
1511
|
+
cycleSelection(ctx, -1);
|
|
1512
|
+
return true;
|
|
1513
|
+
case "edit-text": {
|
|
1514
|
+
const only = selection.length === 1 ? selection[0] : void 0;
|
|
1515
|
+
if (only !== void 0 && startEditing(ctx, only)) return true;
|
|
1516
|
+
const draft = DRAFT_OF[ctx.state.tool];
|
|
1517
|
+
if (draft === void 0) return false;
|
|
1518
|
+
createDraft(ctx, draft, null, center(ctx.state.view), true);
|
|
1519
|
+
return true;
|
|
1520
|
+
}
|
|
1521
|
+
case "undo":
|
|
1522
|
+
case "redo":
|
|
1523
|
+
finishEditing(ctx);
|
|
1524
|
+
undoOrRedo(ctx, id);
|
|
1525
|
+
return true;
|
|
1526
|
+
case "copy":
|
|
1527
|
+
case "cut": {
|
|
1528
|
+
const data = clipboardOf(ctx);
|
|
1529
|
+
if (data === null) return false;
|
|
1530
|
+
ctx.actions.push({
|
|
1531
|
+
_tag: "Clipboard",
|
|
1532
|
+
data
|
|
1533
|
+
});
|
|
1534
|
+
if (id === "cut" && send(ctx, [{
|
|
1535
|
+
_tag: "Delete",
|
|
1536
|
+
ids: selection
|
|
1537
|
+
}]) !== null) set(ctx, { selection: [] });
|
|
1538
|
+
return true;
|
|
1539
|
+
}
|
|
1540
|
+
case "duplicate": {
|
|
1541
|
+
const as = selection.map((sid, i) => newId(ctx.state, ctx.index.shapes.get(sid)?.kind ?? "Sticky", i + 1));
|
|
1542
|
+
if (send(ctx, [{
|
|
1543
|
+
_tag: "Duplicate",
|
|
1544
|
+
ids: selection,
|
|
1545
|
+
as
|
|
1546
|
+
}]) !== null) set(ctx, { selection: as });
|
|
1547
|
+
return true;
|
|
1548
|
+
}
|
|
1549
|
+
case "connect": {
|
|
1550
|
+
const [a, b] = selection;
|
|
1551
|
+
if (a === void 0 || b === void 0) return false;
|
|
1552
|
+
const arrow = newId(ctx.state, "Arrow");
|
|
1553
|
+
const style = ctx.state.style;
|
|
1554
|
+
const shape = {
|
|
1555
|
+
kind: "Arrow",
|
|
1556
|
+
id: arrow,
|
|
1557
|
+
from: {
|
|
1558
|
+
_tag: "Shape",
|
|
1559
|
+
id: a,
|
|
1560
|
+
at: null
|
|
1561
|
+
},
|
|
1562
|
+
to: {
|
|
1563
|
+
_tag: "Shape",
|
|
1564
|
+
id: b,
|
|
1565
|
+
at: null
|
|
1566
|
+
},
|
|
1567
|
+
...opt("color", style.color),
|
|
1568
|
+
...opt("start", style.start),
|
|
1569
|
+
...opt("end", style.end),
|
|
1570
|
+
...opt("route", style.route),
|
|
1571
|
+
...opt("dash", style.dash)
|
|
1572
|
+
};
|
|
1573
|
+
if (send(ctx, [{
|
|
1574
|
+
_tag: "Create",
|
|
1575
|
+
shape
|
|
1576
|
+
}]) !== null) {
|
|
1577
|
+
set(ctx, { selection: [arrow] });
|
|
1578
|
+
announce(ctx, "Connected");
|
|
1579
|
+
}
|
|
1580
|
+
return true;
|
|
1581
|
+
}
|
|
1582
|
+
case "group":
|
|
1583
|
+
if (send(ctx, [{
|
|
1584
|
+
_tag: "Group",
|
|
1585
|
+
ids: selection
|
|
1586
|
+
}]) !== null) announce(ctx, "Grouped");
|
|
1587
|
+
return true;
|
|
1588
|
+
case "ungroup":
|
|
1589
|
+
if (send(ctx, [{
|
|
1590
|
+
_tag: "Ungroup",
|
|
1591
|
+
ids: selection
|
|
1592
|
+
}]) !== null) announce(ctx, "Ungrouped");
|
|
1593
|
+
return true;
|
|
1594
|
+
case "lock":
|
|
1595
|
+
case "unlock":
|
|
1596
|
+
if (send(ctx, [{
|
|
1597
|
+
_tag: "Lock",
|
|
1598
|
+
ids: selection,
|
|
1599
|
+
locked: id === "lock"
|
|
1600
|
+
}]) !== null) announce(ctx, id === "lock" ? "Locked" : "Unlocked");
|
|
1601
|
+
return true;
|
|
1602
|
+
case "distribute-horizontal":
|
|
1603
|
+
case "distribute-vertical":
|
|
1604
|
+
if (send(ctx, [{
|
|
1605
|
+
_tag: "Distribute",
|
|
1606
|
+
ids: selection,
|
|
1607
|
+
axis: id === "distribute-horizontal" ? "horizontal" : "vertical"
|
|
1608
|
+
}]) !== null) announce(ctx, labelOf(id));
|
|
1609
|
+
return true;
|
|
1610
|
+
case "zoom-in":
|
|
1611
|
+
zoomTo(ctx, "in");
|
|
1612
|
+
return true;
|
|
1613
|
+
case "zoom-out":
|
|
1614
|
+
zoomTo(ctx, "out");
|
|
1615
|
+
return true;
|
|
1616
|
+
case "zoom-reset":
|
|
1617
|
+
zoomTo(ctx, "reset");
|
|
1618
|
+
return true;
|
|
1619
|
+
case "zoom-to-fit":
|
|
1620
|
+
zoomTo(ctx, "fit");
|
|
1621
|
+
return true;
|
|
1622
|
+
case "zoom-to-selection":
|
|
1623
|
+
zoomTo(ctx, "selection");
|
|
1624
|
+
return true;
|
|
1625
|
+
case "toggle-grid":
|
|
1626
|
+
set(ctx, { grid: ctx.state.grid === null ? 16 : null });
|
|
1627
|
+
announce(ctx, ctx.state.grid === null ? "Grid off" : "Grid on");
|
|
1628
|
+
return true;
|
|
1629
|
+
default: return false;
|
|
1630
|
+
}
|
|
1631
|
+
};
|
|
1632
|
+
const STYLE_KEYS = {
|
|
1633
|
+
Sticky: [
|
|
1634
|
+
"color",
|
|
1635
|
+
"size",
|
|
1636
|
+
"align"
|
|
1637
|
+
],
|
|
1638
|
+
Box: [
|
|
1639
|
+
"color",
|
|
1640
|
+
"size",
|
|
1641
|
+
"align",
|
|
1642
|
+
"fill",
|
|
1643
|
+
"dash"
|
|
1644
|
+
],
|
|
1645
|
+
Text: [
|
|
1646
|
+
"color",
|
|
1647
|
+
"size",
|
|
1648
|
+
"align"
|
|
1649
|
+
],
|
|
1650
|
+
Arrow: [
|
|
1651
|
+
"color",
|
|
1652
|
+
"start",
|
|
1653
|
+
"end",
|
|
1654
|
+
"route",
|
|
1655
|
+
"dash"
|
|
1656
|
+
],
|
|
1657
|
+
Ink: [
|
|
1658
|
+
"color",
|
|
1659
|
+
"size",
|
|
1660
|
+
"brush"
|
|
1661
|
+
],
|
|
1662
|
+
Image: [],
|
|
1663
|
+
Timestamp: []
|
|
1664
|
+
};
|
|
1665
|
+
const restyle = (ctx, style) => {
|
|
1666
|
+
const merged = {
|
|
1667
|
+
color: style.color ?? ctx.state.style.color,
|
|
1668
|
+
size: style.size ?? ctx.state.style.size,
|
|
1669
|
+
align: style.align ?? ctx.state.style.align,
|
|
1670
|
+
fill: style.fill ?? ctx.state.style.fill,
|
|
1671
|
+
dash: style.dash ?? ctx.state.style.dash,
|
|
1672
|
+
brush: style.brush ?? ctx.state.style.brush,
|
|
1673
|
+
start: style.start ?? ctx.state.style.start,
|
|
1674
|
+
end: style.end ?? ctx.state.style.end,
|
|
1675
|
+
route: style.route ?? ctx.state.style.route
|
|
1676
|
+
};
|
|
1677
|
+
set(ctx, { style: merged });
|
|
1678
|
+
if (ctx.state.readOnly) return;
|
|
1679
|
+
const commands = [];
|
|
1680
|
+
for (const id of ctx.state.selection) {
|
|
1681
|
+
const shape = ctx.index.shapes.get(id);
|
|
1682
|
+
if (shape === void 0 || shape.locked) continue;
|
|
1683
|
+
const keys = STYLE_KEYS[shape.kind];
|
|
1684
|
+
const has = (key) => keys.includes(key);
|
|
1685
|
+
const patch = {
|
|
1686
|
+
...has("color") ? opt("color", style.color) : {},
|
|
1687
|
+
...has("size") ? opt("size", style.size) : {},
|
|
1688
|
+
...has("align") ? opt("align", style.align) : {},
|
|
1689
|
+
...has("fill") ? opt("fill", style.fill) : {},
|
|
1690
|
+
...has("dash") ? opt("dash", style.dash) : {},
|
|
1691
|
+
...has("brush") ? opt("brush", style.brush) : {},
|
|
1692
|
+
...has("start") ? opt("start", style.start) : {},
|
|
1693
|
+
...has("end") ? opt("end", style.end) : {},
|
|
1694
|
+
...has("route") ? opt("route", style.route) : {}
|
|
1695
|
+
};
|
|
1696
|
+
if (Object.keys(patch).length > 0) commands.push({
|
|
1697
|
+
_tag: "Update",
|
|
1698
|
+
id,
|
|
1699
|
+
patch
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
send(ctx, commands);
|
|
1703
|
+
};
|
|
1704
|
+
const paste = (ctx, input) => {
|
|
1705
|
+
const clipboard = input.clipboard;
|
|
1706
|
+
if (clipboard.shapes.length === 0) return null;
|
|
1707
|
+
if (input.ids.length !== 0 && input.ids.length !== clipboard.shapes.length) return { error: invalid(["input", "ids"], `one id per clipboard shape (${clipboard.shapes.length}), or none`, "Pass a fresh id for each clipboard shape, or an empty list to have them generated.") };
|
|
1708
|
+
const local = toMap(clipboard.shapes);
|
|
1709
|
+
const box = boundsOf(local, local.keys());
|
|
1710
|
+
if (box === null) return null;
|
|
1711
|
+
const ids = input.ids.length === 0 ? clipboard.shapes.map((shape, i) => newId(ctx.state, shape.kind, i + 1)) : input.ids;
|
|
1712
|
+
let place = null;
|
|
1713
|
+
if (input.at !== null) place = {
|
|
1714
|
+
_tag: "At",
|
|
1715
|
+
x: round(input.at.x - box.w / 2),
|
|
1716
|
+
y: round(input.at.y - box.h / 2)
|
|
1717
|
+
};
|
|
1718
|
+
else if (clipboard.shapes.every((shape) => {
|
|
1719
|
+
const there = ctx.index.shapes.get(shape.id);
|
|
1720
|
+
return there !== void 0 && there.x === shape.x && there.y === shape.y;
|
|
1721
|
+
})) place = {
|
|
1722
|
+
_tag: "At",
|
|
1723
|
+
x: round(box.x + DEFAULTS.duplicate),
|
|
1724
|
+
y: round(box.y + DEFAULTS.duplicate)
|
|
1725
|
+
};
|
|
1726
|
+
if (send(ctx, [place === null ? {
|
|
1727
|
+
_tag: "Paste",
|
|
1728
|
+
clipboard,
|
|
1729
|
+
as: ids
|
|
1730
|
+
} : {
|
|
1731
|
+
_tag: "Paste",
|
|
1732
|
+
clipboard,
|
|
1733
|
+
place,
|
|
1734
|
+
as: ids
|
|
1735
|
+
}]) !== null) {
|
|
1736
|
+
set(ctx, {
|
|
1737
|
+
selection: ids,
|
|
1738
|
+
tool: "select"
|
|
1739
|
+
});
|
|
1740
|
+
announce(ctx, ids.length === 1 ? "Pasted 1 shape" : `Pasted ${ids.length} shapes`);
|
|
1741
|
+
}
|
|
1742
|
+
return { ok: void 0 };
|
|
1743
|
+
};
|
|
1744
|
+
const pasteText = (ctx, text, at) => {
|
|
1745
|
+
if (text === "") return;
|
|
1746
|
+
const id = newId(ctx.state, "Text");
|
|
1747
|
+
const p = at ?? center(ctx.state.view);
|
|
1748
|
+
const style = ctx.state.style;
|
|
1749
|
+
const shape = {
|
|
1750
|
+
kind: "Text",
|
|
1751
|
+
id,
|
|
1752
|
+
text: text.slice(0, LIMITS.text),
|
|
1753
|
+
...opt("color", style.color),
|
|
1754
|
+
...opt("size", style.size),
|
|
1755
|
+
...opt("align", style.align)
|
|
1756
|
+
};
|
|
1757
|
+
if (send(ctx, [{
|
|
1758
|
+
_tag: "Create",
|
|
1759
|
+
shape,
|
|
1760
|
+
place: {
|
|
1761
|
+
_tag: "At",
|
|
1762
|
+
x: round(p.x),
|
|
1763
|
+
y: round(p.y)
|
|
1764
|
+
}
|
|
1765
|
+
}]) !== null) set(ctx, {
|
|
1766
|
+
selection: [id],
|
|
1767
|
+
tool: "select"
|
|
1768
|
+
});
|
|
1769
|
+
};
|
|
1770
|
+
/** `text` cut to at most `max` UTF-16 code units without splitting a surrogate pair. */
|
|
1771
|
+
const clip = (text, max) => {
|
|
1772
|
+
if (text.length <= max) return text;
|
|
1773
|
+
const last = text.charCodeAt(max - 1);
|
|
1774
|
+
return text.slice(0, last >= 55296 && last <= 56319 ? max - 1 : max);
|
|
1775
|
+
};
|
|
1776
|
+
const rollback = (ctx, transaction) => {
|
|
1777
|
+
const prefix = `${ctx.state.session}-`;
|
|
1778
|
+
const n = transaction.slice(prefix.length);
|
|
1779
|
+
if (!transaction.startsWith(prefix) || !/^(?:0|[1-9]\d*)$/.test(n) || Number(n) >= ctx.state.seq) return;
|
|
1780
|
+
const take = (stack) => {
|
|
1781
|
+
const at = stack.findIndex((entry) => entry.transaction === transaction);
|
|
1782
|
+
if (at === -1) return {
|
|
1783
|
+
rest: stack,
|
|
1784
|
+
entry: null
|
|
1785
|
+
};
|
|
1786
|
+
return {
|
|
1787
|
+
rest: [...stack.slice(0, at), ...stack.slice(at + 1)],
|
|
1788
|
+
entry: stack[at] ?? null
|
|
1789
|
+
};
|
|
1790
|
+
};
|
|
1791
|
+
const fromUndo = take(ctx.state.undo);
|
|
1792
|
+
const fromRedo = take(ctx.state.redo);
|
|
1793
|
+
let undo = fromUndo.rest;
|
|
1794
|
+
let redo = fromRedo.rest;
|
|
1795
|
+
const undoRestores = fromUndo.entry?.restores ?? null;
|
|
1796
|
+
const redoRestores = fromRedo.entry?.restores ?? null;
|
|
1797
|
+
if (undoRestores !== null) redo = pushEntry(redo, {
|
|
1798
|
+
transaction: undoRestores,
|
|
1799
|
+
restores: null
|
|
1800
|
+
});
|
|
1801
|
+
if (redoRestores !== null) undo = pushEntry(undo, {
|
|
1802
|
+
transaction: redoRestores,
|
|
1803
|
+
restores: null
|
|
1804
|
+
});
|
|
1805
|
+
set(ctx, {
|
|
1806
|
+
undo,
|
|
1807
|
+
redo
|
|
1808
|
+
});
|
|
1809
|
+
const mode = ctx.state.mode;
|
|
1810
|
+
if (mode._tag === "EditingText" && mode.created === transaction) set(ctx, { mode: { _tag: "Idle" } });
|
|
1811
|
+
announce(ctx, "That change could not be saved");
|
|
1812
|
+
};
|
|
1813
|
+
const step = (fonts, state, board, index, input, presence) => {
|
|
1814
|
+
const ctx = {
|
|
1815
|
+
fonts,
|
|
1816
|
+
board,
|
|
1817
|
+
index,
|
|
1818
|
+
held: heldBy(state, presence),
|
|
1819
|
+
presence,
|
|
1820
|
+
state,
|
|
1821
|
+
actions: [],
|
|
1822
|
+
handled: false
|
|
1823
|
+
};
|
|
1824
|
+
const kept = existing(index, state.selection);
|
|
1825
|
+
if (kept.length !== state.selection.length) set(ctx, { selection: kept });
|
|
1826
|
+
if (ctx.state.mode._tag === "EditingText" && !index.shapes.has(ctx.state.mode.id)) set(ctx, { mode: { _tag: "Idle" } });
|
|
1827
|
+
const lost = claimsOf(ctx.state.mode).find((id) => ctx.held.has(id));
|
|
1828
|
+
if (lost !== void 0) {
|
|
1829
|
+
const mode = ctx.state.mode;
|
|
1830
|
+
if (mode._tag === "EditingText" && mode.draft !== mode.original) ctx.actions.push({
|
|
1831
|
+
_tag: "Unsaved",
|
|
1832
|
+
id: mode.id,
|
|
1833
|
+
text: mode.draft
|
|
1834
|
+
});
|
|
1835
|
+
set(ctx, { mode: { _tag: "Idle" } });
|
|
1836
|
+
refuse(ctx, [lost]);
|
|
1837
|
+
}
|
|
1838
|
+
const before = presenceOf(state, index);
|
|
1839
|
+
switch (input._tag) {
|
|
1840
|
+
case "PointerDown":
|
|
1841
|
+
pointerDown(ctx, input);
|
|
1842
|
+
break;
|
|
1843
|
+
case "PointerMove":
|
|
1844
|
+
set(ctx, { modifiers: input.modifiers });
|
|
1845
|
+
pointerMove(ctx, input.point, input.samples, input.pointer);
|
|
1846
|
+
ctx.handled = true;
|
|
1847
|
+
break;
|
|
1848
|
+
case "PointerUp":
|
|
1849
|
+
set(ctx, {
|
|
1850
|
+
modifiers: input.modifiers,
|
|
1851
|
+
pointerKind: input.pointer
|
|
1852
|
+
});
|
|
1853
|
+
if (gestureActive(ctx.state.mode)) pointerMove(ctx, input.point, [], input.pointer);
|
|
1854
|
+
pointerUp(ctx, input.point);
|
|
1855
|
+
ctx.handled = true;
|
|
1856
|
+
break;
|
|
1857
|
+
case "PointerLeave":
|
|
1858
|
+
if (!gestureActive(ctx.state.mode)) set(ctx, {
|
|
1859
|
+
pointer: null,
|
|
1860
|
+
hover: { _tag: "Nothing" }
|
|
1861
|
+
});
|
|
1862
|
+
break;
|
|
1863
|
+
case "PointerCancel":
|
|
1864
|
+
ctx.handled = cancelGesture(ctx);
|
|
1865
|
+
break;
|
|
1866
|
+
case "DoubleClick": {
|
|
1867
|
+
if (ctx.state.readOnly) break;
|
|
1868
|
+
finishEditing(ctx);
|
|
1869
|
+
const hit = probeHit(ctx, input.point, ctx.state.pointerKind);
|
|
1870
|
+
if (hit._tag === "Shape") ctx.handled = startEditing(ctx, hit.id);
|
|
1871
|
+
else if (hit._tag === "Nothing" && ctx.state.tool === "select") {
|
|
1872
|
+
createDraft(ctx, "text", null, input.point, true);
|
|
1873
|
+
ctx.handled = true;
|
|
1874
|
+
}
|
|
1875
|
+
break;
|
|
1876
|
+
}
|
|
1877
|
+
case "KeyDown": {
|
|
1878
|
+
set(ctx, { modifiers: input.modifiers });
|
|
1879
|
+
const mode = ctx.state.mode;
|
|
1880
|
+
if (mode._tag === "EditingText") {
|
|
1881
|
+
if (input.key === "Escape" || input.key === "Enter" && input.modifiers.meta) {
|
|
1882
|
+
finishEditing(ctx);
|
|
1883
|
+
ctx.handled = true;
|
|
1884
|
+
}
|
|
1885
|
+
break;
|
|
1886
|
+
}
|
|
1887
|
+
if (input.key === " ") {
|
|
1888
|
+
ctx.handled = holdSpace(ctx);
|
|
1889
|
+
break;
|
|
1890
|
+
}
|
|
1891
|
+
if (gestureActive(mode)) {
|
|
1892
|
+
if (input.key === "Escape") ctx.handled = cancelGesture(ctx);
|
|
1893
|
+
else refreshGesture(ctx);
|
|
1894
|
+
break;
|
|
1895
|
+
}
|
|
1896
|
+
for (const id of actionsForKey(input.key, input.modifiers)) if (runAction(ctx, id)) {
|
|
1897
|
+
ctx.handled = true;
|
|
1898
|
+
break;
|
|
1899
|
+
}
|
|
1900
|
+
break;
|
|
1901
|
+
}
|
|
1902
|
+
case "KeyUp":
|
|
1903
|
+
set(ctx, { modifiers: input.modifiers });
|
|
1904
|
+
if (input.key === " " && ctx.state.spaceTool !== null) {
|
|
1905
|
+
releaseSpace(ctx);
|
|
1906
|
+
ctx.handled = true;
|
|
1907
|
+
break;
|
|
1908
|
+
}
|
|
1909
|
+
refreshGesture(ctx);
|
|
1910
|
+
ctx.handled = gestureActive(ctx.state.mode);
|
|
1911
|
+
break;
|
|
1912
|
+
case "Modifiers":
|
|
1913
|
+
set(ctx, { modifiers: input.modifiers });
|
|
1914
|
+
refreshGesture(ctx);
|
|
1915
|
+
break;
|
|
1916
|
+
case "Wheel": {
|
|
1917
|
+
const view = ctx.state.view;
|
|
1918
|
+
if (input.modifiers.meta) {
|
|
1919
|
+
const factor = clamp(1 - input.dy * ZOOM.step, .5, 2);
|
|
1920
|
+
setView(ctx, zoomAt(view, input.point, view.zoom * factor), false);
|
|
1921
|
+
} else setView(ctx, {
|
|
1922
|
+
...view,
|
|
1923
|
+
x: view.x + input.dx / view.zoom,
|
|
1924
|
+
y: view.y + input.dy / view.zoom
|
|
1925
|
+
}, false);
|
|
1926
|
+
ctx.handled = true;
|
|
1927
|
+
break;
|
|
1928
|
+
}
|
|
1929
|
+
case "Zoom":
|
|
1930
|
+
setView(ctx, zoomAt(ctx.state.view, input.point, ctx.state.view.zoom * input.factor), false);
|
|
1931
|
+
ctx.handled = true;
|
|
1932
|
+
break;
|
|
1933
|
+
case "Pan": {
|
|
1934
|
+
const view = ctx.state.view;
|
|
1935
|
+
setView(ctx, {
|
|
1936
|
+
...view,
|
|
1937
|
+
x: view.x + input.dx / view.zoom,
|
|
1938
|
+
y: view.y + input.dy / view.zoom
|
|
1939
|
+
}, false);
|
|
1940
|
+
ctx.handled = true;
|
|
1941
|
+
break;
|
|
1942
|
+
}
|
|
1943
|
+
case "ZoomTo":
|
|
1944
|
+
zoomTo(ctx, input.to);
|
|
1945
|
+
ctx.handled = true;
|
|
1946
|
+
break;
|
|
1947
|
+
case "SetView":
|
|
1948
|
+
set(ctx, { view: input.view });
|
|
1949
|
+
ctx.handled = true;
|
|
1950
|
+
break;
|
|
1951
|
+
case "Follow":
|
|
1952
|
+
set(ctx, { following: input.actor });
|
|
1953
|
+
ctx.handled = true;
|
|
1954
|
+
break;
|
|
1955
|
+
case "Inset":
|
|
1956
|
+
set(ctx, { inset: input.inset });
|
|
1957
|
+
ctx.handled = true;
|
|
1958
|
+
break;
|
|
1959
|
+
case "Blur":
|
|
1960
|
+
if (gestureActive(ctx.state.mode)) cancelGesture(ctx);
|
|
1961
|
+
releaseSpace(ctx);
|
|
1962
|
+
set(ctx, { modifiers: NO_MODIFIERS });
|
|
1963
|
+
break;
|
|
1964
|
+
case "Text": {
|
|
1965
|
+
const mode = ctx.state.mode;
|
|
1966
|
+
if (mode._tag === "EditingText") {
|
|
1967
|
+
set(ctx, { mode: {
|
|
1968
|
+
...mode,
|
|
1969
|
+
draft: input.value.slice(0, LIMITS.text)
|
|
1970
|
+
} });
|
|
1971
|
+
ctx.handled = true;
|
|
1972
|
+
}
|
|
1973
|
+
break;
|
|
1974
|
+
}
|
|
1975
|
+
case "Action":
|
|
1976
|
+
if (ctx.state.mode._tag === "EditingText" && input.action !== "cancel") finishEditing(ctx);
|
|
1977
|
+
ctx.handled = runAction(ctx, input.action);
|
|
1978
|
+
break;
|
|
1979
|
+
case "Style":
|
|
1980
|
+
restyle(ctx, input.style);
|
|
1981
|
+
ctx.handled = true;
|
|
1982
|
+
break;
|
|
1983
|
+
case "Select":
|
|
1984
|
+
finishEditing(ctx);
|
|
1985
|
+
cancelGesture(ctx);
|
|
1986
|
+
set(ctx, { selection: existing(index, input.ids) });
|
|
1987
|
+
ctx.handled = true;
|
|
1988
|
+
break;
|
|
1989
|
+
case "Copy":
|
|
1990
|
+
if (ctx.state.mode._tag !== "EditingText") ctx.handled = runAction(ctx, "copy");
|
|
1991
|
+
break;
|
|
1992
|
+
case "Cut":
|
|
1993
|
+
if (ctx.state.mode._tag !== "EditingText") ctx.handled = runAction(ctx, "cut");
|
|
1994
|
+
break;
|
|
1995
|
+
case "Paste": {
|
|
1996
|
+
if (ctx.state.mode._tag === "EditingText" || ctx.state.readOnly) break;
|
|
1997
|
+
const result = paste(ctx, input);
|
|
1998
|
+
if (result !== null && "error" in result) return result;
|
|
1999
|
+
ctx.handled = result !== null;
|
|
2000
|
+
break;
|
|
2001
|
+
}
|
|
2002
|
+
case "PasteText":
|
|
2003
|
+
if (ctx.state.mode._tag === "EditingText" || ctx.state.readOnly) break;
|
|
2004
|
+
pasteText(ctx, input.text, input.at);
|
|
2005
|
+
ctx.handled = input.text !== "";
|
|
2006
|
+
break;
|
|
2007
|
+
case "Run":
|
|
2008
|
+
if (ctx.state.readOnly) {
|
|
2009
|
+
announce(ctx, "This board is read-only");
|
|
2010
|
+
break;
|
|
2011
|
+
}
|
|
2012
|
+
finishEditing(ctx);
|
|
2013
|
+
ctx.handled = send(ctx, input.commands, { reason: input.reason }) !== null;
|
|
2014
|
+
break;
|
|
2015
|
+
case "Rejected":
|
|
2016
|
+
rollback(ctx, input.transaction);
|
|
2017
|
+
ctx.handled = true;
|
|
2018
|
+
break;
|
|
2019
|
+
case "ReadOnly":
|
|
2020
|
+
if (input.readOnly && !ctx.state.readOnly) set(ctx, { mode: { _tag: "Idle" } });
|
|
2021
|
+
set(ctx, {
|
|
2022
|
+
readOnly: input.readOnly,
|
|
2023
|
+
tool: input.readOnly && ctx.state.tool !== "select" && ctx.state.tool !== "hand" ? "select" : ctx.state.tool,
|
|
2024
|
+
spaceTool: input.readOnly && ctx.state.spaceTool !== null && ctx.state.spaceTool !== "hand" ? "select" : ctx.state.spaceTool
|
|
2025
|
+
});
|
|
2026
|
+
ctx.handled = true;
|
|
2027
|
+
break;
|
|
2028
|
+
case "Sync":
|
|
2029
|
+
if (ctx.state.following !== null && !presence.some((entry) => entry.actor === ctx.state.following)) {
|
|
2030
|
+
set(ctx, { following: null });
|
|
2031
|
+
announce(ctx, "Stopped following: they left the board");
|
|
2032
|
+
}
|
|
2033
|
+
ctx.handled = true;
|
|
2034
|
+
break;
|
|
2035
|
+
case "BeginUpload":
|
|
2036
|
+
if (ctx.state.readOnly) break;
|
|
2037
|
+
beginUpload(ctx, input.at, input.uploads);
|
|
2038
|
+
ctx.handled = true;
|
|
2039
|
+
break;
|
|
2040
|
+
case "EndUpload":
|
|
2041
|
+
set(ctx, { uploads: ctx.state.uploads.filter((upload) => upload.id !== input.id) });
|
|
2042
|
+
ctx.handled = true;
|
|
2043
|
+
break;
|
|
2044
|
+
case "InsertImage":
|
|
2045
|
+
if (ctx.state.readOnly || input.images.length === 0) break;
|
|
2046
|
+
insertImages(ctx, input.at, input.images);
|
|
2047
|
+
ctx.handled = true;
|
|
2048
|
+
break;
|
|
2049
|
+
case "Chat":
|
|
2050
|
+
set(ctx, { chat: input.text === null ? null : clip(input.text, LIMITS.chat) });
|
|
2051
|
+
ctx.handled = true;
|
|
2052
|
+
}
|
|
2053
|
+
const leader = presence.findLast((entry) => entry.actor === ctx.state.following);
|
|
2054
|
+
if (leader !== void 0) {
|
|
2055
|
+
const view = tidyView(followView(ctx.state.view, ctx.state.inset, leader.viewport));
|
|
2056
|
+
if (!jsonEqual(view, ctx.state.view)) {
|
|
2057
|
+
set(ctx, { view });
|
|
2058
|
+
ctx.actions.push({
|
|
2059
|
+
_tag: "Viewport",
|
|
2060
|
+
view,
|
|
2061
|
+
animate: true
|
|
2062
|
+
});
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
const after = presenceOf(ctx.state, index);
|
|
2066
|
+
if (!jsonEqual(before, after)) ctx.actions.push({
|
|
2067
|
+
_tag: "Presence",
|
|
2068
|
+
presence: after
|
|
2069
|
+
});
|
|
2070
|
+
return { ok: {
|
|
2071
|
+
state: ctx.state,
|
|
2072
|
+
actions: ctx.actions,
|
|
2073
|
+
handled: ctx.handled,
|
|
2074
|
+
cursor: cursorOf(ctx.state, index, ctx.held)
|
|
2075
|
+
} };
|
|
2076
|
+
};
|
|
2077
|
+
export { heldBy, initialState, step };
|