-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathwall-viewport-nav.js
More file actions
455 lines (455 loc) · 15.8 KB
/
Copy pathwall-viewport-nav.js
File metadata and controls
455 lines (455 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Pan/zoom navigation bindings for #wallSurface (wheel, space-drag, middle-drag,
// and Pan-mode empty-canvas pointer navigation). Listeners use the wall mount
// AbortSignal; space-held state clears on blur.
import { clampPan, getViewportState, isNavigationSuppressed, panBy, scheduleSaveViewport, setViewportState, zoomAround, } from "./wall-viewport.js";
import { isWallPanMode } from "./wall-canvas-mode.js";
const WHEEL_ZOOM_FACTOR = 1.08;
// Approximate pixel sizes for line/page wheel modes (Firefox, some mice).
const WHEEL_LINE_PX = 16;
const WHEEL_PAGE_PX = 800;
// Arrow-key pan step (screen px). Shift pans in coarse steps.
const ARROW_PAN_STEP_PX = 64;
const ARROW_PAN_STEP_COARSE_PX = ARROW_PAN_STEP_PX * 4;
/** Normalize wheel deltas to pixels regardless of deltaMode. */
function wheelPixels(ev) {
const unit = ev.deltaMode === WheelEvent.DOM_DELTA_LINE
? WHEEL_LINE_PX
: ev.deltaMode === WheelEvent.DOM_DELTA_PAGE
? WHEEL_PAGE_PX
: 1;
return { dx: ev.deltaX * unit, dy: ev.deltaY * unit };
}
let spaceHeld = false;
let spacePanActive = false;
let middlePanActive = false;
let panStartX = 0;
let panStartY = 0;
let panStartPanX = 0;
let panStartPanY = 0;
let navigationSurface = null;
let activeNavigationCleanup = null;
let activeNavigationKind = "none";
let activeTouchPointerId = null;
let pinchDistance = 0;
let touchResumeBlocked = false;
const activeTouchPointers = new Map();
const capturedPointerIds = new Set();
const PINCH_DISTANCE_MIN_PX = 8;
const PINCH_FACTOR_MIN = 0.5;
const PINCH_FACTOR_MAX = 2;
function clearSpaceHeld() {
spaceHeld = false;
spacePanActive = false;
}
function clearTouchGestureState() {
activeTouchPointerId = null;
pinchDistance = 0;
touchResumeBlocked = false;
activeTouchPointers.clear();
}
function setPanningClass(active) {
navigationSurface?.classList.toggle("wall-surface--panning", active);
}
function rememberCapturedPointerId(pointerId) {
capturedPointerIds.add(pointerId);
}
function trySetPointerCapture(surface, pointerId) {
if (typeof surface.setPointerCapture !== "function")
return;
try {
surface.setPointerCapture(pointerId);
rememberCapturedPointerId(pointerId);
}
catch {
// Some test environments and pointer types do not support capture.
}
}
function tryReleasePointerCapture(surface, pointerId) {
if (!surface || typeof surface.releasePointerCapture !== "function")
return;
try {
surface.releasePointerCapture(pointerId);
}
catch {
// Safe to ignore: release is best-effort during teardown/cancel.
}
}
function releaseCapturedPointers() {
const surface = navigationSurface;
for (const pointerId of capturedPointerIds) {
tryReleasePointerCapture(surface, pointerId);
}
capturedPointerIds.clear();
}
function pointerDistance(a, b) {
const dx = b.clientX - a.clientX;
const dy = b.clientY - a.clientY;
return Math.hypot(dx, dy);
}
function pointerMidpoint(a, b) {
return {
clientX: (a.clientX + b.clientX) / 2,
clientY: (a.clientY + b.clientY) / 2,
};
}
function activeTouchPair() {
if (activeTouchPointers.size !== 2)
return null;
return Array.from(activeTouchPointers.values());
}
function beginTouchSinglePan(clientX, clientY, pointerId) {
beginPanGesture(clientX, clientY, getViewportState);
activeTouchPointerId = pointerId;
setPanningClass(true);
}
function maybePromoteTouchPinch() {
const pair = activeTouchPair();
if (!pair) {
activeTouchPointerId = null;
pinchDistance = 0;
return;
}
activeTouchPointerId = null;
pinchDistance = pointerDistance(pair[0], pair[1]);
setPanningClass(true);
}
function isEmptyCanvasGestureTarget(surface, target) {
if (!(target instanceof Element))
return false;
if (!surface.contains(target))
return false;
if (target.closest(".wall-note"))
return false;
if (target.closest(".wall-note__resize-handle"))
return false;
if (target.closest(".wall-edge-hit"))
return false;
if (target.closest(".wall-note-context-menu"))
return false;
if (target.closest("button, textarea, input, select, [contenteditable='true']"))
return false;
return true;
}
function clearTouchPointer(pointerId) {
tryReleasePointerCapture(navigationSurface, pointerId);
activeTouchPointers.delete(pointerId);
capturedPointerIds.delete(pointerId);
}
export function cancelWallNavigationGestures() {
const cleanup = activeNavigationCleanup;
activeNavigationCleanup = null;
if (cleanup)
cleanup();
clearTouchGestureState();
middlePanActive = false;
spacePanActive = false;
setPanningClass(false);
releaseCapturedPointers();
activeNavigationKind = "none";
}
function onWheel(ev) {
if (isNavigationSuppressed(ev.target))
return;
ev.preventDefault();
const { dx, dy } = wheelPixels(ev);
// Shift is the primary zoom modifier; Ctrl/Cmd also zoom because browsers
// deliver trackpad pinch-to-zoom as synthetic Ctrl+wheel events.
if (ev.shiftKey || ev.ctrlKey || ev.metaKey) {
// On Windows, Shift+wheel is remapped to horizontal scroll, so the delta
// arrives in dx (dy ~ 0). Fall back to dx so zoom direction stays correct.
const zoomDelta = dy !== 0 ? dy : dx;
// A zero-delta wheel event (some inertia/end events, synthetic devices)
// carries no direction; do nothing rather than zooming out by default.
if (zoomDelta === 0)
return;
const factor = zoomDelta < 0 ? WHEEL_ZOOM_FACTOR : 1 / WHEEL_ZOOM_FACTOR;
zoomAround(ev.clientX, ev.clientY, factor);
return;
}
panBy(-dx, -dy);
}
function onKeyDown(ev) {
if (ev.code !== "Space" || ev.repeat)
return;
if (isNavigationSuppressed(ev.target))
return;
ev.preventDefault();
spaceHeld = true;
}
// Arrow keys pan the canvas (additive to wheel / Space+drag / middle-drag).
// Direction = the way the viewport moves toward content, matching scroll-to-pan.
function onArrowKeyDown(ev, isOpen) {
if (ev.ctrlKey || ev.metaKey || ev.altKey)
return;
let dx = 0;
let dy = 0;
const step = ev.shiftKey ? ARROW_PAN_STEP_COARSE_PX : ARROW_PAN_STEP_PX;
switch (ev.key) {
case "ArrowRight":
dx = -step;
break;
case "ArrowLeft":
dx = step;
break;
case "ArrowDown":
dy = -step;
break;
case "ArrowUp":
dy = step;
break;
default:
return;
}
if (!isOpen())
return;
if (isNavigationSuppressed(ev.target))
return;
ev.preventDefault();
panBy(dx, dy);
}
function onKeyUp(ev) {
if (ev.code !== "Space")
return;
spaceHeld = false;
spacePanActive = false;
scheduleSaveViewport();
}
function onBlur() {
clearSpaceHeld();
}
function beginPanGesture(clientX, clientY, getPan) {
panStartX = clientX;
panStartY = clientY;
const p = getPan();
panStartPanX = p.panX;
panStartPanY = p.panY;
}
function movePanGesture(clientX, clientY, setPan) {
setPan(panStartPanX + (clientX - panStartX), panStartPanY + (clientY - panStartY));
}
function startPointerPanGesture(surface, signal, ev, onActiveChange) {
cancelWallNavigationGestures();
activeNavigationKind = "pointer";
onActiveChange(true);
setPanningClass(true);
beginPanGesture(ev.clientX, ev.clientY, getViewportState);
trySetPointerCapture(surface, ev.pointerId);
let ended = false;
const pointerId = ev.pointerId;
const onMove = (mv) => {
if (mv.pointerId !== pointerId)
return;
mv.preventDefault();
const st = getViewportState();
movePanGesture(mv.clientX, mv.clientY, (px, py) => {
setViewportState({ panX: clampPan(px), panY: clampPan(py), zoom: st.zoom });
});
};
const cleanup = () => {
if (ended)
return;
ended = true;
document.removeEventListener("pointermove", onMove);
document.removeEventListener("pointerup", onUp);
document.removeEventListener("pointercancel", onUp);
signal.removeEventListener("abort", cleanup);
tryReleasePointerCapture(surface, pointerId);
capturedPointerIds.delete(pointerId);
onActiveChange(false);
setPanningClass(false);
if (activeNavigationCleanup === cleanup)
activeNavigationCleanup = null;
activeNavigationKind = "none";
scheduleSaveViewport();
};
const onUp = (up) => {
if (up.pointerId !== pointerId)
return;
cleanup();
};
activeNavigationCleanup = cleanup;
signal.addEventListener("abort", cleanup);
document.addEventListener("pointermove", onMove, { passive: false });
document.addEventListener("pointerup", onUp);
document.addEventListener("pointercancel", onUp);
}
function startTouchNavigationGesture(surface, signal, ev) {
if (activeNavigationKind !== "touch") {
cancelWallNavigationGestures();
activeNavigationKind = "touch";
clearTouchGestureState();
let ended = false;
const onMove = (mv) => {
const tracked = activeTouchPointers.get(mv.pointerId);
if (!tracked)
return;
tracked.clientX = mv.clientX;
tracked.clientY = mv.clientY;
if (activeTouchPointers.size === 1) {
if (touchResumeBlocked || activeTouchPointerId !== mv.pointerId)
return;
mv.preventDefault();
const st = getViewportState();
movePanGesture(mv.clientX, mv.clientY, (px, py) => {
setViewportState({ panX: clampPan(px), panY: clampPan(py), zoom: st.zoom });
});
return;
}
const pair = activeTouchPair();
if (!pair) {
activeTouchPointerId = null;
pinchDistance = 0;
return;
}
const currentDistance = pointerDistance(pair[0], pair[1]);
if (currentDistance < PINCH_DISTANCE_MIN_PX) {
pinchDistance = currentDistance;
return;
}
if (pinchDistance < PINCH_DISTANCE_MIN_PX) {
pinchDistance = currentDistance;
return;
}
let factor = currentDistance / pinchDistance;
if (!Number.isFinite(factor) || factor <= 0) {
pinchDistance = currentDistance;
return;
}
factor = Math.max(PINCH_FACTOR_MIN, Math.min(PINCH_FACTOR_MAX, factor));
const midpoint = pointerMidpoint(pair[0], pair[1]);
mv.preventDefault();
zoomAround(midpoint.clientX, midpoint.clientY, factor);
pinchDistance = currentDistance;
};
const maybeEndSession = () => {
if (activeTouchPointers.size === 0) {
cleanup();
}
else if (activeTouchPointers.size === 1) {
activeTouchPointerId = null;
pinchDistance = 0;
touchResumeBlocked = true;
setPanningClass(false);
}
else {
activeTouchPointerId = null;
maybePromoteTouchPinch();
}
};
const onUp = (up) => {
if (!activeTouchPointers.has(up.pointerId))
return;
clearTouchPointer(up.pointerId);
maybeEndSession();
};
const cleanup = () => {
if (ended)
return;
ended = true;
document.removeEventListener("pointermove", onMove);
document.removeEventListener("pointerup", onUp);
document.removeEventListener("pointercancel", onUp);
signal.removeEventListener("abort", cleanup);
releaseCapturedPointers();
clearTouchGestureState();
setPanningClass(false);
if (activeNavigationCleanup === cleanup)
activeNavigationCleanup = null;
activeNavigationKind = "none";
scheduleSaveViewport();
};
activeNavigationCleanup = cleanup;
signal.addEventListener("abort", cleanup);
document.addEventListener("pointermove", onMove, { passive: false });
document.addEventListener("pointerup", onUp);
document.addEventListener("pointercancel", onUp);
}
activeTouchPointers.set(ev.pointerId, { clientX: ev.clientX, clientY: ev.clientY });
trySetPointerCapture(surface, ev.pointerId);
if (activeTouchPointers.size === 1 && !touchResumeBlocked) {
beginTouchSinglePan(ev.clientX, ev.clientY, ev.pointerId);
return;
}
maybePromoteTouchPinch();
}
function bindPanPointer(surface, signal, shouldStart, onActiveChange) {
surface.addEventListener("pointerdown", (ev) => {
if (!shouldStart(ev))
return;
if (isNavigationSuppressed(ev.target))
return;
ev.preventDefault();
startPointerPanGesture(surface, signal, ev, onActiveChange);
}, { signal });
}
function bindPanModeCanvasNavigation(surface, signal) {
surface.addEventListener("pointerdown", (ev) => {
if (!isWallPanMode())
return;
if (ev.button !== 0)
return;
if (!isEmptyCanvasGestureTarget(surface, ev.target))
return;
if (isNavigationSuppressed(ev.target))
return;
ev.preventDefault();
if (ev.pointerType === "touch") {
startTouchNavigationGesture(surface, signal, ev);
return;
}
startPointerPanGesture(surface, signal, ev, () => {
// Pan mode owns the visual state via .wall-surface--pan-mode and
// .wall-surface--panning, so there is no extra per-gesture flag here.
});
}, { signal });
}
/**
* Bind wheel / keyboard / space+drag / middle-drag pan on the wall surface.
* All listeners abort when `signal` fires (wall teardown).
*/
export function bindWallNavigation(surface, signal, isOpen = () => true) {
cancelWallNavigationGestures();
navigationSurface = surface;
const opts = { signal, passive: false };
surface.addEventListener("wheel", onWheel, opts);
window.addEventListener("keydown", onKeyDown, { signal });
window.addEventListener("keyup", onKeyUp, { signal });
window.addEventListener("keydown", (ev) => onArrowKeyDown(ev, isOpen), { signal });
surface.addEventListener("blur", onBlur, { signal, capture: true });
window.addEventListener("blur", onBlur, { signal });
signal.addEventListener("abort", () => {
cancelWallNavigationGestures();
if (navigationSurface === surface)
navigationSurface = null;
}, { once: true });
bindPanPointer(surface, signal, (ev) => ev.button === 1, (active) => {
middlePanActive = active;
});
bindPanPointer(surface, signal, (ev) => ev.button === 0 && spaceHeld && !ev.shiftKey, (active) => {
spacePanActive = active;
});
bindPanModeCanvasNavigation(surface, signal);
}
/** True while Space is held (marquee / note drag should defer to pan). */
export function isSpacePanArmed() {
return spaceHeld;
}
/** For tests: drive the arrow-key pan handler directly. */
export function __onArrowKeyDownForTest(ev, isOpen = () => true) {
onArrowKeyDown(ev, isOpen);
}
/** For tests: drive the wheel pan/zoom handler directly. */
export function __onWheelForTest(ev) {
onWheel(ev);
}
/** For tests: whether space-to-pan is armed. */
export function __isSpaceHeldForTest() {
return spaceHeld;
}
export function __resetNavStateForTest() {
cancelWallNavigationGestures();
spaceHeld = false;
spacePanActive = false;
middlePanActive = false;
navigationSurface = null;
}