@widgetic/canvas 0.5.43 → 0.5.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/dist/canvas/DomCanvas.svelte +56 -3
  2. package/dist/canvas/DomCanvas.svelte.d.ts +59 -28
  3. package/dist/canvas/autosaveScheduler.js +89 -37
  4. package/dist/canvas/boardFrame.js +1 -1
  5. package/dist/canvas/boardSurface.js +1 -1
  6. package/dist/canvas/canvasLogger.js +19 -7
  7. package/dist/canvas/chromeLayout.js +21 -19
  8. package/dist/canvas/eventScope.js +14 -10
  9. package/dist/canvas/host/CanvasHost.d.ts +87 -2
  10. package/dist/canvas/host/CanvasHost.js +1948 -1394
  11. package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
  12. package/dist/canvas/host/CanvasHostApi.js +32 -5
  13. package/dist/canvas/host/boardFiles.d.ts +90 -0
  14. package/dist/canvas/host/boardFiles.js +155 -0
  15. package/dist/canvas/host/browserServices.d.ts +17 -0
  16. package/dist/canvas/host/browserServices.js +211 -65
  17. package/dist/canvas/host/conversion.js +170 -82
  18. package/dist/canvas/host/hostObject.js +7 -7
  19. package/dist/canvas/host/nearCards.js +21 -18
  20. package/dist/canvas/host/pageEvents.js +5 -5
  21. package/dist/canvas/host/pictureSaves.js +11 -9
  22. package/dist/canvas/host/screenRects.js +31 -28
  23. package/dist/canvas/host/storedPictures.js +243 -149
  24. package/dist/canvas/host/strokeSnap.js +143 -88
  25. package/dist/canvas/index.d.ts +2 -0
  26. package/dist/canvas/openMarks.js +2 -2
  27. package/dist/canvas/outsideClickDeselect.js +7 -6
  28. package/dist/canvas/panZoomPosition.js +2 -2
  29. package/dist/canvas/preferredTextFont.js +25 -13
  30. package/dist/canvas/shapes/ShapeLibrary.js +148 -138
  31. package/dist/canvas/shapes/shapeImageStore.js +4 -4
  32. package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
  33. package/dist/canvas/toolbarPosition.js +2 -2
  34. package/dist/canvas/types.d.ts +17 -0
  35. package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
  36. package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
  37. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
  38. package/dist/canvas/ui/ColorField.svelte +7 -2
  39. package/dist/canvas/ui/SelectionPanel.svelte +105 -42
  40. package/dist/canvas/ui/ShapesMenu.svelte +65 -31
  41. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
  42. package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
  43. package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
  44. package/dist/canvas/ui/ToolButton.svelte +35 -54
  45. package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
  46. package/dist/canvas/ui/colorModel.js +32 -21
  47. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  48. package/dist/canvas/ui/floatingCard.d.ts +26 -4
  49. package/dist/canvas/ui/floatingCard.js +43 -21
  50. package/dist/canvas/ui/floatingLayer.js +6 -5
  51. package/dist/canvas/ui/icons.d.ts +1 -1
  52. package/dist/canvas/ui/icons.js +1 -1
  53. package/dist/canvas/ui/keyHints.js +11 -9
  54. package/dist/canvas/ui/popoverSide.js +3 -2
  55. package/dist/canvas/ui/selectionTraits.d.ts +12 -0
  56. package/dist/canvas/ui/selectionTraits.js +66 -35
  57. package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
  58. package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
  59. package/dist/canvas/view/CanvasStage.svelte +7 -2
  60. package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
  61. package/dist/canvas/view/SelectionOverlay.svelte +17 -10
  62. package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
  63. package/dist/canvas/view/erasingMarks.js +7 -7
  64. package/dist/canvas/view/geometry.d.ts +1 -1
  65. package/dist/canvas/view/geometry.js +90 -71
  66. package/dist/canvas/view/liftedScene.js +36 -24
  67. package/dist/canvas/view/mountedScene.js +79 -76
  68. package/dist/components/CanvasBoardPoster.svelte +206 -0
  69. package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
  70. package/dist/components/CanvasToolbarPlaceholder.svelte +115 -0
  71. package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +9 -0
  72. package/dist/components/index.d.ts +7 -0
  73. package/dist/components/index.js +8 -0
  74. package/dist/components/preferenceStorage.js +2 -2
  75. package/dist/engine/EngineCore.d.ts +111 -2
  76. package/dist/engine/EngineCore.js +1013 -707
  77. package/dist/engine/codec/canvasDocument.d.ts +7 -0
  78. package/dist/engine/codec/canvasDocument.js +172 -186
  79. package/dist/engine/codec/hostReadableKeys.js +1 -1
  80. package/dist/engine/core/arrange.js +38 -26
  81. package/dist/engine/core/arrow.d.ts +18 -8
  82. package/dist/engine/core/arrow.js +109 -84
  83. package/dist/engine/core/arrowBinding.d.ts +1 -1
  84. package/dist/engine/core/arrowBinding.js +126 -101
  85. package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
  86. package/dist/engine/core/arrowBindingRecords.js +48 -29
  87. package/dist/engine/core/brush.d.ts +8 -0
  88. package/dist/engine/core/brush.js +98 -78
  89. package/dist/engine/core/cameraMove.d.ts +73 -0
  90. package/dist/engine/core/cameraMove.js +172 -0
  91. package/dist/engine/core/defaults.js +51 -90
  92. package/dist/engine/core/freehand.js +163 -147
  93. package/dist/engine/core/geometry.js +54 -47
  94. package/dist/engine/core/history.js +55 -38
  95. package/dist/engine/core/hitTest.js +81 -68
  96. package/dist/engine/core/matrix.js +20 -18
  97. package/dist/engine/core/pathData.js +210 -183
  98. package/dist/engine/core/rotationReset.js +6 -5
  99. package/dist/engine/core/scene.d.ts +22 -0
  100. package/dist/engine/core/scene.js +288 -139
  101. package/dist/engine/core/scribble.js +84 -59
  102. package/dist/engine/core/shapePaths.js +15 -12
  103. package/dist/engine/core/shapeTools.js +63 -72
  104. package/dist/engine/core/snapping.js +117 -65
  105. package/dist/engine/core/style.d.ts +10 -0
  106. package/dist/engine/core/style.js +82 -33
  107. package/dist/engine/core/transform.d.ts +6 -0
  108. package/dist/engine/core/transform.js +103 -83
  109. package/dist/engine/core/types.d.ts +7 -0
  110. package/dist/engine/core/types.js +4 -4
  111. package/dist/engine/core/viewport.js +36 -23
  112. package/dist/engine/input/PointerController.d.ts +15 -1
  113. package/dist/engine/input/PointerController.js +549 -466
  114. package/dist/engine/input/arrowHandles.js +12 -12
  115. package/dist/engine/input/gestures.d.ts +3 -1
  116. package/dist/engine/input/gestures.js +340 -266
  117. package/dist/engine/input/hoverCursor.js +1 -1
  118. package/dist/engine/input/keyboard.d.ts +22 -3
  119. package/dist/engine/input/keyboard.js +131 -32
  120. package/dist/engine/input/selectionHandles.js +50 -45
  121. package/dist/engine/input/types.d.ts +7 -0
  122. package/dist/engine/input/types.js +1 -1
  123. package/dist/engine/render/rasterize.js +158 -86
  124. package/dist/engine/state.d.ts +18 -0
  125. package/dist/engine/state.js +25 -4
  126. package/dist/engine/text/layoutText.js +62 -43
  127. package/dist/engine/text/textMetrics.js +10 -8
  128. package/dist/engine/widgets.js +137 -101
  129. package/dist/fonts/interFont.js +83 -30
  130. package/dist/fonts/workspaceFonts.d.ts +6 -0
  131. package/dist/fonts/workspaceFonts.js +108 -34
  132. package/dist/i18n/labels.js +4 -4
  133. package/dist/i18n/language.d.ts +3 -3
  134. package/dist/i18n/language.js +4 -4
  135. package/dist/i18n/messages.d.ts +6 -0
  136. package/dist/i18n/messages.js +7 -1
  137. package/dist/i18n/shortcuts.d.ts +6 -4
  138. package/dist/i18n/shortcuts.js +30 -26
  139. package/dist/index.d.ts +2 -1
  140. package/dist/poster/boardPlacement.d.ts +80 -0
  141. package/dist/poster/boardPlacement.js +140 -0
  142. package/dist/poster/boardPoster.d.ts +80 -0
  143. package/dist/poster/boardPoster.js +251 -0
  144. package/dist/poster/paintBoardPoster.d.ts +49 -0
  145. package/dist/poster/paintBoardPoster.js +112 -0
  146. package/dist/poster/posterPictures.d.ts +32 -0
  147. package/dist/poster/posterPictures.js +138 -0
  148. package/dist/settings/workspaceToolSettings.d.ts +6 -0
  149. package/dist/settings/workspaceToolSettings.js +58 -34
  150. package/dist/tools/components/catalog.js +74 -64
  151. package/dist/tools/components/grouping.js +28 -6
  152. package/dist/tools/definitions.js +31 -9
  153. package/dist/tools/executors.js +214 -106
  154. package/dist/tools/hostAdapter.d.ts +5 -1
  155. package/dist/tools/hostAdapter.js +15 -11
  156. package/dist/tools/sceneSummary.js +57 -43
  157. package/dist/tools/sketchIntent.js +21 -13
  158. package/dist/tools/styleArguments.d.ts +35 -0
  159. package/dist/tools/styleArguments.js +93 -0
  160. package/dist/tools/types.d.ts +26 -1
  161. package/dist/tools/types.js +2 -1
  162. package/package.json +1 -1
@@ -6,21 +6,69 @@
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 __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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ };
29
+ var __generator = (this && this.__generator) || function (thisArg, body) {
30
+ 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);
31
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
32
+ function verb(n) { return function (v) { return step([n, v]); }; }
33
+ function step(op) {
34
+ if (f) throw new TypeError("Generator is already executing.");
35
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
36
+ 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;
37
+ if (y = 0, t) op = [op[0] & 2, t.value];
38
+ switch (op[0]) {
39
+ case 0: case 1: t = op; break;
40
+ case 4: _.label++; return { value: op[1], done: false };
41
+ case 5: _.label++; y = op[1]; op = [0]; continue;
42
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
43
+ default:
44
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
45
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
46
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
47
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
48
+ if (t[2]) _.ops.pop();
49
+ _.trys.pop(); continue;
50
+ }
51
+ op = body.call(thisArg, _);
52
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
53
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
54
+ }
55
+ };
9
56
  import { createFixedAdvanceMeasure } from '../../engine/text/layoutText.js';
10
57
  import { getTransientHostFor } from '../eventScope.js';
11
- const hasDocument = () => typeof document !== 'undefined' && typeof document.createElement === 'function';
58
+ import { pictureMimeTypeOf } from './boardFiles.js';
59
+ var hasDocument = function () { return typeof document !== 'undefined' && typeof document.createElement === 'function'; };
12
60
  /** A text measurer that uses the browser's canvas, or a fixed advance where there is none (the server, a test). */
13
61
  export function createBrowserMeasureText() {
14
62
  if (!hasDocument())
15
63
  return createFixedAdvanceMeasure();
16
- let context;
17
- const fallback = createFixedAdvanceMeasure();
18
- return (text, font) => {
64
+ var context;
65
+ var fallback = createFixedAdvanceMeasure();
66
+ return function (text, font) {
19
67
  if (context === undefined) {
20
68
  try {
21
69
  context = document.createElement('canvas').getContext('2d');
22
70
  }
23
- catch {
71
+ catch (_a) {
24
72
  context = null;
25
73
  }
26
74
  }
@@ -35,62 +83,63 @@ export function createBrowserMeasureText() {
35
83
  export function loadImageElement(source) {
36
84
  if (typeof Image === 'undefined')
37
85
  return Promise.resolve(null);
38
- return new Promise((resolve) => {
39
- const image = new Image();
86
+ return new Promise(function (resolve) {
87
+ var image = new Image();
40
88
  // A picture from another site is only usable in a canvas when that site allows it; a data address needs nothing.
41
89
  if (/^https?:/i.test(source))
42
90
  image.crossOrigin = 'anonymous';
43
- image.onload = () => {
91
+ image.onload = function () {
44
92
  if (typeof image.decode === 'function') {
45
- image.decode().then(() => resolve(image), () => resolve(image.naturalWidth > 0 ? image : null));
93
+ image.decode().then(function () { return resolve(image); }, function () { return resolve(image.naturalWidth > 0 ? image : null); });
46
94
  }
47
95
  else {
48
96
  resolve(image);
49
97
  }
50
98
  };
51
- image.onerror = () => resolve(null);
99
+ image.onerror = function () { return resolve(null); };
52
100
  image.src = source;
53
101
  });
54
102
  }
55
103
  /** The services the rasterizer needs, made of the browser. */
56
104
  export function createBrowserRasterServices(measureText) {
57
105
  return {
58
- createCanvas(width, height) {
106
+ createCanvas: function (width, height) {
59
107
  if (!hasDocument())
60
108
  return null;
61
109
  try {
62
- const canvas = document.createElement('canvas');
110
+ var canvas = document.createElement('canvas');
63
111
  canvas.width = width;
64
112
  canvas.height = height;
65
113
  return canvas;
66
114
  }
67
- catch {
115
+ catch (_a) {
68
116
  return null;
69
117
  }
70
118
  },
71
119
  loadImage: loadImageElement,
72
- createPath: (data) => new Path2D(data),
73
- measureText
120
+ createPath: function (data) { return new Path2D(data); },
121
+ measureText: measureText
74
122
  };
75
123
  }
76
124
  /** Whether the person's keyboard says Cmd rather than Ctrl. */
77
125
  export function isApplePlatform() {
126
+ var _a;
78
127
  if (typeof navigator === 'undefined')
79
128
  return false;
80
- const platform = navigator.userAgentData?.platform || navigator.platform || navigator.userAgent || '';
129
+ var platform = ((_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) || navigator.platform || navigator.userAgent || '';
81
130
  return /Mac|iPhone|iPad|iPod/i.test(platform);
82
131
  }
83
132
  /** 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 const IMAGE_MAX_SIDE = 2048;
133
+ export var IMAGE_MAX_SIDE = 2048;
85
134
  /** Pictures smaller than this are stored as they came. */
86
- export const IMAGE_RESIZE_THRESHOLD_BYTES = 2.5 * 1024 * 1024;
135
+ export var IMAGE_RESIZE_THRESHOLD_BYTES = 2.5 * 1024 * 1024;
87
136
  /** What a picture without a size of its own (an SVG with no width and height) is given. */
88
- export const IMAGE_FALLBACK_SIDE = 300;
137
+ export var IMAGE_FALLBACK_SIDE = 300;
89
138
  function readAsDataUrl(file) {
90
- return new Promise((resolve, reject) => {
91
- const reader = new FileReader();
92
- reader.onload = () => (typeof reader.result === 'string' ? resolve(reader.result) : reject(new Error('The file could not be read')));
93
- reader.onerror = () => reject(reader.error ?? new Error('The file could not be read'));
139
+ return new Promise(function (resolve, reject) {
140
+ var reader = new FileReader();
141
+ reader.onload = function () { return (typeof reader.result === 'string' ? resolve(reader.result) : reject(new Error('The file could not be read'))); };
142
+ reader.onerror = function () { var _a; return reject((_a = reader.error) !== null && _a !== void 0 ? _a : new Error('The file could not be read')); };
94
143
  reader.readAsDataURL(file);
95
144
  });
96
145
  }
@@ -99,68 +148,165 @@ function readAsDataUrl(file) {
99
148
  * `IMAGE_MAX_SIDE` so the saved board does not grow by megabytes; small files, and every SVG, are kept as they are.
100
149
  * Rejects when the file is not a picture the browser can show.
101
150
  */
102
- export async function readImageFile(file, fileName = '') {
103
- const name = fileName || file.name || '';
104
- const dataUrl = await readAsDataUrl(file);
105
- const image = await loadImageElement(dataUrl);
106
- if (!image)
107
- throw new Error('The file is not a picture the browser can show');
108
- let width = image.naturalWidth || image.width || IMAGE_FALLBACK_SIDE;
109
- let height = image.naturalHeight || image.height || IMAGE_FALLBACK_SIDE;
110
- let src = dataUrl;
111
- const isSvg = file.type === 'image/svg+xml' || /\.svg$/i.test(name);
112
- const tooBig = Math.max(width, height) > IMAGE_MAX_SIDE;
113
- if (!isSvg && tooBig && file.size > IMAGE_RESIZE_THRESHOLD_BYTES && hasDocument()) {
114
- try {
115
- const scale = IMAGE_MAX_SIDE / Math.max(width, height);
116
- const scaledWidth = Math.max(1, Math.round(width * scale));
117
- const scaledHeight = Math.max(1, Math.round(height * scale));
118
- const canvas = document.createElement('canvas');
119
- canvas.width = scaledWidth;
120
- canvas.height = scaledHeight;
121
- const context = canvas.getContext('2d');
122
- if (context) {
123
- context.drawImage(image, 0, 0, scaledWidth, scaledHeight);
124
- // Pictures that can have see-through parts stay PNG; photos become JPEG.
125
- const keepsAlpha = /png|gif|webp/i.test(file.type);
126
- src = canvas.toDataURL(keepsAlpha ? 'image/png' : 'image/jpeg', 0.88);
127
- width = scaledWidth;
128
- height = scaledHeight;
151
+ export function readImageFile(file_1) {
152
+ return __awaiter(this, arguments, Promise, function (file, fileName) {
153
+ var name, dataUrl, image, width, height, src, isSvg, tooBig, scale, scaledWidth, scaledHeight, canvas, context, keepsAlpha;
154
+ if (fileName === void 0) { fileName = ''; }
155
+ return __generator(this, function (_a) {
156
+ switch (_a.label) {
157
+ case 0:
158
+ name = fileName || file.name || '';
159
+ return [4 /*yield*/, readAsDataUrl(file)];
160
+ case 1:
161
+ dataUrl = _a.sent();
162
+ return [4 /*yield*/, loadImageElement(dataUrl)];
163
+ case 2:
164
+ image = _a.sent();
165
+ if (!image)
166
+ throw new Error('The file is not a picture the browser can show');
167
+ width = image.naturalWidth || image.width || IMAGE_FALLBACK_SIDE;
168
+ height = image.naturalHeight || image.height || IMAGE_FALLBACK_SIDE;
169
+ src = dataUrl;
170
+ isSvg = file.type === 'image/svg+xml' || /\.svg$/i.test(name);
171
+ tooBig = Math.max(width, height) > IMAGE_MAX_SIDE;
172
+ if (!isSvg && tooBig && file.size > IMAGE_RESIZE_THRESHOLD_BYTES && hasDocument()) {
173
+ try {
174
+ scale = IMAGE_MAX_SIDE / Math.max(width, height);
175
+ scaledWidth = Math.max(1, Math.round(width * scale));
176
+ scaledHeight = Math.max(1, Math.round(height * scale));
177
+ canvas = document.createElement('canvas');
178
+ canvas.width = scaledWidth;
179
+ canvas.height = scaledHeight;
180
+ context = canvas.getContext('2d');
181
+ if (context) {
182
+ context.drawImage(image, 0, 0, scaledWidth, scaledHeight);
183
+ keepsAlpha = /png|gif|webp/i.test(file.type);
184
+ src = canvas.toDataURL(keepsAlpha ? 'image/png' : 'image/jpeg', 0.88);
185
+ width = scaledWidth;
186
+ height = scaledHeight;
187
+ }
188
+ }
189
+ catch (error) {
190
+ console.warn('Canvas: the picture could not be scaled down, it is stored as it came', error);
191
+ }
192
+ }
193
+ return [2 /*return*/, { src: src, width: width, height: height, fileName: name }];
129
194
  }
130
- }
131
- catch (error) {
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 };
195
+ });
196
+ });
197
+ }
198
+ /** How long the library gets to hand over a picture's bytes before the board settles for its address. */
199
+ export var LIBRARY_PICTURE_TIMEOUT_MS = 10000;
200
+ /** A library picture bigger than this is not read into the board: the board holds its address (a photo of 40 MB is not board material). */
201
+ export var LIBRARY_PICTURE_MAX_BYTES = 30 * 1024 * 1024;
202
+ /** The size of the picture behind an address: read with the CORS mode first, then as a plain picture (to be shown, not to be drawn on a canvas). */
203
+ function measureImageAddress(source) {
204
+ return __awaiter(this, void 0, Promise, function () {
205
+ var image;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0: return [4 /*yield*/, loadImageElement(source)];
209
+ case 1:
210
+ image = _a.sent();
211
+ if (image)
212
+ return [2 /*return*/, { width: image.naturalWidth || IMAGE_FALLBACK_SIDE, height: image.naturalHeight || IMAGE_FALLBACK_SIDE }];
213
+ if (typeof Image === 'undefined')
214
+ return [2 /*return*/, null];
215
+ return [2 /*return*/, new Promise(function (resolve) {
216
+ var plain = new Image();
217
+ plain.onload = function () { return resolve({ width: plain.naturalWidth || IMAGE_FALLBACK_SIDE, height: plain.naturalHeight || IMAGE_FALLBACK_SIDE }); };
218
+ plain.onerror = function () { return resolve(null); };
219
+ plain.src = source;
220
+ })];
221
+ }
222
+ });
223
+ });
224
+ }
225
+ /**
226
+ * Reads a file of the person's library into a picture for the board, remembering where the library keeps it.
227
+ *
228
+ * The bytes are fetched when the library lets the page (CORS), so the board and the screenshots of it do not depend on the library
229
+ * being reachable, and a large photo is scaled down like any picture the person drops. When the bytes cannot be had (no CORS, an
230
+ * error, too slow, too big) the board holds the file's address instead: it still shows the picture, and the library stays the owner.
231
+ * Null when the browser cannot show the file at all.
232
+ */
233
+ export function readLibraryPicture(file) {
234
+ return __awaiter(this, void 0, Promise, function () {
235
+ var library, controller_1, timer, response, blob, typed, _a, error_1, size;
236
+ var _b;
237
+ return __generator(this, function (_c) {
238
+ switch (_c.label) {
239
+ case 0:
240
+ library = { libraryFileId: file.id, libraryFileUrl: file.url };
241
+ if (!(typeof fetch === 'function')) return [3 /*break*/, 7];
242
+ controller_1 = typeof AbortController === 'function' ? new AbortController() : null;
243
+ timer = controller_1 ? setTimeout(function () { return controller_1.abort(); }, LIBRARY_PICTURE_TIMEOUT_MS) : null;
244
+ _c.label = 1;
245
+ case 1:
246
+ _c.trys.push([1, 5, 6, 7]);
247
+ return [4 /*yield*/, fetch(file.url, controller_1 ? { signal: controller_1.signal } : undefined)];
248
+ case 2:
249
+ response = _c.sent();
250
+ if (!response.ok)
251
+ throw new Error("The library answered ".concat(response.status));
252
+ return [4 /*yield*/, response.blob()];
253
+ case 3:
254
+ blob = _c.sent();
255
+ if (blob.size > LIBRARY_PICTURE_MAX_BYTES)
256
+ throw new Error('The file is too big to be kept in the board');
257
+ typed = new Blob([blob], { type: pictureMimeTypeOf({ name: file.name, url: file.url, mimeType: (_b = file.mimeType) !== null && _b !== void 0 ? _b : (blob.type || null) }) });
258
+ _a = [{}];
259
+ return [4 /*yield*/, readImageFile(typed, file.name)];
260
+ case 4: return [2 /*return*/, __assign.apply(void 0, [__assign.apply(void 0, _a.concat([(_c.sent())])), library])];
261
+ case 5:
262
+ error_1 = _c.sent();
263
+ console.warn('Canvas: the library file could not be read into the board, it is shown from its address', file.name, error_1);
264
+ return [3 /*break*/, 7];
265
+ case 6:
266
+ if (timer)
267
+ clearTimeout(timer);
268
+ return [7 /*endfinally*/];
269
+ case 7: return [4 /*yield*/, measureImageAddress(file.url)];
270
+ case 8:
271
+ size = _c.sent();
272
+ if (!size) {
273
+ console.warn('Canvas: the library file is not a picture the browser can show', file.name);
274
+ return [2 /*return*/, null];
275
+ }
276
+ return [2 /*return*/, __assign({ src: file.url, width: size.width, height: size.height, fileName: file.name }, library)];
277
+ }
278
+ });
279
+ });
136
280
  }
137
281
  /**
138
282
  * Opens the browser's file chooser for pictures and answers with what was chosen (an empty list when the person
139
283
  * cancels). Must be called while a click or a key press is being handled, or the browser refuses to open the chooser.
140
284
  * The hidden input goes into the shadow root the canvas is in, so a partner's own page never sees it.
141
285
  */
142
- export function chooseImageFiles(scope, options = {}) {
286
+ export function chooseImageFiles(scope, options) {
287
+ if (options === void 0) { options = {}; }
143
288
  if (!hasDocument())
144
289
  return Promise.resolve([]);
145
- return new Promise((resolve) => {
146
- const input = document.createElement('input');
290
+ return new Promise(function (resolve) {
291
+ var _a;
292
+ var input = document.createElement('input');
147
293
  input.type = 'file';
148
294
  // Every kind of picture by default; the SVG entry of the shapes menu asks for vector files only.
149
- input.accept = options.accept ?? 'image/*,.svg';
295
+ input.accept = (_a = options.accept) !== null && _a !== void 0 ? _a : 'image/*,.svg';
150
296
  input.multiple = !!options.multiple;
151
297
  input.style.display = 'none';
152
- const host = getTransientHostFor(scope);
153
- let settled = false;
154
- const finish = (files) => {
298
+ var host = getTransientHostFor(scope);
299
+ var settled = false;
300
+ var finish = function (files) {
155
301
  if (settled)
156
302
  return;
157
303
  settled = true;
158
304
  input.remove();
159
305
  resolve(files);
160
306
  };
161
- input.addEventListener('change', () => finish(Array.from(input.files ?? [])));
307
+ input.addEventListener('change', function () { var _a; return finish(Array.from((_a = input.files) !== null && _a !== void 0 ? _a : [])); });
162
308
  // The browsers that say so tell us when the chooser was closed without a file.
163
- input.addEventListener('cancel', () => finish([]));
309
+ input.addEventListener('cancel', function () { return finish([]); });
164
310
  host.appendChild(input);
165
311
  input.click();
166
312
  });