update encoding & decoding
This commit is contained in:
parent
baa7b3293a
commit
bc6cc83b1e
@ -72,9 +72,15 @@ const DEFAULT_OVERSCROLL_ALLOWANCE = 0.2;
|
|||||||
interface EncodedConstraints {
|
interface EncodedConstraints {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
|
// width
|
||||||
w: number;
|
w: number;
|
||||||
|
// height
|
||||||
h: number;
|
h: number;
|
||||||
|
// animateOnNextUpdate
|
||||||
|
a: boolean;
|
||||||
|
// lockZoom
|
||||||
l: boolean;
|
l: boolean;
|
||||||
|
// viewportZoomFactor
|
||||||
v: number;
|
v: number;
|
||||||
// overscrollAllowance
|
// overscrollAllowance
|
||||||
oa: number;
|
oa: number;
|
||||||
@ -91,6 +97,7 @@ export const encodeConstraints = (constraints: ScrollConstraints): string => {
|
|||||||
y: constraints.y,
|
y: constraints.y,
|
||||||
w: constraints.width,
|
w: constraints.width,
|
||||||
h: constraints.height,
|
h: constraints.height,
|
||||||
|
a: !!constraints.animateOnNextUpdate,
|
||||||
l: !!constraints.lockZoom,
|
l: !!constraints.lockZoom,
|
||||||
v: constraints.viewportZoomFactor ?? 1,
|
v: constraints.viewportZoomFactor ?? 1,
|
||||||
oa: constraints.overscrollAllowance ?? DEFAULT_OVERSCROLL_ALLOWANCE,
|
oa: constraints.overscrollAllowance ?? DEFAULT_OVERSCROLL_ALLOWANCE,
|
||||||
@ -111,14 +118,14 @@ export const decodeConstraints = (encoded: string): ScrollConstraints => {
|
|||||||
const decodedStr = window.atob(decodeURIComponent(encoded));
|
const decodedStr = window.atob(decodeURIComponent(encoded));
|
||||||
const parsed = JSON.parse(decodedStr) as EncodedConstraints;
|
const parsed = JSON.parse(decodedStr) as EncodedConstraints;
|
||||||
return {
|
return {
|
||||||
x: parsed.x ?? 0,
|
x: parsed.x || 0,
|
||||||
y: parsed.y ?? 0,
|
y: parsed.y || 0,
|
||||||
width: parsed.w ?? 0,
|
width: parsed.w || 0,
|
||||||
height: parsed.h ?? 0,
|
height: parsed.h || 0,
|
||||||
lockZoom: parsed.l ?? false,
|
lockZoom: parsed.l || false,
|
||||||
viewportZoomFactor: parsed.v ?? 1,
|
viewportZoomFactor: parsed.v || 1,
|
||||||
animateOnNextUpdate: false,
|
animateOnNextUpdate: parsed.a || false,
|
||||||
overscrollAllowance: parsed.oa ?? DEFAULT_OVERSCROLL_ALLOWANCE,
|
overscrollAllowance: parsed.oa || DEFAULT_OVERSCROLL_ALLOWANCE,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// return safe defaults if decoding fails
|
// return safe defaults if decoding fails
|
||||||
@ -127,9 +134,10 @@ export const decodeConstraints = (encoded: string): ScrollConstraints => {
|
|||||||
y: 0,
|
y: 0,
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
|
animateOnNextUpdate: false,
|
||||||
lockZoom: false,
|
lockZoom: false,
|
||||||
viewportZoomFactor: 1,
|
viewportZoomFactor: 1,
|
||||||
animateOnNextUpdate: false,
|
overscrollAllowance: DEFAULT_OVERSCROLL_ALLOWANCE,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user