@widgetic/canvas 0.5.46 → 0.5.48
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 +4 -1
- package/dist/canvas/DomCanvas.svelte +5 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +1 -0
- package/dist/canvas/eventScope.js +7 -1
- package/dist/canvas/host/CanvasHost.d.ts +5 -3
- package/dist/canvas/host/CanvasHost.js +114 -74
- package/dist/canvas/host/pauseCollector.d.ts +18 -0
- package/dist/canvas/host/pauseCollector.js +45 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +40 -3
- package/dist/canvas/ui/CanvasHint.svelte +10 -9
- package/dist/canvas/ui/CanvasToolbar.svelte +17 -14
- package/dist/canvas/ui/SelectionPanel.svelte +14 -12
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +0 -2
- package/dist/canvas/ui/ShapesMenu.svelte +6 -4
- package/dist/canvas/ui/ToolButton.svelte +7 -6
- package/dist/canvas/ui/floatingCard.d.ts +28 -8
- package/dist/canvas/ui/floatingCard.js +28 -8
- package/dist/canvas/ui/selectionTraits.d.ts +8 -4
- package/dist/canvas/ui/selectionTraits.js +10 -18
- package/dist/canvas/view/SelectionOverlay.svelte +5 -5
- package/dist/canvas/view/WidgetChromeLayer.svelte +10 -2
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +1 -1
- package/dist/components/input-controls/FontSelectorIC.svelte +1 -1
- package/dist/engine/EngineCore.d.ts +33 -8
- package/dist/engine/EngineCore.js +66 -30
- package/dist/engine/core/brush.d.ts +24 -0
- package/dist/engine/core/brush.js +145 -13
- package/dist/engine/core/shapeTools.d.ts +23 -18
- package/dist/engine/core/shapeTools.js +45 -17
- package/dist/engine/core/style.d.ts +18 -2
- package/dist/engine/core/style.js +57 -25
- package/dist/engine/core/styleMemory.d.ts +88 -0
- package/dist/engine/core/styleMemory.js +189 -0
- package/dist/engine/input/gestures.d.ts +8 -3
- package/dist/engine/input/gestures.js +123 -48
- package/dist/engine/input/keyboard.js +14 -5
- package/dist/poster/boardPlacement.d.ts +4 -1
- package/dist/poster/boardPlacement.js +6 -3
- package/dist/settings/rememberedStyles.d.ts +22 -0
- package/dist/settings/rememberedStyles.js +47 -0
- package/dist/settings/rememberedTool.js +2 -1
- package/dist/settings/workspaceToolSettings.d.ts +17 -0
- package/dist/settings/workspaceToolSettings.js +4 -1
- package/dist/tools/hostAdapter.js +0 -1
- package/package.json +4 -4
|
@@ -53,8 +53,11 @@ export function pathSupportsRoundness(shapeKey) {
|
|
|
53
53
|
function roundnessIsLineEnds(shapeKey) {
|
|
54
54
|
return getShapeByKey(shapeKey)?.cornerRadiusMode === 'strokeRound';
|
|
55
55
|
}
|
|
56
|
-
/**
|
|
57
|
-
|
|
56
|
+
/**
|
|
57
|
+
* The widest a line may be made with the Width control, for every kind of shape, an arrow and a line drawn by hand (the pen's too: see
|
|
58
|
+
* `EngineCore.setPenStyle`). It was 200; a line thicker than 50 is not needed. A board saved with a thicker one opens as it was.
|
|
59
|
+
*/
|
|
60
|
+
export const MAX_STROKE_WIDTH = 50;
|
|
58
61
|
/** The thinnest line a shape that is only a line can be given (the brush's own least is the same): with none, nothing of it is left. */
|
|
59
62
|
export const MIN_LINE_STROKE_WIDTH = 1;
|
|
60
63
|
/**
|
|
@@ -94,49 +97,78 @@ export function lineColorOf(shape) {
|
|
|
94
97
|
return shape.type === 'rect' || shape.type === 'ellipse' || shape.type === 'path' ? shape.stroke : '';
|
|
95
98
|
}
|
|
96
99
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
100
|
+
* The keys of a patch that a brush stroke takes, at the values it takes them at: the Stroke colour is the colour of the outline, the
|
|
101
|
+
* Width is the nominal width of the brush (from the brush's least to the widest), and the opacity is the whole stroke's. Everything else
|
|
102
|
+
* the patch carries means nothing to a stroke and is left out.
|
|
99
103
|
*/
|
|
104
|
+
function acceptedBrushStylePatch(patch) {
|
|
105
|
+
const accepted = {};
|
|
106
|
+
if (patch.opacity !== undefined)
|
|
107
|
+
accepted.opacity = clampNumber(patch.opacity, 0, 1);
|
|
108
|
+
if (patch.stroke !== undefined)
|
|
109
|
+
accepted.stroke = patch.stroke;
|
|
110
|
+
if (patch.strokeWidth !== undefined)
|
|
111
|
+
accepted.strokeWidth = clampNumber(patch.strokeWidth, MIN_BRUSH_WIDTH, MAX_STROKE_WIDTH);
|
|
112
|
+
return accepted;
|
|
113
|
+
}
|
|
114
|
+
/** A patch for a brush stroke: the Stroke colour is the colour of the outline, the Width draws the brush again at the new width. Same object back when nothing changes. */
|
|
100
115
|
function applyBrushStylePatch(shape, patch) {
|
|
116
|
+
const accepted = acceptedBrushStylePatch(patch);
|
|
101
117
|
let next = shape;
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
next = { ...next, fill: patch.stroke };
|
|
109
|
-
if (patch.strokeWidth !== undefined) {
|
|
110
|
-
const strokeWidth = clampNumber(patch.strokeWidth, MIN_BRUSH_WIDTH, MAX_STROKE_WIDTH);
|
|
111
|
-
if (strokeWidth !== next.strokeWidth)
|
|
112
|
-
next = reshapeBrushStroke(next, { strokeWidth });
|
|
113
|
-
}
|
|
118
|
+
if (accepted.opacity !== undefined && accepted.opacity !== next.opacity)
|
|
119
|
+
next = { ...next, opacity: accepted.opacity };
|
|
120
|
+
if (accepted.stroke !== undefined && accepted.stroke !== next.fill)
|
|
121
|
+
next = { ...next, fill: accepted.stroke };
|
|
122
|
+
if (accepted.strokeWidth !== undefined && accepted.strokeWidth !== next.strokeWidth)
|
|
123
|
+
next = reshapeBrushStroke(next, { strokeWidth: accepted.strokeWidth });
|
|
114
124
|
return next;
|
|
115
125
|
}
|
|
116
|
-
/**
|
|
117
|
-
|
|
126
|
+
/**
|
|
127
|
+
* A number of a patch brought into the range its key has (a width from 0 to 50, an opacity from 0 to 1, a size from 4 to 600 ...). A key
|
|
128
|
+
* with no range keeps the number as it is. The one place the ranges are applied, so a value kept for later and a value written to a shape
|
|
129
|
+
* can never disagree on what the key allows.
|
|
130
|
+
*/
|
|
131
|
+
export function clampedStyleNumber(key, value) {
|
|
132
|
+
const range = RANGES[key];
|
|
133
|
+
return range ? clampNumber(value, range[0], range[1]) : value;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The keys of a patch that a shape takes, at the values it takes them at: what `applyStylePatch` writes into the shape, whether or not
|
|
137
|
+
* the shape already has it. A number is brought into its range, a line is never made so thin that it is gone, and the Fill of a shape
|
|
138
|
+
* that is only a line (a check, a cross) is its Stroke. This is also what an edit of the selection says about the next element of its
|
|
139
|
+
* kind (see `styleMemory.ts`), so the memory and the shape always agree on what a value is.
|
|
140
|
+
*/
|
|
141
|
+
export function acceptedStylePatch(shape, requested) {
|
|
118
142
|
if (isBrushStroke(shape))
|
|
119
|
-
return
|
|
143
|
+
return acceptedBrushStylePatch(requested);
|
|
120
144
|
let patch = requested;
|
|
121
145
|
if (patch.fill !== undefined && paintsFillOnStroke(shape)) {
|
|
122
146
|
// The panel's Fill colour is the colour of the line for these icons.
|
|
123
147
|
const { fill, ...rest } = patch;
|
|
124
148
|
patch = { ...rest, stroke: fill };
|
|
125
149
|
}
|
|
126
|
-
const
|
|
127
|
-
const
|
|
128
|
-
for (const key of allowed) {
|
|
150
|
+
const accepted = {};
|
|
151
|
+
for (const key of keysFor(shape)) {
|
|
129
152
|
if (!(key in patch))
|
|
130
153
|
continue;
|
|
131
154
|
let value = patch[key];
|
|
132
155
|
if (value === undefined)
|
|
133
156
|
continue;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
value = clampNumber(value, range[0], range[1]);
|
|
157
|
+
if (typeof value === 'number')
|
|
158
|
+
value = clampedStyleNumber(key, value);
|
|
137
159
|
// A line is never made so thin that it is gone (an arrow with no line would vanish with nothing left to take hold of).
|
|
138
160
|
if (key === 'strokeWidth' && typeof value === 'number')
|
|
139
161
|
value = Math.max(value, minimumStrokeWidthOf(shape));
|
|
162
|
+
accepted[key] = value;
|
|
163
|
+
}
|
|
164
|
+
return accepted;
|
|
165
|
+
}
|
|
166
|
+
/** Applies the keys of a patch that mean something for the shape. Same object back when nothing changes. */
|
|
167
|
+
export function applyStylePatch(shape, requested) {
|
|
168
|
+
if (isBrushStroke(shape))
|
|
169
|
+
return applyBrushStylePatch(shape, requested);
|
|
170
|
+
const changes = {};
|
|
171
|
+
for (const [key, value] of Object.entries(acceptedStylePatch(shape, requested))) {
|
|
140
172
|
if (shape[key] !== value)
|
|
141
173
|
changes[key] = value;
|
|
142
174
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the board remembers of how the person last styled each kind of element, so that the next element of that kind starts the way
|
|
3
|
+
* the last one was left ("after changing the fill and the colour of the stroke and the stroke width, those should be maintained across
|
|
4
|
+
* all the shapes until the user edits them again. The same should be true for the other elements as well", 19 Nov 2026).
|
|
5
|
+
*
|
|
6
|
+
* An element is of one of five kinds, and each kind remembers only what the person did to elements of that kind:
|
|
7
|
+
*
|
|
8
|
+
* - `shape`: a rectangle, an ellipse, a triangle and every shape of the library that has an inside (fill, stroke, width, opacity, roundness).
|
|
9
|
+
* - `lineIcon`: the four shapes of the library that are only a line (a check, a cross, a plus, a minus): the colour of the line, its width,
|
|
10
|
+
* its opacity and how round its ends are. They have no inside, so a fill chosen for a rectangle is no business of theirs, and a colour
|
|
11
|
+
* chosen for a check does not turn the next rectangle's outline.
|
|
12
|
+
* - `arrow`: the colour, the width and the opacity of the line (an arrow has no inside and no corners).
|
|
13
|
+
* - `text`: the font, the size, bold, italic, underline, the alignment, the colour of the letters and the opacity.
|
|
14
|
+
* - `pen`: a line drawn by hand. Its three settings are the pen of the Draw tool (`EngineState.penStyle`), so an edit of a drawn line is
|
|
15
|
+
* an edit of the pen, and the engine keeps the pen itself; this file only says that the edit is one.
|
|
16
|
+
*
|
|
17
|
+
* The memory is a set of patches in the panel's own keys (`StylePatch`). An edit of the selection is turned into one by `styleEditOf`,
|
|
18
|
+
* through `acceptedStylePatch`, so a value is remembered exactly as the shape took it (a width of 0 is 1 for an arrow, a width of 900 is
|
|
19
|
+
* 50), and a new element is given the memory by `withRememberedStyle`, through `applyStylePatch`, the one door every style goes through:
|
|
20
|
+
* the head of an arrow follows its width and the corners of a triangle are drawn again for their roundness, as when the panel is used.
|
|
21
|
+
*
|
|
22
|
+
* Everything here is a pure function of values. The engine holds the memory, records the edits of `setStyle` into it and tells the host,
|
|
23
|
+
* which keeps it for the workspace (`CanvasHost`, `workspaceToolSettings.ts`).
|
|
24
|
+
*/
|
|
25
|
+
import type { StylePatch } from './style.js';
|
|
26
|
+
import type { ShapeObject } from './types.js';
|
|
27
|
+
/** The kinds of element whose last style is remembered. */
|
|
28
|
+
export type StyleMemoryKind = 'shape' | 'lineIcon' | 'arrow' | 'text' | 'pen';
|
|
29
|
+
/** The kinds remembered as a patch of the panel's keys: the pen is not (it is the pen of the Draw tool, `EngineState.penStyle`). */
|
|
30
|
+
export type PatchedStyleKind = Exclude<StyleMemoryKind, 'pen'>;
|
|
31
|
+
export declare const PATCHED_STYLE_KINDS: readonly PatchedStyleKind[];
|
|
32
|
+
/**
|
|
33
|
+
* The keys of an edit that each kind remembers. What is not here is the element's own business and is never carried over: its place and
|
|
34
|
+
* size, its text, how it is rotated, what it is tied to. (A key that a kind does not take at all, such as a fill for an arrow, is not
|
|
35
|
+
* here either: `acceptedStylePatch` has already dropped it.)
|
|
36
|
+
*/
|
|
37
|
+
export declare const REMEMBERED_STYLE_KEYS: Readonly<Record<StyleMemoryKind, readonly (keyof StylePatch)[]>>;
|
|
38
|
+
/** The kind of element a shape is, or null for what has no style to remember (a picture, a widget card). */
|
|
39
|
+
export declare function styleMemoryKindOf(shape: ShapeObject): StyleMemoryKind | null;
|
|
40
|
+
/** What an edit of one element says about the next element of its kind. */
|
|
41
|
+
export interface StyleEdit {
|
|
42
|
+
kind: StyleMemoryKind;
|
|
43
|
+
style: StylePatch;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What an edit says about the next element of the shape's kind: the keys of the patch that the shape takes and that its kind
|
|
47
|
+
* remembers, at the values the shape takes them at. Null when it says nothing (a picture, or a patch of keys the kind has no use for).
|
|
48
|
+
*
|
|
49
|
+
* The keys the shape already holds are in it too: a person who picks the colour a shape has already has said what the next one should
|
|
50
|
+
* be, and it may be another colour than the memory holds.
|
|
51
|
+
*/
|
|
52
|
+
export declare function styleEditOf(shape: ShapeObject, patch: StylePatch): StyleEdit | null;
|
|
53
|
+
/** The last style of each kind of element that is remembered as a patch (empty where the person has not styled one yet). */
|
|
54
|
+
export type RememberedStyles = {
|
|
55
|
+
readonly [kind in PatchedStyleKind]: Readonly<StylePatch>;
|
|
56
|
+
};
|
|
57
|
+
export declare const NO_REMEMBERED_STYLES: RememberedStyles;
|
|
58
|
+
/**
|
|
59
|
+
* The memory with some edits laid over it, and the kinds that came out different. Where the edits say nothing new the very same object
|
|
60
|
+
* comes back with no kind named, so a drag that goes on saying the same colour tells nobody anything. The pen is not part of the memory
|
|
61
|
+
* (the engine keeps it), so an edit of a drawn line is passed over here.
|
|
62
|
+
*/
|
|
63
|
+
export declare function rememberStyleEdits(styles: RememberedStyles, edits: readonly StyleEdit[]): {
|
|
64
|
+
styles: RememberedStyles;
|
|
65
|
+
changed: PatchedStyleKind[];
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* What the storage holds for a kind, as a memory can hold it: key by key, a value that is not one of the key is dropped (the rest stays),
|
|
69
|
+
* a number is brought into its range (a width that was kept when the widest was 200 comes down to 50), and a key the kind does not
|
|
70
|
+
* remember is left out. Anything that is not an object is no memory at all.
|
|
71
|
+
*/
|
|
72
|
+
export declare function sanitizeRememberedStyle(kind: PatchedStyleKind, stored: unknown): Readonly<StylePatch>;
|
|
73
|
+
/**
|
|
74
|
+
* What the storage holds for every kind, as a memory can hold it (`sanitizeRememberedStyle` for each). Nothing stored: nothing remembered.
|
|
75
|
+
* A kind the storage has no part for is nothing remembered for that kind.
|
|
76
|
+
*/
|
|
77
|
+
export declare function sanitizeRememberedStyles(stored: {
|
|
78
|
+
readonly [kind in PatchedStyleKind]?: unknown;
|
|
79
|
+
} | null): RememberedStyles;
|
|
80
|
+
/**
|
|
81
|
+
* A new element with what the person last chose for its kind laid over what it starts with. `leave` names keys of the memory that the
|
|
82
|
+
* element keeps as it starts (the rounded square keeps the soft corners it is chosen for). The element comes out as the panel would
|
|
83
|
+
* make it: the head of an arrow follows its width, the corners of a triangle are drawn again for their roundness. The very same object
|
|
84
|
+
* back when there is nothing to lay over.
|
|
85
|
+
*/
|
|
86
|
+
export declare function withRememberedStyle<Element extends ShapeObject>(element: Element, remembered: Readonly<StylePatch> | undefined, options?: {
|
|
87
|
+
leave?: readonly (keyof StylePatch)[];
|
|
88
|
+
}): Element;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the board remembers of how the person last styled each kind of element, so that the next element of that kind starts the way
|
|
3
|
+
* the last one was left ("after changing the fill and the colour of the stroke and the stroke width, those should be maintained across
|
|
4
|
+
* all the shapes until the user edits them again. The same should be true for the other elements as well", 19 Nov 2026).
|
|
5
|
+
*
|
|
6
|
+
* An element is of one of five kinds, and each kind remembers only what the person did to elements of that kind:
|
|
7
|
+
*
|
|
8
|
+
* - `shape`: a rectangle, an ellipse, a triangle and every shape of the library that has an inside (fill, stroke, width, opacity, roundness).
|
|
9
|
+
* - `lineIcon`: the four shapes of the library that are only a line (a check, a cross, a plus, a minus): the colour of the line, its width,
|
|
10
|
+
* its opacity and how round its ends are. They have no inside, so a fill chosen for a rectangle is no business of theirs, and a colour
|
|
11
|
+
* chosen for a check does not turn the next rectangle's outline.
|
|
12
|
+
* - `arrow`: the colour, the width and the opacity of the line (an arrow has no inside and no corners).
|
|
13
|
+
* - `text`: the font, the size, bold, italic, underline, the alignment, the colour of the letters and the opacity.
|
|
14
|
+
* - `pen`: a line drawn by hand. Its three settings are the pen of the Draw tool (`EngineState.penStyle`), so an edit of a drawn line is
|
|
15
|
+
* an edit of the pen, and the engine keeps the pen itself; this file only says that the edit is one.
|
|
16
|
+
*
|
|
17
|
+
* The memory is a set of patches in the panel's own keys (`StylePatch`). An edit of the selection is turned into one by `styleEditOf`,
|
|
18
|
+
* through `acceptedStylePatch`, so a value is remembered exactly as the shape took it (a width of 0 is 1 for an arrow, a width of 900 is
|
|
19
|
+
* 50), and a new element is given the memory by `withRememberedStyle`, through `applyStylePatch`, the one door every style goes through:
|
|
20
|
+
* the head of an arrow follows its width and the corners of a triangle are drawn again for their roundness, as when the panel is used.
|
|
21
|
+
*
|
|
22
|
+
* Everything here is a pure function of values. The engine holds the memory, records the edits of `setStyle` into it and tells the host,
|
|
23
|
+
* which keeps it for the workspace (`CanvasHost`, `workspaceToolSettings.ts`).
|
|
24
|
+
*/
|
|
25
|
+
import { isArrow } from './arrow.js';
|
|
26
|
+
import { isBrushStroke } from './brush.js';
|
|
27
|
+
import { acceptedStylePatch, applyStylePatch, clampedStyleNumber, paintsFillOnStroke } from './style.js';
|
|
28
|
+
export const PATCHED_STYLE_KINDS = ['shape', 'lineIcon', 'arrow', 'text'];
|
|
29
|
+
// The keys of the panel that each kind remembers (a list for each kind, so that `REMEMBERED_STYLE_KEYS` below reads as a table).
|
|
30
|
+
const SHAPE_STYLE_KEYS = ['fill', 'stroke', 'strokeWidth', 'strokeStyle', 'opacity', 'roundness'];
|
|
31
|
+
const LINE_ICON_STYLE_KEYS = ['stroke', 'strokeWidth', 'strokeStyle', 'opacity', 'roundness'];
|
|
32
|
+
const ARROW_STYLE_KEYS = ['stroke', 'strokeWidth', 'strokeStyle', 'opacity'];
|
|
33
|
+
const TEXT_STYLE_KEYS = ['color', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'underline', 'align', 'opacity'];
|
|
34
|
+
const PEN_STYLE_KEYS = ['stroke', 'strokeWidth', 'opacity'];
|
|
35
|
+
/**
|
|
36
|
+
* The keys of an edit that each kind remembers. What is not here is the element's own business and is never carried over: its place and
|
|
37
|
+
* size, its text, how it is rotated, what it is tied to. (A key that a kind does not take at all, such as a fill for an arrow, is not
|
|
38
|
+
* here either: `acceptedStylePatch` has already dropped it.)
|
|
39
|
+
*/
|
|
40
|
+
export const REMEMBERED_STYLE_KEYS = Object.freeze({
|
|
41
|
+
shape: SHAPE_STYLE_KEYS,
|
|
42
|
+
lineIcon: LINE_ICON_STYLE_KEYS,
|
|
43
|
+
arrow: ARROW_STYLE_KEYS,
|
|
44
|
+
text: TEXT_STYLE_KEYS,
|
|
45
|
+
pen: PEN_STYLE_KEYS
|
|
46
|
+
});
|
|
47
|
+
/** The kind of element a shape is, or null for what has no style to remember (a picture, a widget card). */
|
|
48
|
+
export function styleMemoryKindOf(shape) {
|
|
49
|
+
switch (shape.type) {
|
|
50
|
+
case 'rect':
|
|
51
|
+
case 'ellipse':
|
|
52
|
+
return 'shape';
|
|
53
|
+
case 'text':
|
|
54
|
+
return 'text';
|
|
55
|
+
case 'path':
|
|
56
|
+
if (isBrushStroke(shape))
|
|
57
|
+
return 'pen';
|
|
58
|
+
if (isArrow(shape))
|
|
59
|
+
return 'arrow';
|
|
60
|
+
return paintsFillOnStroke(shape) ? 'lineIcon' : 'shape';
|
|
61
|
+
default:
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* What an edit says about the next element of the shape's kind: the keys of the patch that the shape takes and that its kind
|
|
67
|
+
* remembers, at the values the shape takes them at. Null when it says nothing (a picture, or a patch of keys the kind has no use for).
|
|
68
|
+
*
|
|
69
|
+
* The keys the shape already holds are in it too: a person who picks the colour a shape has already has said what the next one should
|
|
70
|
+
* be, and it may be another colour than the memory holds.
|
|
71
|
+
*/
|
|
72
|
+
export function styleEditOf(shape, patch) {
|
|
73
|
+
const kind = styleMemoryKindOf(shape);
|
|
74
|
+
if (!kind)
|
|
75
|
+
return null;
|
|
76
|
+
const accepted = acceptedStylePatch(shape, patch);
|
|
77
|
+
const style = {};
|
|
78
|
+
for (const key of REMEMBERED_STYLE_KEYS[kind]) {
|
|
79
|
+
const value = accepted[key];
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
continue;
|
|
82
|
+
// A number that is no number is not a width or a size: the memory keeps what it had.
|
|
83
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
84
|
+
continue;
|
|
85
|
+
style[key] = value;
|
|
86
|
+
}
|
|
87
|
+
return Object.keys(style).length > 0 ? { kind, style: style } : null;
|
|
88
|
+
}
|
|
89
|
+
export const NO_REMEMBERED_STYLES = Object.freeze({
|
|
90
|
+
shape: Object.freeze({}),
|
|
91
|
+
lineIcon: Object.freeze({}),
|
|
92
|
+
arrow: Object.freeze({}),
|
|
93
|
+
text: Object.freeze({})
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* The memory with some edits laid over it, and the kinds that came out different. Where the edits say nothing new the very same object
|
|
97
|
+
* comes back with no kind named, so a drag that goes on saying the same colour tells nobody anything. The pen is not part of the memory
|
|
98
|
+
* (the engine keeps it), so an edit of a drawn line is passed over here.
|
|
99
|
+
*/
|
|
100
|
+
export function rememberStyleEdits(styles, edits) {
|
|
101
|
+
let next = styles;
|
|
102
|
+
const changed = [];
|
|
103
|
+
for (const { kind, style } of edits) {
|
|
104
|
+
if (kind === 'pen')
|
|
105
|
+
continue;
|
|
106
|
+
const current = next[kind];
|
|
107
|
+
let merged = null;
|
|
108
|
+
for (const [key, value] of Object.entries(style)) {
|
|
109
|
+
if (Object.is((merged ?? current)[key], value))
|
|
110
|
+
continue;
|
|
111
|
+
merged = { ...(merged ?? current), [key]: value };
|
|
112
|
+
}
|
|
113
|
+
if (!merged)
|
|
114
|
+
continue;
|
|
115
|
+
next = Object.freeze({ ...next, [kind]: Object.freeze(merged) });
|
|
116
|
+
if (!changed.includes(kind))
|
|
117
|
+
changed.push(kind);
|
|
118
|
+
}
|
|
119
|
+
return { styles: next, changed };
|
|
120
|
+
}
|
|
121
|
+
/** The values that are words of a fixed list: a key and what it may be. */
|
|
122
|
+
const WORDS_OF_KEY = {
|
|
123
|
+
strokeStyle: ['solid', 'dashed', 'dotted'],
|
|
124
|
+
fontWeight: ['normal', 'bold'],
|
|
125
|
+
fontStyle: ['normal', 'italic'],
|
|
126
|
+
align: ['left', 'center', 'right']
|
|
127
|
+
};
|
|
128
|
+
/** The keys whose value is a colour or a name: a text that is not empty. */
|
|
129
|
+
const TEXT_VALUED_KEYS = new Set(['fill', 'stroke', 'color', 'fontFamily']);
|
|
130
|
+
/** The keys whose value is a number, which has a range (see `clampedStyleNumber`). */
|
|
131
|
+
const NUMBER_VALUED_KEYS = new Set(['strokeWidth', 'opacity', 'roundness', 'fontSize']);
|
|
132
|
+
/** A value of a key as the storage may hold it, brought to what the key can be, or undefined when it is no value of the key at all. */
|
|
133
|
+
function checkedStyleValue(key, value) {
|
|
134
|
+
if (TEXT_VALUED_KEYS.has(key))
|
|
135
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
|
136
|
+
if (NUMBER_VALUED_KEYS.has(key))
|
|
137
|
+
return typeof value === 'number' && Number.isFinite(value) ? clampedStyleNumber(key, value) : undefined;
|
|
138
|
+
if (key === 'underline')
|
|
139
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
140
|
+
const words = WORDS_OF_KEY[key];
|
|
141
|
+
return words && typeof value === 'string' && words.includes(value) ? value : undefined;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* What the storage holds for a kind, as a memory can hold it: key by key, a value that is not one of the key is dropped (the rest stays),
|
|
145
|
+
* a number is brought into its range (a width that was kept when the widest was 200 comes down to 50), and a key the kind does not
|
|
146
|
+
* remember is left out. Anything that is not an object is no memory at all.
|
|
147
|
+
*/
|
|
148
|
+
export function sanitizeRememberedStyle(kind, stored) {
|
|
149
|
+
if (!stored || typeof stored !== 'object')
|
|
150
|
+
return Object.freeze({});
|
|
151
|
+
const source = stored;
|
|
152
|
+
const style = {};
|
|
153
|
+
for (const key of REMEMBERED_STYLE_KEYS[kind]) {
|
|
154
|
+
const checked = checkedStyleValue(key, source[key]);
|
|
155
|
+
if (checked !== undefined)
|
|
156
|
+
style[key] = checked;
|
|
157
|
+
}
|
|
158
|
+
return Object.freeze(style);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* What the storage holds for every kind, as a memory can hold it (`sanitizeRememberedStyle` for each). Nothing stored: nothing remembered.
|
|
162
|
+
* A kind the storage has no part for is nothing remembered for that kind.
|
|
163
|
+
*/
|
|
164
|
+
export function sanitizeRememberedStyles(stored) {
|
|
165
|
+
if (!stored)
|
|
166
|
+
return NO_REMEMBERED_STYLES;
|
|
167
|
+
const sanitized = {};
|
|
168
|
+
for (const kind of PATCHED_STYLE_KINDS)
|
|
169
|
+
sanitized[kind] = sanitizeRememberedStyle(kind, stored[kind]);
|
|
170
|
+
return Object.freeze(sanitized);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A new element with what the person last chose for its kind laid over what it starts with. `leave` names keys of the memory that the
|
|
174
|
+
* element keeps as it starts (the rounded square keeps the soft corners it is chosen for). The element comes out as the panel would
|
|
175
|
+
* make it: the head of an arrow follows its width, the corners of a triangle are drawn again for their roundness. The very same object
|
|
176
|
+
* back when there is nothing to lay over.
|
|
177
|
+
*/
|
|
178
|
+
export function withRememberedStyle(element, remembered, options = {}) {
|
|
179
|
+
if (!remembered)
|
|
180
|
+
return element;
|
|
181
|
+
let patch = remembered;
|
|
182
|
+
if (options.leave && options.leave.length > 0) {
|
|
183
|
+
const withoutLeft = { ...remembered };
|
|
184
|
+
for (const key of options.leave)
|
|
185
|
+
delete withoutLeft[key];
|
|
186
|
+
patch = withoutLeft;
|
|
187
|
+
}
|
|
188
|
+
return applyStylePatch(element, patch);
|
|
189
|
+
}
|
|
@@ -63,9 +63,14 @@ export declare function createPanGesture(context: GestureContext, start: Pointer
|
|
|
63
63
|
}): Gesture;
|
|
64
64
|
/**
|
|
65
65
|
* Draws with the brush. The line is as thick as the hand presses: a pen's own pressure, or, for a mouse or a finger, the time the
|
|
66
|
-
* hand spends (a slow stroke swells, a quick one thins).
|
|
67
|
-
*
|
|
68
|
-
*
|
|
66
|
+
* hand spends (a slow stroke swells, a quick one thins). The colour, the width and the opacity of the line are the pen's
|
|
67
|
+
* (`EngineState.penStyle`), which the settings panel sets before a line is drawn.
|
|
68
|
+
*
|
|
69
|
+
* Shift holds the line straight for as long as it is down. The straight stretch begins where the pointer is when the key goes down
|
|
70
|
+
* (what the hand has drawn up to there stays as it is) and runs to the pointer, with the colour and the thickness of the line
|
|
71
|
+
* drawn without Shift: the brush is the same, held at one pressure. When Shift is let go the stretch is kept up to where the
|
|
72
|
+
* pointer was, and the hand goes on drawing from there as before; Shift down again starts another stretch. With Shift down as the
|
|
73
|
+
* press begins the whole line is straight from the point of the press.
|
|
69
74
|
*/
|
|
70
75
|
export declare function createDrawGesture(context: GestureContext, start: PointerInput): Gesture;
|
|
71
76
|
/**
|