Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type ZoomGestureWrapperProps = Omit<HTMLAttributes<HTMLDivElement>, 'style'> & {
enablePinch?: boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
};

export function ZoomGestureWrapper({
Expand All @@ -17,11 +19,12 @@ export function ZoomGestureWrapper({
style,
enablePinch = true,
enableWheel = true,
zoomStep = 0.1,
...props
}: ZoomGestureWrapperProps) {
const options = useMemo<ZoomGestureOptions>(
() => ({ enablePinch, enableWheel }),
[enablePinch, enableWheel],
() => ({ enablePinch, enableWheel, zoomStep }),
[enablePinch, enableWheel, zoomStep],
);
const { elementRef } = useZoomGesture(documentId, options);

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-zoom/src/shared/hooks/use-pinch-zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function useZoomGesture(documentId: string, options: ZoomGestureOptions =
viewportElementRef,
options.enablePinch,
options.enableWheel,
options.zoomStep,
]);

return { elementRef };
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-zoom/src/shared/hooks/use-zoom-gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function useZoomGesture(documentId: string, options: ZoomGestureOptions =
viewportElementRef,
options.enablePinch,
options.enableWheel,
options.zoomStep,
]);

return { elementRef };
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-zoom/src/shared/utils/pinch-zoom-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface ZoomGestureOptions {
enablePinch?: boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
}

export interface ZoomGestureDeps {
Expand Down Expand Up @@ -41,7 +43,7 @@ export function setupZoomGestures({
zoomProvides,
options = {},
}: ZoomGestureDeps) {
const { enablePinch = true, enableWheel = true } = options;
const { enablePinch = true, enableWheel = true, zoomStep = 0.1 } = options;
if (typeof window === 'undefined') {
return () => {};
}
Expand Down Expand Up @@ -244,9 +246,10 @@ export function setupZoomGestures({
clearTimeout(wheelZoomTimeout);
}

const zoomFactor = 1 - e.deltaY * 0.01;
// Utilizing deltaY sign instead of raw value to eliminate discrepancies between browsers
const zoomFactor = 1 - Math.sign(e.deltaY) * zoomStep; // Should this use zoomStep configured by the plugin config?
accumulatedWheelScale *= zoomFactor;
accumulatedWheelScale = Math.max(0.1, Math.min(10, accumulatedWheelScale));
accumulatedWheelScale = clamp(accumulatedWheelScale, 0.1, 10);
updateTransform(accumulatedWheelScale);

wheelZoomTimeout = setTimeout(() => {
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-zoom/src/shared/utils/zoom-gesture-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface ZoomGestureOptions {
enablePinch?: boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
}

export interface ZoomGestureDeps {
Expand Down Expand Up @@ -41,7 +43,7 @@ export function setupZoomGestures({
viewportGap = 0,
options = {},
}: ZoomGestureDeps) {
const { enablePinch = true, enableWheel = true } = options;
const { enablePinch = true, enableWheel = true, zoomStep = 0.1 } = options;
if (typeof window === 'undefined') {
return () => {};
}
Expand Down Expand Up @@ -228,9 +230,10 @@ export function setupZoomGestures({
clearTimeout(wheelZoomTimeout);
}

const zoomFactor = 1 - e.deltaY * 0.01;
// Utilizing deltaY sign instead of raw value to eliminate discrepancies between browsers
const zoomFactor = 1 - Math.sign(e.deltaY) * zoomStep; // Should this use zoomStep configured by the plugin config?
accumulatedWheelScale *= zoomFactor;
accumulatedWheelScale = Math.max(0.1, Math.min(10, accumulatedWheelScale));
accumulatedWheelScale = clamp(accumulatedWheelScale, 0.1, 10);
updateTransform(accumulatedWheelScale);

wheelZoomTimeout = setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
enablePinch?: boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
};

let {
Expand All @@ -19,12 +21,14 @@
class: propsClass,
enablePinch = true,
enableWheel = true,
zoomStep = 0.1,
...restProps
}: ZoomGestureWrapperProps = $props();

const zoomGesture = useZoomGesture(() => documentId, {
enablePinch: () => enablePinch,
enableWheel: () => enableWheel,
zoomStep: () => zoomStep,
});
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface UseZoomGestureOptions {
enablePinch?: () => boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: () => boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: () => number;
}

/**
Expand All @@ -36,6 +38,7 @@ export function useZoomGesture(
const documentId = $derived(getDocumentId());
const enablePinch = $derived(options.enablePinch?.() ?? true);
const enableWheel = $derived(options.enableWheel?.() ?? true);
const zoomStep = $derived(options.zoomStep?.() ?? 0.1);

$effect(() => {
const element = elementRef;
Expand Down Expand Up @@ -63,7 +66,7 @@ export function useZoomGesture(
documentId: docId,
viewportProvides: viewport,
zoomProvides: zoom,
options: { enablePinch: pinchEnabled, enableWheel: wheelEnabled },
options: { enablePinch: pinchEnabled, enableWheel: wheelEnabled, zoomStep },
});

return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface UseZoomGestureOptions {
enablePinch?: () => boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: () => boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: () => number;
}

/**
Expand All @@ -36,6 +38,7 @@ export function useZoomGesture(
const documentId = $derived(getDocumentId());
const enablePinch = $derived(options.enablePinch?.() ?? true);
const enableWheel = $derived(options.enableWheel?.() ?? true);
const zoomStep = $derived(options.zoomStep?.() ?? 0.1);

$effect(() => {
const element = elementRef;
Expand Down Expand Up @@ -63,7 +66,7 @@ export function useZoomGesture(
documentId: docId,
zoomProvides: zoom,
viewportGap: viewport?.getViewportGap() || 0,
options: { enablePinch: pinchEnabled, enableWheel: wheelEnabled },
options: { enablePinch: pinchEnabled, enableWheel: wheelEnabled, zoomStep },
});

return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ interface Props {
enablePinch?: boolean;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: boolean;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
}

const props = withDefaults(defineProps<Props>(), {
enablePinch: true,
enableWheel: true,
zoomStep: 0.1,
});

const { elementRef } = useZoomGesture(() => props.documentId, {
enablePinch: toRef(() => props.enablePinch),
enableWheel: toRef(() => props.enableWheel),
zoomStep: toRef(() => props.zoomStep),
});
</script>
8 changes: 6 additions & 2 deletions packages/plugin-zoom/src/vue/hooks/use-pinch-zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface UseZoomGestureOptions {
enablePinch?: MaybeRefOrGetter<boolean>;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: MaybeRefOrGetter<boolean>;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
}

/**
Expand All @@ -39,14 +41,16 @@ export function useZoomGesture(
() => toValue(documentId),
() => toValue(options.enablePinch ?? true),
() => toValue(options.enableWheel ?? true),
() => toValue(options.zoomStep ?? 0.1),
],
([element, viewport, zoom, docId, enablePinch, enableWheel]: [
([element, viewport, zoom, docId, enablePinch, enableWheel, zoomStep]: [
HTMLDivElement | null,
ViewportCapability | null,
ZoomCapability | null,
string,
boolean,
boolean,
number,
]) => {
// Clean up previous setup
if (cleanup) {
Expand All @@ -67,7 +71,7 @@ export function useZoomGesture(
documentId: docId,
viewportProvides: viewport,
zoomProvides: zoom,
options: { enablePinch, enableWheel },
options: { enablePinch, enableWheel, zoomStep },
});
},
{ immediate: true },
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-zoom/src/vue/hooks/use-zoom-gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface UseZoomGestureOptions {
enablePinch?: MaybeRefOrGetter<boolean>;
/** Enable wheel zoom with ctrl/cmd key (default: true) */
enableWheel?: MaybeRefOrGetter<boolean>;
/** Override wheel zoom step; 0.1 = 10% (default: 0.1) */
zoomStep?: number;
}

/**
Expand All @@ -39,14 +41,16 @@ export function useZoomGesture(
() => toValue(documentId),
() => toValue(options.enablePinch ?? true),
() => toValue(options.enableWheel ?? true),
() => toValue(options.zoomStep ?? 0.1),
],
([element, viewport, zoom, docId, enablePinch, enableWheel]: [
([element, viewport, zoom, docId, enablePinch, enableWheel, zoomStep]: [
HTMLDivElement | null,
ViewportCapability | null,
ZoomCapability | null,
string,
boolean,
boolean,
number,
]) => {
// Clean up previous setup
if (cleanup) {
Expand All @@ -67,7 +71,7 @@ export function useZoomGesture(
documentId: docId,
zoomProvides: zoom,
viewportGap: viewport?.getViewportGap() || 0,
options: { enablePinch, enableWheel },
options: { enablePinch, enableWheel, zoomStep },
});
},
{ immediate: true },
Expand Down