@widgetic/canvas 0.5.41 → 0.5.42

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 (85) hide show
  1. package/dist/canvas/autosaveScheduler.js +37 -89
  2. package/dist/canvas/boardFrame.js +1 -1
  3. package/dist/canvas/boardSurface.js +1 -1
  4. package/dist/canvas/canvasLogger.js +7 -19
  5. package/dist/canvas/chromeLayout.js +17 -18
  6. package/dist/canvas/eventScope.js +10 -14
  7. package/dist/canvas/host/CanvasHost.js +1286 -1536
  8. package/dist/canvas/host/CanvasHostApi.js +4 -19
  9. package/dist/canvas/host/browserServices.js +65 -116
  10. package/dist/canvas/host/conversion.js +82 -170
  11. package/dist/canvas/host/hostObject.js +7 -7
  12. package/dist/canvas/host/pageEvents.js +4 -4
  13. package/dist/canvas/host/screenRects.js +28 -31
  14. package/dist/canvas/host/strokeSnap.js +88 -143
  15. package/dist/canvas/openMarks.js +2 -2
  16. package/dist/canvas/outsideClickDeselect.js +6 -7
  17. package/dist/canvas/panZoomPosition.js +2 -2
  18. package/dist/canvas/preferredTextFont.js +13 -25
  19. package/dist/canvas/shapes/ShapeLibrary.js +138 -148
  20. package/dist/canvas/shapes/shapeImageStore.js +3 -3
  21. package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
  22. package/dist/canvas/toolbarPosition.js +2 -2
  23. package/dist/canvas/ui/colorModel.js +21 -32
  24. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  25. package/dist/canvas/ui/floatingCard.js +18 -18
  26. package/dist/canvas/ui/floatingLayer.js +5 -6
  27. package/dist/canvas/ui/keyHints.js +9 -11
  28. package/dist/canvas/ui/popoverSide.js +2 -3
  29. package/dist/canvas/ui/selectionTraits.js +34 -40
  30. package/dist/canvas/view/erasingMarks.js +7 -7
  31. package/dist/canvas/view/geometry.js +71 -90
  32. package/dist/components/preferenceStorage.js +2 -2
  33. package/dist/engine/EngineCore.js +696 -795
  34. package/dist/engine/codec/canvasDocument.js +183 -147
  35. package/dist/engine/codec/hostReadableKeys.js +1 -1
  36. package/dist/engine/core/arrange.js +26 -38
  37. package/dist/engine/core/arrow.js +76 -89
  38. package/dist/engine/core/arrowBinding.js +98 -116
  39. package/dist/engine/core/arrowBindingRecords.js +23 -38
  40. package/dist/engine/core/brush.js +67 -94
  41. package/dist/engine/core/defaults.js +90 -51
  42. package/dist/engine/core/freehand.js +147 -163
  43. package/dist/engine/core/geometry.js +47 -54
  44. package/dist/engine/core/history.js +33 -50
  45. package/dist/engine/core/hitTest.js +68 -81
  46. package/dist/engine/core/matrix.js +18 -20
  47. package/dist/engine/core/pathData.js +183 -210
  48. package/dist/engine/core/rotationReset.js +5 -6
  49. package/dist/engine/core/scene.js +129 -187
  50. package/dist/engine/core/scribble.js +59 -84
  51. package/dist/engine/core/shapePaths.js +12 -15
  52. package/dist/engine/core/shapeTools.js +72 -63
  53. package/dist/engine/core/snapping.js +65 -117
  54. package/dist/engine/core/style.js +31 -64
  55. package/dist/engine/core/transform.js +83 -94
  56. package/dist/engine/core/types.js +4 -4
  57. package/dist/engine/core/viewport.js +23 -36
  58. package/dist/engine/input/PointerController.js +467 -474
  59. package/dist/engine/input/arrowHandles.js +12 -12
  60. package/dist/engine/input/gestures.js +262 -308
  61. package/dist/engine/input/hoverCursor.js +1 -1
  62. package/dist/engine/input/keyboard.js +27 -30
  63. package/dist/engine/input/selectionHandles.js +45 -50
  64. package/dist/engine/input/types.js +1 -1
  65. package/dist/engine/render/rasterize.js +86 -158
  66. package/dist/engine/state.js +4 -15
  67. package/dist/engine/text/layoutText.js +43 -62
  68. package/dist/engine/text/textMetrics.js +8 -10
  69. package/dist/engine/widgets.js +101 -137
  70. package/dist/fonts/interFont.js +17 -68
  71. package/dist/fonts/workspaceFonts.js +32 -90
  72. package/dist/i18n/labels.js +4 -4
  73. package/dist/i18n/language.js +4 -4
  74. package/dist/i18n/messages.js +1 -1
  75. package/dist/i18n/shortcuts.js +14 -14
  76. package/dist/settings/workspaceToolSettings.js +32 -48
  77. package/dist/tools/components/catalog.js +64 -74
  78. package/dist/tools/components/grouping.js +6 -28
  79. package/dist/tools/definitions.js +5 -5
  80. package/dist/tools/executors.js +105 -141
  81. package/dist/tools/hostAdapter.js +11 -13
  82. package/dist/tools/sceneSummary.js +43 -57
  83. package/dist/tools/sketchIntent.js +13 -21
  84. package/dist/tools/types.js +1 -1
  85. package/package.json +1 -1
@@ -41,7 +41,7 @@ export function hostCustomTypeOf(object) {
41
41
  return null;
42
42
  }
43
43
  function viewOf(object, index, scene) {
44
- var view = {
44
+ const view = {
45
45
  id: object.id,
46
46
  _objectId: object.id,
47
47
  type: hostTypeOf(object),
@@ -54,7 +54,7 @@ function viewOf(object, index, scene) {
54
54
  view._isPrimaryWidget = object.isPrimary;
55
55
  }
56
56
  if (object.type === 'group') {
57
- view.objects = scene.filter(function (member) { return member.parentId === object.id; }).map(function (member) { return viewOf(member, index, scene); });
57
+ view.objects = scene.filter((member) => member.parentId === object.id).map((member) => viewOf(member, index, scene));
58
58
  }
59
59
  return view;
60
60
  }
@@ -62,7 +62,7 @@ function viewOf(object, index, scene) {
62
62
  export function hostObjectById(engine, id) {
63
63
  if (!id)
64
64
  return null;
65
- var object = engine.getObject(id);
65
+ const object = engine.getObject(id);
66
66
  if (!object)
67
67
  return null;
68
68
  return viewOf(object, engine.sceneIndex(), engine.state.scene);
@@ -72,11 +72,11 @@ export function hostObjectById(engine, id) {
72
72
  * Null when nothing is selected.
73
73
  */
74
74
  export function selectedHostObject(engine) {
75
- var selected = engine.selectedObjects();
75
+ const selected = engine.selectedObjects();
76
76
  if (selected.length === 0)
77
77
  return null;
78
- var index = engine.sceneIndex();
79
- var scene = engine.state.scene;
78
+ const index = engine.sceneIndex();
79
+ const scene = engine.state.scene;
80
80
  if (selected.length === 1)
81
81
  return viewOf(selected[0], index, scene);
82
82
  return {
@@ -84,7 +84,7 @@ export function selectedHostObject(engine) {
84
84
  _objectId: '',
85
85
  type: 'activeSelection',
86
86
  _customType: null,
87
- objects: selected.map(function (object) { return viewOf(object, index, scene); })
87
+ objects: selected.map((object) => viewOf(object, index, scene))
88
88
  };
89
89
  }
90
90
  /** The shapes behind a list of units (a group counts through its members), for the conversion payload. */
@@ -12,17 +12,17 @@
12
12
  * Kept free of Svelte so that Node can test it. A page rendered on the server has no `window` and gets nothing.
13
13
  */
14
14
  import { canvasWarn } from '../canvasLogger.js';
15
- export var CANVAS_SELECT_TOOL_EVENT = 'widgetic:canvas-select-tool';
16
- export var WIDGET_POINTER_BLOCK_EVENT = 'widgetic:widget-shape-pointer-block';
15
+ export const CANVAS_SELECT_TOOL_EVENT = 'widgetic:canvas-select-tool';
16
+ export const WIDGET_POINTER_BLOCK_EVENT = 'widgetic:widget-shape-pointer-block';
17
17
  function dispatchOnWindow(name, detail) {
18
18
  if (typeof window === 'undefined' || typeof CustomEvent === 'undefined')
19
19
  return;
20
20
  try {
21
- window.dispatchEvent(detail === undefined ? new CustomEvent(name) : new CustomEvent(name, { detail: detail }));
21
+ window.dispatchEvent(detail === undefined ? new CustomEvent(name) : new CustomEvent(name, { detail }));
22
22
  }
23
23
  catch (error) {
24
24
  // A page that cannot take the event keeps its canvas.
25
- canvasWarn("Canvas: could not send ".concat(name), error);
25
+ canvasWarn(`Canvas: could not send ${name}`, error);
26
26
  }
27
27
  }
28
28
  /** The Select tool was chosen. */
@@ -11,8 +11,8 @@
11
11
  import { boxToWorldMatrix } from '../../engine/core/matrix.js';
12
12
  import { shapeBounds } from '../../engine/core/scene.js';
13
13
  function cssScaleOf(frame) {
14
- var layoutWidth = frame.layoutWidth && frame.layoutWidth > 0 ? frame.layoutWidth : frame.width;
15
- var layoutHeight = frame.layoutHeight && frame.layoutHeight > 0 ? frame.layoutHeight : frame.height;
14
+ const layoutWidth = frame.layoutWidth && frame.layoutWidth > 0 ? frame.layoutWidth : frame.width;
15
+ const layoutHeight = frame.layoutHeight && frame.layoutHeight > 0 ? frame.layoutHeight : frame.height;
16
16
  return {
17
17
  scaleX: layoutWidth > 0 && frame.width > 0 ? frame.width / layoutWidth : 1,
18
18
  scaleY: layoutHeight > 0 && frame.height > 0 ? frame.height / layoutHeight : 1
@@ -20,7 +20,7 @@ function cssScaleOf(frame) {
20
20
  }
21
21
  /** A point of the world on the page. */
22
22
  export function worldPointToPage(viewport, frame, x, y) {
23
- var _a = cssScaleOf(frame), scaleX = _a.scaleX, scaleY = _a.scaleY;
23
+ const { scaleX, scaleY } = cssScaleOf(frame);
24
24
  return {
25
25
  x: frame.left + (viewport.x + x * viewport.zoom) * scaleX,
26
26
  y: frame.top + (viewport.y + y * viewport.zoom) * scaleY
@@ -28,12 +28,12 @@ export function worldPointToPage(viewport, frame, x, y) {
28
28
  }
29
29
  /** The matrix from a box's own pixels to the page, or null when something is not a finite number. */
30
30
  export function boxToPageMatrix(box, viewport, frame) {
31
- var _a = cssScaleOf(frame), scaleX = _a.scaleX, scaleY = _a.scaleY;
32
- var world = boxToWorldMatrix(box);
31
+ const { scaleX, scaleY } = cssScaleOf(frame);
32
+ const world = boxToWorldMatrix(box);
33
33
  // page = frame + scale * (viewport.translate + zoom * world)
34
- var zoomX = viewport.zoom * scaleX;
35
- var zoomY = viewport.zoom * scaleY;
36
- var matrix = [
34
+ const zoomX = viewport.zoom * scaleX;
35
+ const zoomY = viewport.zoom * scaleY;
36
+ const matrix = [
37
37
  world[0] * zoomX,
38
38
  world[1] * zoomY,
39
39
  world[2] * zoomX,
@@ -45,34 +45,31 @@ export function boxToPageMatrix(box, viewport, frame) {
45
45
  }
46
46
  /** The geometry of a card on the page. Null when the numbers are not usable. */
47
47
  export function widgetPreviewGeometry(shape, viewport, frame) {
48
- var matrix = boxToPageMatrix(shape, viewport, frame);
48
+ const matrix = boxToPageMatrix(shape, viewport, frame);
49
49
  if (!matrix)
50
50
  return null;
51
- var localWidth = shape.width || 1;
52
- var localHeight = shape.height || 1;
53
- var corners = [
51
+ const localWidth = shape.width || 1;
52
+ const localHeight = shape.height || 1;
53
+ const corners = [
54
54
  [0, 0],
55
55
  [localWidth, 0],
56
56
  [0, localHeight],
57
57
  [localWidth, localHeight]
58
- ].map(function (_a) {
59
- var x = _a[0], y = _a[1];
60
- return ({
61
- x: matrix[0] * x + matrix[2] * y + matrix[4],
62
- y: matrix[1] * x + matrix[3] * y + matrix[5]
63
- });
64
- });
65
- var xs = corners.map(function (corner) { return corner.x; });
66
- var ys = corners.map(function (corner) { return corner.y; });
67
- var left = Math.min.apply(Math, xs);
68
- var top = Math.min.apply(Math, ys);
58
+ ].map(([x, y]) => ({
59
+ x: matrix[0] * x + matrix[2] * y + matrix[4],
60
+ y: matrix[1] * x + matrix[3] * y + matrix[5]
61
+ }));
62
+ const xs = corners.map((corner) => corner.x);
63
+ const ys = corners.map((corner) => corner.y);
64
+ const left = Math.min(...xs);
65
+ const top = Math.min(...ys);
69
66
  return {
70
- left: left,
71
- top: top,
72
- width: Math.max.apply(Math, xs) - left,
73
- height: Math.max.apply(Math, ys) - top,
74
- localWidth: localWidth,
75
- localHeight: localHeight,
67
+ left,
68
+ top,
69
+ width: Math.max(...xs) - left,
70
+ height: Math.max(...ys) - top,
71
+ localWidth,
72
+ localHeight,
76
73
  // The old canvas answered with the matrix of the card's pixels, one unit per local pixel.
77
74
  matrix: [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]]
78
75
  };
@@ -83,8 +80,8 @@ export function screenRectOfShape(shape, viewport, frame) {
83
80
  }
84
81
  /** The box of a rectangle of the world on the page, with the board it sits on. */
85
82
  export function screenRectOfBounds(bounds, viewport, frame) {
86
- var topLeft = worldPointToPage(viewport, frame, bounds.x, bounds.y);
87
- var bottomRight = worldPointToPage(viewport, frame, bounds.x + bounds.width, bounds.y + bounds.height);
83
+ const topLeft = worldPointToPage(viewport, frame, bounds.x, bounds.y);
84
+ const bottomRight = worldPointToPage(viewport, frame, bounds.x + bounds.width, bounds.y + bounds.height);
88
85
  return {
89
86
  left: Math.min(topLeft.x, bottomRight.x),
90
87
  top: Math.min(topLeft.y, bottomRight.y),
@@ -10,53 +10,6 @@
10
10
  *
11
11
  * The functions are plain TypeScript over the engine's records, so they run (and are tested) in Node.
12
12
  */
13
- var __assign = (this && this.__assign) || function () {
14
- __assign = Object.assign || function(t) {
15
- for (var s, i = 1, n = arguments.length; i < n; i++) {
16
- s = arguments[i];
17
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
18
- t[p] = s[p];
19
- }
20
- return t;
21
- };
22
- return __assign.apply(this, arguments);
23
- };
24
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
25
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
- return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
- step((generator = generator.apply(thisArg, _arguments || [])).next());
31
- });
32
- };
33
- var __generator = (this && this.__generator) || function (thisArg, body) {
34
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
35
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
36
- function verb(n) { return function (v) { return step([n, v]); }; }
37
- function step(op) {
38
- if (f) throw new TypeError("Generator is already executing.");
39
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
40
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
41
- if (y = 0, t) op = [op[0] & 2, t.value];
42
- switch (op[0]) {
43
- case 0: case 1: t = op; break;
44
- case 4: _.label++; return { value: op[1], done: false };
45
- case 5: _.label++; y = op[1]; op = [0]; continue;
46
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
47
- default:
48
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
49
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
50
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
51
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
52
- if (t[2]) _.ops.pop();
53
- _.trys.pop(); continue;
54
- }
55
- op = body.call(thisArg, _);
56
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
57
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
58
- }
59
- };
60
13
  import { brushCentreline, isBrushStroke } from '../../engine/core/brush.js';
61
14
  import { outlineBounds } from '../../engine/core/hitTest.js';
62
15
  import { parsePathData } from '../../engine/core/pathData.js';
@@ -70,8 +23,7 @@ import { CANVAS_SHAPE_SNAP_ENABLED, CANVAS_SNAP_SHAPE_LABELS, confidentSnapShape
70
23
  * (`CANVAS_SHAPE_SNAP_ENABLED`, false as shipped) turns it on for every page when it is flipped, which is the one
71
24
  * line to roll the behaviour out or back. The flag is a parameter so a test can hold both of its values.
72
25
  */
73
- export function isShapeSnapOn(pageAsked, packageFlag) {
74
- if (packageFlag === void 0) { packageFlag = CANVAS_SHAPE_SNAP_ENABLED; }
26
+ export function isShapeSnapOn(pageAsked, packageFlag = CANVAS_SHAPE_SNAP_ENABLED) {
75
27
  return packageFlag === true || pageAsked === true;
76
28
  }
77
29
  /**
@@ -82,71 +34,67 @@ export function isShapeSnapOn(pageAsked, packageFlag) {
82
34
  */
83
35
  export function strokeMeasurementPoints(stroke, handLine) {
84
36
  if (handLine && handLine.length >= 2)
85
- return handLine.map(function (point) { return ({ x: point.x - stroke.x, y: point.y - stroke.y }); });
37
+ return handLine.map((point) => ({ x: point.x - stroke.x, y: point.y - stroke.y }));
86
38
  if (isBrushStroke(stroke))
87
39
  return brushCentreline(stroke);
88
40
  return flattenPathCommands(parsePathData(stroke.d));
89
41
  }
90
42
  /** What the geometry says about a stroke: its measurements, or null when it is too small or too plain to measure. */
91
43
  export function measureDrawnStroke(stroke, handLine) {
92
- var points = strokeMeasurementPoints(stroke, handLine);
44
+ const points = strokeMeasurementPoints(stroke, handLine);
93
45
  if (points.length < 2)
94
46
  return null;
95
- var measurements = measureStroke(points);
47
+ const measurements = measureStroke(points);
96
48
  if (!measurements)
97
49
  return null;
98
- return { points: points, measurements: measurements };
50
+ return { points, measurements };
99
51
  }
100
52
  /**
101
53
  * Decides whether a stroke is a clean shape. Resolves to null for "leave it as drawn".
102
54
  * Deliberately never rejects.
103
55
  */
104
- export function decideStrokeSnap(stroke, context, handLine) {
105
- return __awaiter(this, void 0, Promise, function () {
106
- var measured, points, measurements, rankings, candidates, decisive, strokeImage, answer, named_1, error_1;
107
- var _a, _b, _c, _d, _e, _f;
108
- return __generator(this, function (_g) {
109
- switch (_g.label) {
110
- case 0:
111
- _g.trys.push([0, 2, , 3]);
112
- measured = measureDrawnStroke(stroke, handLine);
113
- if (!measured)
114
- return [2 /*return*/, null];
115
- points = measured.points, measurements = measured.measurements;
116
- rankings = rankSnapShapes(measurements);
117
- if (rankings.length === 0)
118
- return [2 /*return*/, null];
119
- candidates = rankings.map(function (ranking) { return (__assign(__assign({}, ranking), { label: CANVAS_SNAP_SHAPE_LABELS[ranking.shape] })); });
120
- decisive = confidentSnapShape(rankings);
121
- if (decisive)
122
- return [2 /*return*/, { shape: decisive.shape, source: 'geometry', axisTilt: measurements.axisTilt }];
123
- if (!context.recognize) {
124
- (_a = context.log) === null || _a === void 0 ? void 0 : _a.call(context, 'Shape snap: ambiguous and no recognizer configured, leaving the stroke as drawn', {
125
- candidates: candidates.map(function (candidate) { return candidate.shape; })
126
- });
127
- return [2 /*return*/, null];
128
- }
129
- strokeImage = renderStrokeSnapImage(points);
130
- return [4 /*yield*/, context.recognize(__assign({ measurements: measurements, candidates: candidates, best: (_b = candidates[0]) !== null && _b !== void 0 ? _b : null }, (strokeImage ? { image: strokeImage } : {})))];
131
- case 1:
132
- answer = _g.sent();
133
- named_1 = (_c = answer === null || answer === void 0 ? void 0 : answer.shape) !== null && _c !== void 0 ? _c : null;
134
- if (!named_1 || !candidates.some(function (candidate) { return candidate.shape === named_1; })) {
135
- (_d = context.log) === null || _d === void 0 ? void 0 : _d.call(context, 'Shape snap: the recognizer declined or named a shape the geometry ruled out', {
136
- shape: named_1,
137
- reason: (_e = answer === null || answer === void 0 ? void 0 : answer.reason) !== null && _e !== void 0 ? _e : 'no_answer'
138
- });
139
- return [2 /*return*/, null];
140
- }
141
- return [2 /*return*/, { shape: named_1, source: 'recognizer', axisTilt: measurements.axisTilt }];
142
- case 2:
143
- error_1 = _g.sent();
144
- (_f = context.log) === null || _f === void 0 ? void 0 : _f.call(context, 'Shape snap: failed, leaving the stroke as drawn', error_1);
145
- return [2 /*return*/, null];
146
- case 3: return [2 /*return*/];
147
- }
56
+ export async function decideStrokeSnap(stroke, context, handLine) {
57
+ try {
58
+ const measured = measureDrawnStroke(stroke, handLine);
59
+ if (!measured)
60
+ return null;
61
+ const { points, measurements } = measured;
62
+ // Only shapes the measurements cannot rule out are ever considered, and the same list is the only roster
63
+ // the host may choose from.
64
+ const rankings = rankSnapShapes(measurements);
65
+ if (rankings.length === 0)
66
+ return null;
67
+ const candidates = rankings.map((ranking) => ({ ...ranking, label: CANVAS_SNAP_SHAPE_LABELS[ranking.shape] }));
68
+ const decisive = confidentSnapShape(rankings);
69
+ if (decisive)
70
+ return { shape: decisive.shape, source: 'geometry', axisTilt: measurements.axisTilt };
71
+ if (!context.recognize) {
72
+ context.log?.('Shape snap: ambiguous and no recognizer configured, leaving the stroke as drawn', {
73
+ candidates: candidates.map((candidate) => candidate.shape)
74
+ });
75
+ return null;
76
+ }
77
+ const strokeImage = renderStrokeSnapImage(points);
78
+ const answer = await context.recognize({
79
+ measurements,
80
+ candidates,
81
+ best: candidates[0] ?? null,
82
+ ...(strokeImage ? { image: strokeImage } : {})
148
83
  });
149
- });
84
+ const named = answer?.shape ?? null;
85
+ if (!named || !candidates.some((candidate) => candidate.shape === named)) {
86
+ context.log?.('Shape snap: the recognizer declined or named a shape the geometry ruled out', {
87
+ shape: named,
88
+ reason: answer?.reason ?? 'no_answer'
89
+ });
90
+ return null;
91
+ }
92
+ return { shape: named, source: 'recognizer', axisTilt: measurements.axisTilt };
93
+ }
94
+ catch (error) {
95
+ context.log?.('Shape snap: failed, leaving the stroke as drawn', error);
96
+ return null;
97
+ }
150
98
  }
151
99
  /**
152
100
  * The clean shape for a stroke: the same place and size (where the hand went, when it is known, else the box of the
@@ -154,67 +102,64 @@ export function decideStrokeSnap(stroke, context, handLine) {
154
102
  * The circle and the two library shapes stay square. Null when the shape cannot be built.
155
103
  */
156
104
  export function buildSnappedShape(stroke, shape, services, handLine) {
157
- var _a;
158
- var box = (_a = (handLine && handLine.length >= 2 ? outlineBounds(handLine) : null)) !== null && _a !== void 0 ? _a : shapeBounds(stroke);
159
- var placement = planSnapPlacement(shape, { left: box.x, top: box.y, width: box.width, height: box.height });
160
- var area = placement.bounds;
161
- var start = { x: area.left, y: area.top };
162
- var end = { x: area.left + area.width, y: area.top + area.height };
163
- var id = services.createId('obj');
164
- var replacement = null;
105
+ const box = (handLine && handLine.length >= 2 ? outlineBounds(handLine) : null) ?? shapeBounds(stroke);
106
+ const placement = planSnapPlacement(shape, { left: box.x, top: box.y, width: box.width, height: box.height });
107
+ const area = placement.bounds;
108
+ const start = { x: area.left, y: area.top };
109
+ const end = { x: area.left + area.width, y: area.top + area.height };
110
+ const id = services.createId('obj');
111
+ let replacement = null;
165
112
  if (shape === 'diamond' || shape === 'hexagon') {
166
- replacement = buildLibraryShape({ id: id, x: area.left, y: area.top, width: area.width, height: area.height, key: shape });
113
+ replacement = buildLibraryShape({ id, x: area.left, y: area.top, width: area.width, height: area.height, key: shape });
167
114
  }
168
115
  else {
169
- var kind = shape === 'oval' ? 'ellipse' : shape;
116
+ const kind = shape === 'oval' ? 'ellipse' : shape;
170
117
  replacement = buildDraggedShape({
171
- kind: kind,
172
- start: start,
173
- end: end,
118
+ kind,
119
+ start,
120
+ end,
174
121
  constrain: false,
175
122
  dragged: true,
176
123
  wireframe: true,
177
- createId: function () { return id; },
124
+ createId: () => id,
178
125
  random: services.random
179
126
  });
180
127
  }
181
128
  if (!replacement)
182
129
  return null;
183
- return __assign(__assign({}, replacement), { parentId: stroke.parentId, opacity: stroke.opacity, fill: 'transparent', fillOpacity: 0,
130
+ return {
131
+ ...replacement,
132
+ parentId: stroke.parentId,
133
+ opacity: stroke.opacity,
134
+ fill: 'transparent',
135
+ fillOpacity: 0,
184
136
  // A brush stroke keeps its colour in the fill of its outline: the clean shape draws it as a line.
185
- stroke: lineColorOf(stroke), strokeWidth: stroke.strokeWidth, strokeStyle: stroke.strokeStyle });
137
+ stroke: lineColorOf(stroke),
138
+ strokeWidth: stroke.strokeWidth,
139
+ strokeStyle: stroke.strokeStyle
140
+ };
186
141
  }
187
142
  /**
188
143
  * Decides and, when the answer is a shape, replaces the stroke on the board in one undo step. Resolves to the
189
144
  * decision that was applied, or null when the stroke stays.
190
145
  */
191
- export function snapDrawnStroke(engine, stroke, context, handLine) {
192
- return __awaiter(this, void 0, Promise, function () {
193
- var decision, replacement;
194
- var _a, _b;
195
- return __generator(this, function (_c) {
196
- switch (_c.label) {
197
- case 0: return [4 /*yield*/, decideStrokeSnap(stroke, context, handLine)];
198
- case 1:
199
- decision = _c.sent();
200
- if (!decision)
201
- return [2 /*return*/, null];
202
- // While the host was thinking the person may have moved, restyled or erased the stroke: then it is theirs.
203
- if (engine.getObject(stroke.id) !== stroke)
204
- return [2 /*return*/, null];
205
- replacement = buildSnappedShape(stroke, decision.shape, engine.services, handLine);
206
- if (!replacement)
207
- return [2 /*return*/, null];
208
- try {
209
- engine.applyScene(replaceObject(engine.state.scene, stroke.id, replacement), 'push');
210
- }
211
- catch (error) {
212
- (_a = context.log) === null || _a === void 0 ? void 0 : _a.call(context, 'Shape snap: could not put the clean shape on the board', error);
213
- return [2 /*return*/, null];
214
- }
215
- (_b = context.log) === null || _b === void 0 ? void 0 : _b.call(context, 'Shape snap: stroke replaced with the clean shape', __assign(__assign({}, decision), { id: replacement.id }));
216
- return [2 /*return*/, decision];
217
- }
218
- });
219
- });
146
+ export async function snapDrawnStroke(engine, stroke, context, handLine) {
147
+ const decision = await decideStrokeSnap(stroke, context, handLine);
148
+ if (!decision)
149
+ return null;
150
+ // While the host was thinking the person may have moved, restyled or erased the stroke: then it is theirs.
151
+ if (engine.getObject(stroke.id) !== stroke)
152
+ return null;
153
+ const replacement = buildSnappedShape(stroke, decision.shape, engine.services, handLine);
154
+ if (!replacement)
155
+ return null;
156
+ try {
157
+ engine.applyScene(replaceObject(engine.state.scene, stroke.id, replacement), 'push');
158
+ }
159
+ catch (error) {
160
+ context.log?.('Shape snap: could not put the clean shape on the board', error);
161
+ return null;
162
+ }
163
+ context.log?.('Shape snap: stroke replaced with the clean shape', { ...decision, id: replacement.id });
164
+ return decision;
220
165
  }
@@ -5,7 +5,7 @@
5
5
  * Nothing reads them but a person with the panel open, so a page that has no User Timing API (or a browser that refuses a
6
6
  * mark) loses nothing.
7
7
  */
8
- export var CANVAS_OPEN_MARKS = {
8
+ export const CANVAS_OPEN_MARKS = {
9
9
  /** The board is in place with the data the page gave it, a moment before `onReady` is called. */
10
10
  READY: 'canvas:ready',
11
11
  /** `loadCanvas` began reading a saved canvas. */
@@ -20,7 +20,7 @@ export function markCanvasOpenStep(stepName) {
20
20
  return;
21
21
  performance.mark(stepName);
22
22
  }
23
- catch (_a) {
23
+ catch {
24
24
  // Marks are diagnostics only.
25
25
  }
26
26
  }
@@ -1,25 +1,24 @@
1
1
  /**
2
2
  * Document-level outside press: deselect when the press is outside the canvas root and its portaled UI (item B18).
3
3
  */
4
- export var CANVAS_UI_ATTRIBUTE = 'data-canvas-ui';
4
+ export const CANVAS_UI_ATTRIBUTE = 'data-canvas-ui';
5
5
  export function isCanvasOwnedTarget(event, canvasRoot) {
6
6
  if (!canvasRoot)
7
7
  return false;
8
- var path = typeof event.composedPath === 'function' ? event.composedPath() : [];
8
+ const path = typeof event.composedPath === 'function' ? event.composedPath() : [];
9
9
  if (path.length > 0) {
10
- for (var _i = 0, path_1 = path; _i < path_1.length; _i++) {
11
- var node = path_1[_i];
10
+ for (const node of path) {
12
11
  if (!(node instanceof Node))
13
12
  continue;
14
13
  if (node === canvasRoot || canvasRoot.contains(node))
15
14
  return true;
16
- if (node instanceof Element && node.closest("[".concat(CANVAS_UI_ATTRIBUTE, "]")))
15
+ if (node instanceof Element && node.closest(`[${CANVAS_UI_ATTRIBUTE}]`))
17
16
  return true;
18
17
  }
19
18
  return false;
20
19
  }
21
- var target = event.target;
22
- if (target instanceof Node && (canvasRoot.contains(target) || (target instanceof Element && target.closest("[".concat(CANVAS_UI_ATTRIBUTE, "]"))))) {
20
+ const target = event.target;
21
+ if (target instanceof Node && (canvasRoot.contains(target) || (target instanceof Element && target.closest(`[${CANVAS_UI_ATTRIBUTE}]`)))) {
23
22
  return true;
24
23
  }
25
24
  return false;
@@ -8,8 +8,8 @@
8
8
  */
9
9
  import { CHROME_ANCHORS, normalizeAnchor } from './chromeLayout.js';
10
10
  /** The places the zoom control can stand: `TL TC TR` (top), `CL CR` (the sides) and `BL BC BR` (bottom). */
11
- export var PAN_ZOOM_POSITIONS = CHROME_ANCHORS;
12
- export var DEFAULT_PAN_ZOOM_POSITION = 'BL';
11
+ export const PAN_ZOOM_POSITIONS = CHROME_ANCHORS;
12
+ export const DEFAULT_PAN_ZOOM_POSITION = 'BL';
13
13
  /** The position to use for what a page passed: anything that is not one of the eight is the default. */
14
14
  export function normalizePanZoomPosition(value) {
15
15
  return normalizeAnchor(value, DEFAULT_PAN_ZOOM_POSITION);
@@ -2,18 +2,9 @@
2
2
  * The font family a person chose for a text, kept in the canvas's preference store (the browser's `localStorage`, or the
3
3
  * store a partner page installed with `setCanvasPreferenceStorage`), so that the next text they place starts with it.
4
4
  */
5
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
6
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
7
- if (ar || !(i in from)) {
8
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
9
- ar[i] = from[i];
10
- }
11
- }
12
- return to.concat(ar || Array.prototype.slice.call(from));
13
- };
14
5
  import { getCanvasPreferenceStorage } from '../components/preferenceStorage.js';
15
6
  /** The font families the selection panel offers for a text, in the order of its list. */
16
- export var TEXT_FONT_FAMILIES = [
7
+ export const TEXT_FONT_FAMILIES = [
17
8
  'Inter',
18
9
  'Comic Sans MS',
19
10
  'Arial',
@@ -25,50 +16,47 @@ export var TEXT_FONT_FAMILIES = [
25
16
  'Impact'
26
17
  ];
27
18
  /** Families added from the workspace brand kit (not persisted in this list; merged in the panel). */
28
- var workspaceFontFamilies = [];
19
+ let workspaceFontFamilies = [];
29
20
  export function setWorkspaceFontFamilies(families) {
30
- workspaceFontFamilies = __spreadArray([], new Set(families.filter(Boolean)), true);
21
+ workspaceFontFamilies = [...new Set(families.filter(Boolean))];
31
22
  }
32
23
  export function fontFamiliesForPicker() {
33
- var merged = __spreadArray([], TEXT_FONT_FAMILIES, true);
34
- for (var _i = 0, workspaceFontFamilies_1 = workspaceFontFamilies; _i < workspaceFontFamilies_1.length; _i++) {
35
- var family = workspaceFontFamilies_1[_i];
24
+ const merged = [...TEXT_FONT_FAMILIES];
25
+ for (const family of workspaceFontFamilies) {
36
26
  if (!merged.includes(family))
37
27
  merged.push(family);
38
28
  }
39
29
  return merged;
40
30
  }
41
31
  /** Inter + Comic Sans for the picker’s “Default fonts” group. */
42
- export var PRIMARY_CANVAS_FONT_FAMILIES = ['Inter', 'Comic Sans MS'];
32
+ export const PRIMARY_CANVAS_FONT_FAMILIES = ['Inter', 'Comic Sans MS'];
43
33
  export function builtInFontFamiliesForPicker() {
44
- return TEXT_FONT_FAMILIES.filter(function (family) { return !PRIMARY_CANVAS_FONT_FAMILIES.includes(family); });
34
+ return TEXT_FONT_FAMILIES.filter((family) => !PRIMARY_CANVAS_FONT_FAMILIES.includes(family));
45
35
  }
46
36
  export function workspaceFontFamiliesForPicker() {
47
- return __spreadArray([], workspaceFontFamilies, true);
37
+ return [...workspaceFontFamilies];
48
38
  }
49
39
  /** The key the choice is kept under. */
50
- export var PREFERRED_TEXT_FONT_STORAGE_KEY = 'widgetic-canvas:preferred-text-font';
40
+ export const PREFERRED_TEXT_FONT_STORAGE_KEY = 'widgetic-canvas:preferred-text-font';
51
41
  /** The font family kept from an earlier choice, or null when none was made (or the store refuses, or the name is no longer offered). */
52
42
  export function readPreferredTextFontFamily() {
53
- var _a;
54
43
  try {
55
- var stored = (_a = getCanvasPreferenceStorage()) === null || _a === void 0 ? void 0 : _a.getItem(PREFERRED_TEXT_FONT_STORAGE_KEY);
44
+ const stored = getCanvasPreferenceStorage()?.getItem(PREFERRED_TEXT_FONT_STORAGE_KEY);
56
45
  return stored && fontFamiliesForPicker().includes(stored) ? stored : null;
57
46
  }
58
- catch (_b) {
47
+ catch {
59
48
  // A store that blocks reading is the same as a store with nothing in it.
60
49
  return null;
61
50
  }
62
51
  }
63
52
  /** Keeps the choice for the next text. A store that refuses the write is not an error: the choice then lasts until the page closes. */
64
53
  export function writePreferredTextFontFamily(fontFamily) {
65
- var _a;
66
54
  if (!fontFamiliesForPicker().includes(fontFamily))
67
55
  return;
68
56
  try {
69
- (_a = getCanvasPreferenceStorage()) === null || _a === void 0 ? void 0 : _a.setItem(PREFERRED_TEXT_FONT_STORAGE_KEY, fontFamily);
57
+ getCanvasPreferenceStorage()?.setItem(PREFERRED_TEXT_FONT_STORAGE_KEY, fontFamily);
70
58
  }
71
- catch (_b) {
59
+ catch {
72
60
  // Nothing to do: the engine still holds the choice for this session.
73
61
  }
74
62
  }