mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-07-02 01:22:07 -04:00
Keep snap helper safe without context (#1828)
This commit is contained in:
@@ -23,8 +23,10 @@
|
|||||||
* @returns {{x: number, y: number, guides: Array}}
|
* @returns {{x: number, y: number, guides: Array}}
|
||||||
*/
|
*/
|
||||||
export function computeSnap(layer, nx, ny, ctx) {
|
export function computeSnap(layer, nx, ny, ctx) {
|
||||||
const SNAP_PX = 6 / Math.max(ctx.zoom, 0.0001);
|
if (!layer || !layer.canvas || !ctx) return { x: nx, y: ny, guides: [] };
|
||||||
const cw = ctx.canvasW, ch = ctx.canvasH;
|
const zoom = Number.isFinite(Number(ctx.zoom)) ? Number(ctx.zoom) : 1;
|
||||||
|
const SNAP_PX = 6 / Math.max(zoom, 0.0001);
|
||||||
|
const cw = Number(ctx.canvasW) || 0, ch = Number(ctx.canvasH) || 0;
|
||||||
const w = layer.canvas.width, h = layer.canvas.height;
|
const w = layer.canvas.width, h = layer.canvas.height;
|
||||||
|
|
||||||
const vTargets = [
|
const vTargets = [
|
||||||
|
|||||||
@@ -36,6 +36,28 @@ def test_compute_snap_tolerates_non_array_other_layers():
|
|||||||
assert r["x"] == 10 and r["y"] == 10 and r["guides"] == []
|
assert r["x"] == 10 and r["y"] == 10 and r["guides"] == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
||||||
|
def test_compute_snap_tolerates_missing_layer_or_context():
|
||||||
|
js = f"""
|
||||||
|
import {{ computeSnap }} from '{_HELPER.as_posix()}';
|
||||||
|
console.log(JSON.stringify([
|
||||||
|
computeSnap(null, 10, 20, {{ zoom: 1, canvasW: 800, canvasH: 600 }}),
|
||||||
|
computeSnap({{ id: 'L1' }}, 11, 21, {{ zoom: 1, canvasW: 800, canvasH: 600 }}),
|
||||||
|
computeSnap({{ id: 'L1', canvas: {{ width: 100, height: 50 }} }}, 12, 22, null)
|
||||||
|
]));
|
||||||
|
"""
|
||||||
|
proc = subprocess.run(
|
||||||
|
["node", "--input-type=module"],
|
||||||
|
input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
|
||||||
|
)
|
||||||
|
assert proc.returncode == 0, proc.stderr
|
||||||
|
assert json.loads(proc.stdout.strip()) == [
|
||||||
|
{"x": 10, "y": 20, "guides": []},
|
||||||
|
{"x": 11, "y": 21, "guides": []},
|
||||||
|
{"x": 12, "y": 22, "guides": []},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
|
||||||
def test_compute_snap_still_snaps_to_a_layer_edge():
|
def test_compute_snap_still_snaps_to_a_layer_edge():
|
||||||
other = [{"id": "L2", "visible": True, "offset": {"x": 12, "y": 300},
|
other = [{"id": "L2", "visible": True, "offset": {"x": 12, "y": 300},
|
||||||
|
|||||||
Reference in New Issue
Block a user