@widgetic/canvas 0.5.42 → 0.5.43

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 (62) hide show
  1. package/README.md +1 -0
  2. package/dist/canvas/DomCanvas.svelte +49 -20
  3. package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
  4. package/dist/canvas/chromeLayout.d.ts +14 -3
  5. package/dist/canvas/chromeLayout.js +14 -3
  6. package/dist/canvas/host/CanvasHost.d.ts +47 -5
  7. package/dist/canvas/host/CanvasHost.js +256 -91
  8. package/dist/canvas/host/nearCards.d.ts +33 -0
  9. package/dist/canvas/host/nearCards.js +78 -0
  10. package/dist/canvas/host/pageEvents.d.ts +16 -4
  11. package/dist/canvas/host/pageEvents.js +13 -4
  12. package/dist/canvas/host/pictureSaves.d.ts +33 -0
  13. package/dist/canvas/host/pictureSaves.js +42 -0
  14. package/dist/canvas/host/screenRects.d.ts +5 -0
  15. package/dist/canvas/host/storedPictures.d.ts +88 -0
  16. package/dist/canvas/host/storedPictures.js +288 -0
  17. package/dist/canvas/index.d.ts +1 -1
  18. package/dist/canvas/index.js +1 -1
  19. package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
  20. package/dist/canvas/shapes/shapeImageStore.js +7 -0
  21. package/dist/canvas/toolbarPosition.d.ts +2 -2
  22. package/dist/canvas/toolbarPosition.js +2 -2
  23. package/dist/canvas/ui/SelectionPanel.svelte +28 -5
  24. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
  25. package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
  26. package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
  27. package/dist/canvas/ui/ToolButton.svelte +8 -2
  28. package/dist/canvas/ui/floatingCard.d.ts +2 -2
  29. package/dist/canvas/ui/floatingCard.js +2 -2
  30. package/dist/canvas/ui/floatingLayer.d.ts +13 -2
  31. package/dist/canvas/ui/floatingLayer.js +14 -2
  32. package/dist/canvas/view/CanvasStage.svelte +101 -67
  33. package/dist/canvas/view/LeftToRightScope.svelte +21 -0
  34. package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
  35. package/dist/canvas/view/ObjectView.svelte +15 -10
  36. package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
  37. package/dist/canvas/view/liftedScene.d.ts +31 -0
  38. package/dist/canvas/view/liftedScene.js +146 -0
  39. package/dist/canvas/view/mountedScene.d.ts +71 -0
  40. package/dist/canvas/view/mountedScene.js +363 -0
  41. package/dist/engine/EngineCore.d.ts +15 -1
  42. package/dist/engine/EngineCore.js +30 -4
  43. package/dist/engine/codec/canvasDocument.d.ts +5 -0
  44. package/dist/engine/codec/canvasDocument.js +31 -4
  45. package/dist/engine/core/history.d.ts +6 -0
  46. package/dist/engine/core/history.js +16 -0
  47. package/dist/engine/core/scene.d.ts +10 -0
  48. package/dist/engine/core/scene.js +24 -0
  49. package/dist/engine/core/types.d.ts +8 -2
  50. package/dist/engine/input/gestures.js +4 -2
  51. package/dist/engine/state.d.ts +20 -0
  52. package/dist/engine/state.js +1 -0
  53. package/dist/fonts/interFont.d.ts +10 -1
  54. package/dist/fonts/interFont.js +36 -13
  55. package/dist/i18n/language.d.ts +3 -3
  56. package/dist/i18n/messages.d.ts +8 -1
  57. package/dist/i18n/messages.js +9 -1
  58. package/dist/index.d.ts +1 -1
  59. package/dist/index.js +1 -1
  60. package/package.json +1 -1
  61. package/dist/canvas/ui/SelectorBar.svelte +0 -84
  62. package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
@@ -0,0 +1,21 @@
1
+ <!--
2
+ The element that says "left to right" for the board, and has nothing else to say.
3
+
4
+ The board is a drawing, so it keeps its direction on every screen (see `CanvasStage.svelte`). The attribute is set here, in a
5
+ component of its own, and not on the board's own element, on purpose. Svelte writes `element.dir = element.dir` again in the
6
+ update of any element that has a `dir` attribute, and it puts that line in the one update that all the changing attributes of
7
+ a component share. The board's element changes its style at every frame of a pan or a zoom (the dot grid), so the write ran
8
+ at every one of them, and the browser answers a write of `dir` by working out the style of everything below the element again:
9
+ a restyle of every object at every frame, 55 to 70% of the CPU of a frame with 3000 objects (measured with the stress page).
10
+ Here nothing ever changes, so the line runs once, when the board is made.
11
+
12
+ `contents`: the element has no box of its own, so the board lays itself out in the canvas exactly as if it were not here, and
13
+ what is below it still inherits the direction (`direction` is inherited through the element's style, box or no box).
14
+ -->
15
+ <script lang="ts">
16
+ import type { Snippet } from 'svelte';
17
+
18
+ let { children }: { children: Snippet } = $props();
19
+ </script>
20
+
21
+ <div class="contents" dir="ltr">{@render children()}</div>
@@ -0,0 +1,7 @@
1
+ import type { Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ children: Snippet;
4
+ };
5
+ declare const LeftToRightScope: import("svelte").Component<$$ComponentProps, {}, "">;
6
+ type LeftToRightScope = ReturnType<typeof LeftToRightScope>;
7
+ export default LeftToRightScope;
@@ -35,7 +35,7 @@
35
35
  /** What the page said about this card's widget (only read for a widget card). */
36
36
  runtime?: WidgetRuntime;
37
37
  pending?: PendingWidgetState | undefined;
38
- /** The words of a widget card, said by the page's name for the widget (changes when the page renames it). */
38
+ /** Counts the times the page said its widgets changed (`widgetNameRevision`): a card asks again whether its widget is still there. */
39
39
  nameRevision?: number;
40
40
  /** Counts the times fonts finished loading: the lines of a text are drawn again, because letters changed width. */
41
41
  fontRevision?: number;
@@ -79,12 +79,16 @@
79
79
  );
80
80
  const textStyle = $derived(look.type === 'text' ? textBoxCss(look) : '');
81
81
 
82
- // The name a card shows. Reads the revision so a rename on the page redraws the card.
83
- const cardName = $derived.by(() => {
82
+ // A card whose widget is gone says so. (A card whose widget is there draws no words: its name is in the selection panel.)
83
+ // The engine's flag and the page's `isWidgetKnown` are not something a view can watch, so the card asks again whenever the page
84
+ // marked its widget as gone (the runtime changes) or said that its widgets changed (the revision). Without these reads a card
85
+ // that was drawn empty stays empty when its widget is found to be gone, and the person is never told.
86
+ const cardOrphaned = $derived.by(() => {
87
+ if (look.type !== 'widget') return false;
84
88
  void nameRevision;
85
- return look.type === 'widget' ? host.widgetLabel(look) : '';
89
+ void runtime.orphaned;
90
+ return host.isCardOrphaned(look);
86
91
  });
87
- const cardOrphaned = $derived(look.type === 'widget' ? host.isCardOrphaned(look) : false);
88
92
 
89
93
  // Laid out again when the text, its style or its width change, and when the fonts arrive: not when the text is only carried.
90
94
  const textLayout = $derived.by(() => {
@@ -223,14 +227,15 @@
223
227
  >
224
228
  {#if look.thumbnail && !runtime.hiddenImage}
225
229
  <img class="size-full select-none object-fill" src={look.thumbnail} alt="" draggable="false" />
226
- {:else if !runtime.hiddenImage}
227
- <!-- No screenshot yet: a dashed card with the widget's name, as the saved boards always showed it. -->
230
+ {:else if cardOrphaned && !runtime.hiddenImage}
231
+ <!-- The widget is gone and no screenshot is left: a dashed card says so. A card whose widget is there says nothing:
232
+ the page lays the live widget over it, and a "Widget preview" placeholder would show through while it builds. -->
228
233
  <div
229
234
  class="flex size-full flex-col items-center justify-center gap-1 rounded-lg border-[1.5px] border-dashed border-slate-400 px-3 text-center"
230
235
  >
231
- <span class="text-[28px] leading-none" aria-hidden="true">{cardOrphaned ? '⚠️' : '🧩'}</span>
232
- <span class="max-w-full truncate text-sm font-semibold text-slate-800">{cardOrphaned ? $t('canvas.widget.unlinkedName') : cardName}</span>
233
- <span class="text-xs text-slate-500">{cardOrphaned ? $t('canvas.widget.removeFromCanvas') : $t('canvas.draw.widgetPreview')}</span>
236
+ <span class="text-[28px] leading-none" aria-hidden="true">⚠️</span>
237
+ <span class="max-w-full truncate text-sm font-semibold text-slate-800">{$t('canvas.widget.unlinkedName')}</span>
238
+ <span class="text-xs text-slate-500">{$t('canvas.widget.removeFromCanvas')}</span>
234
239
  </div>
235
240
  {/if}
236
241
  {#if primaryRing && look.isPrimary}
@@ -7,7 +7,7 @@ interface Props {
7
7
  /** What the page said about this card's widget (only read for a widget card). */
8
8
  runtime?: WidgetRuntime;
9
9
  pending?: PendingWidgetState | undefined;
10
- /** The words of a widget card, said by the page's name for the widget (changes when the page renames it). */
10
+ /** Counts the times the page said its widgets changed (`widgetNameRevision`): a card asks again whether its widget is still there. */
11
11
  nameRevision?: number;
12
12
  /** Counts the times fonts finished loading: the lines of a text are drawn again, because letters changed width. */
13
13
  fontRevision?: number;
@@ -0,0 +1,31 @@
1
+ import type { ObjectId, SceneObject } from '../../engine/core/types.js';
2
+ import type { CarriedShapes } from '../../engine/state.js';
3
+ /** A carried selection is drawn as one layer only when at least this many of its objects have an element: a few cost nothing to write. */
4
+ export declare const LIFT_MINIMUM_CARRIED = 40;
5
+ /**
6
+ * ...and only when the objects that are not carried (and have an element) are at most this share of the ones that are, when the drag
7
+ * begins: the work of a step is the work for those, so that is how much of the work is left (a third at the most, with one half).
8
+ */
9
+ export declare const LIFT_UNCARRIED_SHARE = 0.5;
10
+ /** What the stage draws: the objects, and how far the world layer is moved on top of the view's own move, in world units. */
11
+ export interface LiftedScene {
12
+ objects: readonly SceneObject[];
13
+ offsetX: number;
14
+ offsetY: number;
15
+ }
16
+ export interface LiftedView {
17
+ /**
18
+ * What to draw for the objects that have an element (`mounted`, from the engine's records) and what the engine says is carried
19
+ * (`carry`, null when nothing is). Called at every step of a drag, so it does one pass over the list and nothing more.
20
+ */
21
+ show(mounted: readonly SceneObject[], carry: CarriedShapes | null): LiftedScene;
22
+ }
23
+ /** How many of the objects are carried. */
24
+ export declare function countCarried(objects: readonly SceneObject[], shapeIds: ReadonlySet<ObjectId>): number;
25
+ /**
26
+ * The records to draw while the carried shapes are lifted: each carried object as it was when the drag began (the very same record at
27
+ * every step), each of the others as the engine holds it, moved back by how far the selection has been carried. Null when a carried
28
+ * object has no record from the start, which says that what the engine handed over is not what this drag is made of.
29
+ */
30
+ export declare function recordsWhileLifted(mounted: readonly SceneObject[], carry: CarriedShapes): SceneObject[] | null;
31
+ export declare function createLiftedView(): LiftedView;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * A big selection that is carried, drawn as one layer.
3
+ *
4
+ * A drag writes a new record for every shape it carries, at every step, and every object that gets a new record is told, works out
5
+ * its placement and has its element written: with the whole of a board of 1,000 objects selected that is 1,000 elements at every
6
+ * frame of the drag, which is the one thing a board of this size could not do within a frame (18.7 ms at 1,000 objects, 58 ms at 3,000).
7
+ *
8
+ * What is carried does not change except for where it stands, and the world layer already moves as a whole for a pan: so while the
9
+ * pointer is down the carried objects are drawn from the records they had when the drag began, which are the same records at every
10
+ * step (the stage's list holds the same objects, and an object that is the same record as before is not told anything), and the world
11
+ * layer is moved by how far the selection has been carried, once. The objects that are not carried would move with the layer, so each of
12
+ * them is drawn from its own record moved back by the same distance: it stands where it stood. Both come to the same place on
13
+ * the screen as the records the engine holds, which is the truth and is what is drawn again, with no distance, the moment the drag ends.
14
+ *
15
+ * carried: on the screen = view + zoom * (distance + record from the start) = where the engine's record is
16
+ * not carried: on the screen = view + zoom * (distance + (record - distance)) = where the engine's record is
17
+ *
18
+ * The cost of a step is then the work for the objects that are *not* carried, and none for the ones that are. That is why it pays when
19
+ * most of what has an element is carried (select all, then drag), and not when a few shapes are carried over a crowd: `show` looks at
20
+ * the numbers and keeps the plain way (the stage draws the engine's records) when the other way would not be cheaper.
21
+ *
22
+ * Nothing about the engine changes: it writes its records at every step (the frame, the guides, the ties of the arrows, the host's own
23
+ * layers and the undo step all read them), and its `carry` in the overlay only says what is carried, from which records and how far
24
+ * (`CarriedShapes`). Where there is nothing like it, or anything about it is not as expected, the objects are drawn the plain way.
25
+ *
26
+ * Plain TypeScript with no Svelte file in it, so that Node can test it.
27
+ */
28
+ import { isBoxedObject } from '../../engine/core/types.js';
29
+ import { canvasLog } from '../canvasLogger.js';
30
+ /** A carried selection is drawn as one layer only when at least this many of its objects have an element: a few cost nothing to write. */
31
+ export const LIFT_MINIMUM_CARRIED = 40;
32
+ /**
33
+ * ...and only when the objects that are not carried (and have an element) are at most this share of the ones that are, when the drag
34
+ * begins: the work of a step is the work for those, so that is how much of the work is left (a third at the most, with one half).
35
+ */
36
+ export const LIFT_UNCARRIED_SHARE = 0.5;
37
+ const NO_OBJECTS = Object.freeze([]);
38
+ /** An object moved by a distance (the same object when the distance is none): a group draws nothing and has no place to move. */
39
+ function movedBy(object, distanceX, distanceY) {
40
+ if ((distanceX === 0 && distanceY === 0) || !isBoxedObject(object))
41
+ return object;
42
+ return { ...object, x: object.x + distanceX, y: object.y + distanceY };
43
+ }
44
+ /** How many of the objects are carried. */
45
+ export function countCarried(objects, shapeIds) {
46
+ let carriedCount = 0;
47
+ for (const object of objects) {
48
+ if (shapeIds.has(object.id))
49
+ carriedCount += 1;
50
+ }
51
+ return carriedCount;
52
+ }
53
+ /**
54
+ * The records to draw while the carried shapes are lifted: each carried object as it was when the drag began (the very same record at
55
+ * every step), each of the others as the engine holds it, moved back by how far the selection has been carried. Null when a carried
56
+ * object has no record from the start, which says that what the engine handed over is not what this drag is made of.
57
+ */
58
+ export function recordsWhileLifted(mounted, carry) {
59
+ const records = new Array(mounted.length);
60
+ for (let position = 0; position < mounted.length; position += 1) {
61
+ const object = mounted[position];
62
+ if (carry.shapeIds.has(object.id)) {
63
+ const recordFromTheStart = carry.startRecords.get(object.id);
64
+ if (recordFromTheStart === undefined)
65
+ return null;
66
+ records[position] = recordFromTheStart;
67
+ }
68
+ else {
69
+ records[position] = movedBy(object, -carry.deltaX, -carry.deltaY);
70
+ }
71
+ }
72
+ return records;
73
+ }
74
+ /** Whether two lists hold the very same records in the same order. */
75
+ function sameRecordsInOrder(first, second) {
76
+ if (first === second)
77
+ return true;
78
+ if (first.length !== second.length)
79
+ return false;
80
+ for (let position = 0; position < first.length; position += 1) {
81
+ if (first[position] !== second[position])
82
+ return false;
83
+ }
84
+ return true;
85
+ }
86
+ export function createLiftedView() {
87
+ // The drag that was seen last, told by the set of its shapes (one set for a drag, the same one at every step), and what was decided for it.
88
+ let seenDrag = null;
89
+ let decision = 'undecided';
90
+ // What was handed out last: the same records again are handed out as the very same list, so that the stage is told nothing.
91
+ let lastList = NO_OBJECTS;
92
+ // Once the work has failed the objects are drawn the plain way from then on (and the failure is said once).
93
+ let failed = false;
94
+ const drawnPlain = (mounted) => ({ objects: mounted, offsetX: 0, offsetY: 0 });
95
+ return {
96
+ show(mounted, carry) {
97
+ if (carry === null) {
98
+ seenDrag = null;
99
+ decision = 'undecided';
100
+ lastList = NO_OBJECTS;
101
+ return drawnPlain(mounted);
102
+ }
103
+ if (failed)
104
+ return drawnPlain(mounted);
105
+ try {
106
+ if (carry.shapeIds !== seenDrag) {
107
+ seenDrag = carry.shapeIds;
108
+ decision = 'undecided';
109
+ lastList = NO_OBJECTS;
110
+ }
111
+ if (decision === 'plain')
112
+ return drawnPlain(mounted);
113
+ const carriedCount = countCarried(mounted, carry.shapeIds);
114
+ const uncarriedCount = mounted.length - carriedCount;
115
+ if (decision === 'undecided') {
116
+ const worthIt = carriedCount >= LIFT_MINIMUM_CARRIED && uncarriedCount <= carriedCount * LIFT_UNCARRIED_SHARE;
117
+ decision = worthIt ? 'lifted' : 'plain';
118
+ // Said once for the drag, and only for the drags that are drawn another way: the plain way is every other drag.
119
+ if (worthIt)
120
+ canvasLog(`Canvas: a drag of ${carriedCount} objects (${uncarriedCount} others with an element) is drawn as one layer`);
121
+ }
122
+ else if (uncarriedCount > carriedCount) {
123
+ // The drag has come to a crowd of objects that are not carried: moving each of them back costs more than writing the carried ones.
124
+ decision = 'plain';
125
+ canvasLog(`Canvas: the drag came to ${uncarriedCount} objects that are not carried, so it is drawn object by object from here`);
126
+ }
127
+ if (decision === 'plain')
128
+ return drawnPlain(mounted);
129
+ const records = recordsWhileLifted(mounted, carry);
130
+ if (records === null) {
131
+ decision = 'plain';
132
+ canvasLog('Canvas: a carried object had no record from the start of the drag, so the drag is drawn object by object');
133
+ return drawnPlain(mounted);
134
+ }
135
+ const list = sameRecordsInOrder(records, lastList) ? lastList : records;
136
+ lastList = list;
137
+ return { objects: list, offsetX: carry.deltaX, offsetY: carry.deltaY };
138
+ }
139
+ catch (error) {
140
+ failed = true;
141
+ console.warn('Canvas: a carried selection could not be drawn as one layer, so every object is drawn by itself', error);
142
+ return drawnPlain(mounted);
143
+ }
144
+ }
145
+ };
146
+ }
@@ -0,0 +1,71 @@
1
+ import type { Readable } from 'svelte/store';
2
+ import type { Scene } from '../../engine/core/scene.js';
3
+ import type { ObjectId, SceneObject, Size, Viewport } from '../../engine/core/types.js';
4
+ /** A board with fewer objects than this keeps an element for every one of them. */
5
+ export declare const CULLING_MINIMUM_OBJECTS = 150;
6
+ /** How far past the view an object is given an element, as a share of the view's width and height, on every side. */
7
+ export declare const NEAR_MARGIN_RATIO = 0.5;
8
+ /**
9
+ * How far past the view an object that has an element keeps it, in the same measure. More than `NEAR_MARGIN_RATIO`: an object
10
+ * is not made at the edge of the near area and taken away the moment the view has moved a little, which would have a pan that
11
+ * goes out and back build the same objects twice (a few hundred of them, when the whole board is in view). The most that can have
12
+ * an element is what is in this area, which is nine views. It is not used by a view that has just zoomed in, nor by the first
13
+ * area of a board (see `nearAreaAround`).
14
+ */
15
+ export declare const KEEP_MARGIN_RATIO = 1;
16
+ /**
17
+ * How far the view may go past where it was when the near area was made, as a share of the view, before the area is made again.
18
+ * What is left of the margin at that moment is the room that a quick pan has before it reaches an object without an element.
19
+ */
20
+ export declare const REFRESH_DISTANCE_RATIO = 0.15;
21
+ /** The area is made again once the view has zoomed in by this factor: the objects that are not near the view now lose their element at once. */
22
+ export declare const ZOOM_IN_REFRESH_RATIO = 1.5;
23
+ /** How many objects that had no element get one in the first frame, when many are wanted at once (what follows adapts, see below). */
24
+ export declare const MOUNT_BATCH_SIZE = 30;
25
+ /** The batch never gets smaller than this, so that a very slow device (or a frame that is slow for another reason, a zoom with a thousand objects in view) still gets somewhere... */
26
+ export declare const MIN_MOUNT_BATCH_SIZE = 16;
27
+ /** ...and never larger than this, however quick the frames are: one frame must stay a frame's work. */
28
+ export declare const MAX_MOUNT_BATCH_SIZE = 120;
29
+ /** What the batch grows by after a frame that came on time. */
30
+ export declare const MOUNT_BATCH_GROWTH = 8;
31
+ /** A frame that comes later than this after the one before it (a frame at 60 Hz is 16.7 ms) says the last batch was too much. */
32
+ export declare const SLOW_FRAME_MS = 24;
33
+ /** A frame that comes sooner than this after the one before it says the last batch left room. */
34
+ export declare const QUICK_FRAME_MS = 20;
35
+ /** Frames further apart than this were not being drawn (a hidden tab): they say nothing of how much a batch costs. */
36
+ export declare const PAUSED_FRAME_MS = 500;
37
+ /** The list before anything was handed over (and the list of an empty board): one shared list. */
38
+ export declare const NOTHING_MOUNTED: readonly SceneObject[];
39
+ /** What the board knows when it asks: the records, where the view is, and the text that is being typed in. */
40
+ export interface MountedSceneInput {
41
+ scene: Scene;
42
+ viewport: Viewport;
43
+ /** The size of the board element in CSS pixels. A board with no size yet has every object drawn: nothing is known to be far. */
44
+ viewSize: Size;
45
+ /** The text that is being typed in, or null: it keeps its element wherever it is. */
46
+ editingTextId: ObjectId | null;
47
+ }
48
+ export interface MountedSceneOptions {
49
+ /** Boards with fewer objects than this are not culled (default `CULLING_MINIMUM_OBJECTS`). */
50
+ minimumObjects?: number;
51
+ /**
52
+ * How many objects get an element in one frame. Left out, the number starts at `MOUNT_BATCH_SIZE` and follows how the frames
53
+ * are going; given, it stays what it is (what a test asks for).
54
+ */
55
+ batchSize?: number;
56
+ /**
57
+ * Calls `callback` at the next frame, with the time of that frame when it is known, and returns what cancels it (default:
58
+ * `requestAnimationFrame`). Where there is no frame to wait for (the server) every object that is wanted is made at once.
59
+ */
60
+ scheduleFrame?: (callback: (frameTime?: number) => void) => () => void;
61
+ }
62
+ export interface MountedScene extends Readable<readonly SceneObject[]> {
63
+ /**
64
+ * Takes what the board knows now. Called at every frame of a pan or a drag, so it is quick when nothing that matters changed:
65
+ * the same records, the view still inside the near area, the same text being typed in.
66
+ */
67
+ take(input: MountedSceneInput): void;
68
+ /** Stops what is waiting for a frame. The store keeps its last list. */
69
+ destroy(): void;
70
+ }
71
+ export declare function createMountedScene(options?: MountedSceneOptions): MountedScene;