@orbat-mapper/tactical-draw 0.3.3 → 0.4.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.
Files changed (2) hide show
  1. package/dist/index.mjs +43 -15
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -1547,6 +1547,26 @@ function snappedDrawPosition(snap, event) {
1547
1547
  originalEvent: event.originalEvent
1548
1548
  })?.coordinate ?? event.coordinate;
1549
1549
  }
1550
+ /** Read Shift through engine event wrappers without assuming a DOM event type. */
1551
+ function shiftKeyPressed(originalEvent) {
1552
+ let event = originalEvent;
1553
+ for (let depth = 0; depth < 4; depth += 1) {
1554
+ if (typeof event !== "object" || event === null) return false;
1555
+ if ("shiftKey" in event && event.shiftKey === true) return true;
1556
+ if (!("originalEvent" in event)) return false;
1557
+ event = event.originalEvent;
1558
+ }
1559
+ return false;
1560
+ }
1561
+ /**
1562
+ * Apply a rule's angular constraint to `points` when the gesture asks for it
1563
+ * (Shift held) and the rule defines one. Returns `points` unchanged otherwise,
1564
+ * so callers can use the result unconditionally.
1565
+ */
1566
+ function constrainedDrawPoints(rule, points, activePointIndex, originalEvent) {
1567
+ if (!rule?.constrainAngles || !shiftKeyPressed(originalEvent)) return points;
1568
+ return rule.constrainAngles(points, activePointIndex);
1569
+ }
1550
1570
  const DEFAULT_TOLERANCE_PIXELS = 12;
1551
1571
  function graphicCandidates(graphics) {
1552
1572
  const candidates = [];
@@ -1992,7 +2012,9 @@ function runFixedLengthDraw(options) {
1992
2012
  ctx.commit(buildSnapshot);
1993
2013
  return;
1994
2014
  }
2015
+ const activePointIndex = committed.length;
1995
2016
  committed.push(position);
2017
+ committed.splice(0, committed.length, ...constrainedDrawPoints(rule, committed, activePointIndex, event.originalEvent));
1996
2018
  rubberBand = void 0;
1997
2019
  if (committed.length >= required) {
1998
2020
  ctx.commit(buildSnapshot);
@@ -2009,8 +2031,9 @@ function runFixedLengthDraw(options) {
2009
2031
  if (ctx.settled) return;
2010
2032
  if (committed.length === 0) return;
2011
2033
  rubberBand = snappedDrawPosition(snap, event);
2034
+ const points = constrainedDrawPoints(rule, [...committed, rubberBand], committed.length, event.originalEvent);
2035
+ rubberBand = points[committed.length] ?? rubberBand;
2012
2036
  renderPreview();
2013
- const points = [...committed, rubberBand];
2014
2037
  const phase = transientStarted ? "move" : "start";
2015
2038
  transientStarted = true;
2016
2039
  transientEmitter.emit({
@@ -2292,7 +2315,7 @@ function runVariableLengthDraw(options) {
2292
2315
  const idx = insertionIndex(committed.length);
2293
2316
  committed.splice(idx, 0, [...pos]);
2294
2317
  if (rule) {
2295
- const derived = rule.derive(committed);
2318
+ const derived = rule.derive(constrainedDrawPoints(rule, committed, idx, event.originalEvent));
2296
2319
  committed.splice(0, committed.length, ...derived);
2297
2320
  }
2298
2321
  committed.splice(0, committed.length, ...roundControlPoints(committed));
@@ -2312,10 +2335,12 @@ function runVariableLengthDraw(options) {
2312
2335
  if (ctx.settled) return;
2313
2336
  if (committed.length === 0) return;
2314
2337
  rubberBand = snappedDrawPosition(snap, event);
2315
- renderPreview();
2316
2338
  const idx = insertionIndex(committed.length);
2317
- const points = [...committed];
2318
- points.splice(idx, 0, rubberBand);
2339
+ const spine = [...committed];
2340
+ spine.splice(idx, 0, rubberBand);
2341
+ const points = constrainedDrawPoints(rule, spine, idx, event.originalEvent);
2342
+ rubberBand = points[idx] ?? rubberBand;
2343
+ renderPreview();
2319
2344
  const phase = transientStarted ? "move" : "start";
2320
2345
  transientStarted = true;
2321
2346
  transientEmitter.emit({
@@ -2924,7 +2949,7 @@ function begun(kind, initialBase, move) {
2924
2949
  drag: {
2925
2950
  kind,
2926
2951
  move,
2927
- end: (pixel) => move(pixel).points
2952
+ end: (pixel, modifiers) => move(pixel, modifiers).points
2928
2953
  },
2929
2954
  initialPoints: clonePoints(initialBase)
2930
2955
  };
@@ -2941,14 +2966,17 @@ function beginHandleDrag(args) {
2941
2966
  const { hit, points, projection, rule } = args;
2942
2967
  const { fromPixel } = projection;
2943
2968
  const preDragPoints = clonePoints(points);
2944
- const snap = (next, activePointIndex) => rule ? rule.transform({
2945
- previous: preDragPoints,
2946
- next,
2947
- activePointIndex
2948
- }) : next;
2969
+ const snap = (next, activePointIndex, modifiers) => {
2970
+ const transformed = rule ? rule.transform({
2971
+ previous: preDragPoints,
2972
+ next,
2973
+ activePointIndex
2974
+ }) : next;
2975
+ return modifiers?.constrainAngles && rule?.constrainAngles ? rule.constrainAngles(transformed, activePointIndex) : transformed;
2976
+ };
2949
2977
  const dragOnePoint = (base, activeIndex) => {
2950
- const move = (pixel) => {
2951
- return { points: snap(base.map((p, i) => i === activeIndex ? fromPixel(pixel) : [...p]), activeIndex) };
2978
+ const move = (pixel, modifiers) => {
2979
+ return { points: snap(base.map((p, i) => i === activeIndex ? fromPixel(pixel) : [...p]), activeIndex, modifiers) };
2952
2980
  };
2953
2981
  return move;
2954
2982
  };
@@ -4840,7 +4868,7 @@ function runEdit(opts) {
4840
4868
  function paintDragSnapshot(pixel, info) {
4841
4869
  if (!activeDrag) return;
4842
4870
  const resolvedPixel = snappedPixel(pixel, info, "reshape", activeDrag.controlPointIndex);
4843
- const snapshot = activeDrag.fsm.move(resolvedPixel);
4871
+ const snapshot = activeDrag.fsm.move(resolvedPixel, { constrainAngles: info?.shiftKey });
4844
4872
  workingPoints.splice(0, workingPoints.length, ...snapshot.points);
4845
4873
  renderWorkingGraphic();
4846
4874
  renderHandlesLayer();
@@ -4941,7 +4969,7 @@ function runEdit(opts) {
4941
4969
  const { fsm, preDragPoints, controlPointIndex } = activeDrag;
4942
4970
  activeDrag = null;
4943
4971
  driver.setDragPanEnabled(true);
4944
- const next = fsm.end(snappedPixel(pixel, info, "reshape", controlPointIndex));
4972
+ const next = fsm.end(snappedPixel(pixel, info, "reshape", controlPointIndex), { constrainAngles: info?.shiftKey });
4945
4973
  emitTransient("end", next, controlPointIndex);
4946
4974
  replaceWorkingPoints(preDragPoints);
4947
4975
  commitGesture(next);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbat-mapper/tactical-draw",
3
- "version": "0.3.3",
3
+ "version": "0.4.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",
@@ -39,7 +39,7 @@
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@orbat-mapper/control-measures": "^0.5.1"
42
+ "@orbat-mapper/control-measures": "^0.6.0"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "vitest": ">=4.0.0"