@widgetic/canvas 0.5.45 → 0.5.46

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 (113) hide show
  1. package/README.md +1 -1
  2. package/dist/canvas/autosaveScheduler.js +37 -89
  3. package/dist/canvas/boardFrame.js +1 -1
  4. package/dist/canvas/boardSurface.js +1 -1
  5. package/dist/canvas/canvasLogger.js +7 -19
  6. package/dist/canvas/chromeLayout.js +19 -21
  7. package/dist/canvas/eventScope.js +10 -14
  8. package/dist/canvas/host/CanvasHost.d.ts +6 -2
  9. package/dist/canvas/host/CanvasHost.js +1615 -1847
  10. package/dist/canvas/host/CanvasHostApi.js +4 -19
  11. package/dist/canvas/host/boardFiles.js +35 -51
  12. package/dist/canvas/host/browserServices.js +108 -196
  13. package/dist/canvas/host/conversion.js +82 -170
  14. package/dist/canvas/host/hostObject.js +7 -7
  15. package/dist/canvas/host/nearCards.js +18 -21
  16. package/dist/canvas/host/pageEvents.js +5 -5
  17. package/dist/canvas/host/pictureSaves.js +9 -11
  18. package/dist/canvas/host/screenRects.js +28 -31
  19. package/dist/canvas/host/storedPictures.js +149 -243
  20. package/dist/canvas/host/strokeSnap.js +88 -143
  21. package/dist/canvas/index.d.ts +1 -1
  22. package/dist/canvas/openMarks.js +2 -2
  23. package/dist/canvas/outsideClickDeselect.js +6 -7
  24. package/dist/canvas/panZoomPosition.js +2 -2
  25. package/dist/canvas/preferredTextFont.js +13 -25
  26. package/dist/canvas/shapes/ShapeLibrary.js +138 -148
  27. package/dist/canvas/shapes/shapeImageStore.js +4 -4
  28. package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
  29. package/dist/canvas/toolbarPosition.js +2 -2
  30. package/dist/canvas/types.d.ts +7 -1
  31. package/dist/canvas/ui/colorModel.js +21 -32
  32. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  33. package/dist/canvas/ui/floatingCard.js +21 -21
  34. package/dist/canvas/ui/floatingLayer.js +5 -6
  35. package/dist/canvas/ui/keyHints.js +9 -11
  36. package/dist/canvas/ui/popoverSide.js +2 -3
  37. package/dist/canvas/ui/selectionTraits.js +35 -41
  38. package/dist/canvas/view/CanvasStage.svelte +1 -1
  39. package/dist/canvas/view/erasingMarks.js +7 -7
  40. package/dist/canvas/view/geometry.js +71 -90
  41. package/dist/canvas/view/liftedScene.js +24 -36
  42. package/dist/canvas/view/mountedScene.js +76 -79
  43. package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
  44. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
  45. package/dist/components/CanvasToolbarPlaceholder.svelte +25 -11
  46. package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +6 -0
  47. package/dist/components/boardLoaderDelay.d.ts +39 -0
  48. package/dist/components/boardLoaderDelay.js +71 -0
  49. package/dist/components/preferenceStorage.js +2 -2
  50. package/dist/engine/EngineCore.d.ts +9 -2
  51. package/dist/engine/EngineCore.js +798 -907
  52. package/dist/engine/codec/canvasDocument.js +192 -155
  53. package/dist/engine/codec/hostReadableKeys.js +1 -1
  54. package/dist/engine/core/arrange.js +26 -38
  55. package/dist/engine/core/arrow.js +79 -92
  56. package/dist/engine/core/arrowBinding.js +98 -116
  57. package/dist/engine/core/arrowBindingRecords.js +23 -38
  58. package/dist/engine/core/brush.js +84 -96
  59. package/dist/engine/core/cameraMove.js +39 -51
  60. package/dist/engine/core/defaults.js +92 -51
  61. package/dist/engine/core/freehand.js +147 -163
  62. package/dist/engine/core/geometry.js +47 -54
  63. package/dist/engine/core/history.js +38 -55
  64. package/dist/engine/core/hitTest.js +68 -81
  65. package/dist/engine/core/matrix.js +18 -20
  66. package/dist/engine/core/pathData.js +183 -210
  67. package/dist/engine/core/rotationReset.js +5 -6
  68. package/dist/engine/core/scene.js +160 -224
  69. package/dist/engine/core/scribble.js +59 -84
  70. package/dist/engine/core/shapePaths.js +12 -15
  71. package/dist/engine/core/shapeTools.js +72 -63
  72. package/dist/engine/core/snapping.js +65 -117
  73. package/dist/engine/core/style.js +32 -65
  74. package/dist/engine/core/transform.js +84 -95
  75. package/dist/engine/core/types.js +4 -4
  76. package/dist/engine/core/viewport.js +23 -36
  77. package/dist/engine/input/PointerController.js +513 -526
  78. package/dist/engine/input/arrowHandles.js +12 -12
  79. package/dist/engine/input/gestures.js +267 -310
  80. package/dist/engine/input/hoverCursor.js +1 -1
  81. package/dist/engine/input/keyboard.js +35 -39
  82. package/dist/engine/input/selectionHandles.js +45 -50
  83. package/dist/engine/input/types.js +1 -1
  84. package/dist/engine/render/rasterize.js +86 -158
  85. package/dist/engine/state.js +6 -17
  86. package/dist/engine/text/layoutText.js +43 -62
  87. package/dist/engine/text/textMetrics.js +8 -10
  88. package/dist/engine/widgets.js +101 -137
  89. package/dist/fonts/interFont.js +30 -83
  90. package/dist/fonts/workspaceFonts.js +33 -92
  91. package/dist/i18n/labels.js +4 -4
  92. package/dist/i18n/language.js +4 -4
  93. package/dist/i18n/messages.js +1 -1
  94. package/dist/i18n/shortcuts.js +14 -14
  95. package/dist/index.d.ts +1 -1
  96. package/dist/poster/boardPlacement.js +28 -30
  97. package/dist/poster/boardPoster.js +58 -83
  98. package/dist/poster/paintBoardPoster.js +16 -39
  99. package/dist/poster/posterPictures.js +27 -43
  100. package/dist/settings/rememberedTool.d.ts +50 -0
  101. package/dist/settings/rememberedTool.js +68 -0
  102. package/dist/settings/workspaceToolSettings.d.ts +8 -0
  103. package/dist/settings/workspaceToolSettings.js +35 -48
  104. package/dist/tools/components/catalog.js +64 -74
  105. package/dist/tools/components/grouping.js +6 -28
  106. package/dist/tools/definitions.js +5 -5
  107. package/dist/tools/executors.js +137 -169
  108. package/dist/tools/hostAdapter.js +11 -13
  109. package/dist/tools/sceneSummary.js +43 -57
  110. package/dist/tools/sketchIntent.js +13 -21
  111. package/dist/tools/styleArguments.js +19 -21
  112. package/dist/tools/types.js +1 -1
  113. package/package.json +2 -2
@@ -19,26 +19,6 @@
19
19
  *
20
20
  * Nothing here touches the DOM or the page, so it runs in Node tests.
21
21
  */
22
- var __assign = (this && this.__assign) || function () {
23
- __assign = Object.assign || function(t) {
24
- for (var s, i = 1, n = arguments.length; i < n; i++) {
25
- s = arguments[i];
26
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
27
- t[p] = s[p];
28
- }
29
- return t;
30
- };
31
- return __assign.apply(this, arguments);
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
22
  import { parseCanvasDocument } from '../engine/codec/canvasDocument.js';
43
23
  import { HOST_READABLE_KEYS } from '../engine/codec/hostReadableKeys.js';
44
24
  import { brushReach, brushSizeFor } from '../engine/core/freehand.js';
@@ -46,30 +26,30 @@ import { inflateRect, rectCenter, rectsIntersect, rotatedBoxBounds } from '../en
46
26
  import { CANVAS_DOCUMENT_VERSION, isBoxedObject } from '../engine/core/types.js';
47
27
  import { sanitizeViewport, visibleWorldRect } from '../engine/core/viewport.js';
48
28
  /** The version of the poster's own format: a poster of another version is not read (the next visit makes a new one). */
49
- export var BOARD_POSTER_VERSION = 1;
29
+ export const BOARD_POSTER_VERSION = 1;
50
30
  /**
51
31
  * The kind a page keeps the poster under in its cache (the canvas id is the key). The creator, which writes it, and the page that
52
32
  * reads it before the creator is there agree on this one name, so it lives with the format.
53
33
  */
54
- export var BOARD_POSTER_STORAGE_KIND = 'canvasBoardPoster';
34
+ export const BOARD_POSTER_STORAGE_KIND = 'canvasBoardPoster';
55
35
  /** The size of the screen the saved view is taken to have when the page does not say (a large window). */
56
- export var DEFAULT_POSTER_SURFACE = Object.freeze({ width: 1600, height: 1000 });
36
+ export const DEFAULT_POSTER_SURFACE = Object.freeze({ width: 1600, height: 1000 });
57
37
  /** The most objects a poster holds; a board with more keeps the ones nearest the middle of the view. */
58
- export var POSTER_MAX_OBJECTS = 400;
38
+ export const POSTER_MAX_OBJECTS = 400;
59
39
  /** The most bytes of saved text a poster holds (what is kept next to the page stays small, and reading it stays fast). */
60
- export var POSTER_MAX_BYTES = 240000;
40
+ export const POSTER_MAX_BYTES = 240_000;
61
41
  /** Inline picture bytes longer than this are left out of the poster (the picture appears with the board). */
62
- export var POSTER_MAX_INLINE_PICTURE = 24000;
42
+ export const POSTER_MAX_INLINE_PICTURE = 24_000;
63
43
  /** Screen pixels kept around the saved view on every side, so a small pan, or a larger window than last time, still has its objects. */
64
- export var POSTER_MARGIN = 160;
44
+ export const POSTER_MARGIN = 160;
65
45
  /** The most records `parseBoardPoster` reads, whatever a poster says it holds. */
66
- var POSTER_READ_LIMIT = 2000;
67
- var MIN_SURFACE_SIDE = 200;
68
- var MAX_SURFACE_SIDE = 8000;
46
+ const POSTER_READ_LIMIT = 2_000;
47
+ const MIN_SURFACE_SIDE = 200;
48
+ const MAX_SURFACE_SIDE = 8_000;
69
49
  /** The kinds of object a poster draws (a group has no look of its own). */
70
- var DRAWN_TYPES = new Set(['rect', 'ellipse', 'path', 'text', 'image', 'widget']);
50
+ const DRAWN_TYPES = new Set(['rect', 'ellipse', 'path', 'text', 'image', 'widget']);
71
51
  /** The keys that may hold the bytes of a picture inline. */
72
- var INLINE_PICTURE_KEYS = ['src', 'thumbnail'];
52
+ const INLINE_PICTURE_KEYS = ['src', 'thumbnail'];
73
53
  function isRecord(value) {
74
54
  return typeof value === 'object' && value !== null && !Array.isArray(value);
75
55
  }
@@ -85,16 +65,16 @@ function readJson(source) {
85
65
  try {
86
66
  return JSON.parse(source);
87
67
  }
88
- catch (_a) {
68
+ catch {
89
69
  return null;
90
70
  }
91
71
  }
92
72
  /** A surface the page named, kept to sizes a board can have; the default when it named none or nonsense. */
93
73
  export function sanitizeSurface(value) {
94
74
  if (!isRecord(value))
95
- return __assign({}, DEFAULT_POSTER_SURFACE);
96
- var width = finiteNumber(value.width, DEFAULT_POSTER_SURFACE.width);
97
- var height = finiteNumber(value.height, DEFAULT_POSTER_SURFACE.height);
75
+ return { ...DEFAULT_POSTER_SURFACE };
76
+ const width = finiteNumber(value.width, DEFAULT_POSTER_SURFACE.width);
77
+ const height = finiteNumber(value.height, DEFAULT_POSTER_SURFACE.height);
98
78
  return {
99
79
  width: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(width))),
100
80
  height: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(height)))
@@ -102,13 +82,12 @@ export function sanitizeSurface(value) {
102
82
  }
103
83
  /** A saved record without the picture bytes that are too long to keep (the same record when it has none). */
104
84
  function withoutLongPictures(record) {
105
- var result = record;
106
- for (var _i = 0, INLINE_PICTURE_KEYS_1 = INLINE_PICTURE_KEYS; _i < INLINE_PICTURE_KEYS_1.length; _i++) {
107
- var key = INLINE_PICTURE_KEYS_1[_i];
108
- var value = record[key];
85
+ let result = record;
86
+ for (const key of INLINE_PICTURE_KEYS) {
87
+ const value = record[key];
109
88
  if (typeof value === 'string' && value.startsWith('data:') && value.length > POSTER_MAX_INLINE_PICTURE) {
110
89
  if (result === record)
111
- result = __assign({}, record);
90
+ result = { ...record };
112
91
  delete result[key];
113
92
  }
114
93
  }
@@ -119,10 +98,10 @@ function withoutLongPictures(record) {
119
98
  * room for its line and for a turn. Generous on purpose: it only decides what is kept, and the poster is drawn clipped anyway.
120
99
  */
121
100
  function roughBoundsOf(record) {
122
- var strokeWidth = Math.min(200, Math.max(0, finiteNumber(record.strokeWidth, 3)));
123
- var isBrush = isRecord(record.ink);
124
- var reach = isBrush ? brushReach(brushSizeFor(strokeWidth)) : strokeWidth * Math.SQRT1_2 + 1;
125
- var box = {
101
+ const strokeWidth = Math.min(200, Math.max(0, finiteNumber(record.strokeWidth, 3)));
102
+ const isBrush = isRecord(record.ink);
103
+ const reach = isBrush ? brushReach(brushSizeFor(strokeWidth)) : strokeWidth * Math.SQRT1_2 + 1;
104
+ const box = {
126
105
  x: finiteNumber(record.x, 0),
127
106
  y: finiteNumber(record.y, 0),
128
107
  width: Math.max(1, finiteNumber(record.width, 100)),
@@ -135,76 +114,73 @@ function roughBoundsOf(record) {
135
114
  * The poster of a saved board: the records that touch the view it was saved at, or null when there is nothing to show (a text that
136
115
  * is not a saved board, an empty board, nothing in view). `source` is the saved text, or the same thing already parsed.
137
116
  */
138
- export function createBoardPoster(source, options) {
139
- if (options === void 0) { options = {}; }
140
- var savedDocument = readJson(source);
117
+ export function createBoardPoster(source, options = {}) {
118
+ const savedDocument = readJson(source);
141
119
  if (!isRecord(savedDocument) || savedDocument.version !== CANVAS_DOCUMENT_VERSION)
142
120
  return null;
143
- var canvas = savedDocument.canvas;
144
- var list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
121
+ const canvas = savedDocument.canvas;
122
+ const list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
145
123
  if (list.length === 0)
146
124
  return null;
147
- var viewport = sanitizeViewport(savedDocument.viewport);
148
- var surface = sanitizeSurface(options.surface);
149
- var maxObjects = Math.max(1, Math.min(POSTER_MAX_OBJECTS, Math.floor(finiteNumber(options.maxObjects, POSTER_MAX_OBJECTS))));
150
- var maxBytes = Math.max(1000, Math.min(POSTER_MAX_BYTES, Math.floor(finiteNumber(options.maxBytes, POSTER_MAX_BYTES))));
125
+ const viewport = sanitizeViewport(savedDocument.viewport);
126
+ const surface = sanitizeSurface(options.surface);
127
+ const maxObjects = Math.max(1, Math.min(POSTER_MAX_OBJECTS, Math.floor(finiteNumber(options.maxObjects, POSTER_MAX_OBJECTS))));
128
+ const maxBytes = Math.max(1_000, Math.min(POSTER_MAX_BYTES, Math.floor(finiteNumber(options.maxBytes, POSTER_MAX_BYTES))));
151
129
  // What the saved view showed, with room around it, in world units.
152
- var keptArea = inflateRect(visibleWorldRect(viewport, surface), POSTER_MARGIN / viewport.zoom);
153
- var middle = rectCenter(keptArea);
154
- var candidates = [];
155
- for (var index = 0; index < list.length; index += 1) {
156
- var raw = list[index];
130
+ const keptArea = inflateRect(visibleWorldRect(viewport, surface), POSTER_MARGIN / viewport.zoom);
131
+ const middle = rectCenter(keptArea);
132
+ const candidates = [];
133
+ for (let index = 0; index < list.length; index += 1) {
134
+ const raw = list[index];
157
135
  if (!isRecord(raw))
158
136
  continue;
159
- var type = raw[HOST_READABLE_KEYS.objectType];
137
+ const type = raw[HOST_READABLE_KEYS.objectType];
160
138
  if (typeof type !== 'string' || !DRAWN_TYPES.has(type))
161
139
  continue;
162
140
  // The reader treats these as "not there"; so does the poster.
163
141
  if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
164
142
  continue;
165
- var bounds = roughBoundsOf(raw);
143
+ const bounds = roughBoundsOf(raw);
166
144
  if (!rectsIntersect(bounds, keptArea))
167
145
  continue;
168
- var record = withoutLongPictures(raw);
169
- var bytes = 0;
146
+ const record = withoutLongPictures(raw);
147
+ let bytes = 0;
170
148
  try {
171
149
  bytes = JSON.stringify(record).length;
172
150
  }
173
- catch (_a) {
151
+ catch {
174
152
  // A record that cannot be written down cannot be kept.
175
153
  continue;
176
154
  }
177
- var center = rectCenter(bounds);
178
- candidates.push({ index: index, record: record, bytes: bytes, distance: Math.pow((center.x - middle.x), 2) + Math.pow((center.y - middle.y), 2) });
155
+ const center = rectCenter(bounds);
156
+ candidates.push({ index, record, bytes, distance: (center.x - middle.x) ** 2 + (center.y - middle.y) ** 2 });
179
157
  }
180
158
  if (candidates.length === 0)
181
159
  return null;
182
- var kept = candidates;
183
- var totalBytes = candidates.reduce(function (total, candidate) { return total + candidate.bytes; }, 0);
160
+ let kept = candidates;
161
+ const totalBytes = candidates.reduce((total, candidate) => total + candidate.bytes, 0);
184
162
  if (candidates.length > maxObjects || totalBytes > maxBytes) {
185
163
  // Too much for a poster: keep what is nearest the middle of the view, as long as it fits, and put it back in z-order.
186
- var byDistance = __spreadArray([], candidates, true).sort(function (first, second) { return first.distance - second.distance; });
164
+ const byDistance = [...candidates].sort((first, second) => first.distance - second.distance);
187
165
  kept = [];
188
- var usedBytes = 0;
189
- for (var _i = 0, byDistance_1 = byDistance; _i < byDistance_1.length; _i++) {
190
- var candidate = byDistance_1[_i];
166
+ let usedBytes = 0;
167
+ for (const candidate of byDistance) {
191
168
  if (kept.length >= maxObjects || usedBytes + candidate.bytes > maxBytes)
192
169
  continue;
193
170
  kept.push(candidate);
194
171
  usedBytes += candidate.bytes;
195
172
  }
196
- kept.sort(function (first, second) { return first.index - second.index; });
173
+ kept.sort((first, second) => first.index - second.index);
197
174
  if (kept.length === 0)
198
175
  return null;
199
176
  }
200
- return { version: BOARD_POSTER_VERSION, viewport: viewport, surface: surface, objects: kept.map(function (candidate) { return candidate.record; }) };
177
+ return { version: BOARD_POSTER_VERSION, viewport, surface, objects: kept.map((candidate) => candidate.record) };
201
178
  }
202
179
  /** The address a picture is drawn from: its own, or the library's when the board's own is gone (an object URL does not outlive a page). */
203
180
  function pictureAddressOf(shape) {
204
- var _a;
205
181
  if (shape.src && !shape.src.startsWith('blob:'))
206
182
  return shape.src;
207
- return (_a = shape.libraryFileUrl) !== null && _a !== void 0 ? _a : null;
183
+ return shape.libraryFileUrl ?? null;
208
184
  }
209
185
  /**
210
186
  * A poster read back from whatever a page kept. Null when it is not a poster of this version or holds nothing that can be drawn.
@@ -215,23 +191,22 @@ export function parseBoardPoster(value) {
215
191
  return null;
216
192
  if (!Array.isArray(value.objects) || value.objects.length === 0 || value.objects.length > POSTER_READ_LIMIT)
217
193
  return null;
218
- var parsed = parseCanvasDocument({
194
+ const parsed = parseCanvasDocument({
219
195
  version: CANVAS_DOCUMENT_VERSION,
220
196
  viewport: value.viewport,
221
197
  canvas: { objects: value.objects }
222
198
  });
223
199
  if (parsed.status !== 'ok')
224
200
  return null;
225
- var shapes = [];
226
- for (var _i = 0, _a = parsed.scene; _i < _a.length; _i++) {
227
- var object = _a[_i];
201
+ const shapes = [];
202
+ for (const object of parsed.scene) {
228
203
  if (!isBoxedObject(object))
229
204
  continue;
230
- shapes.push(object.type === 'image' ? __assign(__assign({}, object), { src: pictureAddressOf(object) }) : object);
205
+ shapes.push(object.type === 'image' ? { ...object, src: pictureAddressOf(object) } : object);
231
206
  }
232
207
  if (shapes.length === 0)
233
208
  return null;
234
- return { viewport: parsed.viewport, surface: sanitizeSurface(value.surface), shapes: shapes };
209
+ return { viewport: parsed.viewport, surface: sanitizeSurface(value.surface), shapes };
235
210
  }
236
211
  /**
237
212
  * Whether two posters are the same, for a page that keeps the latest and writes only what changed. Compares what would be written,
@@ -245,7 +220,7 @@ export function boardPostersMatch(first, second) {
245
220
  try {
246
221
  return JSON.stringify(first) === JSON.stringify(second);
247
222
  }
248
- catch (_a) {
223
+ catch {
249
224
  return false;
250
225
  }
251
226
  }
@@ -7,26 +7,6 @@
7
7
  * `window`: the context and the services are handed in, so the drawing order and the geometry are tested in Node with a recording
8
8
  * context.
9
9
  */
10
- var __assign = (this && this.__assign) || function () {
11
- __assign = Object.assign || function(t) {
12
- for (var s, i = 1, n = arguments.length; i < n; i++) {
13
- s = arguments[i];
14
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
15
- t[p] = s[p];
16
- }
17
- return t;
18
- };
19
- return __assign.apply(this, arguments);
20
- };
21
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
22
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
23
- if (ar || !(i in from)) {
24
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
25
- ar[i] = from[i];
26
- }
27
- }
28
- return to.concat(ar || Array.prototype.slice.call(from));
29
- };
30
10
  import { inflateRect, rectsIntersect } from '../engine/core/geometry.js';
31
11
  import { shapeVisualBounds } from '../engine/core/scene.js';
32
12
  import { visibleWorldRect } from '../engine/core/viewport.js';
@@ -34,7 +14,7 @@ import { imageSourcesOf, paintShape } from '../engine/render/rasterize.js';
34
14
  import { createFixedAdvanceMeasure } from '../engine/text/layoutText.js';
35
15
  import { posterReferenceKey } from './posterPictures.js';
36
16
  /** Screen pixels around the area that still get painted, so a stroke that starts just outside it has its edge. */
37
- var PAINT_MARGIN = 64;
17
+ const PAINT_MARGIN = 64;
38
18
  /** The addresses of the pictures a poster needs (its images, and the screenshots of its widget cards). */
39
19
  export function posterPictureSources(poster) {
40
20
  return imageSourcesOf(poster.shapes);
@@ -44,34 +24,33 @@ export function posterPictureSources(poster) {
44
24
  * screenshot of a widget card): they have no address in the saved text, only the reference the host knows them by. Once each.
45
25
  */
46
26
  export function posterPictureReferences(poster) {
47
- var references = new Set();
48
- for (var _i = 0, _a = poster.shapes; _i < _a.length; _i++) {
49
- var shape = _a[_i];
27
+ const references = new Set();
28
+ for (const shape of poster.shapes) {
50
29
  if (shape.type === 'image' && !shape.src && shape.imageRef)
51
30
  references.add(shape.imageRef);
52
31
  if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef)
53
32
  references.add(shape.thumbnailRef);
54
33
  }
55
- return __spreadArray([], references, true);
34
+ return [...references];
56
35
  }
57
36
  /**
58
37
  * The poster with each picture that is kept by reference named by its key (`posterReferenceKey`), which is what the painter asks
59
38
  * the loaded pictures for. The same poster when there is none. A picture that has an address keeps it.
60
39
  */
61
40
  export function withReferencedPictures(poster) {
62
- var changed = false;
63
- var shapes = poster.shapes.map(function (shape) {
41
+ let changed = false;
42
+ const shapes = poster.shapes.map((shape) => {
64
43
  if (shape.type === 'image' && !shape.src && shape.imageRef) {
65
44
  changed = true;
66
- return __assign(__assign({}, shape), { src: posterReferenceKey(shape.imageRef) });
45
+ return { ...shape, src: posterReferenceKey(shape.imageRef) };
67
46
  }
68
47
  if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef) {
69
48
  changed = true;
70
- return __assign(__assign({}, shape), { thumbnail: posterReferenceKey(shape.thumbnailRef) });
49
+ return { ...shape, thumbnail: posterReferenceKey(shape.thumbnailRef) };
71
50
  }
72
51
  return shape;
73
52
  });
74
- return changed ? __assign(__assign({}, poster), { shapes: shapes }) : poster;
53
+ return changed ? { ...poster, shapes } : poster;
75
54
  }
76
55
  /**
77
56
  * Paints the poster: clears the area, sets the saved view, and draws the shapes that touch the area, bottom first. A shape that
@@ -79,34 +58,32 @@ export function withReferencedPictures(poster) {
79
58
  * handed over, and the painter draws nothing of it).
80
59
  */
81
60
  export function paintBoardPoster(context, poster, target, dependencies) {
82
- var pixelRatio = target.pixelRatio > 0 ? target.pixelRatio : 1;
83
- var viewport = poster.viewport;
61
+ const pixelRatio = target.pixelRatio > 0 ? target.pixelRatio : 1;
62
+ const { viewport } = poster;
84
63
  context.setTransform(1, 0, 0, 1, 0, 0);
85
64
  context.clearRect(0, 0, Math.ceil(target.width * pixelRatio), Math.ceil(target.height * pixelRatio));
86
65
  // World units to device pixels: the view of the saved board, scaled by the screen's density.
87
66
  context.setTransform(pixelRatio * viewport.zoom, 0, 0, pixelRatio * viewport.zoom, pixelRatio * viewport.x, pixelRatio * viewport.y);
88
- var painted = inflateRect(visibleWorldRect(viewport, target), PAINT_MARGIN / viewport.zoom);
89
- var count = 0;
90
- for (var _i = 0, _a = poster.shapes; _i < _a.length; _i++) {
91
- var shape = _a[_i];
67
+ const painted = inflateRect(visibleWorldRect(viewport, target), PAINT_MARGIN / viewport.zoom);
68
+ let count = 0;
69
+ for (const shape of poster.shapes) {
92
70
  try {
93
71
  if (!rectsIntersect(shapeVisualBounds(shape), painted))
94
72
  continue;
95
73
  paintShape(context, shape, dependencies);
96
74
  count += 1;
97
75
  }
98
- catch (_b) {
76
+ catch {
99
77
  // One shape that cannot be drawn must not take the poster with it.
100
78
  }
101
79
  }
102
80
  return count;
103
81
  }
104
82
  export function posterPaintDependencies(services) {
105
- var _a;
106
83
  return {
107
84
  createPath: services.createPath,
108
85
  imageFor: services.pictureFor,
109
- measureText: (_a = services.measureText) !== null && _a !== void 0 ? _a : createFixedAdvanceMeasure(),
86
+ measureText: services.measureText ?? createFixedAdvanceMeasure(),
110
87
  widgetFallbackName: ''
111
88
  };
112
89
  }
@@ -9,36 +9,26 @@
9
9
  * The painter asks for a picture by a key. For a picture with an address the key is the address; for one kept by reference it is
10
10
  * `posterReferenceKey(reference)`, so the bytes the host hands over (a data URL, possibly megabytes) are never a map key.
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
- };
21
12
  /** What the page has loaded, by key. */
22
- var loadedPictures = new Map();
13
+ const loadedPictures = new Map();
23
14
  /** What has been asked for already, loaded or not, so one picture is never fetched twice. */
24
- var askedFor = new Set();
15
+ const askedFor = new Set();
25
16
  /** Who wants to know when a picture arrives: every poster that is on the screen. */
26
- var listeners = new Set();
17
+ const listeners = new Set();
27
18
  /** The most pictures kept; the oldest go first. A poster holds few, so this is a ceiling, not a working size. */
28
- var MAX_KEPT_PICTURES = 64;
19
+ const MAX_KEPT_PICTURES = 64;
29
20
  /** The key the painter asks for a picture by when the board keeps it in the host's store under `reference`. */
30
21
  export function posterReferenceKey(reference) {
31
- return "ref:".concat(reference);
22
+ return `ref:${reference}`;
32
23
  }
33
24
  /** The picture for a key, once it has loaded. Null while it has not, or when it never will. */
34
25
  export function posterPictureFor(key) {
35
- var _a;
36
- return key ? ((_a = loadedPictures.get(key)) !== null && _a !== void 0 ? _a : null) : null;
26
+ return key ? (loadedPictures.get(key) ?? null) : null;
37
27
  }
38
28
  /** Calls `listener` whenever a picture has arrived. Returns what stops it. */
39
29
  export function subscribeToPosterPictures(listener) {
40
30
  listeners.add(listener);
41
- return function () {
31
+ return () => {
42
32
  listeners.delete(listener);
43
33
  };
44
34
  }
@@ -46,16 +36,15 @@ export function subscribeToPosterPictures(listener) {
46
36
  function imageMaker(createImage) {
47
37
  if (createImage)
48
38
  return createImage;
49
- return typeof Image === 'undefined' ? null : function () { return new Image(); };
39
+ return typeof Image === 'undefined' ? null : () => new Image();
50
40
  }
51
41
  /** Tells every poster on the screen that a picture has arrived, once each; one that fails does not stop the others from hearing. */
52
42
  function notifyListeners() {
53
- for (var _i = 0, _a = __spreadArray([], listeners, true); _i < _a.length; _i++) {
54
- var listener = _a[_i];
43
+ for (const listener of [...listeners]) {
55
44
  try {
56
45
  listener();
57
46
  }
58
- catch (_b) {
47
+ catch {
59
48
  // A subscriber that fails does not stop the others from hearing.
60
49
  }
61
50
  }
@@ -63,22 +52,22 @@ function notifyListeners() {
63
52
  /** Starts loading `source` (an address or a data URL) under `key`. A picture that cannot be loaded stays asked for: it costs one try. */
64
53
  function startLoading(key, source, make) {
65
54
  try {
66
- var image_1 = make();
67
- image_1.decoding = 'async';
68
- image_1.onload = function () {
69
- loadedPictures.set(key, image_1);
55
+ const image = make();
56
+ image.decoding = 'async';
57
+ image.onload = () => {
58
+ loadedPictures.set(key, image);
70
59
  if (loadedPictures.size > MAX_KEPT_PICTURES) {
71
- var oldest = loadedPictures.keys().next().value;
60
+ const oldest = loadedPictures.keys().next().value;
72
61
  if (oldest !== undefined)
73
62
  loadedPictures.delete(oldest);
74
63
  }
75
64
  notifyListeners();
76
65
  };
77
66
  // Left out: the board draws it when it has it.
78
- image_1.onerror = function () { };
79
- image_1.src = source;
67
+ image.onerror = () => { };
68
+ image.src = source;
80
69
  }
81
- catch (_a) {
70
+ catch {
82
71
  // The browser refused the picture; the poster goes on without it.
83
72
  }
84
73
  }
@@ -87,11 +76,10 @@ function startLoading(key, source, make) {
87
76
  * arrives tells every subscriber, once. `createImage` is for tests; the browser's `Image` is used otherwise.
88
77
  */
89
78
  export function requestPosterPictures(sources, createImage) {
90
- var make = imageMaker(createImage);
79
+ const make = imageMaker(createImage);
91
80
  if (!make)
92
81
  return;
93
- for (var _i = 0, sources_1 = sources; _i < sources_1.length; _i++) {
94
- var source = sources_1[_i];
82
+ for (const source of sources) {
95
83
  if (loadedPictures.has(source) || askedFor.has(source))
96
84
  continue;
97
85
  askedFor.add(source);
@@ -104,17 +92,17 @@ export function requestPosterPictures(sources, createImage) {
104
92
  * that fails, or answers with nothing, only means the picture is not in the poster.
105
93
  */
106
94
  export function requestReferencedPictures(references, resolve, createImage) {
107
- var make = imageMaker(createImage);
95
+ const make = imageMaker(createImage);
108
96
  if (!make)
109
97
  return;
110
- var _loop_1 = function (reference) {
111
- var key = posterReferenceKey(reference);
98
+ for (const reference of references) {
99
+ const key = posterReferenceKey(reference);
112
100
  if (loadedPictures.has(key) || askedFor.has(key))
113
- return "continue";
101
+ continue;
114
102
  askedFor.add(key);
115
103
  void Promise.resolve()
116
- .then(function () { return resolve(reference); })
117
- .then(function (source) {
104
+ .then(() => resolve(reference))
105
+ .then((source) => {
118
106
  if (typeof source === 'string' && source !== '') {
119
107
  startLoading(key, source, make);
120
108
  return;
@@ -122,13 +110,9 @@ export function requestReferencedPictures(references, resolve, createImage) {
122
110
  // The host does not have it (yet): it is asked again the next time the poster is drawn.
123
111
  askedFor.delete(key);
124
112
  })
125
- .catch(function () {
113
+ .catch(() => {
126
114
  askedFor.delete(key);
127
115
  });
128
- };
129
- for (var _i = 0, references_1 = references; _i < references_1.length; _i++) {
130
- var reference = references_1[_i];
131
- _loop_1(reference);
132
116
  }
133
117
  }
134
118
  /** Forgets every picture (for tests, and for a page that signs someone out). */
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The tool a board opens with.
3
+ *
4
+ * A board used to open with the pencil every time (where the page shows the drawing tools), so a refresh took the person back to the
5
+ * pencil from whatever tool they had chosen. It now opens with the tool they had last, kept for the workspace beside the rest of their
6
+ * tool settings (`workspaceToolSettings.ts`), and with the pencil (Select, where the page hides the drawing tools) when none was kept.
7
+ *
8
+ * One rule decides it for the board (`CanvasHost`) and for the picture of the toolbar that is drawn before the board is there
9
+ * (`CanvasToolbarPlaceholder`), so the tool that is lit does not change at the hand-over.
10
+ */
11
+ import type { ToolId } from '../engine/state.js';
12
+ import { type SettingsStorageAdapter } from './workspaceToolSettings.js';
13
+ /**
14
+ * The tools a board can open with: the ones that have a button on the toolbar, the shapes of the Shapes menu included. `pan` has no
15
+ * button (it is the H key and the space bar) and a library shape needs the key of its shape, so neither is kept.
16
+ */
17
+ export declare const REMEMBERED_TOOL_IDS: readonly ["select", "draw", "erase", "arrow", "text", "rect", "rounded-rect", "ellipse", "circle", "triangle"];
18
+ export type RememberedToolId = (typeof REMEMBERED_TOOL_IDS)[number];
19
+ /** The tools of the Shapes menu (its button is the one that is lit while one of them is the tool). */
20
+ export declare const SHAPE_TOOL_IDS: readonly ["rect", "rounded-rect", "ellipse", "circle", "triangle"];
21
+ /** The tools of the drawing group: the page takes their buttons off (`showDrawingTools: false`), and a board does not open with a tool it has no button for. */
22
+ export declare const DRAWING_GROUP_TOOL_IDS: readonly ["draw", "erase", "arrow", "text"];
23
+ /** Whether a value is a tool that is kept (anything else that a storage holds is the same as nothing). */
24
+ export declare function isRememberedTool(value: unknown): value is RememberedToolId;
25
+ /** The tool of this change of tool, when it is one that is kept; null for the others (the hand, a library shape). */
26
+ export declare function toolToRemember(tool: ToolId): RememberedToolId | null;
27
+ /** Whether the tool is one of the drawing group, which a page may take off its toolbar. */
28
+ export declare function isDrawingGroupTool(tool: ToolId): boolean;
29
+ /** The tool of a board where none was kept: the pencil where the page shows the drawing tools (a board opens ready for a sketch), else Select. */
30
+ export declare function defaultFirstTool(showDrawingTools: boolean): ToolId;
31
+ /**
32
+ * The tool a board opens with, given what was kept (anything, as it came from a storage) and whether the page shows the drawing tools:
33
+ * what was kept, when the board has a button for it, else the tool of a board where none was kept.
34
+ */
35
+ export declare function toolTheBoardOpensWith(options: {
36
+ rememberedToolId: unknown;
37
+ showDrawingTools: boolean;
38
+ }): ToolId;
39
+ /** Where the tool was kept: the workspace, and the person where the page says who they are. */
40
+ export interface RememberedToolScope {
41
+ workspaceId?: string | null;
42
+ userId?: string | null;
43
+ }
44
+ /** The tool kept for this workspace and person, or null (nothing kept, no workspace to keep it for, a store that refuses, or a value that is no tool). */
45
+ export declare function readRememberedToolId(scope: RememberedToolScope, adapter?: SettingsStorageAdapter): RememberedToolId | null;
46
+ /** The tool a board opens with: read from the storage of the workspace, decided by `toolTheBoardOpensWith`. */
47
+ export declare function firstToolOfBoard(options: RememberedToolScope & {
48
+ showDrawingTools: boolean;
49
+ adapter?: SettingsStorageAdapter;
50
+ }): ToolId;
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The tool a board opens with.
3
+ *
4
+ * A board used to open with the pencil every time (where the page shows the drawing tools), so a refresh took the person back to the
5
+ * pencil from whatever tool they had chosen. It now opens with the tool they had last, kept for the workspace beside the rest of their
6
+ * tool settings (`workspaceToolSettings.ts`), and with the pencil (Select, where the page hides the drawing tools) when none was kept.
7
+ *
8
+ * One rule decides it for the board (`CanvasHost`) and for the picture of the toolbar that is drawn before the board is there
9
+ * (`CanvasToolbarPlaceholder`), so the tool that is lit does not change at the hand-over.
10
+ */
11
+ import { loadWorkspaceToolSettings } from './workspaceToolSettings.js';
12
+ /**
13
+ * The tools a board can open with: the ones that have a button on the toolbar, the shapes of the Shapes menu included. `pan` has no
14
+ * button (it is the H key and the space bar) and a library shape needs the key of its shape, so neither is kept.
15
+ */
16
+ export const REMEMBERED_TOOL_IDS = ['select', 'draw', 'erase', 'arrow', 'text', 'rect', 'rounded-rect', 'ellipse', 'circle', 'triangle'];
17
+ /** The tools of the Shapes menu (its button is the one that is lit while one of them is the tool). */
18
+ export const SHAPE_TOOL_IDS = ['rect', 'rounded-rect', 'ellipse', 'circle', 'triangle'];
19
+ /** The tools of the drawing group: the page takes their buttons off (`showDrawingTools: false`), and a board does not open with a tool it has no button for. */
20
+ export const DRAWING_GROUP_TOOL_IDS = ['draw', 'erase', 'arrow', 'text'];
21
+ /** Whether a value is a tool that is kept (anything else that a storage holds is the same as nothing). */
22
+ export function isRememberedTool(value) {
23
+ return typeof value === 'string' && REMEMBERED_TOOL_IDS.includes(value);
24
+ }
25
+ /** The tool of this change of tool, when it is one that is kept; null for the others (the hand, a library shape). */
26
+ export function toolToRemember(tool) {
27
+ return isRememberedTool(tool) ? tool : null;
28
+ }
29
+ /** Whether the tool is one of the drawing group, which a page may take off its toolbar. */
30
+ export function isDrawingGroupTool(tool) {
31
+ return DRAWING_GROUP_TOOL_IDS.includes(tool);
32
+ }
33
+ /** The tool of a board where none was kept: the pencil where the page shows the drawing tools (a board opens ready for a sketch), else Select. */
34
+ export function defaultFirstTool(showDrawingTools) {
35
+ return showDrawingTools ? 'draw' : 'select';
36
+ }
37
+ /**
38
+ * The tool a board opens with, given what was kept (anything, as it came from a storage) and whether the page shows the drawing tools:
39
+ * what was kept, when the board has a button for it, else the tool of a board where none was kept.
40
+ */
41
+ export function toolTheBoardOpensWith(options) {
42
+ const { rememberedToolId, showDrawingTools } = options;
43
+ if (!isRememberedTool(rememberedToolId))
44
+ return defaultFirstTool(showDrawingTools);
45
+ if (!showDrawingTools && isDrawingGroupTool(rememberedToolId))
46
+ return defaultFirstTool(showDrawingTools);
47
+ return rememberedToolId;
48
+ }
49
+ /** The tool kept for this workspace and person, or null (nothing kept, no workspace to keep it for, a store that refuses, or a value that is no tool). */
50
+ export function readRememberedToolId(scope, adapter) {
51
+ const { workspaceId, userId } = scope;
52
+ if (!workspaceId)
53
+ return null;
54
+ try {
55
+ const stored = loadWorkspaceToolSettings({ workspaceId, userId }, adapter)?.tool?.id;
56
+ return isRememberedTool(stored) ? stored : null;
57
+ }
58
+ catch (error) {
59
+ // A store that blocks reading is the same as a store with nothing in it.
60
+ console.warn('[canvas] The tool that was kept could not be read, so the board opens with the usual one:', error);
61
+ return null;
62
+ }
63
+ }
64
+ /** The tool a board opens with: read from the storage of the workspace, decided by `toolTheBoardOpensWith`. */
65
+ export function firstToolOfBoard(options) {
66
+ const { showDrawingTools, adapter, ...scope } = options;
67
+ return toolTheBoardOpensWith({ rememberedToolId: readRememberedToolId(scope, adapter), showDrawingTools });
68
+ }