Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,15 +1,7 @@
|
||||
/**
|
||||
* Geometry for dragging and resizing a box, with smart guides.
|
||||
*
|
||||
* While a box is dragged, its left edge, centre and right edge (and top,
|
||||
* middle and bottom) are compared with every other box's, and with the
|
||||
* canvas's sides, centre and top. When one comes within SNAP_PX screen px of
|
||||
* another it snaps onto it, and every line the box then shares is returned as
|
||||
* a guide for BoxCanvas to draw. A resize does the same for the edges being
|
||||
* dragged only. Away from all of them the 5px grid applies, as it always did.
|
||||
*
|
||||
* Everything here is canvas px. The threshold is in screen px because that is
|
||||
* what the hand feels; the caller divides it by the canvas scale.
|
||||
* Drag/resize geometry with smart guides: moving edges and centres snap to
|
||||
* other boxes and the canvas within a threshold, else to the 5px grid. All
|
||||
* canvas px; the caller converts the screen-px threshold.
|
||||
*/
|
||||
export const GRID = 5;
|
||||
export const MIN_SIZE = 40;
|
||||
@@ -20,9 +12,8 @@ const grid = (n) => Math.round(n / GRID) * GRID;
|
||||
const thirds = (start, size) => [start, start + size / 2, start + size];
|
||||
|
||||
/**
|
||||
* The lines a dragged box can snap to, per axis. Each carries the extent of
|
||||
* what it came from on the other axis, so a guide can be drawn between the
|
||||
* two boxes rather than across the whole canvas. Computed once per drag.
|
||||
* Snap lines per axis, each carrying its source's extent so a guide spans only
|
||||
* the two boxes. Computed once per drag.
|
||||
*/
|
||||
export function snapTargets(others, canvasW) {
|
||||
const x = thirds(0, canvasW).map((at) => ({ at, from: 0, to: 0 }));
|
||||
@@ -37,16 +28,12 @@ export function snapTargets(others, canvasW) {
|
||||
|
||||
export const NO_TARGETS = { x: [], y: [] };
|
||||
|
||||
// Which of the box's edges a gesture moves: all four for a move, otherwise
|
||||
// the ones named by the handle ("ne", "w", …). Spelled out because "move"
|
||||
// itself contains an "e".
|
||||
// Spelled out because "move" contains an "e".
|
||||
function edgesOf(mode) {
|
||||
if (mode === "move") return { w: true, e: true, n: true, s: true };
|
||||
return { w: mode.includes("w"), e: mode.includes("e"), n: mode.includes("n"), s: mode.includes("s") };
|
||||
}
|
||||
|
||||
// The lines of `g` that the gesture is moving, per axis. A move carries the
|
||||
// centres along too; a resize only moves the edge under the pointer.
|
||||
function movingLines(g, mode) {
|
||||
if (mode === "move") return { x: thirds(g.x, g.w), y: thirds(g.y, g.h) };
|
||||
const edges = edgesOf(mode);
|
||||
@@ -59,8 +46,6 @@ function movingLines(g, mode) {
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
// The smallest shift that puts one of the moving lines on a target, or null
|
||||
// if none is within `threshold`.
|
||||
function nearest(moving, targets, threshold) {
|
||||
let best = null;
|
||||
for (const m of moving) {
|
||||
@@ -72,9 +57,7 @@ function nearest(moving, targets, threshold) {
|
||||
return best;
|
||||
}
|
||||
|
||||
// One axis of the result. `lo` and `hi` say whether the gesture moves the
|
||||
// box's start and end edge on this axis; `shift` is the snap from nearest(),
|
||||
// or null to fall back to the grid.
|
||||
// `lo`/`hi`: whether the gesture moves this axis's start/end edge.
|
||||
function resolve(start, size, delta, lo, hi, shift) {
|
||||
const fit = (n) => (shift === null ? grid(n) : Math.round(n + shift));
|
||||
if (lo && hi) return [Math.max(0, fit(start + delta)), size];
|
||||
@@ -86,11 +69,7 @@ function resolve(start, size, delta, lo, hi, shift) {
|
||||
return [start, size];
|
||||
}
|
||||
|
||||
/**
|
||||
* The geometry for a drag of (dx, dy) canvas px from `from`, in `mode`
|
||||
* ("move" or a handle), snapped to `targets` within `threshold` canvas px.
|
||||
* Pass NO_TARGETS for the grid alone.
|
||||
*/
|
||||
/** Pass NO_TARGETS for the grid alone. */
|
||||
export function place(from, mode, dx, dy, targets, threshold) {
|
||||
const edges = edgesOf(mode);
|
||||
// Where the pointer alone would put the box, before any snapping.
|
||||
@@ -108,14 +87,11 @@ export function place(from, mode, dx, dy, targets, threshold) {
|
||||
return { x, y, w, h };
|
||||
}
|
||||
|
||||
// Each target line that one of the moving lines now sits on, spanning both
|
||||
// the box (from..to) and whatever the line came from. Lines from several
|
||||
// boxes at the same place merge into one guide.
|
||||
// Coincident lines from several boxes merge into one guide.
|
||||
function matches(moving, targets, from, to) {
|
||||
const found = new Map();
|
||||
for (const t of targets) {
|
||||
// Within a px, since centres of odd sizes fall on half px and the
|
||||
// geometry is rounded.
|
||||
// Odd-sized centres fall on half px.
|
||||
if (!moving.some((m) => Math.abs(t.at - m) < 1)) continue;
|
||||
const [lo, hi] = found.get(t.at) ?? [from, to];
|
||||
found.set(t.at, [Math.min(lo, t.from), Math.max(hi, t.to)]);
|
||||
|
||||
Reference in New Issue
Block a user