@widgetic/canvas 0.5.34 → 0.5.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -36
- package/dist/canvas/DomCanvas.svelte +454 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
- package/dist/canvas/autosaveScheduler.d.ts +58 -0
- package/dist/canvas/autosaveScheduler.js +157 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +351 -0
- package/dist/canvas/host/CanvasHost.js +1740 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +51 -0
- package/dist/canvas/host/conversion.js +123 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +9 -7
- package/dist/canvas/index.js +9 -7
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +24 -0
- package/dist/canvas/toolbarPosition.js +24 -0
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +20 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +155 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +152 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +51 -122
- package/dist/i18n/messages.js +58 -131
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13299
- package/dist/canvas/Canvas.svelte.d.ts +0 -281
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the input layer exchanges with the shell: plain records, so the whole gesture logic runs in Node tests with
|
|
3
|
+
* simulated pointers and no browser.
|
|
4
|
+
*/
|
|
5
|
+
import type { EngineCore } from '../EngineCore.js';
|
|
6
|
+
import type { ObjectId, PathObject, Point } from '../core/types.js';
|
|
7
|
+
export type PointerKind = 'mouse' | 'pen' | 'touch';
|
|
8
|
+
/** The keys that change what a gesture does while it runs. */
|
|
9
|
+
export interface Modifiers {
|
|
10
|
+
shiftKey: boolean;
|
|
11
|
+
altKey: boolean;
|
|
12
|
+
metaKey: boolean;
|
|
13
|
+
ctrlKey: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const NO_MODIFIERS: Modifiers;
|
|
16
|
+
/** One sample of a pointer, positions in CSS pixels from the top-left corner of the board element. */
|
|
17
|
+
export interface PointerInput extends Modifiers {
|
|
18
|
+
pointerId: number;
|
|
19
|
+
pointerType: PointerKind;
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
/** The button that went down (0 main, 1 middle, 2 secondary); for moves and releases it is the last one that changed. */
|
|
23
|
+
button: number;
|
|
24
|
+
/** Milliseconds, on the same clock for every sample. */
|
|
25
|
+
time: number;
|
|
26
|
+
/**
|
|
27
|
+
* How hard a pen presses, 0 to 1, as the browser reports it. A mouse and a finger report a fixed 0.5 while they are down, so the
|
|
28
|
+
* Draw tool reads this only from a pen, and only when it changes (a pen that reports none works like a mouse).
|
|
29
|
+
*/
|
|
30
|
+
pressure?: number;
|
|
31
|
+
/** The samples the browser folded into this move (a pen reports many per frame), oldest first. */
|
|
32
|
+
coalesced?: readonly {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
time: number;
|
|
36
|
+
pressure?: number;
|
|
37
|
+
}[];
|
|
38
|
+
}
|
|
39
|
+
export interface WheelInput extends Modifiers {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
deltaX: number;
|
|
43
|
+
deltaY: number;
|
|
44
|
+
/** 0 pixels, 1 lines, 2 pages (as `WheelEvent.deltaMode`). */
|
|
45
|
+
deltaMode: number;
|
|
46
|
+
}
|
|
47
|
+
/** One thing a pointer is doing from press to release. */
|
|
48
|
+
export interface Gesture {
|
|
49
|
+
readonly kind: string;
|
|
50
|
+
readonly pointerId: number;
|
|
51
|
+
move(input: PointerInput): void;
|
|
52
|
+
/** The pointer was released: keep what the gesture made. */
|
|
53
|
+
end(input: PointerInput): void;
|
|
54
|
+
/** Something took the gesture away (a second finger, Escape, a lost pointer): leave the board as it was. */
|
|
55
|
+
cancel(): void;
|
|
56
|
+
/** A modifier key changed while the pointer stayed still (Shift pressed in the middle of a drag). */
|
|
57
|
+
modifiersChanged?(modifiers: Modifiers, lastInput: PointerInput | null): void;
|
|
58
|
+
}
|
|
59
|
+
export interface PointerControllerOptions {
|
|
60
|
+
/** Whether moving objects snap to the others. Read at every move. */
|
|
61
|
+
snapEnabled?: () => boolean;
|
|
62
|
+
/** The element selector mode: a click hands an object to the host instead of selecting it. */
|
|
63
|
+
pickMode?: () => boolean;
|
|
64
|
+
onPick?: (id: ObjectId) => void;
|
|
65
|
+
/**
|
|
66
|
+
* A freehand stroke was drawn and is on the board: the host may swap it for a clean shape. `points` is the line the hand
|
|
67
|
+
* drew, as the pointer reported it (in the board's units): the stroke itself holds a smoothed line, which rounds the
|
|
68
|
+
* corners a shape is recognised by. A straight line (Shift) has its two ends.
|
|
69
|
+
*/
|
|
70
|
+
onStrokeFinished?: (stroke: PathObject, points: readonly Point[]) => void;
|
|
71
|
+
/** A gesture started (its kind) or ended (null): the shell hides what must not catch the pointer meanwhile. */
|
|
72
|
+
onGestureChange?: (kind: string | null) => void;
|
|
73
|
+
/** The person double-clicked or double-tapped an object that has nothing built in to do (a widget card). */
|
|
74
|
+
onActivate?: (id: ObjectId) => void;
|
|
75
|
+
/** The page's advanced tools are off: a widget card shows its four corners only and cannot be turned. Read at every press. */
|
|
76
|
+
plainCardChrome?: () => boolean;
|
|
77
|
+
}
|
|
78
|
+
/** What every gesture is made from. */
|
|
79
|
+
export interface GestureContext {
|
|
80
|
+
engine: EngineCore;
|
|
81
|
+
options: PointerControllerOptions;
|
|
82
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the input layer exchanges with the shell: plain records, so the whole gesture logic runs in Node tests with
|
|
3
|
+
* simulated pointers and no browser.
|
|
4
|
+
*/
|
|
5
|
+
export const NO_MODIFIERS = Object.freeze({ shiftKey: false, altKey: false, metaKey: false, ctrlKey: false });
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing the objects of the board onto a 2D canvas: the picture the host gets for a sketch, a selection or a widget
|
|
3
|
+
* card, and the picture the AI is shown.
|
|
4
|
+
*
|
|
5
|
+
* The board itself is DOM and SVG; this is the second way to draw the same records, for the places that need pixels
|
|
6
|
+
* (a PNG data URL). Both read the same numbers (box, rotation, fill, stroke, dash, roundness, the lines of a text from
|
|
7
|
+
* `layoutText`), so what is on the screen is what is sent.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here touches `document` or `window`: the browser's canvas, image loader and `Path2D` are handed in as
|
|
10
|
+
* services, so the drawing order and the geometry are tested in Node with a recording context.
|
|
11
|
+
*/
|
|
12
|
+
import type { Rect, RectObject, ShapeObject } from '../core/types.js';
|
|
13
|
+
import type { MeasureText } from '../text/layoutText.js';
|
|
14
|
+
/** The part of a canvas context this module draws with. The browser's `CanvasRenderingContext2D` fits. */
|
|
15
|
+
export type PaintContext = CanvasRenderingContext2D;
|
|
16
|
+
/** What a picture is drawn onto: the browser's canvas element, or whatever offers the same two calls. */
|
|
17
|
+
export interface RasterCanvas {
|
|
18
|
+
getContext(kind: '2d'): PaintContext | null;
|
|
19
|
+
toDataURL(type?: string): string;
|
|
20
|
+
}
|
|
21
|
+
/** What the browser (or a test) lends to the drawing. */
|
|
22
|
+
export interface RasterServices {
|
|
23
|
+
/** A canvas of this size in device pixels, or null when the browser refuses one. */
|
|
24
|
+
createCanvas(width: number, height: number): RasterCanvas | null;
|
|
25
|
+
/** The decoded picture behind an address, or null when it cannot be loaded (it is then left out, not drawn broken). */
|
|
26
|
+
loadImage(source: string): Promise<unknown | null>;
|
|
27
|
+
/** A path object for the `d` of an SVG path. */
|
|
28
|
+
createPath(data: string): unknown;
|
|
29
|
+
measureText: MeasureText;
|
|
30
|
+
}
|
|
31
|
+
export interface RasterOptions {
|
|
32
|
+
/** Device pixels per world unit. The sketch the AI reads is drawn at 2. */
|
|
33
|
+
scale?: number;
|
|
34
|
+
/** The picture is never made larger than this many pixels (a canvas that is too big comes back blank). */
|
|
35
|
+
maxPixels?: number;
|
|
36
|
+
/** The longest side of the picture in pixels. */
|
|
37
|
+
maxSide?: number;
|
|
38
|
+
/** Painted behind everything; null leaves the picture transparent (a sketch is transparent). */
|
|
39
|
+
background?: string | null;
|
|
40
|
+
/** Room around the objects, in world units. */
|
|
41
|
+
padding?: number;
|
|
42
|
+
/** The words a widget card shows when it has no picture yet. */
|
|
43
|
+
widgetFallbackName?: string;
|
|
44
|
+
}
|
|
45
|
+
export declare const DEFAULT_RASTER_SCALE = 2;
|
|
46
|
+
export declare const DEFAULT_RASTER_MAX_PIXELS = 16000000;
|
|
47
|
+
export declare const DEFAULT_RASTER_MAX_SIDE = 8192;
|
|
48
|
+
/** What `paintShape` needs besides the context. */
|
|
49
|
+
export interface PaintDependencies {
|
|
50
|
+
createPath(data: string): unknown;
|
|
51
|
+
/** The picture for an address, if it was loaded before painting started. */
|
|
52
|
+
imageFor(source: string | null | undefined): unknown | null;
|
|
53
|
+
measureText: MeasureText;
|
|
54
|
+
widgetFallbackName?: string;
|
|
55
|
+
}
|
|
56
|
+
/** The addresses of the pictures a set of shapes needs: images and the screenshots of widget cards. */
|
|
57
|
+
export declare function imageSourcesOf(shapes: readonly ShapeObject[]): string[];
|
|
58
|
+
/** The rectangle every shape fits in, strokes included. Null when there is no shape or it has no size. */
|
|
59
|
+
export declare function rasterBoundsOf(shapes: readonly ShapeObject[]): Rect | null;
|
|
60
|
+
/** The scale a picture of these bounds is drawn at: the one asked for, lowered until it fits the limits. */
|
|
61
|
+
export declare function rasterScaleFor(bounds: Rect, options?: RasterOptions): number;
|
|
62
|
+
/** The corner radius of a rectangle with a roundness of 0..100: 100 is a pill. */
|
|
63
|
+
export declare function cornerRadiusOf(shape: Pick<RectObject, 'width' | 'height' | 'roundness'>): number;
|
|
64
|
+
/**
|
|
65
|
+
* Draws one shape in its own box space and leaves the context as it found it.
|
|
66
|
+
* The context must already map world units to pixels (the caller sets its base transform).
|
|
67
|
+
*/
|
|
68
|
+
export declare function paintShape(context: PaintContext, shape: ShapeObject, deps: PaintDependencies): void;
|
|
69
|
+
/**
|
|
70
|
+
* Draws shapes (bottom first) into a PNG data URL, cropped to their bounds, strokes included. Returns an empty
|
|
71
|
+
* string when there is nothing to draw, the canvas cannot be made, or the browser refuses to export it (a picture
|
|
72
|
+
* from another site without CORS taints the canvas).
|
|
73
|
+
*/
|
|
74
|
+
export declare function renderShapesToDataUrl(shapes: readonly ShapeObject[], services: RasterServices, options?: RasterOptions): Promise<string>;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing the objects of the board onto a 2D canvas: the picture the host gets for a sketch, a selection or a widget
|
|
3
|
+
* card, and the picture the AI is shown.
|
|
4
|
+
*
|
|
5
|
+
* The board itself is DOM and SVG; this is the second way to draw the same records, for the places that need pixels
|
|
6
|
+
* (a PNG data URL). Both read the same numbers (box, rotation, fill, stroke, dash, roundness, the lines of a text from
|
|
7
|
+
* `layoutText`), so what is on the screen is what is sent.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here touches `document` or `window`: the browser's canvas, image loader and `Path2D` are handed in as
|
|
10
|
+
* services, so the drawing order and the geometry are tested in Node with a recording context.
|
|
11
|
+
*/
|
|
12
|
+
import { boxToWorldMatrix } from '../core/matrix.js';
|
|
13
|
+
import { strokeDashArray } from '../core/defaults.js';
|
|
14
|
+
import { shapeVisualBounds } from '../core/scene.js';
|
|
15
|
+
import { unionRects } from '../core/geometry.js';
|
|
16
|
+
import { fontShorthand, layoutTextObject, lineBoxHeight, textDirection } from '../text/layoutText.js';
|
|
17
|
+
export const DEFAULT_RASTER_SCALE = 2;
|
|
18
|
+
export const DEFAULT_RASTER_MAX_PIXELS = 16_000_000;
|
|
19
|
+
export const DEFAULT_RASTER_MAX_SIDE = 8192;
|
|
20
|
+
/** The addresses of the pictures a set of shapes needs: images and the screenshots of widget cards. */
|
|
21
|
+
export function imageSourcesOf(shapes) {
|
|
22
|
+
const sources = new Set();
|
|
23
|
+
for (const shape of shapes) {
|
|
24
|
+
if (shape.type === 'image' && shape.src)
|
|
25
|
+
sources.add(shape.src);
|
|
26
|
+
if (shape.type === 'widget' && shape.thumbnail)
|
|
27
|
+
sources.add(shape.thumbnail);
|
|
28
|
+
}
|
|
29
|
+
return [...sources];
|
|
30
|
+
}
|
|
31
|
+
/** The rectangle every shape fits in, strokes included. Null when there is no shape or it has no size. */
|
|
32
|
+
export function rasterBoundsOf(shapes) {
|
|
33
|
+
const bounds = unionRects(shapes.map(shapeVisualBounds));
|
|
34
|
+
if (!bounds || !(bounds.width > 0) || !(bounds.height > 0))
|
|
35
|
+
return null;
|
|
36
|
+
return bounds;
|
|
37
|
+
}
|
|
38
|
+
/** The scale a picture of these bounds is drawn at: the one asked for, lowered until it fits the limits. */
|
|
39
|
+
export function rasterScaleFor(bounds, options = {}) {
|
|
40
|
+
const requested = options.scale ?? DEFAULT_RASTER_SCALE;
|
|
41
|
+
const maxPixels = options.maxPixels ?? DEFAULT_RASTER_MAX_PIXELS;
|
|
42
|
+
const maxSide = options.maxSide ?? DEFAULT_RASTER_MAX_SIDE;
|
|
43
|
+
const area = Math.max(1, bounds.width * bounds.height);
|
|
44
|
+
const longest = Math.max(1, bounds.width, bounds.height);
|
|
45
|
+
const byArea = Math.sqrt(maxPixels / area);
|
|
46
|
+
const bySide = maxSide / longest;
|
|
47
|
+
return Math.max(0.01, Math.min(requested, byArea, bySide));
|
|
48
|
+
}
|
|
49
|
+
function roundedRectPath(context, width, height, radius) {
|
|
50
|
+
const clamped = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
51
|
+
context.beginPath();
|
|
52
|
+
if (clamped <= 0) {
|
|
53
|
+
context.rect(0, 0, width, height);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
context.moveTo(clamped, 0);
|
|
57
|
+
context.lineTo(width - clamped, 0);
|
|
58
|
+
context.arcTo(width, 0, width, clamped, clamped);
|
|
59
|
+
context.lineTo(width, height - clamped);
|
|
60
|
+
context.arcTo(width, height, width - clamped, height, clamped);
|
|
61
|
+
context.lineTo(clamped, height);
|
|
62
|
+
context.arcTo(0, height, 0, height - clamped, clamped);
|
|
63
|
+
context.lineTo(0, clamped);
|
|
64
|
+
context.arcTo(0, 0, clamped, 0, clamped);
|
|
65
|
+
context.closePath();
|
|
66
|
+
}
|
|
67
|
+
/** The corner radius of a rectangle with a roundness of 0..100: 100 is a pill. */
|
|
68
|
+
export function cornerRadiusOf(shape) {
|
|
69
|
+
return (Math.max(0, Math.min(100, shape.roundness)) / 100) * (Math.min(shape.width, shape.height) / 2);
|
|
70
|
+
}
|
|
71
|
+
function applyStroke(context, shape) {
|
|
72
|
+
if (shape.stroke === 'transparent' || shape.strokeWidth <= 0)
|
|
73
|
+
return false;
|
|
74
|
+
context.strokeStyle = shape.stroke;
|
|
75
|
+
context.lineWidth = shape.strokeWidth;
|
|
76
|
+
const dash = strokeDashArray(shape.strokeStyle, shape.strokeWidth);
|
|
77
|
+
context.setLineDash(dash ?? []);
|
|
78
|
+
// A dotted line is zero-length dashes; only a round cap turns each into a dot.
|
|
79
|
+
if (shape.strokeStyle === 'dotted')
|
|
80
|
+
context.lineCap = 'round';
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
function paintFillAndStroke(context, shape, fillPath) {
|
|
84
|
+
if (shape.fill !== 'transparent' && shape.fillOpacity > 0) {
|
|
85
|
+
const before = context.globalAlpha;
|
|
86
|
+
context.globalAlpha = before * Math.max(0, Math.min(1, shape.fillOpacity));
|
|
87
|
+
context.fillStyle = shape.fill;
|
|
88
|
+
if (fillPath)
|
|
89
|
+
context.fill(fillPath);
|
|
90
|
+
else
|
|
91
|
+
context.fill();
|
|
92
|
+
context.globalAlpha = before;
|
|
93
|
+
}
|
|
94
|
+
if (applyStroke(context, shape)) {
|
|
95
|
+
if (fillPath)
|
|
96
|
+
context.stroke(fillPath);
|
|
97
|
+
else
|
|
98
|
+
context.stroke();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function fontMetricsOf(context, size) {
|
|
102
|
+
const measured = context.measureText('Hg');
|
|
103
|
+
const ascent = measured.fontBoundingBoxAscent;
|
|
104
|
+
const descent = measured.fontBoundingBoxDescent;
|
|
105
|
+
if (typeof ascent === 'number' && typeof descent === 'number' && ascent + descent > 0)
|
|
106
|
+
return { ascent, descent };
|
|
107
|
+
return { ascent: size * 0.8, descent: size * 0.2 };
|
|
108
|
+
}
|
|
109
|
+
function paintText(context, shape, deps) {
|
|
110
|
+
if (shape.backgroundColor && shape.backgroundColor !== 'transparent') {
|
|
111
|
+
context.fillStyle = shape.backgroundColor;
|
|
112
|
+
context.fillRect(0, 0, shape.width, shape.height);
|
|
113
|
+
}
|
|
114
|
+
if (shape.text === '')
|
|
115
|
+
return;
|
|
116
|
+
const layout = layoutTextObject(shape, deps.measureText);
|
|
117
|
+
context.font = fontShorthand(shape);
|
|
118
|
+
context.fillStyle = shape.color;
|
|
119
|
+
context.textBaseline = 'alphabetic';
|
|
120
|
+
context.textAlign = 'left';
|
|
121
|
+
// The text reads the way the board shows it: by the direction of its first letter (a line is placed from its left edge).
|
|
122
|
+
context.direction = textDirection(shape.text);
|
|
123
|
+
const lineHeight = lineBoxHeight(shape);
|
|
124
|
+
const { ascent, descent } = fontMetricsOf(context, shape.fontSize);
|
|
125
|
+
const innerWidth = shape.width - shape.padding * 2;
|
|
126
|
+
for (let index = 0; index < layout.lines.length; index += 1) {
|
|
127
|
+
const line = layout.lines[index];
|
|
128
|
+
if (line === '')
|
|
129
|
+
continue;
|
|
130
|
+
const lineWidth = deps.measureText(line, fontShorthand(shape));
|
|
131
|
+
let left = shape.padding;
|
|
132
|
+
if (shape.align === 'center')
|
|
133
|
+
left = shape.padding + (innerWidth - lineWidth) / 2;
|
|
134
|
+
else if (shape.align === 'right')
|
|
135
|
+
left = shape.padding + (innerWidth - lineWidth);
|
|
136
|
+
const baseline = shape.padding + index * lineHeight + (lineHeight - (ascent + descent)) / 2 + ascent;
|
|
137
|
+
context.fillText(line, left, baseline);
|
|
138
|
+
if (shape.underline) {
|
|
139
|
+
context.fillRect(left, baseline + Math.max(1, shape.fontSize * 0.08), lineWidth, Math.max(1, shape.fontSize / 15));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function paintWidget(context, shape, deps) {
|
|
144
|
+
const picture = deps.imageFor(shape.thumbnail);
|
|
145
|
+
roundedRectPath(context, shape.width, shape.height, 8);
|
|
146
|
+
context.fillStyle = '#ffffff';
|
|
147
|
+
context.fill();
|
|
148
|
+
if (picture) {
|
|
149
|
+
context.save();
|
|
150
|
+
context.clip();
|
|
151
|
+
context.drawImage(picture, 0, 0, shape.width, shape.height);
|
|
152
|
+
context.restore();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
context.strokeStyle = '#94a3b8';
|
|
156
|
+
context.lineWidth = 1.5;
|
|
157
|
+
context.setLineDash([6, 4]);
|
|
158
|
+
context.stroke();
|
|
159
|
+
context.setLineDash([]);
|
|
160
|
+
const name = shape.widgetName || deps.widgetFallbackName || '';
|
|
161
|
+
if (name === '')
|
|
162
|
+
return;
|
|
163
|
+
context.fillStyle = '#1e293b';
|
|
164
|
+
context.textAlign = 'center';
|
|
165
|
+
context.textBaseline = 'middle';
|
|
166
|
+
const size = Math.max(11, Math.min(18, shape.width * 0.06));
|
|
167
|
+
context.font = `600 ${size}px system-ui, sans-serif`;
|
|
168
|
+
context.fillText(name, shape.width / 2, shape.height / 2);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Draws one shape in its own box space and leaves the context as it found it.
|
|
172
|
+
* The context must already map world units to pixels (the caller sets its base transform).
|
|
173
|
+
*/
|
|
174
|
+
export function paintShape(context, shape, deps) {
|
|
175
|
+
if (shape.opacity <= 0)
|
|
176
|
+
return;
|
|
177
|
+
context.save();
|
|
178
|
+
try {
|
|
179
|
+
const matrix = boxToWorldMatrix(shape);
|
|
180
|
+
context.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
|
|
181
|
+
context.globalAlpha = context.globalAlpha * Math.max(0, Math.min(1, shape.opacity));
|
|
182
|
+
switch (shape.type) {
|
|
183
|
+
case 'rect': {
|
|
184
|
+
roundedRectPath(context, shape.width, shape.height, cornerRadiusOf(shape));
|
|
185
|
+
paintFillAndStroke(context, shape);
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case 'ellipse': {
|
|
189
|
+
context.beginPath();
|
|
190
|
+
context.ellipse(shape.width / 2, shape.height / 2, Math.max(0, shape.width / 2), Math.max(0, shape.height / 2), 0, 0, Math.PI * 2);
|
|
191
|
+
paintFillAndStroke(context, shape);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case 'path': {
|
|
195
|
+
context.lineCap = shape.lineCap;
|
|
196
|
+
context.lineJoin = shape.lineJoin;
|
|
197
|
+
paintFillAndStroke(context, shape, deps.createPath(shape.d));
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case 'text': {
|
|
201
|
+
paintText(context, shape, deps);
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case 'image': {
|
|
205
|
+
const picture = deps.imageFor(shape.src);
|
|
206
|
+
if (picture)
|
|
207
|
+
context.drawImage(picture, 0, 0, shape.width, shape.height);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case 'widget': {
|
|
211
|
+
paintWidget(context, shape, deps);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
context.restore();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Draws shapes (bottom first) into a PNG data URL, cropped to their bounds, strokes included. Returns an empty
|
|
222
|
+
* string when there is nothing to draw, the canvas cannot be made, or the browser refuses to export it (a picture
|
|
223
|
+
* from another site without CORS taints the canvas).
|
|
224
|
+
*/
|
|
225
|
+
export async function renderShapesToDataUrl(shapes, services, options = {}) {
|
|
226
|
+
const bounds = rasterBoundsOf(shapes);
|
|
227
|
+
if (!bounds)
|
|
228
|
+
return '';
|
|
229
|
+
const padding = Math.max(0, options.padding ?? 0);
|
|
230
|
+
// Whole world units on every side, so the crop never cuts the last pixel of a stroke.
|
|
231
|
+
const left = Math.floor(bounds.x - padding);
|
|
232
|
+
const top = Math.floor(bounds.y - padding);
|
|
233
|
+
const right = Math.ceil(bounds.x + bounds.width + padding);
|
|
234
|
+
const bottom = Math.ceil(bounds.y + bounds.height + padding);
|
|
235
|
+
const area = { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
|
|
236
|
+
const scale = rasterScaleFor(area, options);
|
|
237
|
+
const pixelWidth = Math.max(1, Math.ceil(area.width * scale));
|
|
238
|
+
const pixelHeight = Math.max(1, Math.ceil(area.height * scale));
|
|
239
|
+
const canvas = services.createCanvas(pixelWidth, pixelHeight);
|
|
240
|
+
const context = canvas?.getContext('2d') ?? null;
|
|
241
|
+
if (!canvas || !context)
|
|
242
|
+
return '';
|
|
243
|
+
const loaded = new Map();
|
|
244
|
+
await Promise.all(imageSourcesOf(shapes).map(async (source) => {
|
|
245
|
+
try {
|
|
246
|
+
const picture = await services.loadImage(source);
|
|
247
|
+
if (picture)
|
|
248
|
+
loaded.set(source, picture);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
// A picture that cannot be loaded is left out of the sketch instead of failing the whole capture.
|
|
252
|
+
}
|
|
253
|
+
}));
|
|
254
|
+
try {
|
|
255
|
+
if (options.background) {
|
|
256
|
+
context.fillStyle = options.background;
|
|
257
|
+
context.fillRect(0, 0, pixelWidth, pixelHeight);
|
|
258
|
+
}
|
|
259
|
+
context.setTransform(scale, 0, 0, scale, -area.x * scale, -area.y * scale);
|
|
260
|
+
const deps = {
|
|
261
|
+
createPath: services.createPath,
|
|
262
|
+
imageFor: (source) => (source ? (loaded.get(source) ?? null) : null),
|
|
263
|
+
measureText: services.measureText,
|
|
264
|
+
widgetFallbackName: options.widgetFallbackName
|
|
265
|
+
};
|
|
266
|
+
for (const shape of shapes)
|
|
267
|
+
paintShape(context, shape, deps);
|
|
268
|
+
return canvas.toDataURL('image/png');
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
return '';
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything the board shows that is not a saved object: which tool is active, what is selected, where the view
|
|
3
|
+
* is, what is half-drawn. One immutable snapshot, replaced on every change, so a view can hold on to a slice of
|
|
4
|
+
* it (the scene, the selection, the viewport) and redraw only when that slice is a different object.
|
|
5
|
+
*/
|
|
6
|
+
import type { ArrowHandleId } from './core/arrow.js';
|
|
7
|
+
import type { Scene } from './core/scene.js';
|
|
8
|
+
import type { ScribbleView } from './core/scribble.js';
|
|
9
|
+
import type { SnapGuide } from './core/snapping.js';
|
|
10
|
+
import type { ObjectId, Rect, SceneObject, Size, Viewport } from './core/types.js';
|
|
11
|
+
export type ToolId = 'select' | 'draw' | 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape' | 'text' | 'erase' | 'pan';
|
|
12
|
+
/** The tools that drag a shape out. */
|
|
13
|
+
export type ShapeToolId = 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape';
|
|
14
|
+
export declare const SHAPE_TOOLS: readonly ToolId[];
|
|
15
|
+
/** A widget card whose widget the host is still working on. Not saved. */
|
|
16
|
+
export type PendingWidgetState = 'duplicating' | 'linking' | 'converting';
|
|
17
|
+
/**
|
|
18
|
+
* What the host says about the card of one widget, by widget id. None of it is saved: the host sets it again
|
|
19
|
+
* after every load.
|
|
20
|
+
*/
|
|
21
|
+
export interface WidgetRuntime {
|
|
22
|
+
/** A live widget is drawn over the card, so the screenshot under it is hidden. */
|
|
23
|
+
hiddenImage: boolean;
|
|
24
|
+
/** The widget behind the card is gone (deleted or moved to another canvas). */
|
|
25
|
+
orphaned: boolean;
|
|
26
|
+
/** The card may be dragged by its body. Off while a live widget covers it: its own bar moves it. */
|
|
27
|
+
bodyDragAllowed: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare const DEFAULT_WIDGET_RUNTIME: WidgetRuntime;
|
|
30
|
+
/** What the person is doing with the selection once the press became a drag (a press that has not moved yet is none of these). */
|
|
31
|
+
export type SelectionDrag = 'move' | 'resize' | 'rotate';
|
|
32
|
+
/** The figure the board shows in the middle of the selection while it is stretched or turned. */
|
|
33
|
+
export type Readout = {
|
|
34
|
+
kind: 'size';
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'angle';
|
|
39
|
+
degrees: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A pointer the board draws itself, in board pixels, while the real one is hidden: when a text's corner is dragged the
|
|
43
|
+
* corner stays on the text's own grid of letters and does not follow the hand, so the arrow is drawn on the corner.
|
|
44
|
+
* `angle` is the direction of the double arrow, in degrees (0 is horizontal).
|
|
45
|
+
*/
|
|
46
|
+
export interface VirtualCursor {
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
angle: number;
|
|
50
|
+
}
|
|
51
|
+
/** What the board draws on top of the objects while something is being done. */
|
|
52
|
+
export interface EngineOverlay {
|
|
53
|
+
/** The rectangle being dragged out to select (world units). */
|
|
54
|
+
marquee: Rect | null;
|
|
55
|
+
/**
|
|
56
|
+
* What the marquee would select if it were let go now: the units it touches, with the ones a Shift-marquee adds to. The
|
|
57
|
+
* board outlines them the way it outlines the unit under the pointer, so the person sees what the release will take; the
|
|
58
|
+
* selection itself is made at the release. The engine hands over a new list only when the units change, never for a
|
|
59
|
+
* marquee that merely grew over the same ones. Ids of units, in paint order.
|
|
60
|
+
*/
|
|
61
|
+
marqueeHover: readonly ObjectId[];
|
|
62
|
+
/** Alignment guides to draw (world units). */
|
|
63
|
+
guides: readonly SnapGuide[];
|
|
64
|
+
/** The shape or stroke being drawn, drawn like any other object. */
|
|
65
|
+
preview: SceneObject | null;
|
|
66
|
+
/** The dashed box of a text field being dragged out (world units). */
|
|
67
|
+
textDraft: Rect | null;
|
|
68
|
+
/** What the selection is being put through. The frame follows it: the handles go while it is carried, the outline dashes while it is stretched. */
|
|
69
|
+
dragging: SelectionDrag | null;
|
|
70
|
+
/** The size or the angle shown in the middle of the selection while it is stretched or turned. */
|
|
71
|
+
readout: Readout | null;
|
|
72
|
+
/** The pointer drawn on a handle while the real one is hidden. */
|
|
73
|
+
virtualCursor: VirtualCursor | null;
|
|
74
|
+
/**
|
|
75
|
+
* The shapes the eraser has marked: they stay where they are, drawn faded, until the pointer is released (then they go in
|
|
76
|
+
* one undo step) or Escape leaves them as they were. Ids of shapes, never of groups: a group is marked by its members.
|
|
77
|
+
*/
|
|
78
|
+
erasing: readonly ObjectId[];
|
|
79
|
+
}
|
|
80
|
+
export declare const EMPTY_OVERLAY: EngineOverlay;
|
|
81
|
+
export interface EngineState {
|
|
82
|
+
scene: Scene;
|
|
83
|
+
/** The selected units: top-level objects (a group is one), or members of the group that has been entered. */
|
|
84
|
+
selectedIds: readonly ObjectId[];
|
|
85
|
+
/** A group whose members can be picked one by one (after a double click). */
|
|
86
|
+
enteredGroupId: ObjectId | null;
|
|
87
|
+
/** The unit under the pointer, outlined as a hint. One at a time. */
|
|
88
|
+
hoverId: ObjectId | null;
|
|
89
|
+
/** The pointer is over the turning dot under the selection: the dot grows. */
|
|
90
|
+
rotationDotHovered: boolean;
|
|
91
|
+
/** The end or the middle of the selected arrow the pointer is over, or is dragging: that handle grows. */
|
|
92
|
+
arrowHandleHovered: ArrowHandleId | null;
|
|
93
|
+
viewport: Viewport;
|
|
94
|
+
/** The size of the board element in CSS pixels. */
|
|
95
|
+
viewSize: Size;
|
|
96
|
+
tool: ToolId;
|
|
97
|
+
/** The shape the library-shape tool places. */
|
|
98
|
+
libraryShapeKey: string | null;
|
|
99
|
+
/** Keep the tool after a shape has been placed (otherwise the tool goes back to Select). */
|
|
100
|
+
stickyTool: boolean;
|
|
101
|
+
/** The CSS cursor of the board. */
|
|
102
|
+
cursor: string;
|
|
103
|
+
overlay: EngineOverlay;
|
|
104
|
+
/** The trail of the eraser while it lives (it outlasts the press by a few frames, as it is eaten away), else null. */
|
|
105
|
+
scribble: ScribbleView | null;
|
|
106
|
+
/** The text object whose text is being typed. */
|
|
107
|
+
editingTextId: ObjectId | null;
|
|
108
|
+
canUndo: boolean;
|
|
109
|
+
canRedo: boolean;
|
|
110
|
+
/** The host is still fetching what the board will show. */
|
|
111
|
+
loading: boolean;
|
|
112
|
+
pending: Readonly<Record<ObjectId, PendingWidgetState>>;
|
|
113
|
+
/** What the host said about each widget card, by widget id. */
|
|
114
|
+
widgets: Readonly<Record<string, WidgetRuntime>>;
|
|
115
|
+
/** The widget whose card is under the pointer (the host draws the bar of its live widget for it). */
|
|
116
|
+
hoverWidgetId: string | null;
|
|
117
|
+
/** The host is moving a card from its own bar: nothing is picked by hovering meanwhile. */
|
|
118
|
+
overlayDragActive: boolean;
|
|
119
|
+
/** Counts every change of the scene. A cheap way to tell that anything changed. */
|
|
120
|
+
revision: number;
|
|
121
|
+
/** Counts the times fonts finished loading: text is laid out again, because letters changed width. */
|
|
122
|
+
fontRevision: number;
|
|
123
|
+
}
|
|
124
|
+
export declare function createInitialState(): EngineState;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything the board shows that is not a saved object: which tool is active, what is selected, where the view
|
|
3
|
+
* is, what is half-drawn. One immutable snapshot, replaced on every change, so a view can hold on to a slice of
|
|
4
|
+
* it (the scene, the selection, the viewport) and redraw only when that slice is a different object.
|
|
5
|
+
*/
|
|
6
|
+
import { INITIAL_VIEWPORT } from './core/viewport.js';
|
|
7
|
+
export const SHAPE_TOOLS = ['rect', 'rounded-rect', 'ellipse', 'circle', 'triangle', 'arrow', 'library-shape'];
|
|
8
|
+
export const DEFAULT_WIDGET_RUNTIME = Object.freeze({ hiddenImage: false, orphaned: false, bodyDragAllowed: false });
|
|
9
|
+
export const EMPTY_OVERLAY = Object.freeze({
|
|
10
|
+
marquee: null,
|
|
11
|
+
marqueeHover: Object.freeze([]),
|
|
12
|
+
guides: Object.freeze([]),
|
|
13
|
+
preview: null,
|
|
14
|
+
textDraft: null,
|
|
15
|
+
dragging: null,
|
|
16
|
+
readout: null,
|
|
17
|
+
virtualCursor: null,
|
|
18
|
+
erasing: Object.freeze([])
|
|
19
|
+
});
|
|
20
|
+
export function createInitialState() {
|
|
21
|
+
return {
|
|
22
|
+
scene: [],
|
|
23
|
+
selectedIds: [],
|
|
24
|
+
enteredGroupId: null,
|
|
25
|
+
hoverId: null,
|
|
26
|
+
rotationDotHovered: false,
|
|
27
|
+
arrowHandleHovered: null,
|
|
28
|
+
viewport: { ...INITIAL_VIEWPORT },
|
|
29
|
+
viewSize: { width: 0, height: 0 },
|
|
30
|
+
tool: 'select',
|
|
31
|
+
libraryShapeKey: null,
|
|
32
|
+
stickyTool: false,
|
|
33
|
+
cursor: 'default',
|
|
34
|
+
overlay: EMPTY_OVERLAY,
|
|
35
|
+
scribble: null,
|
|
36
|
+
editingTextId: null,
|
|
37
|
+
canUndo: false,
|
|
38
|
+
canRedo: false,
|
|
39
|
+
loading: false,
|
|
40
|
+
pending: Object.freeze({}),
|
|
41
|
+
widgets: Object.freeze({}),
|
|
42
|
+
hoverWidgetId: null,
|
|
43
|
+
overlayDragActive: false,
|
|
44
|
+
revision: 0,
|
|
45
|
+
fontRevision: 0
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laying out the text of a text object: which letters go on which line, and how big the box is.
|
|
3
|
+
*
|
|
4
|
+
* The layout is a pure function of the text, the style, the width and a function that measures a piece of text. The
|
|
5
|
+
* browser measures with a canvas context and the tests with a fixed advance, but both give the same answer for the
|
|
6
|
+
* same measuring function. The board draws the lines this returns (each on a line of its own, never wrapped by the
|
|
7
|
+
* browser), and the sketch the AI is shown is drawn from the same lines, so what is on the screen is what is sent.
|
|
8
|
+
*/
|
|
9
|
+
import type { TextObject } from '../core/types.js';
|
|
10
|
+
/** Room so the last letter and the caret stay on the line; more than this reads as padding. */
|
|
11
|
+
export declare const TEXT_FIT_SLACK = 4;
|
|
12
|
+
/** Extra width for italic slant and serif overhang beyond what `measureText` returns. */
|
|
13
|
+
export declare function textFitHorizontalSlack(style: Pick<TextStyleFields, 'fontSize' | 'fontStyle'>): number;
|
|
14
|
+
/** Measures the width of one line of text in a CSS `font` shorthand. */
|
|
15
|
+
export type MeasureText = (text: string, font: string) => number;
|
|
16
|
+
export type TextStyleFields = Pick<TextObject, 'fontSize' | 'fontFamily' | 'fontWeight' | 'fontStyle' | 'lineHeight' | 'padding'>;
|
|
17
|
+
export interface TextLayout {
|
|
18
|
+
/** The lines, top to bottom. An empty text has one empty line. */
|
|
19
|
+
lines: string[];
|
|
20
|
+
/** The widest line, in world units. */
|
|
21
|
+
contentWidth: number;
|
|
22
|
+
/** The size of the whole box, padding included. */
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
}
|
|
26
|
+
/** The CSS `font` shorthand of a text object. */
|
|
27
|
+
export declare function fontShorthand(style: Pick<TextStyleFields, 'fontSize' | 'fontFamily' | 'fontWeight' | 'fontStyle'>): string;
|
|
28
|
+
/** The height of one line, in world units. */
|
|
29
|
+
export declare function lineBoxHeight(style: Pick<TextStyleFields, 'fontSize' | 'lineHeight'>): number;
|
|
30
|
+
/** The vertical room one line needs on the board: full ascent and descent, not only the CSS line-height box. */
|
|
31
|
+
export declare function textLineExtent(style: Pick<TextStyleFields, 'fontSize' | 'lineHeight'>): number;
|
|
32
|
+
/** The way a text is written across a line. */
|
|
33
|
+
export type TextDirection = 'ltr' | 'rtl';
|
|
34
|
+
/**
|
|
35
|
+
* The direction a text is written in: the one of its first letter. This is what the browser's `dir="auto"` does, so the
|
|
36
|
+
* board (a drawing that is `ltr` on every screen), the field a text is typed in and the sketch that is drawn for the AI
|
|
37
|
+
* all place the same text the same way. A text with no letter (digits, signs, nothing) reads left to right.
|
|
38
|
+
*/
|
|
39
|
+
export declare function textDirection(text: string): TextDirection;
|
|
40
|
+
/**
|
|
41
|
+
* Lays text out.
|
|
42
|
+
* `fixedWidth` is the width of the box when the text wraps; null lets the box be as wide as the longest line.
|
|
43
|
+
*/
|
|
44
|
+
export declare function layoutText(text: string, style: TextStyleFields, fixedWidth: number | null, measure: MeasureText): TextLayout;
|
|
45
|
+
/** The layout of a text object as it stands. */
|
|
46
|
+
export declare function layoutTextObject(shape: TextObject, measure: MeasureText): TextLayout;
|
|
47
|
+
/**
|
|
48
|
+
* The text object with the size its text needs: the width when it follows the text, the height always.
|
|
49
|
+
* The same object comes back when nothing changes, so an unchanged text is not redrawn.
|
|
50
|
+
*/
|
|
51
|
+
export declare function fitTextObject(shape: TextObject, measure: MeasureText): TextObject;
|
|
52
|
+
/** A measuring function that needs no browser: every letter is `advance` times the font size wide. For tests and Node. */
|
|
53
|
+
export declare function createFixedAdvanceMeasure(advance?: number): MeasureText;
|