improve debug

This commit is contained in:
Ryan Di 2025-05-12 16:23:18 +10:00
parent 3a566a292c
commit 19608b712f

View File

@ -67,6 +67,8 @@ import {
import { useApp } from "@excalidraw/excalidraw/components/App"; import { useApp } from "@excalidraw/excalidraw/components/App";
import { clamp } from "@excalidraw/math";
import type { RemoteExcalidrawElement } from "@excalidraw/excalidraw/data/reconcile"; import type { RemoteExcalidrawElement } from "@excalidraw/excalidraw/data/reconcile";
import type { RestoredDataState } from "@excalidraw/excalidraw/data/restore"; import type { RestoredDataState } from "@excalidraw/excalidraw/data/restore";
import type { import type {
@ -173,9 +175,9 @@ const ConstraintsSettings = ({
const [activeFrameId, setActiveFrameId] = useState<string | null>(null); const [activeFrameId, setActiveFrameId] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
const hash = new URLSearchParams(window.location.hash.slice(1)); const params = new URLSearchParams(window.location.search);
hash.set("constraints", encodeConstraints(constraints)); params.set("constraints", encodeConstraints(constraints));
window.location.hash = decodeURIComponent(hash.toString()); history.replaceState(null, "", `?${params.toString()}`);
constraints.enabled constraints.enabled
? excalidrawAPI.setScrollConstraints(constraints) ? excalidrawAPI.setScrollConstraints(constraints)
@ -205,6 +207,26 @@ const ConstraintsSettings = ({
}); });
}, [excalidrawAPI]); }, [excalidrawAPI]);
const parseValue = (
value: string,
opts?: {
min?: number;
max?: number;
},
) => {
const { min = -Infinity, max = Infinity } = opts || {};
let parsedValue = parseInt(value);
if (isNaN(parsedValue)) {
parsedValue = 0;
}
return clamp(parsedValue, min, max);
};
const inputStyle = {
width: "4rem",
height: "1rem",
};
return ( return (
<div <div
style={{ style={{
@ -224,6 +246,7 @@ const ConstraintsSettings = ({
style={{ style={{
display: "flex", display: "flex",
gap: "0.6rem", gap: "0.6rem",
alignItems: "center",
}} }}
> >
enabled:{" "} enabled:{" "}
@ -237,54 +260,66 @@ const ConstraintsSettings = ({
x:{" "} x:{" "}
<input <input
placeholder="x" placeholder="x"
size={4} type="number"
step={"10"}
value={constraints.x.toString()} value={constraints.x.toString()}
onChange={(e) => onChange={(e) => {
setConstraints((s) => ({ setConstraints((s) => ({
...s, ...s,
x: parseInt(e.target.value) ?? 0, x: parseValue(e.target.value),
})) }));
} }}
style={inputStyle}
/> />
y:{" "} y:{" "}
<input <input
placeholder="y" placeholder="y"
size={4} type="number"
step={"10"}
value={constraints.y.toString()} value={constraints.y.toString()}
onChange={(e) => onChange={(e) =>
setConstraints((s) => ({ setConstraints((s) => ({
...s, ...s,
y: parseInt(e.target.value) ?? 0, y: parseValue(e.target.value),
})) }))
} }
style={inputStyle}
/> />
w:{" "} w:{" "}
<input <input
placeholder="width" placeholder="width"
size={4} type="number"
step={"10"}
value={constraints.width.toString()} value={constraints.width.toString()}
onChange={(e) => onChange={(e) =>
setConstraints((s) => ({ setConstraints((s) => ({
...s, ...s,
width: parseInt(e.target.value) ?? 200, width: parseValue(e.target.value, {
min: 200,
}),
})) }))
} }
style={inputStyle}
/> />
h:{" "} h:{" "}
<input <input
placeholder="height" placeholder="height"
size={4} type="number"
step={"10"}
value={constraints.height.toString()} value={constraints.height.toString()}
onChange={(e) => onChange={(e) =>
setConstraints((s) => ({ setConstraints((s) => ({
...s, ...s,
height: parseInt(e.target.value) ?? 200, height: parseValue(e.target.value, {
min: 200,
}),
})) }))
} }
style={inputStyle}
/> />
zoomFactor: zoomFactor:
<input <input
placeholder="height" placeholder="zoom factor"
type="number" type="number"
min="0.1" min="0.1"
max="1" max="1"
@ -296,10 +331,11 @@ const ConstraintsSettings = ({
viewportZoomFactor: parseFloat(e.target.value.toString()) ?? 0.7, viewportZoomFactor: parseFloat(e.target.value.toString()) ?? 0.7,
})) }))
} }
style={inputStyle}
/> />
overscrollAllowance: overscrollAllowance:
<input <input
placeholder="height" placeholder="overscroll allowance"
type="number" type="number"
min="0" min="0"
max="1" max="1"
@ -311,6 +347,7 @@ const ConstraintsSettings = ({
overscrollAllowance: parseFloat(e.target.value.toString()) ?? 0.5, overscrollAllowance: parseFloat(e.target.value.toString()) ?? 0.5,
})) }))
} }
style={inputStyle}
/> />
lockZoom:{" "} lockZoom:{" "}
<input <input