@widgetic/canvas 0.5.46 → 0.5.47
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 +2 -1
- package/dist/canvas/DomCanvas.svelte +1 -2
- package/dist/canvas/host/CanvasHost.d.ts +0 -3
- package/dist/canvas/host/CanvasHost.js +102 -67
- 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/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 +2 -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 +7 -2
- 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/workspaceToolSettings.d.ts +17 -0
- package/dist/settings/workspaceToolSettings.js +4 -1
- package/dist/tools/hostAdapter.js +0 -1
- package/package.json +4 -4
|
@@ -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
|
/**
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { arrowPointsInWorld, bendTowards, isArrow, reshapeArrow } from '../core/arrow.js';
|
|
8
8
|
import { pickArrowBinding, positionBoundArrow } from '../core/arrowBinding.js';
|
|
9
9
|
import { bindingOfEnd, releaseUnmovedEnds } from '../core/arrowBindingRecords.js';
|
|
10
|
-
import { buildBrushStroke } from '../core/brush.js';
|
|
10
|
+
import { STRAIGHT_LINE_PRESSURE, buildBrushStroke } from '../core/brush.js';
|
|
11
11
|
import { ARROW_SNAP_DEGREES, MIN_DRAW_DISTANCE, buildDraggedShape, snapPointAround } from '../core/shapeTools.js';
|
|
12
12
|
import { distanceBetween, inflateRect, rectContainsPoint, rectFromPoints, rectsIntersect, unionRects } from '../core/geometry.js';
|
|
13
13
|
import { hitTestLeaf } from '../core/hitTest.js';
|
|
@@ -504,18 +504,18 @@ export function createPanGesture(context, start, options = {}) {
|
|
|
504
504
|
// ──────────────────────────────────────────────────────────── ink
|
|
505
505
|
/** A pen whose pressure never moves by more than this has none to report, and draws as a mouse does. */
|
|
506
506
|
const PEN_PRESSURE_SPREAD = 0.02;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
* hand settles at as soon as the hand moves at an ordinary pace. The straight line is then as thick as the lines drawn by hand
|
|
510
|
-
* beside it (a width of 3 is about 3 px on the screen, and not the 6.6 px of a pen pressing at its middle), and as thick at one
|
|
511
|
-
* end as at the other.
|
|
512
|
-
*/
|
|
513
|
-
const STRAIGHT_LINE_PRESSURE = 0;
|
|
507
|
+
// A point of the line closer than this many screen pixels to the one before it adds nothing the eye can see.
|
|
508
|
+
const MIN_SAMPLE_DISTANCE = 1;
|
|
514
509
|
/**
|
|
515
510
|
* 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
|
|
516
|
-
* hand spends (a slow stroke swells, a quick one thins).
|
|
517
|
-
*
|
|
518
|
-
*
|
|
511
|
+
* hand spends (a slow stroke swells, a quick one thins). The colour, the width and the opacity of the line are the pen's
|
|
512
|
+
* (`EngineState.penStyle`), which the settings panel sets before a line is drawn.
|
|
513
|
+
*
|
|
514
|
+
* 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
|
|
515
|
+
* (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
|
|
516
|
+
* 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
|
|
517
|
+
* pointer was, and the hand goes on drawing from there as before; Shift down again starts another stretch. With Shift down as the
|
|
518
|
+
* press begins the whole line is straight from the point of the press.
|
|
519
519
|
*/
|
|
520
520
|
export function createDrawGesture(context, start) {
|
|
521
521
|
const { engine } = context;
|
|
@@ -524,23 +524,36 @@ export function createDrawGesture(context, start) {
|
|
|
524
524
|
// How the line is drawn (its colour, its width, how much of the board shows through it) is the pen as it was set when the press
|
|
525
525
|
// began: a line already being drawn is not changed by a panel that is touched meanwhile.
|
|
526
526
|
const { color, width: strokeWidth, opacity } = engine.state.penStyle;
|
|
527
|
+
// The points of the line: where the hand went, and the two ends of every stretch that was held straight.
|
|
527
528
|
const samples = [];
|
|
529
|
+
// The stretches held straight, as the indexes of the samples they run between and the pressure each is drawn at.
|
|
530
|
+
const straightStretches = [];
|
|
528
531
|
let leastPressure = Infinity;
|
|
529
532
|
let mostPressure = -Infinity;
|
|
530
|
-
// What a pen has reported
|
|
531
|
-
let
|
|
532
|
-
let
|
|
533
|
-
|
|
533
|
+
// What a pen has reported since the stretch that is open began, to find the pressure that stretch is held at.
|
|
534
|
+
let stretchPressureTotal = 0;
|
|
535
|
+
let stretchPressureCount = 0;
|
|
536
|
+
// The stretch Shift is holding open: the index of the sample it began at, where that sample is on the screen, and where the
|
|
537
|
+
// pointer is now (in the world, and on the screen). Null while Shift is up.
|
|
538
|
+
let openStretchStart = null;
|
|
539
|
+
let openStretchAnchorScreen = { x: start.x, y: start.y };
|
|
540
|
+
let openStretchEnd = null;
|
|
541
|
+
let openStretchEndScreen = { x: start.x, y: start.y };
|
|
534
542
|
let lastScreen = { x: start.x, y: start.y };
|
|
535
|
-
|
|
543
|
+
// Only a pen's pressure means anything: a mouse and a finger report one fixed value while they are down.
|
|
544
|
+
const reportsPressure = (input) => isPen && typeof input.pressure === 'number';
|
|
545
|
+
const notePressure = (input) => {
|
|
546
|
+
leastPressure = Math.min(leastPressure, input.pressure);
|
|
547
|
+
mostPressure = Math.max(mostPressure, input.pressure);
|
|
548
|
+
if (openStretchStart !== null) {
|
|
549
|
+
stretchPressureTotal += input.pressure;
|
|
550
|
+
stretchPressureCount += 1;
|
|
551
|
+
}
|
|
552
|
+
};
|
|
536
553
|
const takeSample = (input) => {
|
|
537
554
|
const world = worldOf(context, input);
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
leastPressure = Math.min(leastPressure, input.pressure);
|
|
541
|
-
mostPressure = Math.max(mostPressure, input.pressure);
|
|
542
|
-
pressureTotal += input.pressure;
|
|
543
|
-
pressureCount += 1;
|
|
555
|
+
if (reportsPressure(input)) {
|
|
556
|
+
notePressure(input);
|
|
544
557
|
samples.push({ ...world, pressure: input.pressure });
|
|
545
558
|
}
|
|
546
559
|
else {
|
|
@@ -549,19 +562,63 @@ export function createDrawGesture(context, start) {
|
|
|
549
562
|
};
|
|
550
563
|
takeSample(start);
|
|
551
564
|
const readsPressure = () => isPen && mostPressure - leastPressure >= PEN_PRESSURE_SPREAD;
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
565
|
+
/** The pressure a straight stretch is held at: the pen's own average while the stretch was drawn, or the mouse's usual one. */
|
|
566
|
+
const stretchPressure = () => (readsPressure() && stretchPressureCount > 0 ? stretchPressureTotal / stretchPressureCount : STRAIGHT_LINE_PRESSURE);
|
|
567
|
+
/** How far the open stretch reaches, in pixels of the screen: one with no length draws nothing and is not kept. */
|
|
568
|
+
const openStretchReach = () => Math.hypot(openStretchEndScreen.x - openStretchAnchorScreen.x, openStretchEndScreen.y - openStretchAnchorScreen.y);
|
|
569
|
+
/** Shift goes down: a straight stretch begins where the hand is, at the last point of the line, and follows the pointer from there. */
|
|
570
|
+
const openStretch = () => {
|
|
571
|
+
if (openStretchStart !== null)
|
|
572
|
+
return;
|
|
573
|
+
openStretchStart = samples.length - 1;
|
|
574
|
+
openStretchAnchorScreen = lastScreen;
|
|
575
|
+
openStretchEnd = null;
|
|
576
|
+
openStretchEndScreen = lastScreen;
|
|
577
|
+
// The pen's pressure at the point the stretch begins is the first of the ones it is averaged from.
|
|
578
|
+
const anchorPressure = samples[openStretchStart].pressure;
|
|
579
|
+
stretchPressureTotal = typeof anchorPressure === 'number' ? anchorPressure : 0;
|
|
580
|
+
stretchPressureCount = typeof anchorPressure === 'number' ? 1 : 0;
|
|
581
|
+
};
|
|
582
|
+
/** The open stretch ends at the pointer: from there the hand goes on, and the line keeps the stretch as it was drawn. */
|
|
583
|
+
const closeStretch = () => {
|
|
584
|
+
if (openStretchStart === null)
|
|
585
|
+
return;
|
|
586
|
+
const from = openStretchStart;
|
|
587
|
+
const end = openStretchEnd;
|
|
588
|
+
const reach = openStretchReach();
|
|
589
|
+
const pressure = stretchPressure();
|
|
590
|
+
const endScreen = openStretchEndScreen;
|
|
591
|
+
openStretchStart = null;
|
|
592
|
+
openStretchEnd = null;
|
|
593
|
+
if (!end || reach < MIN_SAMPLE_DISTANCE)
|
|
594
|
+
return;
|
|
595
|
+
samples.push(end);
|
|
596
|
+
straightStretches.push({ from, to: samples.length - 1, pressure });
|
|
597
|
+
lastScreen = endScreen;
|
|
598
|
+
};
|
|
599
|
+
/** The open stretch follows the pointer. Only the position is taken here: the pen's pressure is noted event by event. */
|
|
600
|
+
const followPointer = (input, pressure) => {
|
|
601
|
+
const world = worldOf(context, input);
|
|
602
|
+
openStretchEnd = typeof pressure === 'number' ? { ...world, pressure } : world;
|
|
603
|
+
openStretchEndScreen = { x: input.x, y: input.y };
|
|
604
|
+
};
|
|
605
|
+
if (start.shiftKey)
|
|
606
|
+
openStretch();
|
|
607
|
+
/** The line as it is now: the samples and the stretches, the open one drawn all the way to the pointer. */
|
|
608
|
+
const lineNow = () => {
|
|
609
|
+
if (openStretchStart === null || !openStretchEnd || openStretchReach() < MIN_SAMPLE_DISTANCE)
|
|
610
|
+
return { points: samples, stretches: straightStretches };
|
|
611
|
+
const points = [...samples, openStretchEnd];
|
|
612
|
+
return { points, stretches: [...straightStretches, { from: openStretchStart, to: points.length - 1, pressure: stretchPressure() }] };
|
|
613
|
+
};
|
|
555
614
|
const build = (finished) => {
|
|
556
615
|
// The brush of the lines beside it: a pen that reports its pressure draws with the pen's brush, anything else with the mouse's.
|
|
557
616
|
const brushKind = readsPressure() ? 'pen' : 'speed';
|
|
558
617
|
const look = { createId: () => strokeId, color, strokeWidth, opacity, pressure: brushKind, finished };
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
const steadyPressure = brushKind === 'pen' && pressureCount > 0 ? pressureTotal / pressureCount : STRAIGHT_LINE_PRESSURE;
|
|
564
|
-
return buildBrushStroke(lineDrawn(), { ...look, steadyPressure });
|
|
618
|
+
// A straight stretch has two ends and an even width (the pen's own average, or the mouse's usual pressure), and it is drawn all
|
|
619
|
+
// the way to the pointer, not trailing behind it as a hand-drawn line does while it is being drawn.
|
|
620
|
+
const { points, stretches } = lineNow();
|
|
621
|
+
return buildBrushStroke(points, { ...look, straightSpans: stretches });
|
|
565
622
|
};
|
|
566
623
|
const showPreview = () => engine.setOverlay({ preview: build(false) });
|
|
567
624
|
showPreview();
|
|
@@ -569,37 +626,54 @@ export function createDrawGesture(context, start) {
|
|
|
569
626
|
kind: 'draw',
|
|
570
627
|
pointerId: start.pointerId,
|
|
571
628
|
move(input) {
|
|
572
|
-
lastInput = input;
|
|
573
|
-
straight = input.shiftKey;
|
|
574
629
|
const moved = input.coalesced && input.coalesced.length > 0 ? input.coalesced : [input];
|
|
575
|
-
|
|
576
|
-
//
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
630
|
+
if (input.shiftKey) {
|
|
631
|
+
// Shift is down (its key may not have been seen, or comes with this very move): the stretch begins where the line ended
|
|
632
|
+
// at the last move, and follows the pointer. The line takes no points while it is held: the stretch has only two ends.
|
|
633
|
+
openStretch();
|
|
634
|
+
for (const event of moved)
|
|
635
|
+
if (reportsPressure(event))
|
|
636
|
+
notePressure(event);
|
|
637
|
+
followPointer(input, reportsPressure(input) ? input.pressure : undefined);
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
// Shift is up: a stretch that was open ends where the pointer was, and the hand goes on from there.
|
|
641
|
+
closeStretch();
|
|
642
|
+
for (const sample of moved) {
|
|
643
|
+
// A new sample closer than a pixel to the last one adds nothing the eye can see.
|
|
644
|
+
if (Math.hypot(sample.x - lastScreen.x, sample.y - lastScreen.y) < MIN_SAMPLE_DISTANCE)
|
|
645
|
+
continue;
|
|
646
|
+
takeSample(sample);
|
|
647
|
+
lastScreen = { x: sample.x, y: sample.y };
|
|
648
|
+
}
|
|
581
649
|
}
|
|
582
650
|
showPreview();
|
|
583
651
|
},
|
|
584
652
|
end(input) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
653
|
+
if (input.shiftKey) {
|
|
654
|
+
// A straight stretch ends where the pointer was let go, even when that is a little beyond the last move (a fast flick).
|
|
655
|
+
// Only the position is taken: a pen reports no pressure once it is lifted, and the stretch holds one pressure anyway.
|
|
656
|
+
openStretch();
|
|
657
|
+
followPointer(input);
|
|
658
|
+
}
|
|
659
|
+
closeStretch();
|
|
590
660
|
const stroke = build(true);
|
|
661
|
+
const { points } = lineNow();
|
|
591
662
|
engine.clearOverlay();
|
|
592
663
|
if (!stroke)
|
|
593
664
|
return;
|
|
594
665
|
engine.addObjects([stroke]);
|
|
595
|
-
context.options.onStrokeFinished?.(stroke,
|
|
666
|
+
context.options.onStrokeFinished?.(stroke, points);
|
|
596
667
|
},
|
|
597
668
|
cancel() {
|
|
598
669
|
engine.clearOverlay();
|
|
599
670
|
},
|
|
600
671
|
modifiersChanged(modifiers) {
|
|
601
|
-
straight
|
|
602
|
-
|
|
672
|
+
// Shift goes down: a straight stretch begins at the pointer. Shift goes up: the stretch ends there, and the hand goes on.
|
|
673
|
+
if (modifiers.shiftKey)
|
|
674
|
+
openStretch();
|
|
675
|
+
else
|
|
676
|
+
closeStretch();
|
|
603
677
|
showPreview();
|
|
604
678
|
}
|
|
605
679
|
};
|
|
@@ -639,7 +713,8 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
639
713
|
wireframe: engine.wireframeMode,
|
|
640
714
|
createId: () => id,
|
|
641
715
|
random: () => colorSeed,
|
|
642
|
-
|
|
716
|
+
// What the person last chose for this kind of shape (the preview and the shape kept are made with the same).
|
|
717
|
+
rememberedStyles: engine.rememberedStyles
|
|
643
718
|
});
|
|
644
719
|
if (kind !== 'arrow' || !dragged || input.altKey || shape.type !== 'path' || !isArrow(shape))
|
|
645
720
|
return { shape, hint: null };
|
|
@@ -113,9 +113,14 @@ function handleEscape(host) {
|
|
|
113
113
|
if (controller.cancelGesture())
|
|
114
114
|
return true;
|
|
115
115
|
if (engine.state.editingTextId) {
|
|
116
|
-
|
|
116
|
+
// Escape says "done writing": the text that was typed stays on the board and stays selected, so that what comes next (a colour,
|
|
117
|
+
// a size, a move) is for it; the next Escape lets go of it. A text that ended empty is not kept, so there is nothing to select.
|
|
118
|
+
const keptTextId = engine.endTextEdit();
|
|
117
119
|
engine.setTool('select');
|
|
118
|
-
|
|
120
|
+
if (keptTextId === null)
|
|
121
|
+
engine.clearSelection();
|
|
122
|
+
else if (!engine.state.selectedIds.includes(keptTextId))
|
|
123
|
+
engine.select([keptTextId]);
|
|
119
124
|
return true;
|
|
120
125
|
}
|
|
121
126
|
// A field of the page that is not ours keeps its Escape.
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* placement is only used in the layout it was measured in. One that is missing, damaged or from another layout is nothing: the
|
|
16
16
|
* page draws its empty board as before and the creator draws the rest when it arrives.
|
|
17
17
|
*
|
|
18
|
-
* It holds nothing private: pixel distances
|
|
18
|
+
* It holds nothing private: pixel distances, the toolbar's place, and the workspace key the canvas already keeps its own choices under.
|
|
19
19
|
*/
|
|
20
20
|
/** Where the placement is kept. The page and the creator are separate programs on one origin, so they meet here. */
|
|
21
21
|
export declare const BOARD_PLACEMENT_STORAGE_KEY = "widgetic.canvas.boardPlacement";
|
|
@@ -42,6 +42,8 @@ export interface BoardPlacement {
|
|
|
42
42
|
toolbarPosition: string | null;
|
|
43
43
|
/** Whether the board shows the drawing tools (the toolbar placeholder draws nothing without them). */
|
|
44
44
|
showDrawingTools: boolean;
|
|
45
|
+
/** The workspace the canvas keeps its choices under (the tool that is on when the board opens), or null. */
|
|
46
|
+
workspaceId: string | null;
|
|
45
47
|
}
|
|
46
48
|
/** The part of a DOM rectangle this file reads. */
|
|
47
49
|
export interface PlacementRect {
|
|
@@ -56,6 +58,7 @@ export interface BoardPlacementDetails {
|
|
|
56
58
|
viewportWidth: number;
|
|
57
59
|
toolbarPosition?: string | null;
|
|
58
60
|
showDrawingTools?: boolean;
|
|
61
|
+
workspaceId?: string | null;
|
|
59
62
|
}
|
|
60
63
|
/** The part of `Storage` this file uses (a test hands in a map). */
|
|
61
64
|
export interface PlacementStorage {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* placement is only used in the layout it was measured in. One that is missing, damaged or from another layout is nothing: the
|
|
16
16
|
* page draws its empty board as before and the creator draws the rest when it arrives.
|
|
17
17
|
*
|
|
18
|
-
* It holds nothing private: pixel distances
|
|
18
|
+
* It holds nothing private: pixel distances, the toolbar's place, and the workspace key the canvas already keeps its own choices under.
|
|
19
19
|
*/
|
|
20
20
|
/** Where the placement is kept. The page and the creator are separate programs on one origin, so they meet here. */
|
|
21
21
|
export const BOARD_PLACEMENT_STORAGE_KEY = 'widgetic.canvas.boardPlacement';
|
|
@@ -62,7 +62,8 @@ export function measureBoardPlacement(host, board, details) {
|
|
|
62
62
|
bottom,
|
|
63
63
|
left,
|
|
64
64
|
toolbarPosition: typeof details.toolbarPosition === 'string' && details.toolbarPosition.length <= 8 ? details.toolbarPosition : null,
|
|
65
|
-
showDrawingTools: details.showDrawingTools !== false
|
|
65
|
+
showDrawingTools: details.showDrawingTools !== false,
|
|
66
|
+
workspaceId: typeof details.workspaceId === 'string' && details.workspaceId.length <= 200 ? details.workspaceId : null
|
|
66
67
|
};
|
|
67
68
|
}
|
|
68
69
|
function defaultStorage() {
|
|
@@ -117,6 +118,7 @@ export function recallBoardPlacement(viewportWidth, storage = defaultStorage())
|
|
|
117
118
|
if (typeof record.showDrawingTools !== 'boolean')
|
|
118
119
|
return null;
|
|
119
120
|
const toolbarPosition = typeof record.toolbarPosition === 'string' && record.toolbarPosition.length <= 8 ? record.toolbarPosition : null;
|
|
121
|
+
const workspaceId = typeof record.workspaceId === 'string' && record.workspaceId.length <= 200 ? record.workspaceId : null;
|
|
120
122
|
return {
|
|
121
123
|
version: BOARD_PLACEMENT_VERSION,
|
|
122
124
|
layout: record.layout,
|
|
@@ -125,7 +127,8 @@ export function recallBoardPlacement(viewportWidth, storage = defaultStorage())
|
|
|
125
127
|
bottom: record.bottom,
|
|
126
128
|
left: record.left,
|
|
127
129
|
toolbarPosition,
|
|
128
|
-
showDrawingTools: record.showDrawingTools
|
|
130
|
+
showDrawingTools: record.showDrawingTools,
|
|
131
|
+
workspaceId
|
|
129
132
|
};
|
|
130
133
|
}
|
|
131
134
|
catch {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How what the person last chose for each kind of element (`engine/core/styleMemory.ts`) is kept for a workspace, and read back.
|
|
3
|
+
*
|
|
4
|
+
* The storage (`workspaceToolSettings.ts`) has a part for each kind: `paint` for the shapes that have an inside, `icon` for the line-only
|
|
5
|
+
* icons of the library, `arrow` and `text`; the pen of the Draw tool has its own part (`pen`), kept by the host from `EngineState.penStyle`.
|
|
6
|
+
* Nothing here touches the storage or the engine: these are the two translations between what the engine remembers and what is stored.
|
|
7
|
+
*/
|
|
8
|
+
import type { PatchedStyleKind, RememberedStyles } from '../engine/core/styleMemory.js';
|
|
9
|
+
import type { WorkspaceToolSettingsV1 } from './workspaceToolSettings.js';
|
|
10
|
+
/**
|
|
11
|
+
* The parts of the stored settings that say what the engine remembers for the kinds that changed, to be laid over what is stored (a part
|
|
12
|
+
* keeps the keys the patch does not say, so a kind that did not change is not named and stays as it is). A kind that holds nothing yet is
|
|
13
|
+
* not named either: nothing is written for an element the person never styled.
|
|
14
|
+
*/
|
|
15
|
+
export declare function settingsOfRememberedStyles(styles: RememberedStyles, kinds: readonly PatchedStyleKind[]): Partial<WorkspaceToolSettingsV1>;
|
|
16
|
+
/**
|
|
17
|
+
* What the stored settings hold for each kind, to be given to the engine (`EngineCore.setRememberedStyles`, which checks every key).
|
|
18
|
+
* No settings, or settings that never held a part: nothing for that kind.
|
|
19
|
+
*/
|
|
20
|
+
export declare function rememberedStylesOfSettings(settings: WorkspaceToolSettingsV1 | null): {
|
|
21
|
+
readonly [kind in PatchedStyleKind]: unknown;
|
|
22
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How what the person last chose for each kind of element (`engine/core/styleMemory.ts`) is kept for a workspace, and read back.
|
|
3
|
+
*
|
|
4
|
+
* The storage (`workspaceToolSettings.ts`) has a part for each kind: `paint` for the shapes that have an inside, `icon` for the line-only
|
|
5
|
+
* icons of the library, `arrow` and `text`; the pen of the Draw tool has its own part (`pen`), kept by the host from `EngineState.penStyle`.
|
|
6
|
+
* Nothing here touches the storage or the engine: these are the two translations between what the engine remembers and what is stored.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The parts of the stored settings that say what the engine remembers for the kinds that changed, to be laid over what is stored (a part
|
|
10
|
+
* keeps the keys the patch does not say, so a kind that did not change is not named and stays as it is). A kind that holds nothing yet is
|
|
11
|
+
* not named either: nothing is written for an element the person never styled.
|
|
12
|
+
*/
|
|
13
|
+
export function settingsOfRememberedStyles(styles, kinds) {
|
|
14
|
+
const settings = {};
|
|
15
|
+
for (const kind of kinds) {
|
|
16
|
+
const remembered = styles[kind];
|
|
17
|
+
if (Object.keys(remembered).length === 0)
|
|
18
|
+
continue;
|
|
19
|
+
switch (kind) {
|
|
20
|
+
case 'shape':
|
|
21
|
+
settings.paint = { ...remembered };
|
|
22
|
+
break;
|
|
23
|
+
case 'lineIcon':
|
|
24
|
+
settings.icon = { ...remembered };
|
|
25
|
+
break;
|
|
26
|
+
case 'arrow':
|
|
27
|
+
settings.arrow = { ...remembered };
|
|
28
|
+
break;
|
|
29
|
+
case 'text':
|
|
30
|
+
settings.text = { ...remembered };
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return settings;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* What the stored settings hold for each kind, to be given to the engine (`EngineCore.setRememberedStyles`, which checks every key).
|
|
38
|
+
* No settings, or settings that never held a part: nothing for that kind.
|
|
39
|
+
*/
|
|
40
|
+
export function rememberedStylesOfSettings(settings) {
|
|
41
|
+
return {
|
|
42
|
+
shape: settings?.paint,
|
|
43
|
+
lineIcon: settings?.icon,
|
|
44
|
+
arrow: settings?.arrow,
|
|
45
|
+
text: settings?.text
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -7,6 +7,12 @@ export interface SettingsStorageAdapter {
|
|
|
7
7
|
load(key: string): string | null;
|
|
8
8
|
save(key: string, value: string): void;
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* What the person last chose for each kind of element, so the next element of the kind starts with it (see `engine/core/styleMemory.ts`,
|
|
12
|
+
* which says what each kind is and what is kept of it). `text`, `paint` (the shapes with an inside), `icon` (the line-only icons of the
|
|
13
|
+
* library: a check, a cross, a plus, a minus) and `arrow` hold keys of the settings panel, each only the ones its kind remembers.
|
|
14
|
+
* Values are checked key by key when they are read, so an entry from an older version, or one that was damaged, gives what it can.
|
|
15
|
+
*/
|
|
10
16
|
export interface WorkspaceToolSettingsV1 {
|
|
11
17
|
v: typeof WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION;
|
|
12
18
|
text?: {
|
|
@@ -17,6 +23,7 @@ export interface WorkspaceToolSettingsV1 {
|
|
|
17
23
|
color?: string;
|
|
18
24
|
align?: 'left' | 'center' | 'right';
|
|
19
25
|
underline?: boolean;
|
|
26
|
+
opacity?: number;
|
|
20
27
|
};
|
|
21
28
|
paint?: {
|
|
22
29
|
fill?: string;
|
|
@@ -26,9 +33,19 @@ export interface WorkspaceToolSettingsV1 {
|
|
|
26
33
|
opacity?: number;
|
|
27
34
|
roundness?: number;
|
|
28
35
|
};
|
|
36
|
+
/** The line-only icons of the library: the colour of the line, its width and opacity, how round its ends are. Left out until the person styles one. */
|
|
37
|
+
icon?: {
|
|
38
|
+
stroke?: string;
|
|
39
|
+
strokeWidth?: number;
|
|
40
|
+
strokeStyle?: 'solid' | 'dashed' | 'dotted';
|
|
41
|
+
opacity?: number;
|
|
42
|
+
roundness?: number;
|
|
43
|
+
};
|
|
29
44
|
arrow?: {
|
|
30
45
|
stroke?: string;
|
|
31
46
|
strokeWidth?: number;
|
|
47
|
+
strokeStyle?: 'solid' | 'dashed' | 'dotted';
|
|
48
|
+
opacity?: number;
|
|
32
49
|
headStart?: boolean;
|
|
33
50
|
headEnd?: boolean;
|
|
34
51
|
};
|