-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathPagedViewport.svelte
More file actions
301 lines (275 loc) · 10.9 KB
/
Copy pathPagedViewport.svelte
File metadata and controls
301 lines (275 loc) · 10.9 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
<script lang="ts">
import type { Snippet } from 'svelte';
import { onDestroy, onMount } from 'svelte';
import { settings } from '$lib/settings';
import { PagedCamera } from '$lib/reader/paged-camera';
import { ContinuousZoomController } from '$lib/reader/zoom-controller';
import type { Size } from '$lib/reader/paged-zoom-layout';
import {
applyPagedBase,
createSessionState,
doubleTapTarget,
pagedLevels
} from '$lib/reader/paged-zoom-session';
import { normalizeWheelDelta, wheelIntentIsZoom } from '$lib/reader/zoom-math';
import { pagedZoom, type PagedZoomApi } from '$lib/reader/paged-zoom';
import { PointerGestureTracker, zoomGestureConfig } from '$lib/reader/input/pointer-tracker';
import { gestureTargetRole } from '$lib/reader/input/gesture-target';
import { TapDiscriminator } from '$lib/reader/input/tap';
import { classifySwipe } from '$lib/reader/input/swipe';
import type { MotionGate } from '$lib/reader/input/motion-gate';
interface Props {
/** Native pixel size of the displayed page or pair — from page data, not DOM. */
contentSize: Size;
/**
* Identity of the displayed content (e.g. the page index). Manga pages
* are overwhelmingly uniform in size, so the base must re-apply on page
* turns even when the dimensions don't change.
*/
pageKey: number | string;
rtl: boolean;
/**
* An edge-gated swipe asked for the page on this VISUAL side ('left' =
* rightward swipe revealing the left page). RTL mapping is the
* caller's concern.
*/
onPageFlip?: (side: 'left' | 'right') => void;
/** A lone tap on the page surface (not text box / chrome). */
onOverlayToggle?: () => void;
children?: Snippet;
}
let { contentSize, pageKey, rtl, onPageFlip, onOverlayToggle, children }: Props = $props();
let wrapperEl: HTMLDivElement | undefined = $state();
let viewportWidth = $state(typeof window !== 'undefined' ? window.innerWidth : 1024);
let viewportHeight = $state(typeof window !== 'undefined' ? window.innerHeight : 768);
// Session state for the controller's dynamic level ladder (plain object —
// read by closures at call time, never rendered).
const session = createSessionState();
const camera = new PagedCamera({
getWrapper: () => wrapperEl,
getViewport: () => ({ width: viewportWidth, height: viewportHeight }),
isClampingEnabled: () => $settings.bounds || $settings.mobile,
getDevicePixelRatio: () => (typeof devicePixelRatio === 'number' ? devicePixelRatio : 1)
});
const controller = new ContinuousZoomController({
surface: camera.surface(),
getLevels: () => pagedLevels(session.baseScale, session.fitScale),
getPageElements: () => {
// During a {#key page} transition both the outgoing and incoming trees
// are in the DOM; anchor only to the live (last) tree's pages.
if (!wrapperEl) return [];
const all = [...wrapperEl.querySelectorAll<HTMLElement>('[data-page-index]')];
if (all.length === 0) return all;
const liveTree = all[all.length - 1].closest('.col-start-1');
return liveTree ? all.filter((el) => el.closest('.col-start-1') === liveTree) : all;
},
getViewport: () => ({ width: viewportWidth, height: viewportHeight }),
onSettled: () => camera.settle()
});
function applyBase(mode: string) {
applyPagedBase(
{ camera, controller, state: session },
mode,
contentSize,
{ width: viewportWidth, height: viewportHeight },
rtl
);
}
// Re-apply on page-identity / content / mode / viewport / direction
// changes. The base is computed from page data, so the {#key page}
// transition overlap can't skew it; pageKey makes uniform-dimension page
// turns (the normal manga case) re-apply too.
let lastSig = '';
$effect(() => {
const mode = $settings.zoomDefault as string;
const sig = `${pageKey}|${contentSize.width}x${contentSize.height}|${mode}|${viewportWidth}x${viewportHeight}|${rtl}`;
if (sig === lastSig) return;
lastSig = sig;
applyBase(mode);
});
// Enabling bounds/mobile mid-session must clamp the current view
// immediately — the camera reads the gate lazily, only on mutations.
$effect(() => {
if ($settings.bounds || $settings.mobile) camera.settle();
});
function handleResize() {
viewportWidth = window.innerWidth;
viewportHeight = window.innerHeight;
}
// ============================================================
// Wheel — attached to the wrapper (non-passive, so ctrl+wheel can
// preventDefault the browser page-zoom)
// ============================================================
/** Interrupt choke points — see MotionGate for the contract. */
const motion: MotionGate = {
beforeZoom() {
camera.stopPan();
tracker.cancelPan();
},
beforeManualPan() {
if (controller.isActive) controller.finishNow();
camera.stopPan();
},
beforeAnimatedScroll() {
// camera.panBy chains glides itself — only the zoom must yield.
if (controller.isActive) controller.finishNow();
}
// No beforeNav: page turns are owned by Reader and re-apply the base
// through the pageKey effect.
};
function handleWheel(e: WheelEvent) {
const modifier = e.ctrlKey || e.metaKey;
if (wheelIntentIsZoom(modifier, $settings.swapWheelBehavior)) {
e.preventDefault();
motion.beforeZoom();
controller.wheelZoom(e);
return;
}
e.preventDefault();
motion.beforeAnimatedScroll();
camera.panBy(
-normalizeWheelDelta(e.deltaX, e.deltaMode),
-normalizeWheelDelta(e.deltaY, e.deltaMode)
);
}
function doubleTap(x: number, y: number) {
const levels = pagedLevels(session.baseScale, session.fitScale);
const target = doubleTapTarget(controller.currentZoom, levels[0]);
motion.beforeZoom();
// Zooming in animates the tapped content toward the CLAMPED center
// position — aiming at the raw center fights the bounds near edges.
controller.animateToLevel(
target,
{ x, y },
target >= 2 ? camera.projectCentered({ x, y }, target) : { x, y }
);
}
function scrollImage(direction: 'up' | 'down') {
motion.beforeAnimatedScroll();
const amount = viewportHeight * 0.75;
camera.panBy(0, direction === 'down' ? -amount : amount);
}
function zoomFitToScreen() {
// Show the whole page without changing the persisted mode — the next
// page turn re-applies the user's zoomDefault.
applyPagedBase(
{ camera, controller, state: session },
'zoomFitToScreen',
contentSize,
{ width: viewportWidth, height: viewportHeight },
rtl
);
}
const api: PagedZoomApi = {
scrollImage,
zoomFitToScreen
};
// ============================================================
// Pointer gestures — classification lives in PointerGestureTracker
// (src/lib/reader/input/pointer-tracker.ts); this config holds only the
// paged surface's policy:
//
// - capture 'deferred': gutter-button clicks and text-selection drags
// deliver natively; capture engages only once a drag crosses threshold
// - mouse/pen on a text box is a selection drag, never a pan; touch has no
// drag-selection gesture, so touch pans everywhere (exactly like the old
// panzoom touch path)
// - swipe-to-flip is classified HERE from pan summaries (onPanEnd below),
// edge-gated via press-time camera state (#186)
// - pan deltas are incremental — the camera accumulates them
// - isPinchAlive lets the tracker resurrect a pinch whose controller state
// was cleared mid-gesture by a base re-application (rotation, page turn)
// ============================================================
// Edge state sampled at press, BEFORE the pan moves the camera — swipe
// classification needs to know what was hidden when the gesture began.
let edgeAtPress = { canRevealLeft: false, canRevealRight: false };
const tracker = new PointerGestureTracker({
getElement: () => wrapperEl,
capturePolicy: 'deferred',
suppressPan: (e) => {
if (gestureTargetRole(e.target) !== 'textbox') return false;
// Any press on a text box marks the next outside tap as a dismissal.
taps.noteTextBoxInteraction();
return e.pointerType !== 'touch';
},
onPress: () => {
motion.beforeManualPan();
edgeAtPress = camera.edgeState();
// Track the drag for an inertial fling on release.
camera.kineticStart();
},
onPanMove: (_p, d) => camera.adjustView(-d.dx, -d.dy),
onPanEnd: (s) => {
const side =
$settings.mobile && !s.cancelled
? classifySwipe({
summary: s,
wasPinch: tracker.wasPinch,
viewport: { width: viewportWidth, height: viewportHeight },
thresholdPercent: $settings.swipeThreshold,
canRevealLeftAtStart: edgeAtPress.canRevealLeft,
canRevealRightAtStart: edgeAtPress.canRevealRight
})
: null;
if (side) {
// The gesture flipped the page — the new page re-applies its base,
// so drop any momentum rather than flinging the outgoing page.
camera.stopPan();
onPageFlip?.(side);
} else if (s.cancelled) {
// OS-cancelled gesture: drop the momentum and settle in place.
camera.stopPan();
camera.settle();
} else {
// Normal release: glide with inertia (or settle if too slow).
camera.kineticStop();
}
},
// The finger remaining after a pinch keeps panning (the old panzoom
// pinch→drag handoff) — safe here because deltas are incremental.
pinchSurvivorPans: true,
...zoomGestureConfig({
beforeZoom: () => motion.beforeZoom(),
controller,
getViewport: () => ({ width: viewportWidth, height: viewportHeight })
})
});
// 'immediate' reproduces the native click/click/dblclick sequence this
// surface was built on — instant overlay toggles, zoom on the second tap.
const taps = new TapDiscriminator({
commitPolicy: 'immediate',
// Native dblclick (which this surface used before) required the clicks
// to land near each other — keep that gate.
maxDoubleTapDistancePx: 40,
onTap: () => onOverlayToggle?.(),
onDoubleTap: (x, y) => doubleTap(x, y)
});
function handleClick(e: MouseEvent) {
if (gestureTargetRole(e.target) !== 'page') return;
if (tracker.wasDrag) return;
taps.tap(e.clientX, e.clientY);
}
onMount(() => {
pagedZoom.set(api);
tracker.attach();
wrapperEl?.addEventListener('wheel', handleWheel, { passive: false });
});
onDestroy(() => {
pagedZoom.set(undefined);
tracker.detach();
taps.cancel();
wrapperEl?.removeEventListener('wheel', handleWheel);
controller.destroy();
camera.destroy();
});
</script>
<svelte:window onresize={handleResize} />
<div
bind:this={wrapperEl}
data-mokuro-reader
style:touch-action="none"
onclick={handleClick}
role="none"
>
{@render children?.()}
</div>