@widgetic/canvas 0.5.40 → 0.5.41
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/README.md +7 -3
- package/dist/canvas/DomCanvas.svelte +4 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +8 -0
- package/dist/canvas/autosaveScheduler.js +89 -37
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +19 -7
- package/dist/canvas/chromeLayout.js +18 -17
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +1556 -1267
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +25 -4
- package/dist/canvas/host/browserServices.js +116 -65
- package/dist/canvas/host/conversion.js +170 -82
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +31 -28
- package/dist/canvas/host/strokeSnap.js +143 -88
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +7 -6
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +25 -13
- package/dist/canvas/shapes/ShapeLibrary.js +148 -138
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/CanvasHint.svelte +16 -3
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/keyHints.js +11 -9
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.js +40 -34
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
- package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
- package/dist/canvas/view/SelectionOverlay.svelte +33 -14
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +14 -4
- package/dist/canvas/view/geometry.js +103 -77
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +4 -1
- package/dist/engine/EngineCore.js +801 -695
- package/dist/engine/codec/canvasDocument.js +173 -173
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +38 -26
- package/dist/engine/core/arrow.d.ts +10 -3
- package/dist/engine/core/arrow.js +96 -75
- package/dist/engine/core/arrowBinding.d.ts +69 -0
- package/dist/engine/core/arrowBinding.js +344 -0
- package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
- package/dist/engine/core/arrowBindingRecords.js +141 -0
- package/dist/engine/core/brush.js +94 -67
- package/dist/engine/core/defaults.js +51 -90
- package/dist/engine/core/freehand.js +163 -147
- package/dist/engine/core/geometry.js +54 -47
- package/dist/engine/core/history.js +50 -33
- package/dist/engine/core/hitTest.d.ts +3 -0
- package/dist/engine/core/hitTest.js +83 -69
- package/dist/engine/core/matrix.js +20 -18
- package/dist/engine/core/pathData.js +210 -183
- package/dist/engine/core/rotationReset.js +6 -5
- package/dist/engine/core/scene.d.ts +3 -0
- package/dist/engine/core/scene.js +192 -129
- package/dist/engine/core/scribble.js +84 -59
- package/dist/engine/core/shapePaths.js +15 -12
- package/dist/engine/core/shapeTools.js +63 -72
- package/dist/engine/core/snapping.js +117 -65
- package/dist/engine/core/style.js +64 -31
- package/dist/engine/core/transform.js +94 -83
- package/dist/engine/core/types.d.ts +29 -0
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +36 -23
- package/dist/engine/input/PointerController.js +476 -447
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +12 -1
- package/dist/engine/input/gestures.js +342 -251
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.d.ts +9 -1
- package/dist/engine/input/keyboard.js +46 -27
- package/dist/engine/input/selectionHandles.js +50 -45
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +158 -86
- package/dist/engine/state.d.ts +6 -1
- package/dist/engine/state.js +17 -5
- package/dist/engine/text/layoutText.js +62 -43
- package/dist/engine/text/textMetrics.js +10 -8
- package/dist/engine/widgets.js +137 -101
- package/dist/fonts/interFont.js +68 -17
- package/dist/fonts/workspaceFonts.js +90 -32
- 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.d.ts +12 -0
- package/dist/i18n/shortcuts.js +32 -18
- package/dist/settings/workspaceToolSettings.js +48 -32
- package/dist/tools/components/catalog.d.ts +11 -0
- package/dist/tools/components/catalog.js +270 -0
- package/dist/tools/components/grouping.d.ts +9 -0
- package/dist/tools/components/grouping.js +33 -0
- package/dist/tools/definitions.d.ts +19 -0
- package/dist/tools/definitions.js +147 -0
- package/dist/tools/executors.d.ts +7 -0
- package/dist/tools/executors.js +324 -0
- package/dist/tools/hostAdapter.d.ts +14 -0
- package/dist/tools/hostAdapter.js +40 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +6 -0
- package/dist/tools/sceneSummary.d.ts +24 -0
- package/dist/tools/sceneSummary.js +170 -0
- package/dist/tools/sketchIntent.d.ts +17 -0
- package/dist/tools/sketchIntent.js +166 -0
- package/dist/tools/types.d.ts +54 -0
- package/dist/tools/types.js +11 -0
- package/package.json +7 -2
|
@@ -9,45 +9,93 @@
|
|
|
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
|
+
};
|
|
12
57
|
import { boxToWorldMatrix } from '../core/matrix.js';
|
|
13
58
|
import { strokeDashArray } from '../core/defaults.js';
|
|
14
59
|
import { shapeVisualBounds } from '../core/scene.js';
|
|
15
60
|
import { unionRects } from '../core/geometry.js';
|
|
16
61
|
import { fontShorthand, layoutTextObject, lineBoxHeight, textDirection } from '../text/layoutText.js';
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
62
|
+
export var DEFAULT_RASTER_SCALE = 2;
|
|
63
|
+
export var DEFAULT_RASTER_MAX_PIXELS = 16000000;
|
|
64
|
+
export var DEFAULT_RASTER_MAX_SIDE = 8192;
|
|
20
65
|
/** The addresses of the pictures a set of shapes needs: images and the screenshots of widget cards. */
|
|
21
66
|
export function imageSourcesOf(shapes) {
|
|
22
|
-
|
|
23
|
-
for (
|
|
67
|
+
var sources = new Set();
|
|
68
|
+
for (var _i = 0, shapes_1 = shapes; _i < shapes_1.length; _i++) {
|
|
69
|
+
var shape = shapes_1[_i];
|
|
24
70
|
if (shape.type === 'image' && shape.src)
|
|
25
71
|
sources.add(shape.src);
|
|
26
72
|
if (shape.type === 'widget' && shape.thumbnail)
|
|
27
73
|
sources.add(shape.thumbnail);
|
|
28
74
|
}
|
|
29
|
-
return [
|
|
75
|
+
return __spreadArray([], sources, true);
|
|
30
76
|
}
|
|
31
77
|
/** The rectangle every shape fits in, strokes included. Null when there is no shape or it has no size. */
|
|
32
78
|
export function rasterBoundsOf(shapes) {
|
|
33
|
-
|
|
79
|
+
var bounds = unionRects(shapes.map(shapeVisualBounds));
|
|
34
80
|
if (!bounds || !(bounds.width > 0) || !(bounds.height > 0))
|
|
35
81
|
return null;
|
|
36
82
|
return bounds;
|
|
37
83
|
}
|
|
38
84
|
/** The scale a picture of these bounds is drawn at: the one asked for, lowered until it fits the limits. */
|
|
39
|
-
export function rasterScaleFor(bounds, options
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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;
|
|
47
95
|
return Math.max(0.01, Math.min(requested, byArea, bySide));
|
|
48
96
|
}
|
|
49
97
|
function roundedRectPath(context, width, height, radius) {
|
|
50
|
-
|
|
98
|
+
var clamped = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
51
99
|
context.beginPath();
|
|
52
100
|
if (clamped <= 0) {
|
|
53
101
|
context.rect(0, 0, width, height);
|
|
@@ -73,8 +121,8 @@ function applyStroke(context, shape) {
|
|
|
73
121
|
return false;
|
|
74
122
|
context.strokeStyle = shape.stroke;
|
|
75
123
|
context.lineWidth = shape.strokeWidth;
|
|
76
|
-
|
|
77
|
-
context.setLineDash(dash
|
|
124
|
+
var dash = strokeDashArray(shape.strokeStyle, shape.strokeWidth);
|
|
125
|
+
context.setLineDash(dash !== null && dash !== void 0 ? dash : []);
|
|
78
126
|
// A dotted line is zero-length dashes; only a round cap turns each into a dot.
|
|
79
127
|
if (shape.strokeStyle === 'dotted')
|
|
80
128
|
context.lineCap = 'round';
|
|
@@ -82,7 +130,7 @@ function applyStroke(context, shape) {
|
|
|
82
130
|
}
|
|
83
131
|
function paintFillAndStroke(context, shape, fillPath) {
|
|
84
132
|
if (shape.fill !== 'transparent' && shape.fillOpacity > 0) {
|
|
85
|
-
|
|
133
|
+
var before = context.globalAlpha;
|
|
86
134
|
context.globalAlpha = before * Math.max(0, Math.min(1, shape.fillOpacity));
|
|
87
135
|
context.fillStyle = shape.fill;
|
|
88
136
|
if (fillPath)
|
|
@@ -99,11 +147,11 @@ function paintFillAndStroke(context, shape, fillPath) {
|
|
|
99
147
|
}
|
|
100
148
|
}
|
|
101
149
|
function fontMetricsOf(context, size) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
150
|
+
var measured = context.measureText('Hg');
|
|
151
|
+
var ascent = measured.fontBoundingBoxAscent;
|
|
152
|
+
var descent = measured.fontBoundingBoxDescent;
|
|
105
153
|
if (typeof ascent === 'number' && typeof descent === 'number' && ascent + descent > 0)
|
|
106
|
-
return { ascent, descent };
|
|
154
|
+
return { ascent: ascent, descent: descent };
|
|
107
155
|
return { ascent: size * 0.8, descent: size * 0.2 };
|
|
108
156
|
}
|
|
109
157
|
function paintText(context, shape, deps) {
|
|
@@ -113,27 +161,27 @@ function paintText(context, shape, deps) {
|
|
|
113
161
|
}
|
|
114
162
|
if (shape.text === '')
|
|
115
163
|
return;
|
|
116
|
-
|
|
164
|
+
var layout = layoutTextObject(shape, deps.measureText);
|
|
117
165
|
context.font = fontShorthand(shape);
|
|
118
166
|
context.fillStyle = shape.color;
|
|
119
167
|
context.textBaseline = 'alphabetic';
|
|
120
168
|
context.textAlign = 'left';
|
|
121
169
|
// The text reads the way the board shows it: by the direction of its first letter (a line is placed from its left edge).
|
|
122
170
|
context.direction = textDirection(shape.text);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
for (
|
|
127
|
-
|
|
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];
|
|
128
176
|
if (line === '')
|
|
129
177
|
continue;
|
|
130
|
-
|
|
131
|
-
|
|
178
|
+
var lineWidth = deps.measureText(line, fontShorthand(shape));
|
|
179
|
+
var left = shape.padding;
|
|
132
180
|
if (shape.align === 'center')
|
|
133
181
|
left = shape.padding + (innerWidth - lineWidth) / 2;
|
|
134
182
|
else if (shape.align === 'right')
|
|
135
183
|
left = shape.padding + (innerWidth - lineWidth);
|
|
136
|
-
|
|
184
|
+
var baseline = shape.padding + index * lineHeight + (lineHeight - (ascent + descent)) / 2 + ascent;
|
|
137
185
|
context.fillText(line, left, baseline);
|
|
138
186
|
if (shape.underline) {
|
|
139
187
|
context.fillRect(left, baseline + Math.max(1, shape.fontSize * 0.08), lineWidth, Math.max(1, shape.fontSize / 15));
|
|
@@ -141,7 +189,7 @@ function paintText(context, shape, deps) {
|
|
|
141
189
|
}
|
|
142
190
|
}
|
|
143
191
|
function paintWidget(context, shape, deps) {
|
|
144
|
-
|
|
192
|
+
var picture = deps.imageFor(shape.thumbnail);
|
|
145
193
|
roundedRectPath(context, shape.width, shape.height, 8);
|
|
146
194
|
context.fillStyle = '#ffffff';
|
|
147
195
|
context.fill();
|
|
@@ -157,14 +205,14 @@ function paintWidget(context, shape, deps) {
|
|
|
157
205
|
context.setLineDash([6, 4]);
|
|
158
206
|
context.stroke();
|
|
159
207
|
context.setLineDash([]);
|
|
160
|
-
|
|
208
|
+
var name = shape.widgetName || deps.widgetFallbackName || '';
|
|
161
209
|
if (name === '')
|
|
162
210
|
return;
|
|
163
211
|
context.fillStyle = '#1e293b';
|
|
164
212
|
context.textAlign = 'center';
|
|
165
213
|
context.textBaseline = 'middle';
|
|
166
|
-
|
|
167
|
-
context.font =
|
|
214
|
+
var size = Math.max(11, Math.min(18, shape.width * 0.06));
|
|
215
|
+
context.font = "600 ".concat(size, "px system-ui, sans-serif");
|
|
168
216
|
context.fillText(name, shape.width / 2, shape.height / 2);
|
|
169
217
|
}
|
|
170
218
|
/**
|
|
@@ -176,7 +224,7 @@ export function paintShape(context, shape, deps) {
|
|
|
176
224
|
return;
|
|
177
225
|
context.save();
|
|
178
226
|
try {
|
|
179
|
-
|
|
227
|
+
var matrix = boxToWorldMatrix(shape);
|
|
180
228
|
context.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
|
|
181
229
|
context.globalAlpha = context.globalAlpha * Math.max(0, Math.min(1, shape.opacity));
|
|
182
230
|
switch (shape.type) {
|
|
@@ -202,7 +250,7 @@ export function paintShape(context, shape, deps) {
|
|
|
202
250
|
break;
|
|
203
251
|
}
|
|
204
252
|
case 'image': {
|
|
205
|
-
|
|
253
|
+
var picture = deps.imageFor(shape.src);
|
|
206
254
|
if (picture)
|
|
207
255
|
context.drawImage(picture, 0, 0, shape.width, shape.height);
|
|
208
256
|
break;
|
|
@@ -222,52 +270,76 @@ export function paintShape(context, shape, deps) {
|
|
|
222
270
|
* string when there is nothing to draw, the canvas cannot be made, or the browser refuses to export it (a picture
|
|
223
271
|
* from another site without CORS taints the canvas).
|
|
224
272
|
*/
|
|
225
|
-
export
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
+
});
|
|
273
345
|
}
|
package/dist/engine/state.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { ArrowHandleId } from './core/arrow.js';
|
|
|
7
7
|
import type { Scene } from './core/scene.js';
|
|
8
8
|
import type { ScribbleView } from './core/scribble.js';
|
|
9
9
|
import type { SnapGuide } from './core/snapping.js';
|
|
10
|
-
import type { ObjectId, Rect, SceneObject, Size, Viewport } from './core/types.js';
|
|
10
|
+
import type { ArrowBindHint, ObjectId, Rect, SceneObject, Size, Viewport } from './core/types.js';
|
|
11
11
|
export type ToolId = 'select' | 'draw' | 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape' | 'text' | 'erase' | 'pan';
|
|
12
12
|
/** The tools that drag a shape out. */
|
|
13
13
|
export type ShapeToolId = 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape';
|
|
@@ -76,6 +76,11 @@ export interface EngineOverlay {
|
|
|
76
76
|
* one undo step) or Escape leaves them as they were. Ids of shapes, never of groups: a group is marked by its members.
|
|
77
77
|
*/
|
|
78
78
|
erasing: readonly ObjectId[];
|
|
79
|
+
/**
|
|
80
|
+
* While an end of an arrow is dragged (the Arrow tool, or a handle of a selected arrow) over a shape it would be tied to: that shape
|
|
81
|
+
* and the point where the end would rest. The board outlines the shape and puts a dot on the point.
|
|
82
|
+
*/
|
|
83
|
+
arrowBind: ArrowBindHint | null;
|
|
79
84
|
}
|
|
80
85
|
export declare const EMPTY_OVERLAY: EngineOverlay;
|
|
81
86
|
export interface EngineState {
|
package/dist/engine/state.js
CHANGED
|
@@ -3,10 +3,21 @@
|
|
|
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
|
+
};
|
|
6
17
|
import { INITIAL_VIEWPORT } from './core/viewport.js';
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
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({
|
|
10
21
|
marquee: null,
|
|
11
22
|
marqueeHover: Object.freeze([]),
|
|
12
23
|
guides: Object.freeze([]),
|
|
@@ -15,7 +26,8 @@ export const EMPTY_OVERLAY = Object.freeze({
|
|
|
15
26
|
dragging: null,
|
|
16
27
|
readout: null,
|
|
17
28
|
virtualCursor: null,
|
|
18
|
-
erasing: Object.freeze([])
|
|
29
|
+
erasing: Object.freeze([]),
|
|
30
|
+
arrowBind: null
|
|
19
31
|
});
|
|
20
32
|
export function createInitialState() {
|
|
21
33
|
return {
|
|
@@ -25,7 +37,7 @@ export function createInitialState() {
|
|
|
25
37
|
hoverId: null,
|
|
26
38
|
rotationDotHovered: false,
|
|
27
39
|
arrowHandleHovered: null,
|
|
28
|
-
viewport: {
|
|
40
|
+
viewport: __assign({}, INITIAL_VIEWPORT),
|
|
29
41
|
viewSize: { width: 0, height: 0 },
|
|
30
42
|
tool: 'select',
|
|
31
43
|
libraryShapeKey: null,
|
|
@@ -6,24 +6,35 @@
|
|
|
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
|
+
};
|
|
9
20
|
import { TEXT_DEFAULTS } from '../core/defaults.js';
|
|
10
21
|
import { textLineExtent } from './textMetrics.js';
|
|
11
22
|
/** Room so the last letter and the caret stay on the line; more than this reads as padding. */
|
|
12
|
-
export
|
|
23
|
+
export var TEXT_FIT_SLACK = 4;
|
|
13
24
|
/** Extra width for italic slant and serif overhang beyond what `measureText` returns. */
|
|
14
25
|
export function textFitHorizontalSlack(style) {
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
var overhang = Math.ceil(style.fontSize * 0.08);
|
|
27
|
+
var italic = style.fontStyle === 'italic' ? Math.ceil(style.fontSize * 0.2) : 0;
|
|
17
28
|
return TEXT_FIT_SLACK + overhang + italic;
|
|
18
29
|
}
|
|
19
30
|
/** The CSS `font` shorthand of a text object. */
|
|
20
31
|
export function fontShorthand(style) {
|
|
21
|
-
|
|
32
|
+
var parts = [];
|
|
22
33
|
if (style.fontStyle === 'italic')
|
|
23
34
|
parts.push('italic');
|
|
24
35
|
if (style.fontWeight === 'bold')
|
|
25
36
|
parts.push('bold');
|
|
26
|
-
parts.push(
|
|
37
|
+
parts.push("".concat(style.fontSize, "px"));
|
|
27
38
|
parts.push(style.fontFamily);
|
|
28
39
|
return parts.join(' ');
|
|
29
40
|
}
|
|
@@ -33,16 +44,17 @@ export function lineBoxHeight(style) {
|
|
|
33
44
|
}
|
|
34
45
|
export { textLineExtent } from './textMetrics.js';
|
|
35
46
|
/** A letter of a script that is written from right to left. Digits, signs and marks have no direction of their own. */
|
|
36
|
-
|
|
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;
|
|
37
48
|
/** A letter of any script. */
|
|
38
|
-
|
|
49
|
+
var ANY_LETTER = /\p{L}/u;
|
|
39
50
|
/**
|
|
40
51
|
* The direction a text is written in: the one of its first letter. This is what the browser's `dir="auto"` does, so the
|
|
41
52
|
* 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
|
|
42
53
|
* all place the same text the same way. A text with no letter (digits, signs, nothing) reads left to right.
|
|
43
54
|
*/
|
|
44
55
|
export function textDirection(text) {
|
|
45
|
-
for (
|
|
56
|
+
for (var _i = 0, text_1 = text; _i < text_1.length; _i++) {
|
|
57
|
+
var character = text_1[_i];
|
|
46
58
|
if (RIGHT_TO_LEFT_LETTER.test(character))
|
|
47
59
|
return 'rtl';
|
|
48
60
|
if (ANY_LETTER.test(character))
|
|
@@ -50,23 +62,24 @@ export function textDirection(text) {
|
|
|
50
62
|
}
|
|
51
63
|
return 'ltr';
|
|
52
64
|
}
|
|
53
|
-
|
|
65
|
+
var segmenter;
|
|
54
66
|
/** The letters of a word as a person counts them (an emoji with a modifier is one), when the platform can tell. */
|
|
55
67
|
function graphemes(word) {
|
|
56
68
|
if (segmenter === undefined) {
|
|
57
|
-
|
|
69
|
+
var Segmenter = Intl.Segmenter;
|
|
58
70
|
segmenter = Segmenter ? new Segmenter(undefined, { granularity: 'grapheme' }) : null;
|
|
59
71
|
}
|
|
60
72
|
if (!segmenter)
|
|
61
73
|
return Array.from(word);
|
|
62
|
-
return Array.from(segmenter.segment(word), (part)
|
|
74
|
+
return Array.from(segmenter.segment(word), function (part) { return part.segment; });
|
|
63
75
|
}
|
|
64
76
|
/** Cuts a word that is wider than a line into pieces that each fit (at least one letter each). */
|
|
65
77
|
function breakLongWord(word, maxWidth, font, measure) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
for (
|
|
69
|
-
|
|
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;
|
|
70
83
|
if (current !== '' && measure(candidate, font) > maxWidth) {
|
|
71
84
|
pieces.push(current);
|
|
72
85
|
current = letter;
|
|
@@ -81,16 +94,18 @@ function breakLongWord(word, maxWidth, font, measure) {
|
|
|
81
94
|
}
|
|
82
95
|
/** Wraps one paragraph (no line breaks in it) into lines no wider than `maxWidth`, breaking at spaces. */
|
|
83
96
|
function wrapParagraph(paragraph, maxWidth, font, measure) {
|
|
97
|
+
var _a, _b;
|
|
84
98
|
if (paragraph === '')
|
|
85
99
|
return [''];
|
|
86
100
|
if (measure(paragraph, font) <= maxWidth)
|
|
87
101
|
return [paragraph];
|
|
88
|
-
|
|
89
|
-
|
|
102
|
+
var lines = [];
|
|
103
|
+
var line = '';
|
|
90
104
|
// Words keep the spaces that follow them, so a line that ends in a space still measures like the browser's.
|
|
91
|
-
|
|
92
|
-
for (
|
|
93
|
-
|
|
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;
|
|
94
109
|
if (measure(candidate.trimEnd(), font) <= maxWidth) {
|
|
95
110
|
line = candidate;
|
|
96
111
|
continue;
|
|
@@ -103,11 +118,11 @@ function wrapParagraph(paragraph, maxWidth, font, measure) {
|
|
|
103
118
|
line = word;
|
|
104
119
|
}
|
|
105
120
|
else {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
lines.push(
|
|
121
|
+
var bareWord = word.trimEnd();
|
|
122
|
+
var pieces = breakLongWord(bareWord, maxWidth, font, measure);
|
|
123
|
+
lines.push.apply(lines, pieces.slice(0, -1));
|
|
109
124
|
// The last piece keeps the spaces that followed the word, so the next word is not glued to it
|
|
110
|
-
line = (pieces[pieces.length - 1]
|
|
125
|
+
line = ((_b = pieces[pieces.length - 1]) !== null && _b !== void 0 ? _b : '') + word.slice(bareWord.length);
|
|
111
126
|
}
|
|
112
127
|
}
|
|
113
128
|
if (line !== '')
|
|
@@ -119,30 +134,32 @@ function wrapParagraph(paragraph, maxWidth, font, measure) {
|
|
|
119
134
|
* `fixedWidth` is the width of the box when the text wraps; null lets the box be as wide as the longest line.
|
|
120
135
|
*/
|
|
121
136
|
export function layoutText(text, style, fixedWidth, measure) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
137
|
+
var font = fontShorthand(style);
|
|
138
|
+
var padding = style.padding;
|
|
139
|
+
var paragraphs = text.split('\n');
|
|
140
|
+
var lines;
|
|
126
141
|
if (fixedWidth === null) {
|
|
127
142
|
lines = paragraphs;
|
|
128
143
|
}
|
|
129
144
|
else {
|
|
130
|
-
|
|
131
|
-
lines = paragraphs.flatMap((paragraph)
|
|
145
|
+
var maxWidth_1 = Math.max(1, fixedWidth - padding * 2);
|
|
146
|
+
lines = paragraphs.flatMap(function (paragraph) { return wrapParagraph(paragraph, maxWidth_1, font, measure); });
|
|
132
147
|
}
|
|
133
|
-
|
|
134
|
-
for (
|
|
148
|
+
var contentWidth = 0;
|
|
149
|
+
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
|
|
150
|
+
var line = lines_1[_i];
|
|
135
151
|
contentWidth = Math.max(contentWidth, measure(line, font));
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
}
|
|
153
|
+
var height = Math.ceil(lines.length * textLineExtent(style) + padding * 2);
|
|
154
|
+
var width = fixedWidth === null
|
|
138
155
|
? Math.max(TEXT_DEFAULTS.minimumWidth, Math.ceil(contentWidth + padding * 2 + textFitHorizontalSlack(style)))
|
|
139
156
|
: fixedWidth;
|
|
140
157
|
if (fixedWidth === null && text === '') {
|
|
141
|
-
|
|
142
|
-
|
|
158
|
+
var standIn = TEXT_DEFAULTS.emptyTextMeasureStandIn;
|
|
159
|
+
var standInWidth = measure(standIn, font);
|
|
143
160
|
width = Math.max(width, Math.ceil(standInWidth + padding * 2 + textFitHorizontalSlack(style)));
|
|
144
161
|
}
|
|
145
|
-
return { lines, contentWidth, width, height };
|
|
162
|
+
return { lines: lines, contentWidth: contentWidth, width: width, height: height };
|
|
146
163
|
}
|
|
147
164
|
/** The layout of a text object as it stands. */
|
|
148
165
|
export function layoutTextObject(shape, measure) {
|
|
@@ -153,16 +170,18 @@ export function layoutTextObject(shape, measure) {
|
|
|
153
170
|
* The same object comes back when nothing changes, so an unchanged text is not redrawn.
|
|
154
171
|
*/
|
|
155
172
|
export function fitTextObject(shape, measure) {
|
|
156
|
-
|
|
157
|
-
|
|
173
|
+
var layout = layoutTextObject(shape, measure);
|
|
174
|
+
var width = shape.autoWidth ? layout.width : shape.width;
|
|
158
175
|
if (width === shape.width && layout.height === shape.height)
|
|
159
176
|
return shape;
|
|
160
|
-
return {
|
|
177
|
+
return __assign(__assign({}, shape), { width: width, height: layout.height });
|
|
161
178
|
}
|
|
162
179
|
/** A measuring function that needs no browser: every letter is `advance` times the font size wide. For tests and Node. */
|
|
163
|
-
export function createFixedAdvanceMeasure(advance
|
|
164
|
-
|
|
165
|
-
|
|
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];
|
|
166
185
|
return Array.from(text).length * advance * (size ? Number(size) : 16);
|
|
167
186
|
};
|
|
168
187
|
}
|