@daasboard/editor 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/camera/CameraInput.d.ts +106 -0
- package/dist/camera/CameraInput.js +398 -0
- package/dist/clipboard/ObjectClipboard.d.ts +40 -0
- package/dist/clipboard/ObjectClipboard.js +243 -0
- package/dist/clipboard/PasteCommitError.d.ts +4 -0
- package/dist/clipboard/PasteCommitError.js +7 -0
- package/dist/clipboard/carrier.d.ts +5 -0
- package/dist/clipboard/carrier.js +59 -0
- package/dist/clipboard/objects.d.ts +21 -0
- package/dist/clipboard/objects.js +362 -0
- package/dist/clipboard/placement.d.ts +11 -0
- package/dist/clipboard/placement.js +21 -0
- package/dist/clipboard/systemImages.d.ts +1 -0
- package/dist/clipboard/systemImages.js +24 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +22 -0
- package/dist/input/TouchInput.d.ts +159 -0
- package/dist/input/TouchInput.js +656 -0
- package/dist/input/visiblePart.d.ts +8 -0
- package/dist/input/visiblePart.js +15 -0
- package/dist/interaction/InteractionLayer.d.ts +83 -0
- package/dist/interaction/InteractionLayer.js +520 -0
- package/dist/props/PropertyController.d.ts +130 -0
- package/dist/props/PropertyController.js +1448 -0
- package/dist/props/PropertyRegistry.d.ts +38 -0
- package/dist/props/PropertyRegistry.js +21 -0
- package/dist/props/builtinProperties.d.ts +23 -0
- package/dist/props/builtinProperties.js +350 -0
- package/dist/props/defaults.d.ts +6 -0
- package/dist/props/defaults.js +29 -0
- package/dist/selection/SelectionModel.d.ts +25 -0
- package/dist/selection/SelectionModel.js +79 -0
- package/dist/text/CanvasEditorView.d.ts +99 -0
- package/dist/text/CanvasEditorView.js +308 -0
- package/dist/text/InputSink.d.ts +157 -0
- package/dist/text/InputSink.js +841 -0
- package/dist/text/TextEditSession.d.ts +173 -0
- package/dist/text/TextEditSession.js +1328 -0
- package/dist/text/clipboard.d.ts +11 -0
- package/dist/text/clipboard.js +489 -0
- package/dist/text/commands.d.ts +48 -0
- package/dist/text/commands.js +329 -0
- package/dist/text/contextMirror.d.ts +51 -0
- package/dist/text/contextMirror.js +292 -0
- package/dist/text/flat.d.ts +35 -0
- package/dist/text/flat.js +556 -0
- package/dist/text/graphemes.d.ts +16 -0
- package/dist/text/graphemes.js +219 -0
- package/dist/text/index.d.ts +13 -0
- package/dist/text/index.js +13 -0
- package/dist/text/inputReducer.d.ts +24 -0
- package/dist/text/inputReducer.js +44 -0
- package/dist/text/keymap.d.ts +17 -0
- package/dist/text/keymap.js +148 -0
- package/dist/text/schema.d.ts +12 -0
- package/dist/text/schema.js +44 -0
- package/dist/text/sessionFocus.d.ts +20 -0
- package/dist/text/sessionFocus.js +51 -0
- package/dist/text/types.d.ts +114 -0
- package/dist/text/types.js +1 -0
- package/dist/tools/CreateTool.d.ts +78 -0
- package/dist/tools/CreateTool.js +258 -0
- package/dist/tools/SelectTool.d.ts +155 -0
- package/dist/tools/SelectTool.js +808 -0
- package/dist/tools/ToolManager.d.ts +63 -0
- package/dist/tools/ToolManager.js +146 -0
- package/dist/tools/connectorEndpoints.d.ts +17 -0
- package/dist/tools/connectorEndpoints.js +53 -0
- package/dist/tools/frameRotation.d.ts +8 -0
- package/dist/tools/frameRotation.js +66 -0
- package/dist/tools/gradientHandles.d.ts +67 -0
- package/dist/tools/gradientHandles.js +110 -0
- package/dist/tools/presetPaths.d.ts +5 -0
- package/dist/tools/presetPaths.js +49 -0
- package/dist/tools/snapResize.d.ts +7 -0
- package/dist/tools/snapResize.js +65 -0
- package/dist/tools/snapping.d.ts +30 -0
- package/dist/tools/snapping.js +280 -0
- package/dist/tools/transform.d.ts +52 -0
- package/dist/tools/transform.js +277 -0
- package/package.json +36 -3
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { browserTimers } from "@daasboard/render-core";
|
|
2
|
+
import { KINETIC_MAX_SPEED, KINETIC_MIN_SPEED, KINETIC_MS, releaseSpeed } from "../camera/CameraInput.js";
|
|
3
|
+
export const TOUCH_SLOP_PX = 10;
|
|
4
|
+
export const PEN_SLOP_PX = 6;
|
|
5
|
+
export const LONG_PRESS_MS = 500;
|
|
6
|
+
export const DOUBLE_TAP_MS = 300;
|
|
7
|
+
export const DOUBLE_TAP_PX = 24;
|
|
8
|
+
export const PINCH_MIN_PX = 16;
|
|
9
|
+
export const FLING_FIT_MS = 80;
|
|
10
|
+
export const FLING_MEAN_MS = 30;
|
|
11
|
+
export const FLING_MIN_SPEED = 0.2;
|
|
12
|
+
export const FLING_RELEASE_MS = 50;
|
|
13
|
+
export const FLING_DECAY = 0.995;
|
|
14
|
+
const FLING_STOP_SPEED = 0.01;
|
|
15
|
+
const FLING_MAX_STEP_MS = 50;
|
|
16
|
+
const HISTORY_MS = 200;
|
|
17
|
+
const DEFAULT_SETTINGS = Object.freeze({ canvasDrag: "select", objectDrag: "move" });
|
|
18
|
+
export function normalizeTouchOptions(value, name = "touch") {
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
return DEFAULT_SETTINGS;
|
|
21
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
22
|
+
throw new TypeError(`${name} must be an object ({ canvasDrag?: "select" | "pan"; objectDrag?: "move" | "pan" }) or undefined, not ${describe(value)}`);
|
|
23
|
+
}
|
|
24
|
+
for (const key of Object.keys(value)) {
|
|
25
|
+
if (key !== "canvasDrag" && key !== "objectDrag")
|
|
26
|
+
throw new TypeError(`${name}.${key} is not an option; ${name} takes canvasDrag and objectDrag`);
|
|
27
|
+
}
|
|
28
|
+
const { canvasDrag = "select", objectDrag = "move" } = value;
|
|
29
|
+
if (canvasDrag !== "select" && canvasDrag !== "pan")
|
|
30
|
+
throw new TypeError(`${name}.canvasDrag must be "select" or "pan", not ${describe(canvasDrag)}`);
|
|
31
|
+
if (objectDrag !== "move" && objectDrag !== "pan")
|
|
32
|
+
throw new TypeError(`${name}.objectDrag must be "move" or "pan", not ${describe(objectDrag)}`);
|
|
33
|
+
return canvasDrag === "select" && objectDrag === "move" ? DEFAULT_SETTINGS : Object.freeze({ canvasDrag, objectDrag });
|
|
34
|
+
}
|
|
35
|
+
function describe(v) {
|
|
36
|
+
if (typeof v === "string")
|
|
37
|
+
return JSON.stringify(v);
|
|
38
|
+
if (v === null || Array.isArray(v))
|
|
39
|
+
return v === null ? "null" : "an array";
|
|
40
|
+
return typeof v === "object" || typeof v === "function" ? `a ${typeof v}` : String(v);
|
|
41
|
+
}
|
|
42
|
+
const IDLE = { kind: "idle" };
|
|
43
|
+
export function flingVelocity(samples, end) {
|
|
44
|
+
const last = samples[samples.length - 1];
|
|
45
|
+
if (!last || !(end - last.t < FLING_RELEASE_MS))
|
|
46
|
+
return null;
|
|
47
|
+
const fit = samples.filter((s) => s.t > last.t - FLING_FIT_MS);
|
|
48
|
+
if (fit.length < 2)
|
|
49
|
+
return null;
|
|
50
|
+
let tm = 0, xm = 0, ym = 0;
|
|
51
|
+
for (const s of fit) {
|
|
52
|
+
tm += s.t;
|
|
53
|
+
xm += s.x;
|
|
54
|
+
ym += s.y;
|
|
55
|
+
}
|
|
56
|
+
tm /= fit.length;
|
|
57
|
+
xm /= fit.length;
|
|
58
|
+
ym /= fit.length;
|
|
59
|
+
let tt = 0, tx = 0, ty = 0;
|
|
60
|
+
for (const s of fit) {
|
|
61
|
+
const dt = s.t - tm;
|
|
62
|
+
tt += dt * dt;
|
|
63
|
+
tx += dt * (s.x - xm);
|
|
64
|
+
ty += dt * (s.y - ym);
|
|
65
|
+
}
|
|
66
|
+
if (!(tt > 0))
|
|
67
|
+
return null;
|
|
68
|
+
const vx = tx / tt, vy = ty / tt;
|
|
69
|
+
const speed = Math.hypot(vx, vy);
|
|
70
|
+
if (!(speed > FLING_MIN_SPEED))
|
|
71
|
+
return null;
|
|
72
|
+
const from = positionAt(samples, last.t - FLING_MEAN_MS);
|
|
73
|
+
const along = ((last.x - from.x) * vx + (last.y - from.y) * vy) / (FLING_MEAN_MS * speed);
|
|
74
|
+
return along > FLING_MIN_SPEED ? { x: vx, y: vy } : null;
|
|
75
|
+
}
|
|
76
|
+
function positionAt(samples, t) {
|
|
77
|
+
let prev = samples[0];
|
|
78
|
+
if (t <= prev.t)
|
|
79
|
+
return { x: prev.x, y: prev.y };
|
|
80
|
+
for (const s of samples) {
|
|
81
|
+
if (s.t >= t) {
|
|
82
|
+
const u = s.t > prev.t ? (t - prev.t) / (s.t - prev.t) : 1;
|
|
83
|
+
return { x: prev.x + (s.x - prev.x) * u, y: prev.y + (s.y - prev.y) * u };
|
|
84
|
+
}
|
|
85
|
+
prev = s;
|
|
86
|
+
}
|
|
87
|
+
return { x: prev.x, y: prev.y };
|
|
88
|
+
}
|
|
89
|
+
const centroid = (contacts) => {
|
|
90
|
+
let x = 0, y = 0;
|
|
91
|
+
for (const c of contacts) {
|
|
92
|
+
x += c.at.x;
|
|
93
|
+
y += c.at.y;
|
|
94
|
+
}
|
|
95
|
+
return { x: x / contacts.length, y: y / contacts.length };
|
|
96
|
+
};
|
|
97
|
+
const spread = (contacts) => contacts.length === 2 ? Math.hypot(contacts[0].at.x - contacts[1].at.x, contacts[0].at.y - contacts[1].at.y) : null;
|
|
98
|
+
const kindOf = (pointerType) => pointerType === "touch" || pointerType === "pen" ? pointerType : null;
|
|
99
|
+
const pressOf = (t) => (typeof t === "string" ? { target: t } : t);
|
|
100
|
+
export class TouchInput {
|
|
101
|
+
o;
|
|
102
|
+
settings;
|
|
103
|
+
stats = { taps: 0, doubleTaps: 0, longPresses: 0, drags: 0, pans: 0, pinches: 0, flings: 0, finishes: 0, cancels: 0 };
|
|
104
|
+
host;
|
|
105
|
+
timers;
|
|
106
|
+
gesture = IDLE;
|
|
107
|
+
ignored = new Map();
|
|
108
|
+
handedOver = false;
|
|
109
|
+
tapping = null;
|
|
110
|
+
lastTap = null;
|
|
111
|
+
fling = null;
|
|
112
|
+
zoomFrame = null;
|
|
113
|
+
finish = null;
|
|
114
|
+
disposed = false;
|
|
115
|
+
constructor(o) {
|
|
116
|
+
this.o = o;
|
|
117
|
+
this.host = o.host;
|
|
118
|
+
this.timers = o.timers ?? browserTimers();
|
|
119
|
+
this.settings = normalizeTouchOptions(o.touch);
|
|
120
|
+
}
|
|
121
|
+
get active() {
|
|
122
|
+
return this.gesture.kind !== "idle";
|
|
123
|
+
}
|
|
124
|
+
get moving() {
|
|
125
|
+
return this.fling !== null || this.finish !== null;
|
|
126
|
+
}
|
|
127
|
+
touching() {
|
|
128
|
+
for (const pointer of this.ignored.values())
|
|
129
|
+
if (pointer === "touch")
|
|
130
|
+
return true;
|
|
131
|
+
return this.contacts().some((c) => c.pointer === "touch");
|
|
132
|
+
}
|
|
133
|
+
pointerDown(e) {
|
|
134
|
+
const pointer = kindOf(e.pointerType);
|
|
135
|
+
if (this.disposed || !pointer)
|
|
136
|
+
return false;
|
|
137
|
+
if (this.contact(e.pointerId))
|
|
138
|
+
return true;
|
|
139
|
+
this.unignore(e.pointerId);
|
|
140
|
+
if (e.isPrimary)
|
|
141
|
+
this.forget(pointer);
|
|
142
|
+
const c = this.newContact(e, pointer);
|
|
143
|
+
return pointer === "pen" ? this.penDown(c, e.button) : this.fingerDown(c);
|
|
144
|
+
}
|
|
145
|
+
pointerMove(e) {
|
|
146
|
+
const pointer = kindOf(e.pointerType);
|
|
147
|
+
if (this.disposed || !pointer)
|
|
148
|
+
return;
|
|
149
|
+
if (pointer === "pen" && e.buttons === 0) {
|
|
150
|
+
this.unignore(e.pointerId);
|
|
151
|
+
if (e.isPrimary)
|
|
152
|
+
this.forget("pen");
|
|
153
|
+
}
|
|
154
|
+
if (this.ignored.has(e.pointerId))
|
|
155
|
+
return;
|
|
156
|
+
const g = this.gesture;
|
|
157
|
+
const c = this.contact(e.pointerId);
|
|
158
|
+
if (!c) {
|
|
159
|
+
if (pointer === "pen" && g.kind === "idle" && e.buttons === 0)
|
|
160
|
+
this.host.hover(this.point(e, pointer, e));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const path = this.track(c, e);
|
|
164
|
+
switch (g.kind) {
|
|
165
|
+
case "press":
|
|
166
|
+
if (this.beyond(c, TOUCH_SLOP_PX))
|
|
167
|
+
this.drag(g);
|
|
168
|
+
return;
|
|
169
|
+
case "host":
|
|
170
|
+
if (g.pen && this.beyond(c, PEN_SLOP_PX))
|
|
171
|
+
g.tap = false;
|
|
172
|
+
this.host.move(c.last);
|
|
173
|
+
return;
|
|
174
|
+
case "camera":
|
|
175
|
+
this.follow(g, c, path);
|
|
176
|
+
return;
|
|
177
|
+
default:
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
pointerUp(e) {
|
|
182
|
+
if (this.disposed || this.unignore(e.pointerId))
|
|
183
|
+
return;
|
|
184
|
+
const g = this.gesture;
|
|
185
|
+
const c = this.contact(e.pointerId);
|
|
186
|
+
if (!c)
|
|
187
|
+
return;
|
|
188
|
+
this.track(c, e);
|
|
189
|
+
switch (g.kind) {
|
|
190
|
+
case "press":
|
|
191
|
+
this.endPress(g);
|
|
192
|
+
return;
|
|
193
|
+
case "host":
|
|
194
|
+
this.gesture = IDLE;
|
|
195
|
+
this.host.release(c.last);
|
|
196
|
+
if (g.pen && g.tap)
|
|
197
|
+
this.tapped(c, g.clicks);
|
|
198
|
+
return;
|
|
199
|
+
case "camera":
|
|
200
|
+
this.lift(g, c);
|
|
201
|
+
return;
|
|
202
|
+
case "secondary":
|
|
203
|
+
this.gesture = IDLE;
|
|
204
|
+
return;
|
|
205
|
+
default:
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
pointerCancel(e) {
|
|
210
|
+
if (this.disposed || this.unignore(e.pointerId) || !this.contact(e.pointerId))
|
|
211
|
+
return;
|
|
212
|
+
this.abort(e.pointerId);
|
|
213
|
+
}
|
|
214
|
+
lostPointerCapture(e) {
|
|
215
|
+
if (this.disposed || !this.contact(e.pointerId))
|
|
216
|
+
return;
|
|
217
|
+
this.abort(e.pointerId);
|
|
218
|
+
}
|
|
219
|
+
key(key) {
|
|
220
|
+
if (this.disposed || this.gesture.kind !== "press")
|
|
221
|
+
return false;
|
|
222
|
+
if (key === "Escape")
|
|
223
|
+
this.abort(null);
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
cancelPress() {
|
|
227
|
+
if (!this.disposed && this.gesture.kind === "press")
|
|
228
|
+
this.abort(null);
|
|
229
|
+
}
|
|
230
|
+
cancel() {
|
|
231
|
+
if (this.disposed)
|
|
232
|
+
return;
|
|
233
|
+
if (this.tapping)
|
|
234
|
+
this.tapping.rejected = true;
|
|
235
|
+
this.stop();
|
|
236
|
+
if (this.gesture.kind !== "idle")
|
|
237
|
+
this.abort(null);
|
|
238
|
+
}
|
|
239
|
+
flush() {
|
|
240
|
+
if (this.zoomFrame !== null) {
|
|
241
|
+
this.timers.cancelFrame(this.zoomFrame);
|
|
242
|
+
this.zoomFrame = null;
|
|
243
|
+
}
|
|
244
|
+
const g = this.gesture;
|
|
245
|
+
if (g.kind !== "camera" || g.zoomDue === null)
|
|
246
|
+
return;
|
|
247
|
+
const at = g.zoomDue;
|
|
248
|
+
g.zoomDue = null;
|
|
249
|
+
const dist = spread(g.contacts);
|
|
250
|
+
const prev = g.dist;
|
|
251
|
+
g.dist = dist;
|
|
252
|
+
if (dist === null || prev === null || dist === prev || dist < PINCH_MIN_PX || prev < PINCH_MIN_PX || !this.host.enabled("zoom"))
|
|
253
|
+
return;
|
|
254
|
+
const ln = Math.log(dist / prev);
|
|
255
|
+
this.host.zoom(ln, g.ref);
|
|
256
|
+
g.scales.push({ at, ln });
|
|
257
|
+
if (g.scales.length > 16)
|
|
258
|
+
g.scales.shift();
|
|
259
|
+
}
|
|
260
|
+
stop() {
|
|
261
|
+
const moved = this.moving;
|
|
262
|
+
if (this.fling) {
|
|
263
|
+
if (this.fling.frame !== null)
|
|
264
|
+
this.timers.cancelFrame(this.fling.frame);
|
|
265
|
+
this.fling = null;
|
|
266
|
+
this.host.motion("fling", false);
|
|
267
|
+
}
|
|
268
|
+
if (this.finish) {
|
|
269
|
+
if (this.finish.frame !== null)
|
|
270
|
+
this.timers.cancelFrame(this.finish.frame);
|
|
271
|
+
this.finish = null;
|
|
272
|
+
}
|
|
273
|
+
return moved;
|
|
274
|
+
}
|
|
275
|
+
dispose() {
|
|
276
|
+
if (this.disposed)
|
|
277
|
+
return;
|
|
278
|
+
this.cancel();
|
|
279
|
+
this.ignored.clear();
|
|
280
|
+
this.disposed = true;
|
|
281
|
+
}
|
|
282
|
+
contacts() {
|
|
283
|
+
const g = this.gesture;
|
|
284
|
+
return g.kind === "idle" ? [] : g.kind === "camera" ? g.contacts : [g.contact];
|
|
285
|
+
}
|
|
286
|
+
contact(id) {
|
|
287
|
+
return this.contacts().find((c) => c.id === id);
|
|
288
|
+
}
|
|
289
|
+
newContact(e, pointer) {
|
|
290
|
+
const down = this.point(e, pointer, e);
|
|
291
|
+
const at = this.o.local(e.clientX, e.clientY);
|
|
292
|
+
return { id: e.pointerId, pointer, down, downAt: at, last: down, at, trail: [{ t: down.timeStamp, x: at.x, y: at.y }] };
|
|
293
|
+
}
|
|
294
|
+
point(e, pointer, s) {
|
|
295
|
+
return { pointerId: e.pointerId, pointer, clientX: s.clientX, clientY: s.clientY, shiftKey: !!e.shiftKey, altKey: !!e.altKey, ctrlKey: !!e.ctrlKey, metaKey: !!e.metaKey, timeStamp: this.time(s.timeStamp ?? e.timeStamp) };
|
|
296
|
+
}
|
|
297
|
+
track(c, e) {
|
|
298
|
+
const coalesced = e.getCoalescedEvents?.() ?? [];
|
|
299
|
+
const samples = coalesced.length ? coalesced : [e];
|
|
300
|
+
const path = [];
|
|
301
|
+
for (const s of samples) {
|
|
302
|
+
const at = this.o.local(s.clientX, s.clientY);
|
|
303
|
+
const t = this.time(s.timeStamp ?? e.timeStamp);
|
|
304
|
+
path.push({ t, x: at.x, y: at.y });
|
|
305
|
+
}
|
|
306
|
+
const at = this.o.local(e.clientX, e.clientY);
|
|
307
|
+
const t = this.time(e.timeStamp);
|
|
308
|
+
const end = path[path.length - 1];
|
|
309
|
+
if (end.x !== at.x || end.y !== at.y || end.t !== t)
|
|
310
|
+
path.push({ t, x: at.x, y: at.y });
|
|
311
|
+
c.last = this.point(e, c.pointer, e);
|
|
312
|
+
c.at = at;
|
|
313
|
+
c.trail.push(...path);
|
|
314
|
+
while (c.trail.length > 2 && c.trail[0].t < t - HISTORY_MS)
|
|
315
|
+
c.trail.shift();
|
|
316
|
+
return path;
|
|
317
|
+
}
|
|
318
|
+
beyond(c, slop) {
|
|
319
|
+
return Math.max(Math.abs(c.at.x - c.downAt.x), Math.abs(c.at.y - c.downAt.y)) >= slop;
|
|
320
|
+
}
|
|
321
|
+
ignore(c) {
|
|
322
|
+
this.ignored.set(c.id, c.pointer);
|
|
323
|
+
}
|
|
324
|
+
unignore(id) {
|
|
325
|
+
if (!this.ignored.delete(id))
|
|
326
|
+
return false;
|
|
327
|
+
if (!this.ignoring("touch"))
|
|
328
|
+
this.handedOver = false;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
ignoring(pointer) {
|
|
332
|
+
for (const p of this.ignored.values())
|
|
333
|
+
if (p === pointer)
|
|
334
|
+
return true;
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
forget(pointer) {
|
|
338
|
+
for (const [id, p] of this.ignored)
|
|
339
|
+
if (p === pointer)
|
|
340
|
+
this.unignore(id);
|
|
341
|
+
}
|
|
342
|
+
penHeld() {
|
|
343
|
+
return this.contacts().some((c) => c.pointer === "pen");
|
|
344
|
+
}
|
|
345
|
+
clicksFor(c) {
|
|
346
|
+
const t = this.lastTap;
|
|
347
|
+
this.lastTap = null;
|
|
348
|
+
return t && t.pointer === c.pointer && c.down.timeStamp - t.at <= DOUBLE_TAP_MS && Math.hypot(c.downAt.x - t.x, c.downAt.y - t.y) <= DOUBLE_TAP_PX ? t.clicks + 1 : 1;
|
|
349
|
+
}
|
|
350
|
+
stopMotion() {
|
|
351
|
+
const own = this.stop();
|
|
352
|
+
const host = this.host.stopMotion();
|
|
353
|
+
return own || host;
|
|
354
|
+
}
|
|
355
|
+
fingerDown(c) {
|
|
356
|
+
if (this.penHeld()) {
|
|
357
|
+
this.ignore(c);
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
const g = this.gesture;
|
|
361
|
+
switch (g.kind) {
|
|
362
|
+
case "idle":
|
|
363
|
+
if (this.ignoring("touch")) {
|
|
364
|
+
this.ignore(c);
|
|
365
|
+
return !this.handedOver;
|
|
366
|
+
}
|
|
367
|
+
return this.firstFinger(c);
|
|
368
|
+
case "press":
|
|
369
|
+
this.clearTimer(g);
|
|
370
|
+
return this.secondFinger(g.contact, c);
|
|
371
|
+
case "host":
|
|
372
|
+
this.stats.cancels++;
|
|
373
|
+
this.gesture = IDLE;
|
|
374
|
+
this.host.cancel();
|
|
375
|
+
return this.secondFinger(g.contact, c);
|
|
376
|
+
case "camera":
|
|
377
|
+
if (g.contacts.length === 1) {
|
|
378
|
+
g.contacts.push(c);
|
|
379
|
+
g.ref = centroid(g.contacts);
|
|
380
|
+
g.dist = spread(g.contacts);
|
|
381
|
+
g.zoomDue = null;
|
|
382
|
+
this.stats.pinches++;
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
this.abort(null);
|
|
386
|
+
this.ignore(c);
|
|
387
|
+
return true;
|
|
388
|
+
default:
|
|
389
|
+
this.ignore(c);
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
firstFinger(c) {
|
|
394
|
+
const stopper = this.stopMotion();
|
|
395
|
+
const { target, hit } = pressOf(this.host.target(c.down));
|
|
396
|
+
const clicks = this.clicksFor(c);
|
|
397
|
+
const enabled = (f) => this.host.enabled(f);
|
|
398
|
+
if (!enabled("menu") && !enabled("pan") && !enabled("zoom") && (target === "pan" || !enabled("tool"))) {
|
|
399
|
+
this.ignore(c);
|
|
400
|
+
this.handedOver = true;
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
this.gesture = { kind: "press", contact: c, target, hit, clicks, stopper, held: false, menu: false, timer: this.timers.setTimeout(() => this.longPress(), LONG_PRESS_MS) };
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
secondFinger(first, c) {
|
|
407
|
+
if (!this.host.enabled("pan") && !this.host.enabled("zoom")) {
|
|
408
|
+
this.gesture = IDLE;
|
|
409
|
+
this.ignore(first);
|
|
410
|
+
this.ignore(c);
|
|
411
|
+
this.handedOver = true;
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
this.camera([first, c], [{ t: c.down.timeStamp, x: 0, y: 0 }]);
|
|
415
|
+
this.stats.pinches++;
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
longPress() {
|
|
419
|
+
const g = this.gesture;
|
|
420
|
+
if (g.kind !== "press" || g.held)
|
|
421
|
+
return;
|
|
422
|
+
g.timer = null;
|
|
423
|
+
g.held = true;
|
|
424
|
+
this.stats.longPresses++;
|
|
425
|
+
if (g.stopper)
|
|
426
|
+
return;
|
|
427
|
+
g.menu = this.host.enabled("menu");
|
|
428
|
+
if (g.menu && g.target !== "handle" && g.target !== "gradient")
|
|
429
|
+
this.host.longPress(g.contact.down);
|
|
430
|
+
}
|
|
431
|
+
drag(g) {
|
|
432
|
+
this.clearTimer(g);
|
|
433
|
+
const c = g.contact;
|
|
434
|
+
if (this.pans(g.target, g.held)) {
|
|
435
|
+
if (!this.host.enabled("pan"))
|
|
436
|
+
return void this.handOver(c);
|
|
437
|
+
this.camera([c], c.trail.map((s) => ({ t: s.t, x: s.x - c.downAt.x, y: s.y - c.downAt.y })), c.downAt);
|
|
438
|
+
this.stats.pans++;
|
|
439
|
+
this.follow(this.gesture, c, []);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (!this.host.enabled("tool"))
|
|
443
|
+
return void this.handOver(c);
|
|
444
|
+
this.stats.drags++;
|
|
445
|
+
this.gesture = { kind: "host", contact: c, pen: false, tap: false, clicks: g.clicks };
|
|
446
|
+
this.host.press(c.down, g.clicks, g.hit);
|
|
447
|
+
if (this.gesture.kind === "host" && this.gesture.contact === c)
|
|
448
|
+
this.host.move(c.last);
|
|
449
|
+
}
|
|
450
|
+
pans(target, held) {
|
|
451
|
+
if (target === "pan")
|
|
452
|
+
return true;
|
|
453
|
+
if (held)
|
|
454
|
+
return false;
|
|
455
|
+
return (target === "canvas" && this.settings.canvasDrag === "pan") || (target === "object" && this.settings.objectDrag === "pan");
|
|
456
|
+
}
|
|
457
|
+
handOver(c) {
|
|
458
|
+
this.gesture = IDLE;
|
|
459
|
+
this.ignore(c);
|
|
460
|
+
if (c.pointer === "touch")
|
|
461
|
+
this.handedOver = true;
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
endPress(g) {
|
|
465
|
+
this.clearTimer(g);
|
|
466
|
+
this.gesture = IDLE;
|
|
467
|
+
const c = g.contact;
|
|
468
|
+
if (g.stopper)
|
|
469
|
+
return;
|
|
470
|
+
if (g.held) {
|
|
471
|
+
if (g.menu)
|
|
472
|
+
this.host.menu(c.last);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (g.target === "pan" || !this.host.enabled("tool"))
|
|
476
|
+
return;
|
|
477
|
+
const tap = (this.tapping = { rejected: false });
|
|
478
|
+
try {
|
|
479
|
+
this.host.press(c.down, g.clicks, g.hit);
|
|
480
|
+
}
|
|
481
|
+
finally {
|
|
482
|
+
this.tapping = null;
|
|
483
|
+
}
|
|
484
|
+
if (tap.rejected)
|
|
485
|
+
return;
|
|
486
|
+
this.host.release(c.down);
|
|
487
|
+
this.tapped(c, g.clicks);
|
|
488
|
+
}
|
|
489
|
+
tapped(c, clicks) {
|
|
490
|
+
this.stats.taps++;
|
|
491
|
+
this.lastTap = { pointer: c.pointer, at: c.last.timeStamp, x: c.downAt.x, y: c.downAt.y, clicks };
|
|
492
|
+
if (clicks === 2) {
|
|
493
|
+
this.stats.doubleTaps++;
|
|
494
|
+
this.host.doubleTap(c.down);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
clearTimer(g) {
|
|
498
|
+
if (g.timer === null)
|
|
499
|
+
return;
|
|
500
|
+
this.timers.clearTimeout(g.timer);
|
|
501
|
+
g.timer = null;
|
|
502
|
+
}
|
|
503
|
+
penDown(c, button) {
|
|
504
|
+
if (this.gesture.kind !== "idle") {
|
|
505
|
+
if (this.penHeld()) {
|
|
506
|
+
this.ignore(c);
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
this.abort(null);
|
|
510
|
+
}
|
|
511
|
+
const clicks = this.clicksFor(c);
|
|
512
|
+
if (button === 2) {
|
|
513
|
+
if (!this.host.enabled("menu"))
|
|
514
|
+
return this.handOver(c);
|
|
515
|
+
this.stopMotion();
|
|
516
|
+
this.gesture = { kind: "secondary", contact: c };
|
|
517
|
+
this.host.secondary(c.down);
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
if (button !== 0)
|
|
521
|
+
return this.handOver(c);
|
|
522
|
+
this.stopMotion();
|
|
523
|
+
const { target, hit } = pressOf(this.host.target(c.down));
|
|
524
|
+
if (target === "pan") {
|
|
525
|
+
if (!this.host.enabled("pan"))
|
|
526
|
+
return this.handOver(c);
|
|
527
|
+
this.camera([c], [{ t: c.down.timeStamp, x: 0, y: 0 }]);
|
|
528
|
+
this.stats.pans++;
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
if (!this.host.enabled("tool"))
|
|
532
|
+
return this.handOver(c);
|
|
533
|
+
this.gesture = { kind: "host", contact: c, pen: true, tap: true, clicks };
|
|
534
|
+
this.host.press(c.down, clicks, hit);
|
|
535
|
+
return true;
|
|
536
|
+
}
|
|
537
|
+
camera(contacts, samples, ref = centroid(contacts)) {
|
|
538
|
+
this.gesture = { kind: "camera", contacts, ref, dist: spread(contacts), zoomDue: null, moved: { x: 0, y: 0 }, samples, scales: [], pinch: null };
|
|
539
|
+
this.host.motion("pan", true);
|
|
540
|
+
}
|
|
541
|
+
follow(g, c, path) {
|
|
542
|
+
const now = c.at;
|
|
543
|
+
for (const s of path.slice(0, -1)) {
|
|
544
|
+
c.at = { x: s.x, y: s.y };
|
|
545
|
+
const ref = centroid(g.contacts);
|
|
546
|
+
g.samples.push({ t: s.t, x: g.moved.x + ref.x - g.ref.x, y: g.moved.y + ref.y - g.ref.y });
|
|
547
|
+
}
|
|
548
|
+
c.at = now;
|
|
549
|
+
const ref = centroid(g.contacts);
|
|
550
|
+
const dx = ref.x - g.ref.x, dy = ref.y - g.ref.y;
|
|
551
|
+
if ((dx || dy) && this.host.enabled("pan"))
|
|
552
|
+
this.host.pan(dx, dy);
|
|
553
|
+
g.moved = { x: g.moved.x + dx, y: g.moved.y + dy };
|
|
554
|
+
g.ref = ref;
|
|
555
|
+
if (g.contacts.length === 2) {
|
|
556
|
+
g.zoomDue = c.last.timeStamp;
|
|
557
|
+
this.zoomFrame ??= this.timers.requestFrame(() => { this.zoomFrame = null; this.flush(); });
|
|
558
|
+
}
|
|
559
|
+
const t = c.last.timeStamp;
|
|
560
|
+
const prev = g.samples[g.samples.length - 1];
|
|
561
|
+
if (!prev || prev.t !== t || prev.x !== g.moved.x || prev.y !== g.moved.y)
|
|
562
|
+
g.samples.push({ t, x: g.moved.x, y: g.moved.y });
|
|
563
|
+
while (g.samples.length > 2 && g.samples[0].t < t - HISTORY_MS)
|
|
564
|
+
g.samples.shift();
|
|
565
|
+
}
|
|
566
|
+
lift(g, c) {
|
|
567
|
+
this.flush();
|
|
568
|
+
if (g.contacts.length === 2)
|
|
569
|
+
g.pinch = centroid(g.contacts);
|
|
570
|
+
g.contacts = g.contacts.filter((x) => x !== c);
|
|
571
|
+
const end = c.last.timeStamp;
|
|
572
|
+
if (g.contacts.length) {
|
|
573
|
+
g.ref = centroid(g.contacts);
|
|
574
|
+
g.dist = spread(g.contacts);
|
|
575
|
+
g.samples.push({ t: end, x: g.moved.x, y: g.moved.y });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
this.gesture = IDLE;
|
|
579
|
+
this.host.motion("pan", false);
|
|
580
|
+
const speed = releaseSpeed(g.scales, end) * 1000;
|
|
581
|
+
if (this.host.enabled("zoom") && Math.abs(speed) >= KINETIC_MIN_SPEED) {
|
|
582
|
+
const v = Math.max(-KINETIC_MAX_SPEED, Math.min(KINETIC_MAX_SPEED, speed));
|
|
583
|
+
this.finish = { anchor: g.pinch ?? g.ref, start: this.timers.now(), distance: (v * KINETIC_MS) / 3000, moved: 0, frame: null };
|
|
584
|
+
this.finish.frame = this.timers.requestFrame(this.finishStep);
|
|
585
|
+
this.stats.finishes++;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
const v = this.host.enabled("pan") ? flingVelocity(g.samples, end) : null;
|
|
589
|
+
if (!v)
|
|
590
|
+
return;
|
|
591
|
+
this.fling = { vx: v.x, vy: v.y, at: this.timers.now(), frame: null };
|
|
592
|
+
this.fling.frame = this.timers.requestFrame(this.flingStep);
|
|
593
|
+
this.stats.flings++;
|
|
594
|
+
this.host.motion("fling", true);
|
|
595
|
+
}
|
|
596
|
+
flingStep = () => {
|
|
597
|
+
const f = this.fling;
|
|
598
|
+
if (!f)
|
|
599
|
+
return;
|
|
600
|
+
f.frame = null;
|
|
601
|
+
const now = this.timers.now();
|
|
602
|
+
const dt = Math.min(FLING_MAX_STEP_MS, Math.max(0, now - f.at));
|
|
603
|
+
f.at = now;
|
|
604
|
+
if (dt > 0)
|
|
605
|
+
this.host.pan(f.vx * dt, f.vy * dt);
|
|
606
|
+
const decay = Math.pow(FLING_DECAY, dt);
|
|
607
|
+
f.vx *= decay;
|
|
608
|
+
f.vy *= decay;
|
|
609
|
+
if (this.fling !== f)
|
|
610
|
+
return;
|
|
611
|
+
if (Math.abs(f.vx) > FLING_STOP_SPEED || Math.abs(f.vy) > FLING_STOP_SPEED)
|
|
612
|
+
f.frame = this.timers.requestFrame(this.flingStep);
|
|
613
|
+
else {
|
|
614
|
+
this.fling = null;
|
|
615
|
+
this.host.motion("fling", false);
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
finishStep = () => {
|
|
619
|
+
const f = this.finish;
|
|
620
|
+
if (!f)
|
|
621
|
+
return;
|
|
622
|
+
f.frame = null;
|
|
623
|
+
const u = Math.min(1, Math.max(0, (this.timers.now() - f.start) / KINETIC_MS));
|
|
624
|
+
const target = f.distance * (1 - (1 - u) ** 3);
|
|
625
|
+
if (target !== f.moved)
|
|
626
|
+
this.host.zoom(target - f.moved, f.anchor);
|
|
627
|
+
f.moved = target;
|
|
628
|
+
if (this.finish !== f)
|
|
629
|
+
return;
|
|
630
|
+
if (u >= 1)
|
|
631
|
+
this.finish = null;
|
|
632
|
+
else
|
|
633
|
+
f.frame = this.timers.requestFrame(this.finishStep);
|
|
634
|
+
};
|
|
635
|
+
abort(gone) {
|
|
636
|
+
this.flush();
|
|
637
|
+
const g = this.gesture;
|
|
638
|
+
this.gesture = IDLE;
|
|
639
|
+
if (g.kind === "idle")
|
|
640
|
+
return;
|
|
641
|
+
this.stats.cancels++;
|
|
642
|
+
for (const c of g.kind === "camera" ? g.contacts : [g.contact])
|
|
643
|
+
if (c.id !== gone)
|
|
644
|
+
this.ignore(c);
|
|
645
|
+
if (g.kind === "press")
|
|
646
|
+
this.clearTimer(g);
|
|
647
|
+
else if (g.kind === "host")
|
|
648
|
+
this.host.cancel();
|
|
649
|
+
else if (g.kind === "camera")
|
|
650
|
+
this.host.motion("pan", false);
|
|
651
|
+
}
|
|
652
|
+
time(ts) {
|
|
653
|
+
const now = this.timers.now();
|
|
654
|
+
return ts !== undefined && Number.isFinite(ts) && ts > 0 && Math.abs(now - ts) < 10_000 ? Math.min(ts, now) : now;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Point, Rect } from "@daasboard/render-core";
|
|
2
|
+
export declare const CARET_MARGIN_PX = 24;
|
|
3
|
+
export declare function visiblePart(box: Rect, view?: Rect | null): Rect | null;
|
|
4
|
+
export declare function revealCaret(caret: {
|
|
5
|
+
left: number;
|
|
6
|
+
top: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}, visible: Rect, margin?: number): Point;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const CARET_MARGIN_PX = 24;
|
|
2
|
+
export function visiblePart(box, view) {
|
|
3
|
+
const x = Math.max(box.x, view ? view.x : box.x);
|
|
4
|
+
const y = Math.max(box.y, view ? view.y : box.y);
|
|
5
|
+
const right = Math.min(box.x + box.w, view ? view.x + view.w : Infinity);
|
|
6
|
+
const bottom = Math.min(box.y + box.h, view ? view.y + view.h : Infinity);
|
|
7
|
+
return right > x && bottom > y ? { x, y, w: right - x, h: bottom - y } : null;
|
|
8
|
+
}
|
|
9
|
+
export function revealCaret(caret, visible, margin = CARET_MARGIN_PX) {
|
|
10
|
+
const axis = (lo, hi, from, to) => (hi - lo > to - from ? (from + to - lo - hi) / 2 : lo < from ? from - lo : hi > to ? to - hi : 0);
|
|
11
|
+
return {
|
|
12
|
+
x: axis(caret.left - margin, caret.left + margin, visible.x, visible.x + visible.w),
|
|
13
|
+
y: axis(caret.top - margin, caret.top + caret.height + margin, visible.y, visible.y + visible.h),
|
|
14
|
+
};
|
|
15
|
+
}
|