@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.
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.js +17 -18
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.js +1286 -1536
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.js +696 -795
- package/dist/engine/codec/canvasDocument.js +183 -147
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +33 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.js +129 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +262 -308
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.js +4 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.js +17 -68
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
|
@@ -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
|
|
63
|
-
export
|
|
64
|
-
export
|
|
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
|
-
|
|
68
|
-
for (
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
125
|
-
context.setLineDash(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
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
for (
|
|
175
|
-
|
|
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
|
-
|
|
179
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
context.font =
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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
|
}
|
package/dist/engine/state.js
CHANGED
|
@@ -3,21 +3,10 @@
|
|
|
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
|
|
19
|
-
export
|
|
20
|
-
export
|
|
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([]),
|
|
@@ -37,7 +26,7 @@ export function createInitialState() {
|
|
|
37
26
|
hoverId: null,
|
|
38
27
|
rotationDotHovered: false,
|
|
39
28
|
arrowHandleHovered: null,
|
|
40
|
-
viewport:
|
|
29
|
+
viewport: { ...INITIAL_VIEWPORT },
|
|
41
30
|
viewSize: { width: 0, height: 0 },
|
|
42
31
|
tool: 'select',
|
|
43
32
|
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
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
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
|
-
|
|
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
|
-
|
|
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),
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
for (
|
|
81
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
-
|
|
106
|
-
for (
|
|
107
|
-
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
lines.push
|
|
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 = (
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
146
|
-
lines = paragraphs.flatMap(
|
|
130
|
+
const maxWidth = Math.max(1, fixedWidth - padding * 2);
|
|
131
|
+
lines = paragraphs.flatMap((paragraph) => wrapParagraph(paragraph, maxWidth, font, measure));
|
|
147
132
|
}
|
|
148
|
-
|
|
149
|
-
for (
|
|
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
|
-
|
|
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
|
-
|
|
159
|
-
|
|
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
|
|
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
|
-
|
|
174
|
-
|
|
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
|
|
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
|
-
|
|
182
|
-
|
|
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
|
}
|
|
@@ -9,18 +9,16 @@ export function estimateFontVerticalMetrics(fontSize) {
|
|
|
9
9
|
/**
|
|
10
10
|
* The height of one line in world units: glyph ascent+descent plus balanced leading inside the CSS line-height box.
|
|
11
11
|
*/
|
|
12
|
-
export function textLineExtent(style, metrics) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var leading = Math.max(0, lineHeight - glyphHeight);
|
|
12
|
+
export function textLineExtent(style, metrics = estimateFontVerticalMetrics(style.fontSize)) {
|
|
13
|
+
const glyphHeight = metrics.ascent + metrics.descent;
|
|
14
|
+
const lineHeight = lineBoxHeight(style);
|
|
15
|
+
const leading = Math.max(0, lineHeight - glyphHeight);
|
|
17
16
|
return glyphHeight + leading;
|
|
18
17
|
}
|
|
19
18
|
/** Splits leading evenly above and below the glyph box inside one line. */
|
|
20
|
-
export function lineVerticalInsets(style, metrics) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
var slack = Math.max(0, extent - glyphHeight);
|
|
19
|
+
export function lineVerticalInsets(style, metrics = estimateFontVerticalMetrics(style.fontSize)) {
|
|
20
|
+
const extent = textLineExtent(style, metrics);
|
|
21
|
+
const glyphHeight = metrics.ascent + metrics.descent;
|
|
22
|
+
const slack = Math.max(0, extent - glyphHeight);
|
|
25
23
|
return { top: slack / 2, bottom: slack / 2 };
|
|
26
24
|
}
|