@widgetic/canvas 0.5.39 → 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 +7 -3
- 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.d.ts +2 -0
- package/dist/canvas/chromeLayout.js +31 -17
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +1556 -1255
- 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/CanvasContextMenu.svelte +69 -39
- package/dist/canvas/ui/CanvasHint.svelte +17 -4
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +27 -10
- package/dist/canvas/ui/ColorField.svelte +1 -1
- package/dist/canvas/ui/SelectionPanel.svelte +24 -20
- package/dist/canvas/ui/ShapesMenu.svelte +0 -2
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/WidgetActions.svelte +1 -1
- package/dist/canvas/ui/ZoomControl.svelte +39 -4
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +2 -2
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +7 -5
- package/dist/canvas/ui/floatingCard.js +23 -21
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/keyHints.d.ts +5 -0
- package/dist/canvas/ui/keyHints.js +23 -7
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.js +40 -34
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +5 -2
- package/dist/canvas/ui/sizeLabelVisibility.js +7 -6
- package/dist/canvas/ui/widgetSelectionPanel.d.ts +6 -0
- package/dist/canvas/ui/widgetSelectionPanel.js +9 -0
- package/dist/canvas/view/ObjectView.svelte +2 -1
- package/dist/canvas/view/SelectionOverlay.svelte +34 -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 +6 -5
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +6 -6
- package/dist/i18n/messages.js +7 -7
- 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
|
@@ -17,9 +17,9 @@ export declare const CANVAS_CONTRACT_FUNCTION_NAMES: readonly ["shapeFillColor",
|
|
|
17
17
|
* (feat/shopify-feedback-canvas) adds the same one, so it keeps the name and the answer (how many objects went): removes
|
|
18
18
|
* the sketch objects around one widget card and keeps the card.
|
|
19
19
|
*/
|
|
20
|
-
export declare const CANVAS_ADDED_FUNCTION_NAMES: readonly ["removeSketchObjectsKeepingWidget"];
|
|
20
|
+
export declare const CANVAS_ADDED_FUNCTION_NAMES: readonly ["removeSketchObjectsKeepingWidget", "getSketchSceneSummary", "executeSketchTool", "beginSketchToolBatch", "endSketchToolBatch"];
|
|
21
21
|
/** Every function the shell exports. */
|
|
22
|
-
export declare const CANVAS_EXPORTED_FUNCTION_NAMES: readonly ["shapeFillColor", "captureCanvasSketch", "absorbCanvasSketchIntoWidget", "deselectAll", "duplicateSelectedObjects", "deleteSelectedObjects", "markSelectedAsDuplicating", "linkObjectToWidget", "linkFrameToWidget", "replaceObjectWithWidgetImage", "updateWidgetImage", "selectWidgetShape", "listWidgetShapeScreenRects", "getWidgetShapeScreenRect", "getObjectScreenRect", "getWidgetShapeDataUrl", "setWidgetShapeLivePreviewHiddenImage", "addWidgetShape", "getLinkedWidgetIds", "keepOnlyWidgetShape", "hydrateWidgetShapeMetadata", "markOrphanedWidgetShapes", "onSelectSelectTool", "onSelectLibraryShapeTool", "onSelectEraseTool", "onSelectDrawTool", "onSelectTextTool", "onSelectArrowTool", "onSelectShapeTool", "onSelectFrameTool", "setPrimaryWidgetShape", "getPrimaryWidgetId", "setWidgetShapeBodyDragAllowed", "translateWidgetShapeByScreen", "commitWidgetShapeTranslate", "setWidgetShapeOverlayDragActive", "selectWidgetShapeById", "getSelectedWidgetShapeId", "getSelectedObject", "getHoveredWidgetShapeId", "onSelectImageTool", "onSelectVideoTool", "onSelectPanTool", "zoomIn", "zoomOut", "zoomTo", "zoomReset", "zoomFit", "onGroupSelection", "onUngroupSelection", "onSelectAllObjects", "onDeselectAllObjects", "flushAutosave", "saveNow", "undo", "redo", "saveCanvas", "loadCanvas", "onClearAllObjects", "addText", "addTriangle", "addRect", "addCircle", "addFrame", "addImage", "removeSketchObjectsKeepingWidget"];
|
|
22
|
+
export declare const CANVAS_EXPORTED_FUNCTION_NAMES: readonly ["shapeFillColor", "captureCanvasSketch", "absorbCanvasSketchIntoWidget", "deselectAll", "duplicateSelectedObjects", "deleteSelectedObjects", "markSelectedAsDuplicating", "linkObjectToWidget", "linkFrameToWidget", "replaceObjectWithWidgetImage", "updateWidgetImage", "selectWidgetShape", "listWidgetShapeScreenRects", "getWidgetShapeScreenRect", "getObjectScreenRect", "getWidgetShapeDataUrl", "setWidgetShapeLivePreviewHiddenImage", "addWidgetShape", "getLinkedWidgetIds", "keepOnlyWidgetShape", "hydrateWidgetShapeMetadata", "markOrphanedWidgetShapes", "onSelectSelectTool", "onSelectLibraryShapeTool", "onSelectEraseTool", "onSelectDrawTool", "onSelectTextTool", "onSelectArrowTool", "onSelectShapeTool", "onSelectFrameTool", "setPrimaryWidgetShape", "getPrimaryWidgetId", "setWidgetShapeBodyDragAllowed", "translateWidgetShapeByScreen", "commitWidgetShapeTranslate", "setWidgetShapeOverlayDragActive", "selectWidgetShapeById", "getSelectedWidgetShapeId", "getSelectedObject", "getHoveredWidgetShapeId", "onSelectImageTool", "onSelectVideoTool", "onSelectPanTool", "zoomIn", "zoomOut", "zoomTo", "zoomReset", "zoomFit", "onGroupSelection", "onUngroupSelection", "onSelectAllObjects", "onDeselectAllObjects", "flushAutosave", "saveNow", "undo", "redo", "saveCanvas", "loadCanvas", "onClearAllObjects", "addText", "addTriangle", "addRect", "addCircle", "addFrame", "addImage", "removeSketchObjectsKeepingWidget", "getSketchSceneSummary", "executeSketchTool", "beginSketchToolBatch", "endSketchToolBatch"];
|
|
23
23
|
export type CanvasExportedFunctionName = (typeof CANVAS_EXPORTED_FUNCTION_NAMES)[number];
|
|
24
24
|
/** What a page gets from `bind:this` besides the component's props: the host's own type of each function. */
|
|
25
25
|
export type CanvasHostApi = Pick<CanvasHost, CanvasExportedFunctionName>;
|
|
@@ -9,8 +9,17 @@
|
|
|
9
9
|
* Kept free of Svelte so that Node can test it: `tests/host/hostSurface.test.mjs` holds this list, the fixture and the
|
|
10
10
|
* shell to each other.
|
|
11
11
|
*/
|
|
12
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
+
if (ar || !(i in from)) {
|
|
15
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
+
ar[i] = from[i];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
+
};
|
|
12
21
|
/** Every function a page already calls, in the order the fixture lists them. */
|
|
13
|
-
export
|
|
22
|
+
export var CANVAS_CONTRACT_FUNCTION_NAMES = [
|
|
14
23
|
'shapeFillColor',
|
|
15
24
|
'captureCanvasSketch',
|
|
16
25
|
'absorbCanvasSketchIntoWidget',
|
|
@@ -82,14 +91,26 @@ export const CANVAS_CONTRACT_FUNCTION_NAMES = [
|
|
|
82
91
|
* (feat/shopify-feedback-canvas) adds the same one, so it keeps the name and the answer (how many objects went): removes
|
|
83
92
|
* the sketch objects around one widget card and keeps the card.
|
|
84
93
|
*/
|
|
85
|
-
export
|
|
94
|
+
export var CANVAS_ADDED_FUNCTION_NAMES = [
|
|
95
|
+
'removeSketchObjectsKeepingWidget',
|
|
96
|
+
'getSketchSceneSummary',
|
|
97
|
+
'executeSketchTool',
|
|
98
|
+
'beginSketchToolBatch',
|
|
99
|
+
'endSketchToolBatch'
|
|
100
|
+
];
|
|
86
101
|
/** Every function the shell exports. */
|
|
87
|
-
export
|
|
102
|
+
export var CANVAS_EXPORTED_FUNCTION_NAMES = __spreadArray(__spreadArray([], CANVAS_CONTRACT_FUNCTION_NAMES, true), CANVAS_ADDED_FUNCTION_NAMES, true);
|
|
88
103
|
/**
|
|
89
104
|
* One exported function of the shell: it calls the host's method of that name, with the host as `this`, and has the
|
|
90
105
|
* method's own type. A page that holds the shell's reference calls it exactly as it calls the host's method.
|
|
91
106
|
*/
|
|
92
107
|
export function forwardToHost(host, name) {
|
|
93
|
-
|
|
108
|
+
var forwarded = function () {
|
|
109
|
+
var values = [];
|
|
110
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
111
|
+
values[_i] = arguments[_i];
|
|
112
|
+
}
|
|
113
|
+
return host[name].apply(host, values);
|
|
114
|
+
};
|
|
94
115
|
return forwarded;
|
|
95
116
|
}
|
|
@@ -6,21 +6,57 @@
|
|
|
6
6
|
* Every function checks for the browser itself and answers with the safest fallback when it is not there (the page
|
|
7
7
|
* is rendered on the server first, and nothing here may throw then).
|
|
8
8
|
*/
|
|
9
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
10
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
11
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
12
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
13
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
14
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
15
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
19
|
+
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);
|
|
20
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
21
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
22
|
+
function step(op) {
|
|
23
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
24
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
25
|
+
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;
|
|
26
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
27
|
+
switch (op[0]) {
|
|
28
|
+
case 0: case 1: t = op; break;
|
|
29
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
30
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
31
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
32
|
+
default:
|
|
33
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
34
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
35
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
36
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
37
|
+
if (t[2]) _.ops.pop();
|
|
38
|
+
_.trys.pop(); continue;
|
|
39
|
+
}
|
|
40
|
+
op = body.call(thisArg, _);
|
|
41
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
42
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
43
|
+
}
|
|
44
|
+
};
|
|
9
45
|
import { createFixedAdvanceMeasure } from '../../engine/text/layoutText.js';
|
|
10
46
|
import { getTransientHostFor } from '../eventScope.js';
|
|
11
|
-
|
|
47
|
+
var hasDocument = function () { return typeof document !== 'undefined' && typeof document.createElement === 'function'; };
|
|
12
48
|
/** A text measurer that uses the browser's canvas, or a fixed advance where there is none (the server, a test). */
|
|
13
49
|
export function createBrowserMeasureText() {
|
|
14
50
|
if (!hasDocument())
|
|
15
51
|
return createFixedAdvanceMeasure();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return (text, font)
|
|
52
|
+
var context;
|
|
53
|
+
var fallback = createFixedAdvanceMeasure();
|
|
54
|
+
return function (text, font) {
|
|
19
55
|
if (context === undefined) {
|
|
20
56
|
try {
|
|
21
57
|
context = document.createElement('canvas').getContext('2d');
|
|
22
58
|
}
|
|
23
|
-
catch {
|
|
59
|
+
catch (_a) {
|
|
24
60
|
context = null;
|
|
25
61
|
}
|
|
26
62
|
}
|
|
@@ -35,62 +71,63 @@ export function createBrowserMeasureText() {
|
|
|
35
71
|
export function loadImageElement(source) {
|
|
36
72
|
if (typeof Image === 'undefined')
|
|
37
73
|
return Promise.resolve(null);
|
|
38
|
-
return new Promise((resolve)
|
|
39
|
-
|
|
74
|
+
return new Promise(function (resolve) {
|
|
75
|
+
var image = new Image();
|
|
40
76
|
// A picture from another site is only usable in a canvas when that site allows it; a data address needs nothing.
|
|
41
77
|
if (/^https?:/i.test(source))
|
|
42
78
|
image.crossOrigin = 'anonymous';
|
|
43
|
-
image.onload = ()
|
|
79
|
+
image.onload = function () {
|
|
44
80
|
if (typeof image.decode === 'function') {
|
|
45
|
-
image.decode().then(()
|
|
81
|
+
image.decode().then(function () { return resolve(image); }, function () { return resolve(image.naturalWidth > 0 ? image : null); });
|
|
46
82
|
}
|
|
47
83
|
else {
|
|
48
84
|
resolve(image);
|
|
49
85
|
}
|
|
50
86
|
};
|
|
51
|
-
image.onerror = ()
|
|
87
|
+
image.onerror = function () { return resolve(null); };
|
|
52
88
|
image.src = source;
|
|
53
89
|
});
|
|
54
90
|
}
|
|
55
91
|
/** The services the rasterizer needs, made of the browser. */
|
|
56
92
|
export function createBrowserRasterServices(measureText) {
|
|
57
93
|
return {
|
|
58
|
-
createCanvas(width, height) {
|
|
94
|
+
createCanvas: function (width, height) {
|
|
59
95
|
if (!hasDocument())
|
|
60
96
|
return null;
|
|
61
97
|
try {
|
|
62
|
-
|
|
98
|
+
var canvas = document.createElement('canvas');
|
|
63
99
|
canvas.width = width;
|
|
64
100
|
canvas.height = height;
|
|
65
101
|
return canvas;
|
|
66
102
|
}
|
|
67
|
-
catch {
|
|
103
|
+
catch (_a) {
|
|
68
104
|
return null;
|
|
69
105
|
}
|
|
70
106
|
},
|
|
71
107
|
loadImage: loadImageElement,
|
|
72
|
-
createPath: (data)
|
|
73
|
-
measureText
|
|
108
|
+
createPath: function (data) { return new Path2D(data); },
|
|
109
|
+
measureText: measureText
|
|
74
110
|
};
|
|
75
111
|
}
|
|
76
112
|
/** Whether the person's keyboard says Cmd rather than Ctrl. */
|
|
77
113
|
export function isApplePlatform() {
|
|
114
|
+
var _a;
|
|
78
115
|
if (typeof navigator === 'undefined')
|
|
79
116
|
return false;
|
|
80
|
-
|
|
117
|
+
var platform = ((_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) || navigator.platform || navigator.userAgent || '';
|
|
81
118
|
return /Mac|iPhone|iPad|iPod/i.test(platform);
|
|
82
119
|
}
|
|
83
120
|
/** The longest side a picture keeps when it is large; a bigger one is scaled down before it is stored in the board. */
|
|
84
|
-
export
|
|
121
|
+
export var IMAGE_MAX_SIDE = 2048;
|
|
85
122
|
/** Pictures smaller than this are stored as they came. */
|
|
86
|
-
export
|
|
123
|
+
export var IMAGE_RESIZE_THRESHOLD_BYTES = 2.5 * 1024 * 1024;
|
|
87
124
|
/** What a picture without a size of its own (an SVG with no width and height) is given. */
|
|
88
|
-
export
|
|
125
|
+
export var IMAGE_FALLBACK_SIDE = 300;
|
|
89
126
|
function readAsDataUrl(file) {
|
|
90
|
-
return new Promise((resolve, reject)
|
|
91
|
-
|
|
92
|
-
reader.onload = ()
|
|
93
|
-
reader.onerror = ()
|
|
127
|
+
return new Promise(function (resolve, reject) {
|
|
128
|
+
var reader = new FileReader();
|
|
129
|
+
reader.onload = function () { return (typeof reader.result === 'string' ? resolve(reader.result) : reject(new Error('The file could not be read'))); };
|
|
130
|
+
reader.onerror = function () { var _a; return reject((_a = reader.error) !== null && _a !== void 0 ? _a : new Error('The file could not be read')); };
|
|
94
131
|
reader.readAsDataURL(file);
|
|
95
132
|
});
|
|
96
133
|
}
|
|
@@ -99,68 +136,82 @@ function readAsDataUrl(file) {
|
|
|
99
136
|
* `IMAGE_MAX_SIDE` so the saved board does not grow by megabytes; small files, and every SVG, are kept as they are.
|
|
100
137
|
* Rejects when the file is not a picture the browser can show.
|
|
101
138
|
*/
|
|
102
|
-
export
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
139
|
+
export function readImageFile(file_1) {
|
|
140
|
+
return __awaiter(this, arguments, Promise, function (file, fileName) {
|
|
141
|
+
var name, dataUrl, image, width, height, src, isSvg, tooBig, scale, scaledWidth, scaledHeight, canvas, context, keepsAlpha;
|
|
142
|
+
if (fileName === void 0) { fileName = ''; }
|
|
143
|
+
return __generator(this, function (_a) {
|
|
144
|
+
switch (_a.label) {
|
|
145
|
+
case 0:
|
|
146
|
+
name = fileName || file.name || '';
|
|
147
|
+
return [4 /*yield*/, readAsDataUrl(file)];
|
|
148
|
+
case 1:
|
|
149
|
+
dataUrl = _a.sent();
|
|
150
|
+
return [4 /*yield*/, loadImageElement(dataUrl)];
|
|
151
|
+
case 2:
|
|
152
|
+
image = _a.sent();
|
|
153
|
+
if (!image)
|
|
154
|
+
throw new Error('The file is not a picture the browser can show');
|
|
155
|
+
width = image.naturalWidth || image.width || IMAGE_FALLBACK_SIDE;
|
|
156
|
+
height = image.naturalHeight || image.height || IMAGE_FALLBACK_SIDE;
|
|
157
|
+
src = dataUrl;
|
|
158
|
+
isSvg = file.type === 'image/svg+xml' || /\.svg$/i.test(name);
|
|
159
|
+
tooBig = Math.max(width, height) > IMAGE_MAX_SIDE;
|
|
160
|
+
if (!isSvg && tooBig && file.size > IMAGE_RESIZE_THRESHOLD_BYTES && hasDocument()) {
|
|
161
|
+
try {
|
|
162
|
+
scale = IMAGE_MAX_SIDE / Math.max(width, height);
|
|
163
|
+
scaledWidth = Math.max(1, Math.round(width * scale));
|
|
164
|
+
scaledHeight = Math.max(1, Math.round(height * scale));
|
|
165
|
+
canvas = document.createElement('canvas');
|
|
166
|
+
canvas.width = scaledWidth;
|
|
167
|
+
canvas.height = scaledHeight;
|
|
168
|
+
context = canvas.getContext('2d');
|
|
169
|
+
if (context) {
|
|
170
|
+
context.drawImage(image, 0, 0, scaledWidth, scaledHeight);
|
|
171
|
+
keepsAlpha = /png|gif|webp/i.test(file.type);
|
|
172
|
+
src = canvas.toDataURL(keepsAlpha ? 'image/png' : 'image/jpeg', 0.88);
|
|
173
|
+
width = scaledWidth;
|
|
174
|
+
height = scaledHeight;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
console.warn('Canvas: the picture could not be scaled down, it is stored as it came', error);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return [2 /*return*/, { src: src, width: width, height: height, fileName: name }];
|
|
129
182
|
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
console.warn('Canvas: the picture could not be scaled down, it is stored as it came', error);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return { src, width, height, fileName: name };
|
|
183
|
+
});
|
|
184
|
+
});
|
|
136
185
|
}
|
|
137
186
|
/**
|
|
138
187
|
* Opens the browser's file chooser for pictures and answers with what was chosen (an empty list when the person
|
|
139
188
|
* cancels). Must be called while a click or a key press is being handled, or the browser refuses to open the chooser.
|
|
140
189
|
* The hidden input goes into the shadow root the canvas is in, so a partner's own page never sees it.
|
|
141
190
|
*/
|
|
142
|
-
export function chooseImageFiles(scope, options
|
|
191
|
+
export function chooseImageFiles(scope, options) {
|
|
192
|
+
if (options === void 0) { options = {}; }
|
|
143
193
|
if (!hasDocument())
|
|
144
194
|
return Promise.resolve([]);
|
|
145
|
-
return new Promise((resolve)
|
|
146
|
-
|
|
195
|
+
return new Promise(function (resolve) {
|
|
196
|
+
var _a;
|
|
197
|
+
var input = document.createElement('input');
|
|
147
198
|
input.type = 'file';
|
|
148
199
|
// Every kind of picture by default; the SVG entry of the shapes menu asks for vector files only.
|
|
149
|
-
input.accept = options.accept
|
|
200
|
+
input.accept = (_a = options.accept) !== null && _a !== void 0 ? _a : 'image/*,.svg';
|
|
150
201
|
input.multiple = !!options.multiple;
|
|
151
202
|
input.style.display = 'none';
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
203
|
+
var host = getTransientHostFor(scope);
|
|
204
|
+
var settled = false;
|
|
205
|
+
var finish = function (files) {
|
|
155
206
|
if (settled)
|
|
156
207
|
return;
|
|
157
208
|
settled = true;
|
|
158
209
|
input.remove();
|
|
159
210
|
resolve(files);
|
|
160
211
|
};
|
|
161
|
-
input.addEventListener('change', ()
|
|
212
|
+
input.addEventListener('change', function () { var _a; return finish(Array.from((_a = input.files) !== null && _a !== void 0 ? _a : [])); });
|
|
162
213
|
// The browsers that say so tell us when the chooser was closed without a file.
|
|
163
|
-
input.addEventListener('cancel', ()
|
|
214
|
+
input.addEventListener('cancel', function () { return finish([]); });
|
|
164
215
|
host.appendChild(input);
|
|
165
216
|
input.click();
|
|
166
217
|
});
|
|
@@ -9,117 +9,205 @@
|
|
|
9
9
|
*
|
|
10
10
|
* Plain TypeScript over the engine's records and the rasterizer: runs in Node.
|
|
11
11
|
*/
|
|
12
|
+
var __assign = (this && this.__assign) || function () {
|
|
13
|
+
__assign = Object.assign || function(t) {
|
|
14
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
15
|
+
s = arguments[i];
|
|
16
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
17
|
+
t[p] = s[p];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
return __assign.apply(this, arguments);
|
|
22
|
+
};
|
|
23
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
24
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
25
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
26
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
27
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
28
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
29
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
33
|
+
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);
|
|
34
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
35
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
36
|
+
function step(op) {
|
|
37
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
38
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
39
|
+
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;
|
|
40
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
41
|
+
switch (op[0]) {
|
|
42
|
+
case 0: case 1: t = op; break;
|
|
43
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
44
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
45
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
46
|
+
default:
|
|
47
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
48
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
49
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
50
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
51
|
+
if (t[2]) _.ops.pop();
|
|
52
|
+
_.trys.pop(); continue;
|
|
53
|
+
}
|
|
54
|
+
op = body.call(thisArg, _);
|
|
55
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
56
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
60
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
61
|
+
if (ar || !(i in from)) {
|
|
62
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
63
|
+
ar[i] = from[i];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
67
|
+
};
|
|
12
68
|
import { leafShapesOf } from '../../engine/core/scene.js';
|
|
13
69
|
import { rasterBoundsOf, renderShapesToDataUrl } from '../../engine/render/rasterize.js';
|
|
14
|
-
import {
|
|
70
|
+
import { selectionConversionBounds } from '../../engine/widgets.js';
|
|
15
71
|
import { hostCustomTypeOf, hostObjectById, hostTypeOf, selectedHostObject } from './hostObject.js';
|
|
16
72
|
/** Message key integrators pass through `translate` as `untitledShapeName` in {@link buildConversionPayload}. */
|
|
17
|
-
export
|
|
73
|
+
export var UNTITLED_SHAPE_MESSAGE_KEY = 'canvas.defaults.untitledShape';
|
|
18
74
|
/** The background of a screenshot: opaque, so a model that reads a transparent PNG as black still sees the drawing. */
|
|
19
|
-
export
|
|
75
|
+
export var SCREENSHOT_BACKGROUND = '#ffffff';
|
|
20
76
|
/** A screenshot is twice the size of the board (as the canvas it replaces made them), so small text stays legible. */
|
|
21
|
-
export
|
|
77
|
+
export var SCREENSHOT_SCALE = 2;
|
|
22
78
|
/** What is selected, as the conversion and the capture see it. Null when nothing is. */
|
|
23
79
|
export function describeSelection(engine) {
|
|
24
|
-
|
|
80
|
+
var _a;
|
|
81
|
+
var selectedIds = engine.state.selectedIds;
|
|
25
82
|
if (selectedIds.length === 0)
|
|
26
83
|
return null;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
84
|
+
var units = engine.selectedObjects();
|
|
85
|
+
var shapes = leafShapesOf(engine.state.scene, selectedIds);
|
|
86
|
+
var bounds = selectionConversionBounds(engine);
|
|
30
87
|
if (units.length === 0 || shapes.length === 0 || !bounds)
|
|
31
88
|
return null;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
89
|
+
var host = selectedHostObject(engine);
|
|
90
|
+
var name = host ? (host.id === '' ? host.type : ((_a = host._customType) !== null && _a !== void 0 ? _a : host.type)) : 'object';
|
|
91
|
+
var kinds = new Set(shapes.map(function (shape) { var _a; return (_a = hostCustomTypeOf(shape)) !== null && _a !== void 0 ? _a : hostTypeOf(shape); }));
|
|
35
92
|
return {
|
|
36
|
-
units,
|
|
37
|
-
shapes,
|
|
38
|
-
bounds,
|
|
39
|
-
name,
|
|
40
|
-
shapeTypes: [
|
|
41
|
-
hasImages: shapes.some((shape)
|
|
42
|
-
hasText: shapes.some((shape)
|
|
93
|
+
units: units,
|
|
94
|
+
shapes: shapes,
|
|
95
|
+
bounds: bounds,
|
|
96
|
+
name: name,
|
|
97
|
+
shapeTypes: __spreadArray([], kinds, true),
|
|
98
|
+
hasImages: shapes.some(function (shape) { return shape.type === 'image' && Boolean(shape.src || shape.imageRef); }),
|
|
99
|
+
hasText: shapes.some(function (shape) { return shape.type === 'text'; })
|
|
43
100
|
};
|
|
44
101
|
}
|
|
45
102
|
/** A PNG of some shapes on a plain background, cropped to what they cover. Empty when there is nothing to draw. */
|
|
46
|
-
export
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
103
|
+
export function screenshotOfShapes(shapes, deps) {
|
|
104
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
105
|
+
var error_1;
|
|
106
|
+
return __generator(this, function (_a) {
|
|
107
|
+
switch (_a.label) {
|
|
108
|
+
case 0:
|
|
109
|
+
if (shapes.length === 0 || !rasterBoundsOf(shapes))
|
|
110
|
+
return [2 /*return*/, ''];
|
|
111
|
+
_a.label = 1;
|
|
112
|
+
case 1:
|
|
113
|
+
_a.trys.push([1, 3, , 4]);
|
|
114
|
+
return [4 /*yield*/, renderShapesToDataUrl(shapes, deps.raster, {
|
|
115
|
+
scale: SCREENSHOT_SCALE,
|
|
116
|
+
background: SCREENSHOT_BACKGROUND,
|
|
117
|
+
widgetFallbackName: deps.widgetFallbackName
|
|
118
|
+
})];
|
|
119
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
120
|
+
case 3:
|
|
121
|
+
error_1 = _a.sent();
|
|
122
|
+
console.warn('Canvas: could not draw the screenshot', error_1);
|
|
123
|
+
return [2 /*return*/, ''];
|
|
124
|
+
case 4: return [2 /*return*/];
|
|
125
|
+
}
|
|
54
126
|
});
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
console.warn('Canvas: could not draw the screenshot', error);
|
|
58
|
-
return '';
|
|
59
|
-
}
|
|
127
|
+
});
|
|
60
128
|
}
|
|
61
129
|
/** What the host needs to turn a selection into a widget, or null when nothing is selected. */
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
130
|
+
export function buildConversionPayload(deps) {
|
|
131
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
132
|
+
var engine, conversions, selection, single, frameId, screenshot, frame;
|
|
133
|
+
return __generator(this, function (_a) {
|
|
134
|
+
switch (_a.label) {
|
|
135
|
+
case 0:
|
|
136
|
+
engine = deps.engine, conversions = deps.conversions;
|
|
137
|
+
selection = describeSelection(engine);
|
|
138
|
+
if (!selection)
|
|
139
|
+
return [2 /*return*/, null];
|
|
140
|
+
single = selection.units.length === 1 ? selection.units[0] : null;
|
|
141
|
+
frameId = single ? single.id : engine.services.createId('selection');
|
|
142
|
+
if (!single) {
|
|
143
|
+
conversions.remember(frameId, { unitIds: selection.units.map(function (unit) { return unit.id; }), bounds: selection.bounds });
|
|
144
|
+
}
|
|
145
|
+
return [4 /*yield*/, screenshotOfShapes(selection.shapes, deps)];
|
|
146
|
+
case 1:
|
|
147
|
+
screenshot = _a.sent();
|
|
148
|
+
frame = single ? hostObjectById(engine, single.id) : selectedHostObject(engine);
|
|
149
|
+
return [2 /*return*/, {
|
|
150
|
+
frameName: selection.name || deps.untitledShapeName,
|
|
151
|
+
frameId: frameId,
|
|
152
|
+
screenshot: screenshot,
|
|
153
|
+
canvasData: {
|
|
154
|
+
frame: frame ? __assign({}, frame) : {},
|
|
155
|
+
children: [],
|
|
156
|
+
bounds: { left: selection.bounds.x, top: selection.bounds.y, width: selection.bounds.width, height: selection.bounds.height }
|
|
157
|
+
},
|
|
158
|
+
metadata: {
|
|
159
|
+
childCount: selection.shapes.length,
|
|
160
|
+
shapeTypes: selection.shapeTypes,
|
|
161
|
+
hasImages: selection.hasImages,
|
|
162
|
+
hasText: selection.hasText
|
|
163
|
+
}
|
|
164
|
+
}];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
92
168
|
}
|
|
93
169
|
/** The extra facts the host is given about what was captured. */
|
|
94
170
|
function metadataOf(selection) {
|
|
95
|
-
|
|
96
|
-
|
|
171
|
+
var _a, _b, _c;
|
|
172
|
+
var metadata = {};
|
|
173
|
+
var only = selection.shapes.length === 1 && selection.units.length === 1 ? selection.shapes[0] : null;
|
|
97
174
|
if (only && only.type === 'image') {
|
|
98
175
|
metadata.hasImage = Boolean(only.src || only.imageRef);
|
|
99
|
-
metadata.imageUrl = only.src
|
|
100
|
-
metadata.originalFilename = only.fileName
|
|
176
|
+
metadata.imageUrl = (_a = only.src) !== null && _a !== void 0 ? _a : null;
|
|
177
|
+
metadata.originalFilename = (_b = only.fileName) !== null && _b !== void 0 ? _b : null;
|
|
101
178
|
}
|
|
102
179
|
if (only && only.type === 'text')
|
|
103
180
|
metadata.text = only.text;
|
|
104
|
-
if (selection.units.length > 1 || selection.units[0]
|
|
181
|
+
if (selection.units.length > 1 || ((_c = selection.units[0]) === null || _c === void 0 ? void 0 : _c.type) === 'group')
|
|
105
182
|
metadata.objectCount = selection.shapes.length;
|
|
106
183
|
return metadata;
|
|
107
184
|
}
|
|
108
185
|
/** What the host is given when a selection is captured (the "capture" button of the selection panel). */
|
|
109
|
-
export
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
186
|
+
export function buildCapturePayload(deps) {
|
|
187
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
188
|
+
var engine, selection, single, screenshot, host;
|
|
189
|
+
var _a, _b;
|
|
190
|
+
return __generator(this, function (_c) {
|
|
191
|
+
switch (_c.label) {
|
|
192
|
+
case 0:
|
|
193
|
+
engine = deps.engine;
|
|
194
|
+
selection = describeSelection(engine);
|
|
195
|
+
if (!selection)
|
|
196
|
+
return [2 /*return*/, null];
|
|
197
|
+
single = selection.units.length === 1 ? selection.units[0] : null;
|
|
198
|
+
return [4 /*yield*/, screenshotOfShapes(selection.shapes, deps)];
|
|
199
|
+
case 1:
|
|
200
|
+
screenshot = _c.sent();
|
|
201
|
+
host = selectedHostObject(engine);
|
|
202
|
+
return [2 /*return*/, {
|
|
203
|
+
screenshot: screenshot,
|
|
204
|
+
objectType: (_a = host === null || host === void 0 ? void 0 : host.type) !== null && _a !== void 0 ? _a : 'unknown',
|
|
205
|
+
customType: (_b = host === null || host === void 0 ? void 0 : host._customType) !== null && _b !== void 0 ? _b : undefined,
|
|
206
|
+
bounds: { left: selection.bounds.x, top: selection.bounds.y, width: selection.bounds.width, height: selection.bounds.height },
|
|
207
|
+
objectId: single ? single.id : engine.services.createId('selection'),
|
|
208
|
+
metadata: metadataOf(selection)
|
|
209
|
+
}];
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
});
|
|
125
213
|
}
|