@widgetic/canvas 0.5.41 → 0.5.43

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 (131) hide show
  1. package/README.md +1 -0
  2. package/dist/canvas/DomCanvas.svelte +49 -20
  3. package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
  4. package/dist/canvas/autosaveScheduler.js +37 -89
  5. package/dist/canvas/boardFrame.js +1 -1
  6. package/dist/canvas/boardSurface.js +1 -1
  7. package/dist/canvas/canvasLogger.js +7 -19
  8. package/dist/canvas/chromeLayout.d.ts +14 -3
  9. package/dist/canvas/chromeLayout.js +30 -20
  10. package/dist/canvas/eventScope.js +10 -14
  11. package/dist/canvas/host/CanvasHost.d.ts +47 -5
  12. package/dist/canvas/host/CanvasHost.js +1481 -1566
  13. package/dist/canvas/host/CanvasHostApi.js +4 -19
  14. package/dist/canvas/host/browserServices.js +65 -116
  15. package/dist/canvas/host/conversion.js +82 -170
  16. package/dist/canvas/host/hostObject.js +7 -7
  17. package/dist/canvas/host/nearCards.d.ts +33 -0
  18. package/dist/canvas/host/nearCards.js +78 -0
  19. package/dist/canvas/host/pageEvents.d.ts +16 -4
  20. package/dist/canvas/host/pageEvents.js +17 -8
  21. package/dist/canvas/host/pictureSaves.d.ts +33 -0
  22. package/dist/canvas/host/pictureSaves.js +42 -0
  23. package/dist/canvas/host/screenRects.d.ts +5 -0
  24. package/dist/canvas/host/screenRects.js +28 -31
  25. package/dist/canvas/host/storedPictures.d.ts +88 -0
  26. package/dist/canvas/host/storedPictures.js +288 -0
  27. package/dist/canvas/host/strokeSnap.js +88 -143
  28. package/dist/canvas/index.d.ts +1 -1
  29. package/dist/canvas/index.js +1 -1
  30. package/dist/canvas/openMarks.js +2 -2
  31. package/dist/canvas/outsideClickDeselect.js +6 -7
  32. package/dist/canvas/panZoomPosition.js +2 -2
  33. package/dist/canvas/preferredTextFont.js +13 -25
  34. package/dist/canvas/shapes/ShapeLibrary.js +138 -148
  35. package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
  36. package/dist/canvas/shapes/shapeImageStore.js +10 -3
  37. package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
  38. package/dist/canvas/toolbarPosition.d.ts +2 -2
  39. package/dist/canvas/toolbarPosition.js +4 -4
  40. package/dist/canvas/ui/SelectionPanel.svelte +28 -5
  41. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
  42. package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
  43. package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
  44. package/dist/canvas/ui/ToolButton.svelte +8 -2
  45. package/dist/canvas/ui/colorModel.js +21 -32
  46. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  47. package/dist/canvas/ui/floatingCard.d.ts +2 -2
  48. package/dist/canvas/ui/floatingCard.js +20 -20
  49. package/dist/canvas/ui/floatingLayer.d.ts +13 -2
  50. package/dist/canvas/ui/floatingLayer.js +18 -7
  51. package/dist/canvas/ui/keyHints.js +9 -11
  52. package/dist/canvas/ui/popoverSide.js +2 -3
  53. package/dist/canvas/ui/selectionTraits.js +34 -40
  54. package/dist/canvas/view/CanvasStage.svelte +101 -67
  55. package/dist/canvas/view/LeftToRightScope.svelte +21 -0
  56. package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
  57. package/dist/canvas/view/ObjectView.svelte +15 -10
  58. package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
  59. package/dist/canvas/view/erasingMarks.js +7 -7
  60. package/dist/canvas/view/geometry.js +71 -90
  61. package/dist/canvas/view/liftedScene.d.ts +31 -0
  62. package/dist/canvas/view/liftedScene.js +146 -0
  63. package/dist/canvas/view/mountedScene.d.ts +71 -0
  64. package/dist/canvas/view/mountedScene.js +363 -0
  65. package/dist/components/preferenceStorage.js +2 -2
  66. package/dist/engine/EngineCore.d.ts +15 -1
  67. package/dist/engine/EngineCore.js +724 -797
  68. package/dist/engine/codec/canvasDocument.d.ts +5 -0
  69. package/dist/engine/codec/canvasDocument.js +212 -149
  70. package/dist/engine/codec/hostReadableKeys.js +1 -1
  71. package/dist/engine/core/arrange.js +26 -38
  72. package/dist/engine/core/arrow.js +76 -89
  73. package/dist/engine/core/arrowBinding.js +98 -116
  74. package/dist/engine/core/arrowBindingRecords.js +23 -38
  75. package/dist/engine/core/brush.js +67 -94
  76. package/dist/engine/core/defaults.js +90 -51
  77. package/dist/engine/core/freehand.js +147 -163
  78. package/dist/engine/core/geometry.js +47 -54
  79. package/dist/engine/core/history.d.ts +6 -0
  80. package/dist/engine/core/history.js +49 -50
  81. package/dist/engine/core/hitTest.js +68 -81
  82. package/dist/engine/core/matrix.js +18 -20
  83. package/dist/engine/core/pathData.js +183 -210
  84. package/dist/engine/core/rotationReset.js +5 -6
  85. package/dist/engine/core/scene.d.ts +10 -0
  86. package/dist/engine/core/scene.js +153 -187
  87. package/dist/engine/core/scribble.js +59 -84
  88. package/dist/engine/core/shapePaths.js +12 -15
  89. package/dist/engine/core/shapeTools.js +72 -63
  90. package/dist/engine/core/snapping.js +65 -117
  91. package/dist/engine/core/style.js +31 -64
  92. package/dist/engine/core/transform.js +83 -94
  93. package/dist/engine/core/types.d.ts +8 -2
  94. package/dist/engine/core/types.js +4 -4
  95. package/dist/engine/core/viewport.js +23 -36
  96. package/dist/engine/input/PointerController.js +467 -474
  97. package/dist/engine/input/arrowHandles.js +12 -12
  98. package/dist/engine/input/gestures.js +265 -309
  99. package/dist/engine/input/hoverCursor.js +1 -1
  100. package/dist/engine/input/keyboard.js +27 -30
  101. package/dist/engine/input/selectionHandles.js +45 -50
  102. package/dist/engine/input/types.js +1 -1
  103. package/dist/engine/render/rasterize.js +86 -158
  104. package/dist/engine/state.d.ts +20 -0
  105. package/dist/engine/state.js +5 -15
  106. package/dist/engine/text/layoutText.js +43 -62
  107. package/dist/engine/text/textMetrics.js +8 -10
  108. package/dist/engine/widgets.js +101 -137
  109. package/dist/fonts/interFont.d.ts +10 -1
  110. package/dist/fonts/interFont.js +47 -75
  111. package/dist/fonts/workspaceFonts.js +32 -90
  112. package/dist/i18n/labels.js +4 -4
  113. package/dist/i18n/language.d.ts +3 -3
  114. package/dist/i18n/language.js +4 -4
  115. package/dist/i18n/messages.d.ts +8 -1
  116. package/dist/i18n/messages.js +10 -2
  117. package/dist/i18n/shortcuts.js +14 -14
  118. package/dist/index.d.ts +1 -1
  119. package/dist/index.js +1 -1
  120. package/dist/settings/workspaceToolSettings.js +32 -48
  121. package/dist/tools/components/catalog.js +64 -74
  122. package/dist/tools/components/grouping.js +6 -28
  123. package/dist/tools/definitions.js +5 -5
  124. package/dist/tools/executors.js +105 -141
  125. package/dist/tools/hostAdapter.js +11 -13
  126. package/dist/tools/sceneSummary.js +43 -57
  127. package/dist/tools/sketchIntent.js +13 -21
  128. package/dist/tools/types.js +1 -1
  129. package/package.json +1 -1
  130. package/dist/canvas/ui/SelectorBar.svelte +0 -84
  131. package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
@@ -9,93 +9,45 @@
9
9
  * Nothing here touches `document` or `window`: the browser's canvas, image loader and `Path2D` are handed in as
10
10
  * services, so the drawing order and the geometry are tested in Node with a recording context.
11
11
  */
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- 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);
23
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
- 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;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
49
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
50
- if (ar || !(i in from)) {
51
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
52
- ar[i] = from[i];
53
- }
54
- }
55
- return to.concat(ar || Array.prototype.slice.call(from));
56
- };
57
12
  import { boxToWorldMatrix } from '../core/matrix.js';
58
13
  import { strokeDashArray } from '../core/defaults.js';
59
14
  import { shapeVisualBounds } from '../core/scene.js';
60
15
  import { unionRects } from '../core/geometry.js';
61
16
  import { fontShorthand, layoutTextObject, lineBoxHeight, textDirection } from '../text/layoutText.js';
62
- export var DEFAULT_RASTER_SCALE = 2;
63
- export var DEFAULT_RASTER_MAX_PIXELS = 16000000;
64
- export var DEFAULT_RASTER_MAX_SIDE = 8192;
17
+ export const DEFAULT_RASTER_SCALE = 2;
18
+ export const DEFAULT_RASTER_MAX_PIXELS = 16_000_000;
19
+ export const DEFAULT_RASTER_MAX_SIDE = 8192;
65
20
  /** The addresses of the pictures a set of shapes needs: images and the screenshots of widget cards. */
66
21
  export function imageSourcesOf(shapes) {
67
- var sources = new Set();
68
- for (var _i = 0, shapes_1 = shapes; _i < shapes_1.length; _i++) {
69
- var shape = shapes_1[_i];
22
+ const sources = new Set();
23
+ for (const shape of shapes) {
70
24
  if (shape.type === 'image' && shape.src)
71
25
  sources.add(shape.src);
72
26
  if (shape.type === 'widget' && shape.thumbnail)
73
27
  sources.add(shape.thumbnail);
74
28
  }
75
- return __spreadArray([], sources, true);
29
+ return [...sources];
76
30
  }
77
31
  /** The rectangle every shape fits in, strokes included. Null when there is no shape or it has no size. */
78
32
  export function rasterBoundsOf(shapes) {
79
- var bounds = unionRects(shapes.map(shapeVisualBounds));
33
+ const bounds = unionRects(shapes.map(shapeVisualBounds));
80
34
  if (!bounds || !(bounds.width > 0) || !(bounds.height > 0))
81
35
  return null;
82
36
  return bounds;
83
37
  }
84
38
  /** The scale a picture of these bounds is drawn at: the one asked for, lowered until it fits the limits. */
85
- export function rasterScaleFor(bounds, options) {
86
- var _a, _b, _c;
87
- if (options === void 0) { options = {}; }
88
- var requested = (_a = options.scale) !== null && _a !== void 0 ? _a : DEFAULT_RASTER_SCALE;
89
- var maxPixels = (_b = options.maxPixels) !== null && _b !== void 0 ? _b : DEFAULT_RASTER_MAX_PIXELS;
90
- var maxSide = (_c = options.maxSide) !== null && _c !== void 0 ? _c : DEFAULT_RASTER_MAX_SIDE;
91
- var area = Math.max(1, bounds.width * bounds.height);
92
- var longest = Math.max(1, bounds.width, bounds.height);
93
- var byArea = Math.sqrt(maxPixels / area);
94
- var bySide = maxSide / longest;
39
+ export function rasterScaleFor(bounds, options = {}) {
40
+ const requested = options.scale ?? DEFAULT_RASTER_SCALE;
41
+ const maxPixels = options.maxPixels ?? DEFAULT_RASTER_MAX_PIXELS;
42
+ const maxSide = options.maxSide ?? DEFAULT_RASTER_MAX_SIDE;
43
+ const area = Math.max(1, bounds.width * bounds.height);
44
+ const longest = Math.max(1, bounds.width, bounds.height);
45
+ const byArea = Math.sqrt(maxPixels / area);
46
+ const bySide = maxSide / longest;
95
47
  return Math.max(0.01, Math.min(requested, byArea, bySide));
96
48
  }
97
49
  function roundedRectPath(context, width, height, radius) {
98
- var clamped = Math.max(0, Math.min(radius, width / 2, height / 2));
50
+ const clamped = Math.max(0, Math.min(radius, width / 2, height / 2));
99
51
  context.beginPath();
100
52
  if (clamped <= 0) {
101
53
  context.rect(0, 0, width, height);
@@ -121,8 +73,8 @@ function applyStroke(context, shape) {
121
73
  return false;
122
74
  context.strokeStyle = shape.stroke;
123
75
  context.lineWidth = shape.strokeWidth;
124
- var dash = strokeDashArray(shape.strokeStyle, shape.strokeWidth);
125
- context.setLineDash(dash !== null && dash !== void 0 ? dash : []);
76
+ const dash = strokeDashArray(shape.strokeStyle, shape.strokeWidth);
77
+ context.setLineDash(dash ?? []);
126
78
  // A dotted line is zero-length dashes; only a round cap turns each into a dot.
127
79
  if (shape.strokeStyle === 'dotted')
128
80
  context.lineCap = 'round';
@@ -130,7 +82,7 @@ function applyStroke(context, shape) {
130
82
  }
131
83
  function paintFillAndStroke(context, shape, fillPath) {
132
84
  if (shape.fill !== 'transparent' && shape.fillOpacity > 0) {
133
- var before = context.globalAlpha;
85
+ const before = context.globalAlpha;
134
86
  context.globalAlpha = before * Math.max(0, Math.min(1, shape.fillOpacity));
135
87
  context.fillStyle = shape.fill;
136
88
  if (fillPath)
@@ -147,11 +99,11 @@ function paintFillAndStroke(context, shape, fillPath) {
147
99
  }
148
100
  }
149
101
  function fontMetricsOf(context, size) {
150
- var measured = context.measureText('Hg');
151
- var ascent = measured.fontBoundingBoxAscent;
152
- var descent = measured.fontBoundingBoxDescent;
102
+ const measured = context.measureText('Hg');
103
+ const ascent = measured.fontBoundingBoxAscent;
104
+ const descent = measured.fontBoundingBoxDescent;
153
105
  if (typeof ascent === 'number' && typeof descent === 'number' && ascent + descent > 0)
154
- return { ascent: ascent, descent: descent };
106
+ return { ascent, descent };
155
107
  return { ascent: size * 0.8, descent: size * 0.2 };
156
108
  }
157
109
  function paintText(context, shape, deps) {
@@ -161,27 +113,27 @@ function paintText(context, shape, deps) {
161
113
  }
162
114
  if (shape.text === '')
163
115
  return;
164
- var layout = layoutTextObject(shape, deps.measureText);
116
+ const layout = layoutTextObject(shape, deps.measureText);
165
117
  context.font = fontShorthand(shape);
166
118
  context.fillStyle = shape.color;
167
119
  context.textBaseline = 'alphabetic';
168
120
  context.textAlign = 'left';
169
121
  // The text reads the way the board shows it: by the direction of its first letter (a line is placed from its left edge).
170
122
  context.direction = textDirection(shape.text);
171
- var lineHeight = lineBoxHeight(shape);
172
- var _a = fontMetricsOf(context, shape.fontSize), ascent = _a.ascent, descent = _a.descent;
173
- var innerWidth = shape.width - shape.padding * 2;
174
- for (var index = 0; index < layout.lines.length; index += 1) {
175
- var line = layout.lines[index];
123
+ const lineHeight = lineBoxHeight(shape);
124
+ const { ascent, descent } = fontMetricsOf(context, shape.fontSize);
125
+ const innerWidth = shape.width - shape.padding * 2;
126
+ for (let index = 0; index < layout.lines.length; index += 1) {
127
+ const line = layout.lines[index];
176
128
  if (line === '')
177
129
  continue;
178
- var lineWidth = deps.measureText(line, fontShorthand(shape));
179
- var left = shape.padding;
130
+ const lineWidth = deps.measureText(line, fontShorthand(shape));
131
+ let left = shape.padding;
180
132
  if (shape.align === 'center')
181
133
  left = shape.padding + (innerWidth - lineWidth) / 2;
182
134
  else if (shape.align === 'right')
183
135
  left = shape.padding + (innerWidth - lineWidth);
184
- var baseline = shape.padding + index * lineHeight + (lineHeight - (ascent + descent)) / 2 + ascent;
136
+ const baseline = shape.padding + index * lineHeight + (lineHeight - (ascent + descent)) / 2 + ascent;
185
137
  context.fillText(line, left, baseline);
186
138
  if (shape.underline) {
187
139
  context.fillRect(left, baseline + Math.max(1, shape.fontSize * 0.08), lineWidth, Math.max(1, shape.fontSize / 15));
@@ -189,7 +141,7 @@ function paintText(context, shape, deps) {
189
141
  }
190
142
  }
191
143
  function paintWidget(context, shape, deps) {
192
- var picture = deps.imageFor(shape.thumbnail);
144
+ const picture = deps.imageFor(shape.thumbnail);
193
145
  roundedRectPath(context, shape.width, shape.height, 8);
194
146
  context.fillStyle = '#ffffff';
195
147
  context.fill();
@@ -205,14 +157,14 @@ function paintWidget(context, shape, deps) {
205
157
  context.setLineDash([6, 4]);
206
158
  context.stroke();
207
159
  context.setLineDash([]);
208
- var name = shape.widgetName || deps.widgetFallbackName || '';
160
+ const name = shape.widgetName || deps.widgetFallbackName || '';
209
161
  if (name === '')
210
162
  return;
211
163
  context.fillStyle = '#1e293b';
212
164
  context.textAlign = 'center';
213
165
  context.textBaseline = 'middle';
214
- var size = Math.max(11, Math.min(18, shape.width * 0.06));
215
- context.font = "600 ".concat(size, "px system-ui, sans-serif");
166
+ const size = Math.max(11, Math.min(18, shape.width * 0.06));
167
+ context.font = `600 ${size}px system-ui, sans-serif`;
216
168
  context.fillText(name, shape.width / 2, shape.height / 2);
217
169
  }
218
170
  /**
@@ -224,7 +176,7 @@ export function paintShape(context, shape, deps) {
224
176
  return;
225
177
  context.save();
226
178
  try {
227
- var matrix = boxToWorldMatrix(shape);
179
+ const matrix = boxToWorldMatrix(shape);
228
180
  context.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
229
181
  context.globalAlpha = context.globalAlpha * Math.max(0, Math.min(1, shape.opacity));
230
182
  switch (shape.type) {
@@ -250,7 +202,7 @@ export function paintShape(context, shape, deps) {
250
202
  break;
251
203
  }
252
204
  case 'image': {
253
- var picture = deps.imageFor(shape.src);
205
+ const picture = deps.imageFor(shape.src);
254
206
  if (picture)
255
207
  context.drawImage(picture, 0, 0, shape.width, shape.height);
256
208
  break;
@@ -270,76 +222,52 @@ export function paintShape(context, shape, deps) {
270
222
  * string when there is nothing to draw, the canvas cannot be made, or the browser refuses to export it (a picture
271
223
  * from another site without CORS taints the canvas).
272
224
  */
273
- export function renderShapesToDataUrl(shapes_2, services_1) {
274
- return __awaiter(this, arguments, Promise, function (shapes, services, options) {
275
- var bounds, padding, left, top, right, bottom, area, scale, pixelWidth, pixelHeight, canvas, context, loaded, deps, _i, shapes_3, shape;
276
- var _this = this;
277
- var _a, _b;
278
- if (options === void 0) { options = {}; }
279
- return __generator(this, function (_c) {
280
- switch (_c.label) {
281
- case 0:
282
- bounds = rasterBoundsOf(shapes);
283
- if (!bounds)
284
- return [2 /*return*/, ''];
285
- padding = Math.max(0, (_a = options.padding) !== null && _a !== void 0 ? _a : 0);
286
- left = Math.floor(bounds.x - padding);
287
- top = Math.floor(bounds.y - padding);
288
- right = Math.ceil(bounds.x + bounds.width + padding);
289
- bottom = Math.ceil(bounds.y + bounds.height + padding);
290
- area = { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
291
- scale = rasterScaleFor(area, options);
292
- pixelWidth = Math.max(1, Math.ceil(area.width * scale));
293
- pixelHeight = Math.max(1, Math.ceil(area.height * scale));
294
- canvas = services.createCanvas(pixelWidth, pixelHeight);
295
- context = (_b = canvas === null || canvas === void 0 ? void 0 : canvas.getContext('2d')) !== null && _b !== void 0 ? _b : null;
296
- if (!canvas || !context)
297
- return [2 /*return*/, ''];
298
- loaded = new Map();
299
- return [4 /*yield*/, Promise.all(imageSourcesOf(shapes).map(function (source) { return __awaiter(_this, void 0, void 0, function () {
300
- var picture, _a;
301
- return __generator(this, function (_b) {
302
- switch (_b.label) {
303
- case 0:
304
- _b.trys.push([0, 2, , 3]);
305
- return [4 /*yield*/, services.loadImage(source)];
306
- case 1:
307
- picture = _b.sent();
308
- if (picture)
309
- loaded.set(source, picture);
310
- return [3 /*break*/, 3];
311
- case 2:
312
- _a = _b.sent();
313
- return [3 /*break*/, 3];
314
- case 3: return [2 /*return*/];
315
- }
316
- });
317
- }); }))];
318
- case 1:
319
- _c.sent();
320
- try {
321
- if (options.background) {
322
- context.fillStyle = options.background;
323
- context.fillRect(0, 0, pixelWidth, pixelHeight);
324
- }
325
- context.setTransform(scale, 0, 0, scale, -area.x * scale, -area.y * scale);
326
- deps = {
327
- createPath: services.createPath,
328
- imageFor: function (source) { var _a; return (source ? ((_a = loaded.get(source)) !== null && _a !== void 0 ? _a : null) : null); },
329
- measureText: services.measureText,
330
- widgetFallbackName: options.widgetFallbackName
331
- };
332
- for (_i = 0, shapes_3 = shapes; _i < shapes_3.length; _i++) {
333
- shape = shapes_3[_i];
334
- paintShape(context, shape, deps);
335
- }
336
- return [2 /*return*/, canvas.toDataURL('image/png')];
337
- }
338
- catch (_d) {
339
- return [2 /*return*/, ''];
340
- }
341
- return [2 /*return*/];
342
- }
343
- });
344
- });
225
+ export async function renderShapesToDataUrl(shapes, services, options = {}) {
226
+ const bounds = rasterBoundsOf(shapes);
227
+ if (!bounds)
228
+ return '';
229
+ const padding = Math.max(0, options.padding ?? 0);
230
+ // Whole world units on every side, so the crop never cuts the last pixel of a stroke.
231
+ const left = Math.floor(bounds.x - padding);
232
+ const top = Math.floor(bounds.y - padding);
233
+ const right = Math.ceil(bounds.x + bounds.width + padding);
234
+ const bottom = Math.ceil(bounds.y + bounds.height + padding);
235
+ const area = { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
236
+ const scale = rasterScaleFor(area, options);
237
+ const pixelWidth = Math.max(1, Math.ceil(area.width * scale));
238
+ const pixelHeight = Math.max(1, Math.ceil(area.height * scale));
239
+ const canvas = services.createCanvas(pixelWidth, pixelHeight);
240
+ const context = canvas?.getContext('2d') ?? null;
241
+ if (!canvas || !context)
242
+ return '';
243
+ const loaded = new Map();
244
+ await Promise.all(imageSourcesOf(shapes).map(async (source) => {
245
+ try {
246
+ const picture = await services.loadImage(source);
247
+ if (picture)
248
+ loaded.set(source, picture);
249
+ }
250
+ catch {
251
+ // A picture that cannot be loaded is left out of the sketch instead of failing the whole capture.
252
+ }
253
+ }));
254
+ try {
255
+ if (options.background) {
256
+ context.fillStyle = options.background;
257
+ context.fillRect(0, 0, pixelWidth, pixelHeight);
258
+ }
259
+ context.setTransform(scale, 0, 0, scale, -area.x * scale, -area.y * scale);
260
+ const deps = {
261
+ createPath: services.createPath,
262
+ imageFor: (source) => (source ? (loaded.get(source) ?? null) : null),
263
+ measureText: services.measureText,
264
+ widgetFallbackName: options.widgetFallbackName
265
+ };
266
+ for (const shape of shapes)
267
+ paintShape(context, shape, deps);
268
+ return canvas.toDataURL('image/png');
269
+ }
270
+ catch {
271
+ return '';
272
+ }
345
273
  }
@@ -48,6 +48,24 @@ export interface VirtualCursor {
48
48
  y: number;
49
49
  angle: number;
50
50
  }
51
+ /**
52
+ * The shapes a drag carries, and how far: what the board needs to draw them as one layer while the pointer is down, with one
53
+ * move of the layer in place of one new record for every shape at every step (see `canvas/view/liftedScene.ts`).
54
+ *
55
+ * The engine does not depend on it: it writes the carried shapes' new records at every step all the same (the frame, the
56
+ * guides, the ties of the arrows, the host's own layers and the undo step all read them), so what this says is only the part
57
+ * of the truth that a view can use to draw the same board with less work. It is a new object at every step of the drag, with
58
+ * the same `startRecords` and `shapeIds` (the same objects, which is what tells a view that the drag is the same one).
59
+ */
60
+ export interface CarriedShapes {
61
+ /** The board as the drag began, by id: the records the carried shapes are drawn from for as long as the drag lasts. */
62
+ startRecords: ReadonlyMap<ObjectId, SceneObject>;
63
+ /** The shapes that are carried (shapes, never groups: a group is carried by its members). */
64
+ shapeIds: ReadonlySet<ObjectId>;
65
+ /** How far they have been carried since the drag began, in world units. */
66
+ deltaX: number;
67
+ deltaY: number;
68
+ }
51
69
  /** What the board draws on top of the objects while something is being done. */
52
70
  export interface EngineOverlay {
53
71
  /** The rectangle being dragged out to select (world units). */
@@ -67,6 +85,8 @@ export interface EngineOverlay {
67
85
  textDraft: Rect | null;
68
86
  /** What the selection is being put through. The frame follows it: the handles go while it is carried, the outline dashes while it is stretched. */
69
87
  dragging: SelectionDrag | null;
88
+ /** While the selection is carried: which shapes, from which records, and how far (a view may draw them as one layer). */
89
+ carry: CarriedShapes | null;
70
90
  /** The size or the angle shown in the middle of the selection while it is stretched or turned. */
71
91
  readout: Readout | null;
72
92
  /** The pointer drawn on a handle while the real one is hidden. */
@@ -3,27 +3,17 @@
3
3
  * is, what is half-drawn. One immutable snapshot, replaced on every change, so a view can hold on to a slice of
4
4
  * it (the scene, the selection, the viewport) and redraw only when that slice is a different object.
5
5
  */
6
- var __assign = (this && this.__assign) || function () {
7
- __assign = Object.assign || function(t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
- t[p] = s[p];
12
- }
13
- return t;
14
- };
15
- return __assign.apply(this, arguments);
16
- };
17
6
  import { INITIAL_VIEWPORT } from './core/viewport.js';
18
- export var SHAPE_TOOLS = ['rect', 'rounded-rect', 'ellipse', 'circle', 'triangle', 'arrow', 'library-shape'];
19
- export var DEFAULT_WIDGET_RUNTIME = Object.freeze({ hiddenImage: false, orphaned: false, bodyDragAllowed: false });
20
- export var EMPTY_OVERLAY = Object.freeze({
7
+ export const SHAPE_TOOLS = ['rect', 'rounded-rect', 'ellipse', 'circle', 'triangle', 'arrow', 'library-shape'];
8
+ export const DEFAULT_WIDGET_RUNTIME = Object.freeze({ hiddenImage: false, orphaned: false, bodyDragAllowed: false });
9
+ export const EMPTY_OVERLAY = Object.freeze({
21
10
  marquee: null,
22
11
  marqueeHover: Object.freeze([]),
23
12
  guides: Object.freeze([]),
24
13
  preview: null,
25
14
  textDraft: null,
26
15
  dragging: null,
16
+ carry: null,
27
17
  readout: null,
28
18
  virtualCursor: null,
29
19
  erasing: Object.freeze([]),
@@ -37,7 +27,7 @@ export function createInitialState() {
37
27
  hoverId: null,
38
28
  rotationDotHovered: false,
39
29
  arrowHandleHovered: null,
40
- viewport: __assign({}, INITIAL_VIEWPORT),
30
+ viewport: { ...INITIAL_VIEWPORT },
41
31
  viewSize: { width: 0, height: 0 },
42
32
  tool: 'select',
43
33
  libraryShapeKey: null,
@@ -6,35 +6,24 @@
6
6
  * same measuring function. The board draws the lines this returns (each on a line of its own, never wrapped by the
7
7
  * browser), and the sketch the AI is shown is drawn from the same lines, so what is on the screen is what is sent.
8
8
  */
9
- var __assign = (this && this.__assign) || function () {
10
- __assign = Object.assign || function(t) {
11
- for (var s, i = 1, n = arguments.length; i < n; i++) {
12
- s = arguments[i];
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
14
- t[p] = s[p];
15
- }
16
- return t;
17
- };
18
- return __assign.apply(this, arguments);
19
- };
20
9
  import { TEXT_DEFAULTS } from '../core/defaults.js';
21
10
  import { textLineExtent } from './textMetrics.js';
22
11
  /** Room so the last letter and the caret stay on the line; more than this reads as padding. */
23
- export var TEXT_FIT_SLACK = 4;
12
+ export const TEXT_FIT_SLACK = 4;
24
13
  /** Extra width for italic slant and serif overhang beyond what `measureText` returns. */
25
14
  export function textFitHorizontalSlack(style) {
26
- var overhang = Math.ceil(style.fontSize * 0.08);
27
- var italic = style.fontStyle === 'italic' ? Math.ceil(style.fontSize * 0.2) : 0;
15
+ const overhang = Math.ceil(style.fontSize * 0.08);
16
+ const italic = style.fontStyle === 'italic' ? Math.ceil(style.fontSize * 0.2) : 0;
28
17
  return TEXT_FIT_SLACK + overhang + italic;
29
18
  }
30
19
  /** The CSS `font` shorthand of a text object. */
31
20
  export function fontShorthand(style) {
32
- var parts = [];
21
+ const parts = [];
33
22
  if (style.fontStyle === 'italic')
34
23
  parts.push('italic');
35
24
  if (style.fontWeight === 'bold')
36
25
  parts.push('bold');
37
- parts.push("".concat(style.fontSize, "px"));
26
+ parts.push(`${style.fontSize}px`);
38
27
  parts.push(style.fontFamily);
39
28
  return parts.join(' ');
40
29
  }
@@ -44,17 +33,16 @@ export function lineBoxHeight(style) {
44
33
  }
45
34
  export { textLineExtent } from './textMetrics.js';
46
35
  /** A letter of a script that is written from right to left. Digits, signs and marks have no direction of their own. */
47
- var RIGHT_TO_LEFT_LETTER = /(?=\p{L})[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}]/u;
36
+ const RIGHT_TO_LEFT_LETTER = /(?=\p{L})[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}]/u;
48
37
  /** A letter of any script. */
49
- var ANY_LETTER = /\p{L}/u;
38
+ const ANY_LETTER = /\p{L}/u;
50
39
  /**
51
40
  * The direction a text is written in: the one of its first letter. This is what the browser's `dir="auto"` does, so the
52
41
  * board (a drawing that is `ltr` on every screen), the field a text is typed in and the sketch that is drawn for the AI
53
42
  * all place the same text the same way. A text with no letter (digits, signs, nothing) reads left to right.
54
43
  */
55
44
  export function textDirection(text) {
56
- for (var _i = 0, text_1 = text; _i < text_1.length; _i++) {
57
- var character = text_1[_i];
45
+ for (const character of text) {
58
46
  if (RIGHT_TO_LEFT_LETTER.test(character))
59
47
  return 'rtl';
60
48
  if (ANY_LETTER.test(character))
@@ -62,24 +50,23 @@ export function textDirection(text) {
62
50
  }
63
51
  return 'ltr';
64
52
  }
65
- var segmenter;
53
+ let segmenter;
66
54
  /** The letters of a word as a person counts them (an emoji with a modifier is one), when the platform can tell. */
67
55
  function graphemes(word) {
68
56
  if (segmenter === undefined) {
69
- var Segmenter = Intl.Segmenter;
57
+ const Segmenter = Intl.Segmenter;
70
58
  segmenter = Segmenter ? new Segmenter(undefined, { granularity: 'grapheme' }) : null;
71
59
  }
72
60
  if (!segmenter)
73
61
  return Array.from(word);
74
- return Array.from(segmenter.segment(word), function (part) { return part.segment; });
62
+ return Array.from(segmenter.segment(word), (part) => part.segment);
75
63
  }
76
64
  /** Cuts a word that is wider than a line into pieces that each fit (at least one letter each). */
77
65
  function breakLongWord(word, maxWidth, font, measure) {
78
- var pieces = [];
79
- var current = '';
80
- for (var _i = 0, _a = graphemes(word); _i < _a.length; _i++) {
81
- var letter = _a[_i];
82
- var candidate = current + letter;
66
+ const pieces = [];
67
+ let current = '';
68
+ for (const letter of graphemes(word)) {
69
+ const candidate = current + letter;
83
70
  if (current !== '' && measure(candidate, font) > maxWidth) {
84
71
  pieces.push(current);
85
72
  current = letter;
@@ -94,18 +81,16 @@ function breakLongWord(word, maxWidth, font, measure) {
94
81
  }
95
82
  /** Wraps one paragraph (no line breaks in it) into lines no wider than `maxWidth`, breaking at spaces. */
96
83
  function wrapParagraph(paragraph, maxWidth, font, measure) {
97
- var _a, _b;
98
84
  if (paragraph === '')
99
85
  return [''];
100
86
  if (measure(paragraph, font) <= maxWidth)
101
87
  return [paragraph];
102
- var lines = [];
103
- var line = '';
88
+ const lines = [];
89
+ let line = '';
104
90
  // Words keep the spaces that follow them, so a line that ends in a space still measures like the browser's.
105
- var words = (_a = paragraph.match(/\S+\s*|\s+/g)) !== null && _a !== void 0 ? _a : [paragraph];
106
- for (var _i = 0, words_1 = words; _i < words_1.length; _i++) {
107
- var word = words_1[_i];
108
- var candidate = line + word;
91
+ const words = paragraph.match(/\S+\s*|\s+/g) ?? [paragraph];
92
+ for (const word of words) {
93
+ const candidate = line + word;
109
94
  if (measure(candidate.trimEnd(), font) <= maxWidth) {
110
95
  line = candidate;
111
96
  continue;
@@ -118,11 +103,11 @@ function wrapParagraph(paragraph, maxWidth, font, measure) {
118
103
  line = word;
119
104
  }
120
105
  else {
121
- var bareWord = word.trimEnd();
122
- var pieces = breakLongWord(bareWord, maxWidth, font, measure);
123
- lines.push.apply(lines, pieces.slice(0, -1));
106
+ const bareWord = word.trimEnd();
107
+ const pieces = breakLongWord(bareWord, maxWidth, font, measure);
108
+ lines.push(...pieces.slice(0, -1));
124
109
  // The last piece keeps the spaces that followed the word, so the next word is not glued to it
125
- line = ((_b = pieces[pieces.length - 1]) !== null && _b !== void 0 ? _b : '') + word.slice(bareWord.length);
110
+ line = (pieces[pieces.length - 1] ?? '') + word.slice(bareWord.length);
126
111
  }
127
112
  }
128
113
  if (line !== '')
@@ -134,32 +119,30 @@ function wrapParagraph(paragraph, maxWidth, font, measure) {
134
119
  * `fixedWidth` is the width of the box when the text wraps; null lets the box be as wide as the longest line.
135
120
  */
136
121
  export function layoutText(text, style, fixedWidth, measure) {
137
- var font = fontShorthand(style);
138
- var padding = style.padding;
139
- var paragraphs = text.split('\n');
140
- var lines;
122
+ const font = fontShorthand(style);
123
+ const padding = style.padding;
124
+ const paragraphs = text.split('\n');
125
+ let lines;
141
126
  if (fixedWidth === null) {
142
127
  lines = paragraphs;
143
128
  }
144
129
  else {
145
- var maxWidth_1 = Math.max(1, fixedWidth - padding * 2);
146
- lines = paragraphs.flatMap(function (paragraph) { return wrapParagraph(paragraph, maxWidth_1, font, measure); });
130
+ const maxWidth = Math.max(1, fixedWidth - padding * 2);
131
+ lines = paragraphs.flatMap((paragraph) => wrapParagraph(paragraph, maxWidth, font, measure));
147
132
  }
148
- var contentWidth = 0;
149
- for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
150
- var line = lines_1[_i];
133
+ let contentWidth = 0;
134
+ for (const line of lines)
151
135
  contentWidth = Math.max(contentWidth, measure(line, font));
152
- }
153
- var height = Math.ceil(lines.length * textLineExtent(style) + padding * 2);
154
- var width = fixedWidth === null
136
+ const height = Math.ceil(lines.length * textLineExtent(style) + padding * 2);
137
+ let width = fixedWidth === null
155
138
  ? Math.max(TEXT_DEFAULTS.minimumWidth, Math.ceil(contentWidth + padding * 2 + textFitHorizontalSlack(style)))
156
139
  : fixedWidth;
157
140
  if (fixedWidth === null && text === '') {
158
- var standIn = TEXT_DEFAULTS.emptyTextMeasureStandIn;
159
- var standInWidth = measure(standIn, font);
141
+ const standIn = TEXT_DEFAULTS.emptyTextMeasureStandIn;
142
+ const standInWidth = measure(standIn, font);
160
143
  width = Math.max(width, Math.ceil(standInWidth + padding * 2 + textFitHorizontalSlack(style)));
161
144
  }
162
- return { lines: lines, contentWidth: contentWidth, width: width, height: height };
145
+ return { lines, contentWidth, width, height };
163
146
  }
164
147
  /** The layout of a text object as it stands. */
165
148
  export function layoutTextObject(shape, measure) {
@@ -170,18 +153,16 @@ export function layoutTextObject(shape, measure) {
170
153
  * The same object comes back when nothing changes, so an unchanged text is not redrawn.
171
154
  */
172
155
  export function fitTextObject(shape, measure) {
173
- var layout = layoutTextObject(shape, measure);
174
- var width = shape.autoWidth ? layout.width : shape.width;
156
+ const layout = layoutTextObject(shape, measure);
157
+ const width = shape.autoWidth ? layout.width : shape.width;
175
158
  if (width === shape.width && layout.height === shape.height)
176
159
  return shape;
177
- return __assign(__assign({}, shape), { width: width, height: layout.height });
160
+ return { ...shape, width, height: layout.height };
178
161
  }
179
162
  /** A measuring function that needs no browser: every letter is `advance` times the font size wide. For tests and Node. */
180
- export function createFixedAdvanceMeasure(advance) {
181
- if (advance === void 0) { advance = 0.5; }
182
- return function (text, font) {
183
- var _a;
184
- var size = (_a = /(\d+(?:\.\d+)?)px/.exec(font)) === null || _a === void 0 ? void 0 : _a[1];
163
+ export function createFixedAdvanceMeasure(advance = 0.5) {
164
+ return (text, font) => {
165
+ const size = /(\d+(?:\.\d+)?)px/.exec(font)?.[1];
185
166
  return Array.from(text).length * advance * (size ? Number(size) : 16);
186
167
  };
187
168
  }