@orbat-mapper/tactical-draw 0.32.0 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1700,6 +1700,11 @@ interface SnapCandidateRequest {
1700
1700
  pixel: PixelCoordinate;
1701
1701
  interaction: SnapInteraction;
1702
1702
  graphicId?: string;
1703
+ /**
1704
+ * Every graphic a multi-selection moves together; candidates of any of them
1705
+ * are skipped, so the group never snaps onto itself. Set instead of `graphicId`.
1706
+ */
1707
+ graphicIds?: readonly string[];
1703
1708
  controlPointIndex?: number;
1704
1709
  originalEvent?: unknown;
1705
1710
  }
@@ -1711,6 +1716,14 @@ interface SnapCandidate {
1711
1716
  controlPointIndex?: number;
1712
1717
  priority?: number;
1713
1718
  }
1719
+ /**
1720
+ * Host snap candidates near `request`. A provider that leaves out the moving
1721
+ * graphic's own candidates should check `request.graphicIds` (a multi-selection)
1722
+ * as well as `request.graphicId`. A move or scale that tests several
1723
+ * points of a graphic (a control measure's control points, a Note's or an
1724
+ * Image's corners and centre) asks once per tested point on each pointer move,
1725
+ * so keep it cheap: return only candidates near `request.pixel`.
1726
+ */
1714
1727
  type SnapCandidateProvider = (request: SnapCandidateRequest) => readonly SnapCandidate[];
1715
1728
  /**
1716
1729
  * A snap the host answers later, such as one to terrain it has to sample
package/dist/index.mjs CHANGED
@@ -3106,6 +3106,7 @@ function graphicCandidates(graphics) {
3106
3106
  return candidates;
3107
3107
  }
3108
3108
  function isSelfCandidate(candidate, request) {
3109
+ if (candidate.graphicId !== void 0 && request.graphicIds?.includes(candidate.graphicId)) return true;
3109
3110
  if (request.graphicId === void 0 || candidate.graphicId !== request.graphicId) return false;
3110
3111
  if (request.interaction === "reshape" && request.controlPointIndex !== void 0) return candidate.controlPointIndex === void 0 || candidate.controlPointIndex === request.controlPointIndex;
3111
3112
  return true;
@@ -3159,7 +3160,7 @@ function interpolatePosition(a, b, t) {
3159
3160
  for (let index = 0; index < dimensions; index += 1) position.push(a[index] + (b[index] - a[index]) * t);
3160
3161
  return position;
3161
3162
  }
3162
- function generatedGeometryCandidates(adapter, paths, request, options) {
3163
+ function generatedGeometryCandidates(project, paths, request, options) {
3163
3164
  const resolved = options === true ? {} : options;
3164
3165
  const edges = resolved.edges ?? true;
3165
3166
  const vertices = resolved.vertices ?? false;
@@ -3177,8 +3178,8 @@ function generatedGeometryCandidates(adapter, paths, request, options) {
3177
3178
  for (let index = 1; index < path.positions.length; index += 1) {
3178
3179
  const a = path.positions[index - 1];
3179
3180
  const b = path.positions[index];
3180
- const aPixel = adapter.getPixelFromCoordinate(a);
3181
- const bPixel = adapter.getPixelFromCoordinate(b);
3181
+ const aPixel = project(a);
3182
+ const bPixel = project(b);
3182
3183
  if (!aPixel || !bPixel) continue;
3183
3184
  const t = clampedSegmentParameter(request.pixel, aPixel, bPixel) ?? 0;
3184
3185
  candidates.push({
@@ -3212,30 +3213,23 @@ function createSnapResolver(adapter, getGraphics, getGeometryFeatures, getOption
3212
3213
  }
3213
3214
  return indexedGeometryPaths;
3214
3215
  };
3215
- return (request) => {
3216
- const options = getOptions();
3217
- const clear = () => options?.onSnap?.(null);
3218
- if (!options?.enabled || options.bypass?.(request.originalEvent)) {
3219
- clear();
3220
- return null;
3221
- }
3216
+ /** The snap for `request` under `options`, without reporting it. */
3217
+ const resolve = (request, options, project) => {
3218
+ if (!options?.enabled || options.bypass?.(request.originalEvent)) return null;
3222
3219
  const tolerance = options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS;
3223
- if (!Number.isFinite(tolerance) || tolerance < 0) {
3224
- clear();
3225
- return null;
3226
- }
3220
+ if (!Number.isFinite(tolerance) || tolerance < 0) return null;
3227
3221
  const candidateLists = [];
3228
3222
  if (options.sources?.graphics !== false) candidateLists.push(builtInCandidates());
3229
3223
  const graphicGeometry = options.sources?.graphicGeometry;
3230
- if (graphicGeometry) candidateLists.push(generatedGeometryCandidates(adapter, geometryPaths(), request, graphicGeometry));
3224
+ if (graphicGeometry) candidateLists.push(generatedGeometryCandidates(project, geometryPaths(), request, graphicGeometry));
3231
3225
  const external = options.sources?.external?.(request);
3232
3226
  if (external) candidateLists.push(external);
3233
3227
  let best = null;
3234
3228
  for (const list of candidateLists) for (const candidate of list) {
3235
3229
  if (isSelfCandidate(candidate, request)) continue;
3236
- const pixel = adapter.getPixelFromCoordinate(candidate.coordinate);
3230
+ const pixel = project(candidate.coordinate);
3237
3231
  if (!pixel) continue;
3238
- const distancePixels = Math.hypot(pixel[0] - request.pixel[0], pixel[1] - request.pixel[1]);
3232
+ const distancePixels = pixelDistance$1(pixel, request.pixel);
3239
3233
  if (distancePixels > tolerance) continue;
3240
3234
  const priority = candidate.priority ?? 0;
3241
3235
  const bestPriority = best?.candidate.priority ?? 0;
@@ -3247,9 +3241,33 @@ function createSnapResolver(adapter, getGraphics, getGeometryFeatures, getOption
3247
3241
  distancePixels
3248
3242
  };
3249
3243
  }
3250
- options.onSnap?.(best);
3251
3244
  return best;
3252
3245
  };
3246
+ const closest = (requests, cursor) => {
3247
+ const options = getOptions();
3248
+ const pixels = /* @__PURE__ */ new Map();
3249
+ const project = (position) => {
3250
+ if (!pixels.has(position)) pixels.set(position, adapter.getPixelFromCoordinate(position));
3251
+ return pixels.get(position);
3252
+ };
3253
+ let best = null;
3254
+ for (const request of requests) {
3255
+ const result = resolve(request, options, project);
3256
+ if (!result) continue;
3257
+ const rank = cursor ? pixelDistance$1(result.pixel, cursor) : result.distancePixels;
3258
+ if (!best || rank < best.rank || rank === best.rank && result.distancePixels < best.result.distancePixels) best = {
3259
+ request,
3260
+ result,
3261
+ rank
3262
+ };
3263
+ }
3264
+ options?.onSnap?.(best?.result ?? null);
3265
+ return best && {
3266
+ request: best.request,
3267
+ result: best.result
3268
+ };
3269
+ };
3270
+ return Object.assign((request) => closest([request])?.result ?? null, { closest });
3253
3271
  }
3254
3272
  //#endregion
3255
3273
  //#region src/tactical-draw/draw-controller.ts
@@ -6227,47 +6245,58 @@ function runEdit(opts) {
6227
6245
  toDragPixel: (landed) => landed
6228
6246
  };
6229
6247
  }
6230
- /** A body drag snaps its moved first control point; null when it can't be placed. */
6231
- function translateTarget(pixel, info, drag) {
6232
- if (drag.fsm.kind !== "body") return null;
6233
- const first = drag.preDragPoints[0];
6234
- if (!first) return null;
6248
+ /**
6249
+ * Every moved control point of a body drag as a snap target, in order, null
6250
+ * where one can't be placed: the first is what `sources.deferred` is asked
6251
+ * about. Empty for any other box drag.
6252
+ */
6253
+ function translateTargets(pixel, info, drag) {
6254
+ if (drag.fsm.kind !== "body") return [];
6235
6255
  const cursor = driver.fromPixel(pixel);
6236
6256
  const origin = driver.fromPixel(drag.downPixel);
6237
- const anchor = [...first];
6238
- anchor[0] = first[0] + cursor[0] - origin[0];
6239
- anchor[1] = first[1] + cursor[1] - origin[1];
6240
- const anchorPixel = driver.toPixel(anchor);
6241
- if (!anchorPixel) return null;
6242
- return {
6243
- request: {
6244
- coordinate: anchor,
6245
- pixel: anchorPixel,
6246
- interaction: "translate",
6247
- graphicId: measure.id,
6248
- originalEvent: info?.native
6249
- },
6250
- toDragPixel: (landed) => {
6251
- const at = driver.fromPixel(landed);
6252
- return driver.toPixel([origin[0] + at[0] - first[0], origin[1] + at[1] - first[1]]) ?? [pixel[0] + landed[0] - anchorPixel[0], pixel[1] + landed[1] - anchorPixel[1]];
6253
- }
6254
- };
6257
+ const dx = cursor[0] - origin[0];
6258
+ const dy = cursor[1] - origin[1];
6259
+ return drag.preDragPoints.map((point) => {
6260
+ const moved = [...point];
6261
+ moved[0] = point[0] + dx;
6262
+ moved[1] = point[1] + dy;
6263
+ const movedPixel = driver.toPixel(moved);
6264
+ if (!movedPixel) return null;
6265
+ return {
6266
+ request: {
6267
+ coordinate: moved,
6268
+ pixel: movedPixel,
6269
+ interaction: "translate",
6270
+ graphicId: measure.id,
6271
+ originalEvent: info?.native
6272
+ },
6273
+ toDragPixel: (landed) => {
6274
+ const at = driver.fromPixel(landed);
6275
+ return driver.toPixel([origin[0] + at[0] - point[0], origin[1] + at[1] - point[1]]) ?? [pixel[0] + landed[0] - movedPixel[0], pixel[1] + landed[1] - movedPixel[1]];
6276
+ }
6277
+ };
6278
+ });
6255
6279
  }
6256
6280
  /**
6257
6281
  * Runs a drag at `pixel` through the immediate snap sources: the drag pixel
6258
6282
  * they put it at, and the target there for `sources.deferred` to answer.
6283
+ * `targetsAt` lists the points the immediate sources test, null where one
6284
+ * can't be placed; the first is the deferred target. The snap nearest the
6285
+ * cursor wins and is reported once.
6259
6286
  */
6260
- function snapDrag(pixel, targetAt) {
6261
- const target = targetAt(pixel);
6262
- const result = target && snap?.(target.request);
6263
- if (!result) return {
6287
+ function snapDrag(pixel, targetsAt) {
6288
+ const targets = targetsAt(pixel);
6289
+ const candidates = targets.filter((target) => target !== null);
6290
+ const hit = snap?.closest(candidates.map((candidate) => candidate.request), pixel);
6291
+ const winner = hit && candidates.find((candidate) => candidate.request === hit.request);
6292
+ if (!winner) return {
6264
6293
  pixel,
6265
- target
6294
+ target: targets[0] ?? null
6266
6295
  };
6267
- const snapped = target.toDragPixel(result.pixel);
6296
+ const snapped = winner.toDragPixel(hit.result.pixel);
6268
6297
  return {
6269
6298
  pixel: snapped,
6270
- target: targetAt(snapped)
6299
+ target: targetsAt(snapped)[0] ?? null
6271
6300
  };
6272
6301
  }
6273
6302
  const deferred = createDeferredDragSnap(deferredSnap, () => ctx.settled);
@@ -6483,7 +6512,7 @@ function runEdit(opts) {
6483
6512
  function paintDragSnapshot(pixel, info) {
6484
6513
  if (!activeDrag) return;
6485
6514
  const { controlPointIndex } = activeDrag;
6486
- const snapped = snapDrag(pixel, (at) => reshapeTarget(at, info, controlPointIndex));
6515
+ const snapped = snapDrag(pixel, (at) => [reshapeTarget(at, info, controlPointIndex)]);
6487
6516
  moveDeferred(snapped.pixel, snapped.target, (at) => paintDragAt(at, info));
6488
6517
  }
6489
6518
  function paintDragAt(resolvedPixel, info) {
@@ -6537,7 +6566,7 @@ function runEdit(opts) {
6537
6566
  function paintBoxDragSnapshot(pixel, info, constrainRotation = false) {
6538
6567
  if (!activeBoxDrag) return;
6539
6568
  const drag = activeBoxDrag;
6540
- const snapped = snapDrag(pixel, (at) => translateTarget(at, info, drag));
6569
+ const snapped = snapDrag(pixel, (at) => translateTargets(at, info, drag));
6541
6570
  moveDeferred(snapped.pixel, snapped.target, (at) => paintBoxDragAt(at, constrainRotation));
6542
6571
  }
6543
6572
  function paintBoxDragAt(resolvedPixel, constrainRotation) {
@@ -6684,7 +6713,7 @@ function runEdit(opts) {
6684
6713
  renderHandlesLayer();
6685
6714
  return;
6686
6715
  }
6687
- const snapped = snapDrag(pixel, (at) => reshapeTarget(at, info, controlPointIndex));
6716
+ const snapped = snapDrag(pixel, (at) => [reshapeTarget(at, info, controlPointIndex)]);
6688
6717
  deferred.finish(releaseDeferred(snapped.pixel, snapped.target), (finalPixel) => {
6689
6718
  const next = fsm.end(finalPixel, { constrainAngles: constrainHeld(info) });
6690
6719
  transient.emit("end", next, controlPointIndex);
@@ -6721,7 +6750,7 @@ function runEdit(opts) {
6721
6750
  preDragPoints,
6722
6751
  downPixel
6723
6752
  };
6724
- const snapped = snapDrag(pixel, (at) => translateTarget(at, info, drag));
6753
+ const snapped = snapDrag(pixel, (at) => translateTargets(at, info, drag));
6725
6754
  deferred.finish(releaseDeferred(snapped.pixel, snapped.target), (finalPixel) => {
6726
6755
  const finalSnapshot = fsm.move(finalPixel, { constrainRotation: constrainHeld(info) });
6727
6756
  replaceWorkingPoints(preDragPoints);
@@ -7490,6 +7519,55 @@ function createFrameCoalescer(run) {
7490
7519
  };
7491
7520
  }
7492
7521
  //#endregion
7522
+ //#region src/tactical-draw/box-snap.ts
7523
+ /**
7524
+ * The closest snap of `points`, tested as one request each and reported once.
7525
+ * With `cursor`, the snap nearest the cursor wins, so the part of the box being
7526
+ * dragged near snaps before a far one. Null with no resolver or no snap.
7527
+ */
7528
+ function closestBoxSnap(context, points, interaction, originalEvent, cursor) {
7529
+ if (!context.snap) return null;
7530
+ const { graphicId, graphicIds } = context;
7531
+ const requests = points.flatMap((coordinate) => {
7532
+ const pixel = context.toPixel(coordinate);
7533
+ return pixel ? [{
7534
+ coordinate,
7535
+ pixel,
7536
+ interaction,
7537
+ graphicId,
7538
+ graphicIds,
7539
+ originalEvent
7540
+ }] : [];
7541
+ });
7542
+ return context.snap.closest(requests, cursor);
7543
+ }
7544
+ /** How far `hit` moves the point it tested on the ground, in Web Mercator metres. */
7545
+ function groundSnapOffset(adapter, { request, result }) {
7546
+ const [fromX, fromY] = adapter.fromLonLat(request.coordinate);
7547
+ const [toX, toY] = adapter.fromLonLat(result.coordinate);
7548
+ return [toX - fromX, toY - fromY];
7549
+ }
7550
+ /**
7551
+ * A corner scale snapped to `target`: the factor that puts the dragged corner
7552
+ * of `startCorners` (TL, TR, BR, BL) at `target` projected on to its diagonal,
7553
+ * with the opposite corner fixed, and where `center` goes for a given factor.
7554
+ * Null for a degenerate box.
7555
+ */
7556
+ function snappedCornerScale(adapter, startCorners, cornerIndex, center, target) {
7557
+ const [anchorX, anchorY] = adapter.fromLonLat(startCorners[(cornerIndex + 2) % 4]);
7558
+ const [cornerX, cornerY] = adapter.fromLonLat(startCorners[cornerIndex]);
7559
+ const [toX, toY] = adapter.fromLonLat(target);
7560
+ const [centerX, centerY] = adapter.fromLonLat(center);
7561
+ const dx = cornerX - anchorX;
7562
+ const dy = cornerY - anchorY;
7563
+ const lengthSquared = dx * dx + dy * dy;
7564
+ if (lengthSquared === 0) return null;
7565
+ return {
7566
+ factor: ((toX - anchorX) * dx + (toY - anchorY) * dy) / lengthSquared,
7567
+ centerAt: (factor) => adapter.toLonLat([anchorX + (centerX - anchorX) * factor, anchorY + (centerY - anchorY) * factor])
7568
+ };
7569
+ }
7570
+ //#endregion
7493
7571
  //#region src/tactical-draw/transform-controller.ts
7494
7572
  /** No point symbol may scale below this factor of its own size — mirrors the box-drag floor. */
7495
7573
  const MIN_POINT_SYMBOL_SIZE = .1;
@@ -7535,7 +7613,7 @@ function defaultGroupSizeAnchor(graphics) {
7535
7613
  */
7536
7614
  function runGroupTransform(opts) {
7537
7615
  const constrainKey = () => opts.editModifiers().constrain;
7538
- const { adapter, capability, imageCapability, noteCapability, markerCapability, graphics, graphicsLayer, previewLayer, handlesLayer, renderOptions: renderOpts, signal, closeOnClickAway = true, interactionStyle, onSession, handleIdPrefix, publishWorkingGraphics, settleWorkingGraphics, isOnGraphicsLayer, onSettled, onClickAwayGraphicsPick } = opts;
7616
+ const { adapter, capability, imageCapability, noteCapability, markerCapability, graphics, graphicsLayer, previewLayer, handlesLayer, renderOptions: renderOpts, signal, closeOnClickAway = true, interactionStyle, onSession, handleIdPrefix, publishWorkingGraphics, settleWorkingGraphics, isOnGraphicsLayer, onSettled, onClickAwayGraphicsPick, snap } = opts;
7539
7617
  const { noteResolveCache } = opts;
7540
7618
  assertCapabilityFor(graphics);
7541
7619
  validateHostResources(graphics);
@@ -8339,6 +8417,7 @@ function runGroupTransform(opts) {
8339
8417
  preDragPoints,
8340
8418
  preDragBox: groupBox,
8341
8419
  downPixel: pixel,
8420
+ bodySnap: boxHit.kind === "body" ? groupBodySnap(pixel) : null,
8342
8421
  lastSnapshot: {
8343
8422
  points: preDragPoints,
8344
8423
  box: groupBox
@@ -8350,8 +8429,48 @@ function runGroupTransform(opts) {
8350
8429
  dragInFlight = true;
8351
8430
  driver.setDragPanEnabled(false);
8352
8431
  };
8353
- function paintBoxDragSnapshot(pixel, constrainRotation = false) {
8432
+ /**
8433
+ * A body drag's snapping, from pointer-down at `downPixel`: the corners and
8434
+ * centre of the box around every member's footprint, without the edit
8435
+ * padding, in lon/lat. A corner near an intersection lines up two sides of
8436
+ * the group, an edge near a line that side. Null without a resolver.
8437
+ */
8438
+ function groupBodySnap(downPixel) {
8439
+ if (!snap) return null;
8440
+ const box = fitTransformBox(members.flatMap((member) => member.lastBoxPixels), boxAngle, 0);
8441
+ if (!box) return null;
8442
+ return {
8443
+ points: [...boxCorners(box), box.center].map((corner) => driver.fromPixel(corner)),
8444
+ origin: driver.fromPixel(downPixel),
8445
+ context: {
8446
+ adapter,
8447
+ snap,
8448
+ toPixel: (position) => driver.toPixel(position),
8449
+ graphicIds: members.map((member) => member.graphic.id)
8450
+ }
8451
+ };
8452
+ }
8453
+ /**
8454
+ * The drag pixel that puts a body drag at `pixel` on the snap nearest the
8455
+ * cursor: every snap point moves by the pointer's lon/lat delta (as
8456
+ * `beginBoxDrag` moves the members), and the winner's offset to its target is
8457
+ * added to the pointer. `pixel` itself with no snap.
8458
+ */
8459
+ function snappedDragPixel(bodySnap, pixel, info) {
8460
+ const { points, origin, context } = bodySnap;
8461
+ const cursor = driver.fromPixel(pixel);
8462
+ const dx = cursor[0] - origin[0];
8463
+ const dy = cursor[1] - origin[1];
8464
+ const hit = closestBoxSnap(context, points.map((point) => [point[0] + dx, point[1] + dy]), "translate", info?.native, pixel);
8465
+ if (!hit) return pixel;
8466
+ const { coordinate: from } = hit.request;
8467
+ const { coordinate: to } = hit.result;
8468
+ return driver.toPixel([cursor[0] + to[0] - from[0], cursor[1] + to[1] - from[1]]) ?? pixel;
8469
+ }
8470
+ function paintBoxDragSnapshot(rawPixel, constrainRotation = false, info) {
8354
8471
  if (!activeBoxDrag) return;
8472
+ const { bodySnap } = activeBoxDrag;
8473
+ const pixel = bodySnap ? snappedDragPixel(bodySnap, rawPixel, info) : rawPixel;
8355
8474
  const snapshot = activeBoxDrag.fsm.move(pixel, { constrainRotation });
8356
8475
  activeBoxDrag.lastSnapshot = snapshot;
8357
8476
  applyConcatenatedPoints(snapshot.points);
@@ -8365,7 +8484,7 @@ function runGroupTransform(opts) {
8365
8484
  const boxDragPaintCoalescer = createFrameCoalescer(paintBoxDragSnapshot);
8366
8485
  const onDriverMove = (pixel, info) => {
8367
8486
  if (ctx.settled) return;
8368
- if (activeBoxDrag) boxDragPaintCoalescer.schedule(pixel, modifierHeld(info, constrainKey()));
8487
+ if (activeBoxDrag) boxDragPaintCoalescer.schedule(pixel, modifierHeld(info, constrainKey()), info);
8369
8488
  };
8370
8489
  const onDriverUp = (pixel, info) => {
8371
8490
  if (!activeBoxDrag) return;
@@ -8376,7 +8495,7 @@ function runGroupTransform(opts) {
8376
8495
  driver.setDragPanEnabled(true);
8377
8496
  return;
8378
8497
  }
8379
- const { fsm, preDragPoints, preDragBox, downPixel, preDragOptionsOverrideByMember, preDragAmplifierPlacementsByMember, preDragPointSymbolStateByMember } = activeBoxDrag;
8498
+ const { fsm, preDragPoints, preDragBox, downPixel, preDragOptionsOverrideByMember, preDragAmplifierPlacementsByMember, preDragPointSymbolStateByMember, bodySnap } = activeBoxDrag;
8380
8499
  activeBoxDrag = null;
8381
8500
  driver.setDragPanEnabled(true);
8382
8501
  if (fsm.kind === "body" && withinClickSlop(pixel, downPixel)) {
@@ -8390,7 +8509,7 @@ function runGroupTransform(opts) {
8390
8509
  shell.noteBodyClick();
8391
8510
  return;
8392
8511
  }
8393
- const finalSnapshot = fsm.move(pixel, { constrainRotation: modifierHeld(info, constrainKey()) });
8512
+ const finalSnapshot = fsm.move(bodySnap ? snappedDragPixel(bodySnap, pixel, info) : pixel, { constrainRotation: modifierHeld(info, constrainKey()) });
8394
8513
  const { scale, rotation } = boxScaleRotation(fsm.kind, finalSnapshot.box, preDragBox);
8395
8514
  const nextPointSymbolStateByMember = scalePointSymbolStates(preDragPointSymbolStateByMember, scale, rotation);
8396
8515
  applyConcatenatedPoints(preDragPoints);
@@ -9210,24 +9329,52 @@ function runImageEdit(opts) {
9210
9329
  const result = drag.fsm.move(pixel, { constrainRotation: modifierHeld(info, constrainKey()) });
9211
9330
  next.position = [...result.points[0]];
9212
9331
  const delta = boxScaleRotation(drag.kind, result.box, drag.startBox);
9213
- if (drag.kind === "scale") next.size = {
9214
- value: Math.max(.1, next.size.value * delta.scale),
9215
- unit: "meters"
9216
- };
9217
- else if (drag.kind === "rotate") next.rotation = normalizeRotation(next.rotation + delta.rotation);
9332
+ if (drag.kind === "scale") {
9333
+ next.size = {
9334
+ value: Math.max(.1, next.size.value * delta.scale),
9335
+ unit: "meters"
9336
+ };
9337
+ return snappedScale(drag, next, info) ?? next;
9338
+ } else if (drag.kind === "rotate") next.rotation = normalizeRotation(next.rotation + delta.rotation);
9218
9339
  else {
9219
- const anchorPixel = driver.toPixel(next.position) ?? pixel;
9220
- const snapped = snap?.({
9221
- coordinate: next.position,
9222
- pixel: anchorPixel,
9223
- interaction: "translate",
9224
- graphicId: image.id,
9225
- originalEvent: info?.native
9226
- });
9227
- if (snapped) next.position = snapped.coordinate;
9340
+ const hit = closestBoxSnap(boxSnap, [next.position, ...cornersOf(next)], "translate", info?.native, pixel);
9341
+ if (hit) next.position = offsetGroundPosition(adapter, next.position, 0, ...groundSnapOffset(adapter, hit));
9228
9342
  }
9229
9343
  return next;
9230
9344
  };
9345
+ const boxSnap = {
9346
+ adapter,
9347
+ snap,
9348
+ toPixel: (position) => driver.toPixel(position),
9349
+ graphicId: image.id
9350
+ };
9351
+ /** `value`'s footprint corners TL, TR, BR, BL on the ground, without the edit padding. */
9352
+ const cornersOf = (value) => {
9353
+ const { width, height } = value.intrinsicSize;
9354
+ return groundBoxCoordinates(adapter, value.position, value.rotation, value.size.value, value.size.value * height / width);
9355
+ };
9356
+ /**
9357
+ * A corner scale whose dragged corner snaps: the snapped point projected on
9358
+ * to the diagonal from the fixed opposite corner gives the factor. Null when
9359
+ * nothing snaps.
9360
+ */
9361
+ const snappedScale = (current, scaled, info) => {
9362
+ const { start, cornerIndex } = current;
9363
+ if (cornerIndex === void 0) return null;
9364
+ const corner = cornersOf(scaled)[cornerIndex];
9365
+ const hit = closestBoxSnap(boxSnap, [corner], "reshape", info?.native);
9366
+ if (!hit) return null;
9367
+ const scale = snappedCornerScale(adapter, cornersOf(start), cornerIndex, start.position, hit.result.coordinate);
9368
+ if (!scale) return null;
9369
+ const value = Math.max(.1, start.size.value * scale.factor);
9370
+ const next = cloneImageGraphic(start);
9371
+ next.size = {
9372
+ value,
9373
+ unit: "meters"
9374
+ };
9375
+ next.position = scale.centerAt(value / start.size.value);
9376
+ return next;
9377
+ };
9231
9378
  const onDown = (pixel, info) => {
9232
9379
  if (ctx.settled || !box || !modes.includes("transform")) return;
9233
9380
  const hit = pickBoxHandle(box, pixel, info?.hitTolerance ?? 12);
@@ -9247,7 +9394,8 @@ function runImageEdit(opts) {
9247
9394
  kind: hit.kind,
9248
9395
  down: [...pixel],
9249
9396
  start: cloneImageGraphic(working),
9250
- startBox: box
9397
+ startBox: box,
9398
+ ...hit.kind === "scale" ? { cornerIndex: hit.index } : {}
9251
9399
  };
9252
9400
  driver.setDragPanEnabled(false);
9253
9401
  };
@@ -9566,13 +9714,24 @@ function runNoteEdit(opts) {
9566
9714
  * opposite edge keeps its ground position. The height follows the new layout,
9567
9715
  * growing or shrinking at the bottom.
9568
9716
  */
9569
- const widened = (current, pixel) => {
9717
+ const widened = (current, pixel, info) => {
9718
+ const pointer = groundAt(pixel);
9719
+ const side = current.index === 1 ? 1 : -1;
9720
+ const next = widenedUnsnapped(current, side, pointer);
9721
+ const corners = cornersOf(next);
9722
+ const edge = side === 1 ? [corners[1], corners[2]] : [corners[0], corners[3]];
9723
+ const midpoint = offsetGroundPosition(adapter, next.position, next.rotation, side * next.width * next.emSize.value / 2, 0);
9724
+ const hit = closestSnap([...edge, midpoint], "reshape", info, pixel);
9725
+ if (!hit) return next;
9726
+ const [dx, dy] = groundSnapOffset(adapter, hit);
9727
+ return widenedUnsnapped(current, side, [pointer[0] + dx, pointer[1] + dy]);
9728
+ };
9729
+ /** {@link widened} for a pointer at `pointer` in Web Mercator metres, without snapping. */
9730
+ const widenedUnsnapped = (current, side, pointer) => {
9570
9731
  const next = cloneNoteGraphic(current.start);
9571
9732
  const { start } = current;
9572
9733
  const em = start.emSize.value;
9573
- const side = current.index === 1 ? 1 : -1;
9574
9734
  const axis = [Math.cos(start.rotation) * side, -Math.sin(start.rotation) * side];
9575
- const pointer = groundAt(pixel);
9576
9735
  const pulled = (pointer[0] - current.downGround[0]) * axis[0] + (pointer[1] - current.downGround[1]) * axis[1];
9577
9736
  const width = Math.min(60, Math.max(2, start.width + pulled / em));
9578
9737
  next.width = width;
@@ -9580,8 +9739,8 @@ function runNoteEdit(opts) {
9580
9739
  };
9581
9740
  const transformed = (pixel, info) => {
9582
9741
  if (!drag) return null;
9583
- if (drag.kind === "width") return widened(drag, pixel);
9584
- const next = cloneNoteGraphic(drag.start);
9742
+ if (drag.kind === "width") return widened(drag, pixel, info);
9743
+ let next = cloneNoteGraphic(drag.start);
9585
9744
  const result = drag.fsm.move(pixel, { constrainRotation: modifierHeld(info, constrainKey()) });
9586
9745
  next.position = [...result.points[0]];
9587
9746
  const delta = boxScaleRotation(drag.kind, result.box, drag.startBox);
@@ -9595,25 +9754,58 @@ function runNoteEdit(opts) {
9595
9754
  };
9596
9755
  if (value !== startEm * delta.scale) {
9597
9756
  const factor = value / startEm;
9598
- const { anchor } = drag;
9757
+ const anchor = boxCorners(drag.startBox)[(drag.cornerIndex + 2) % 4];
9599
9758
  const center = driver.toPixel(drag.start.position);
9600
9759
  if (center) next.position = driver.fromPixel([anchor[0] + (center[0] - anchor[0]) * factor, anchor[1] + (center[1] - anchor[1]) * factor]);
9601
9760
  }
9761
+ next = snappedScale(drag, next, info) ?? next;
9602
9762
  } else if (drag.kind === "rotate") next.rotation = normalizeRotation(next.rotation + delta.rotation);
9603
9763
  else {
9604
- const anchorPixel = driver.toPixel(next.position) ?? pixel;
9605
- const snapped = snap?.({
9606
- coordinate: next.position,
9607
- pixel: anchorPixel,
9608
- interaction: "translate",
9609
- graphicId: note.id,
9610
- originalEvent: info?.native
9611
- });
9612
- if (snapped) next.position = snapped.coordinate;
9764
+ const corners = cornersOf(next, drag.startHeight);
9765
+ const hit = closestSnap([next.position, ...corners], "translate", info, pixel);
9766
+ if (hit) next.position = offsetGroundPosition(adapter, next.position, 0, ...groundSnapOffset(adapter, hit));
9613
9767
  }
9614
9768
  return next;
9615
9769
  };
9616
9770
  /**
9771
+ * A corner scale whose dragged corner snaps: the snapped point projected on
9772
+ * to the diagonal from the fixed opposite corner gives the factor, clamped to
9773
+ * the edit's `emSize` range. Null when nothing snaps.
9774
+ */
9775
+ const snappedScale = (current, scaled, info) => {
9776
+ const { start, startHeight, cornerIndex } = current;
9777
+ const corner = cornersOf(scaled, startHeight)[cornerIndex];
9778
+ const hit = closestSnap([corner], "reshape", info);
9779
+ if (!hit) return null;
9780
+ const scale = snappedCornerScale(adapter, cornersOf(start, startHeight), cornerIndex, start.position, hit.result.coordinate);
9781
+ if (!scale) return null;
9782
+ const [min, max] = current.emSizeRange;
9783
+ const value = Math.min(max, Math.max(min, start.emSize.value * scale.factor));
9784
+ const next = cloneNoteGraphic(start);
9785
+ next.emSize = {
9786
+ value,
9787
+ unit: "meters"
9788
+ };
9789
+ next.position = scale.centerAt(value / start.emSize.value);
9790
+ return next;
9791
+ };
9792
+ /**
9793
+ * `value`'s box corners TL, TR, BR, BL on the ground, without the edit
9794
+ * padding. Pass `height` in em when it is known, to skip measuring the layout.
9795
+ */
9796
+ const cornersOf = (value, height = heightOf(value)) => {
9797
+ const em = value.emSize.value;
9798
+ return groundBoxCoordinates(adapter, value.position, value.rotation, value.width * em, height * em);
9799
+ };
9800
+ const boxSnap = {
9801
+ adapter,
9802
+ snap,
9803
+ toPixel: (position) => driver.toPixel(position),
9804
+ graphicId: note.id
9805
+ };
9806
+ /** {@link closestBoxSnap} of the Note's `points`. */
9807
+ const closestSnap = (points, interaction, info, cursor) => closestBoxSnap(boxSnap, points, interaction, info?.native, cursor);
9808
+ /**
9617
9809
  * The `emSize` range a scale drag may reach: the limits around the size this
9618
9810
  * edit started with. `updateGraphic` can set a size outside them, so a drag
9619
9811
  * from there includes its start value rather than jumping to the limit.
@@ -9647,12 +9839,13 @@ function runNoteEdit(opts) {
9647
9839
  }),
9648
9840
  down: [...pixel],
9649
9841
  start: cloneNoteGraphic(working),
9650
- startBox: box
9842
+ startBox: box,
9843
+ startHeight: heightOf(working)
9651
9844
  };
9652
9845
  drag = hit.kind === "scale" ? {
9653
9846
  ...shared,
9654
9847
  kind: "scale",
9655
- anchor: boxCorners(box)[(hit.index + 2) % 4],
9848
+ cornerIndex: hit.index,
9656
9849
  emSizeRange: emSizeRange(working.emSize.value)
9657
9850
  } : {
9658
9851
  ...shared,
@@ -11409,7 +11602,8 @@ var TacticalDraw = class {
11409
11602
  isOnGraphicsLayer,
11410
11603
  onSettled,
11411
11604
  onClickAwayGraphicsPick,
11412
- editModifiers: this.currentEditModifiers
11605
+ editModifiers: this.currentEditModifiers,
11606
+ snap: this.resolveSnap
11413
11607
  }), (handle) => {
11414
11608
  this.activeTransform = handle;
11415
11609
  return () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbat-mapper/tactical-draw",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Engine-agnostic draw/edit core for tactical control measures (MIL-STD-2525 / APP-6). The MapAdapter ABI and bring-your-own-engine entry point for the tactical-draw package family.",
5
5
  "license": "MIT",
6
6
  "author": "Orbat Mapper",