@web-canvas-ui/react 0.1.2 → 0.3.0
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 +415 -205
- package/index.js +3660 -0
- package/package.json +36 -27
- package/types/canvas-handle.d.ts +22 -0
- package/types/components/Button/button.d.ts +22 -0
- package/types/components/Button/logic.d.ts +3 -0
- package/types/components/CanvasView/canvasView.d.ts +28 -0
- package/types/components/CanvasView/index.d.ts +2 -0
- package/types/components/Carousel/carousel.d.ts +31 -0
- package/types/components/Carousel/logic.d.ts +2 -0
- package/types/components/Checkbox/checkbox.d.ts +8 -0
- package/types/components/ChoiceGroup/choiceGroup.d.ts +38 -0
- package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +1 -1
- package/types/components/ChoiceGroup/logic.d.ts +3 -0
- package/types/components/Circle/circle.d.ts +9 -0
- package/types/components/Collapse/collapse.d.ts +32 -0
- package/types/components/Collapse/logic.d.ts +1 -0
- package/types/components/CustomDraw/customDraw.d.ts +22 -0
- package/types/components/CustomDraw/index.d.ts +2 -0
- package/types/components/DatePicker/datePicker.d.ts +24 -0
- package/types/components/Dialog/dialog.d.ts +20 -0
- package/types/components/Dropdown/dropdown.d.ts +30 -0
- package/types/components/Dropdown/logic.d.ts +5 -0
- package/types/components/Form/form.d.ts +42 -0
- package/types/components/Form/index.d.ts +2 -0
- package/{components/Grid/grid.ts → types/components/Grid/grid.d.ts} +3 -4
- package/{components/GridItem/gridItem.ts → types/components/GridItem/gridItem.d.ts} +1 -4
- package/{components/Group/group.ts → types/components/Group/group.d.ts} +1 -4
- package/types/components/Icon/icon.d.ts +14 -0
- package/types/components/Image/image.d.ts +12 -0
- package/types/components/Input/input.d.ts +32 -0
- package/types/components/Progress/progress.d.ts +15 -0
- package/types/components/Radio/radio.d.ts +8 -0
- package/types/components/Rect/rect.d.ts +11 -0
- package/types/components/Slider/logic.d.ts +1 -0
- package/types/components/Slider/slider.d.ts +17 -0
- package/types/components/Switch/logic.d.ts +2 -0
- package/types/components/Switch/switch.d.ts +14 -0
- package/types/components/Tabs/logic.d.ts +2 -0
- package/types/components/Tabs/tabs.d.ts +35 -0
- package/types/components/Text/text.d.ts +39 -0
- package/types/components/Textarea/textarea.d.ts +10 -0
- package/types/components/Upload/index.d.ts +2 -0
- package/types/components/Upload/upload.d.ts +30 -0
- package/types/components/Video/index.d.ts +4 -0
- package/types/components/Video/logic.d.ts +56 -0
- package/types/components/Video/video.d.ts +54 -0
- package/types/components/draw-utils.d.ts +48 -0
- package/types/components/shared.d.ts +7 -0
- package/types/components/size.d.ts +6 -0
- package/{components/types.ts → types/components/types.d.ts} +5 -13
- package/types/engine/accessibility.d.ts +5 -0
- package/types/engine/carousel.d.ts +10 -0
- package/types/engine/events.d.ts +14 -0
- package/types/engine/forms.d.ts +21 -0
- package/types/engine/hit-test.d.ts +31 -0
- package/types/engine/images.d.ts +8 -0
- package/types/engine/interaction.d.ts +12 -0
- package/types/engine/layout.d.ts +6 -0
- package/types/engine/nodes.d.ts +19 -0
- package/types/engine/state.d.ts +91 -0
- package/types/engine/text-controls.d.ts +10 -0
- package/types/engine/transform.d.ts +13 -0
- package/types/engine/uploads.d.ts +5 -0
- package/types/engine/videos.d.ts +28 -0
- package/types/engine/vnodes.d.ts +36 -0
- package/types/engine.d.ts +38 -0
- package/types/image-assets.d.ts +2 -0
- package/types/input-bridge.d.ts +54 -0
- package/types/react/Canvas.d.ts +4 -0
- package/types/react/components.d.ts +31 -0
- package/types/react/index.d.ts +5 -0
- package/types/renderer.d.ts +20 -0
- package/types/scene.d.ts +15 -0
- package/{types.ts → types/types.d.ts} +48 -142
- package/types/video-assets.d.ts +28 -0
- package/Canvas.tsx +0 -142
- package/__tests__/pure-logic.test.ts +0 -153
- package/__tests__/scene-and-node.test.ts +0 -131
- package/__tests__/scratch-space.test.ts +0 -30
- package/__tests__/textarea-caret.test.ts +0 -96
- package/__tests__/utils.test.ts +0 -114
- package/canvas-handle.ts +0 -114
- package/components/Button/button.ts +0 -57
- package/components/Button/logic.ts +0 -11
- package/components/CanvasView/canvasView.ts +0 -103
- package/components/CanvasView/index.ts +0 -7
- package/components/Carousel/carousel.ts +0 -107
- package/components/Carousel/logic.ts +0 -36
- package/components/Checkbox/checkbox.ts +0 -14
- package/components/ChoiceGroup/choiceGroup.ts +0 -172
- package/components/ChoiceGroup/logic.ts +0 -14
- package/components/Circle/circle.ts +0 -31
- package/components/Collapse/collapse.ts +0 -69
- package/components/Collapse/logic.ts +0 -14
- package/components/CustomDraw/customDraw.ts +0 -50
- package/components/CustomDraw/index.ts +0 -7
- package/components/DatePicker/datePicker.ts +0 -70
- package/components/Dialog/dialog.ts +0 -102
- package/components/Dropdown/dropdown.ts +0 -125
- package/components/Dropdown/logic.ts +0 -29
- package/components/Icon/icon.ts +0 -669
- package/components/Image/image.ts +0 -69
- package/components/Input/input.ts +0 -139
- package/components/Progress/progress.ts +0 -59
- package/components/Radio/radio.ts +0 -14
- package/components/Rect/rect.ts +0 -35
- package/components/Slider/logic.ts +0 -19
- package/components/Slider/slider.ts +0 -58
- package/components/Switch/logic.ts +0 -8
- package/components/Switch/switch.ts +0 -37
- package/components/Tabs/logic.ts +0 -14
- package/components/Tabs/tabs.ts +0 -78
- package/components/Text/text.ts +0 -95
- package/components/Textarea/textarea.ts +0 -16
- package/components/Upload/index.ts +0 -2
- package/components/Upload/upload.ts +0 -66
- package/components/draw-utils.ts +0 -230
- package/components/shared.ts +0 -15
- package/components/size.ts +0 -20
- package/components.ts +0 -98
- package/engine/__tests__/carousel.test.ts +0 -218
- package/engine/__tests__/hit-test.test.ts +0 -282
- package/engine/__tests__/layout.test.ts +0 -474
- package/engine/carousel.ts +0 -150
- package/engine/events.ts +0 -73
- package/engine/hit-test.ts +0 -321
- package/engine/images.ts +0 -41
- package/engine/interaction.ts +0 -442
- package/engine/layout.ts +0 -355
- package/engine/nodes.ts +0 -166
- package/engine/state.ts +0 -75
- package/engine/text-controls.ts +0 -122
- package/engine/transform.ts +0 -61
- package/engine/vnodes.ts +0 -204
- package/engine.ts +0 -162
- package/image-assets.ts +0 -37
- package/index.ts +0 -78
- package/input-bridge.ts +0 -285
- package/renderer.ts +0 -303
- package/scene.ts +0 -122
- package/theme.scss +0 -234
- package/web-canvas-ui-react-0.1.1.tgz +0 -0
- /package/{components/Button/index.ts → types/components/Button/index.d.ts} +0 -0
- /package/{components/Carousel/index.ts → types/components/Carousel/index.d.ts} +0 -0
- /package/{components/Checkbox/index.ts → types/components/Checkbox/index.d.ts} +0 -0
- /package/{components/Circle/index.ts → types/components/Circle/index.d.ts} +0 -0
- /package/{components/Collapse/index.ts → types/components/Collapse/index.d.ts} +0 -0
- /package/{components/DatePicker/index.ts → types/components/DatePicker/index.d.ts} +0 -0
- /package/{components/Dialog/index.ts → types/components/Dialog/index.d.ts} +0 -0
- /package/{components/Dropdown/index.ts → types/components/Dropdown/index.d.ts} +0 -0
- /package/{components/Grid/index.ts → types/components/Grid/index.d.ts} +0 -0
- /package/{components/GridItem/index.ts → types/components/GridItem/index.d.ts} +0 -0
- /package/{components/Group/index.ts → types/components/Group/index.d.ts} +0 -0
- /package/{components/Icon/index.ts → types/components/Icon/index.d.ts} +0 -0
- /package/{components/Image/index.ts → types/components/Image/index.d.ts} +0 -0
- /package/{components/Input/index.ts → types/components/Input/index.d.ts} +0 -0
- /package/{components/Progress/index.ts → types/components/Progress/index.d.ts} +0 -0
- /package/{components/Radio/index.ts → types/components/Radio/index.d.ts} +0 -0
- /package/{components/Rect/index.ts → types/components/Rect/index.d.ts} +0 -0
- /package/{components/Slider/index.ts → types/components/Slider/index.d.ts} +0 -0
- /package/{components/Switch/index.ts → types/components/Switch/index.d.ts} +0 -0
- /package/{components/Tabs/index.ts → types/components/Tabs/index.d.ts} +0 -0
- /package/{components/Text/index.ts → types/components/Text/index.d.ts} +0 -0
- /package/{components/Textarea/index.ts → types/components/Textarea/index.d.ts} +0 -0
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { SceneNode } from '../../scene';
|
|
3
|
-
import type { VNode } from '../../types';
|
|
4
|
-
import { TEXT_LINE_HEIGHT, measureTextWidth } from '../../components/draw-utils';
|
|
5
|
-
import { prepare, activeCollapseKeys, layoutCollapseNodes } from '../layout';
|
|
6
|
-
import { EngineState } from '../state';
|
|
7
|
-
import type { EngineCore, TextControlDescription } from '../state';
|
|
8
|
-
|
|
9
|
-
// --- fixtures ---------------------------------------------------------------
|
|
10
|
-
|
|
11
|
-
function makeCore(canvasWidth = 400, canvasHeight = 200, pixelRatio = 1): EngineCore {
|
|
12
|
-
// prepare only reads context.canvas dims + pixelRatio (for dialog centring).
|
|
13
|
-
return {
|
|
14
|
-
pixelRatio,
|
|
15
|
-
context: { canvas: { width: canvasWidth, height: canvasHeight } },
|
|
16
|
-
} as unknown as EngineCore;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function vnode(
|
|
20
|
-
type: VNode['type'],
|
|
21
|
-
props: Record<string, unknown> = {},
|
|
22
|
-
children: VNode[] = [],
|
|
23
|
-
key: string | null = null,
|
|
24
|
-
): VNode {
|
|
25
|
-
return { type, key, props, children };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// --- passthrough & transform ------------------------------------------------
|
|
29
|
-
|
|
30
|
-
describe('prepare · passthrough', () => {
|
|
31
|
-
it('records effective user visibility on plain nodes', () => {
|
|
32
|
-
const state = new EngineState();
|
|
33
|
-
const out = prepare(
|
|
34
|
-
makeCore(),
|
|
35
|
-
state,
|
|
36
|
-
[vnode('rect', { x: 5 }, [], 'r')],
|
|
37
|
-
[1, 0, 0, 1, 0, 0],
|
|
38
|
-
true,
|
|
39
|
-
'root',
|
|
40
|
-
[],
|
|
41
|
-
);
|
|
42
|
-
expect(out[0].type).toBe('rect');
|
|
43
|
-
expect(out[0].props._userVisible).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('honours visible:false when deriving _userVisible', () => {
|
|
47
|
-
const state = new EngineState();
|
|
48
|
-
const out = prepare(
|
|
49
|
-
makeCore(),
|
|
50
|
-
state,
|
|
51
|
-
[vnode('rect', { visible: false }, [], 'r')],
|
|
52
|
-
[1, 0, 0, 1, 0, 0],
|
|
53
|
-
true,
|
|
54
|
-
'root',
|
|
55
|
-
[],
|
|
56
|
-
);
|
|
57
|
-
expect(out[0].props._userVisible).toBe(false);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// --- text controls ----------------------------------------------------------
|
|
62
|
-
|
|
63
|
-
describe('prepare · text controls', () => {
|
|
64
|
-
it('emits a description whose layout transform includes ancestor offsets', () => {
|
|
65
|
-
const state = new EngineState();
|
|
66
|
-
const descriptions: TextControlDescription[] = [];
|
|
67
|
-
const tree = [
|
|
68
|
-
vnode('group', { x: 50, y: 0 }, [vnode('input', { x: 10, y: 10 }, [], 'i0')], 'g1'),
|
|
69
|
-
];
|
|
70
|
-
prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', descriptions);
|
|
71
|
-
|
|
72
|
-
expect(descriptions).toHaveLength(1);
|
|
73
|
-
expect(descriptions[0].id).toBe('input:root/kg1/ki0');
|
|
74
|
-
// group translate(50,0) ∘ input translate(10,10) → [.., 60, 10].
|
|
75
|
-
expect(descriptions[0].layout.transform).toEqual([1, 0, 0, 1, 60, 10]);
|
|
76
|
-
expect(descriptions[0].layout.width).toBe(200);
|
|
77
|
-
expect(descriptions[0].layout.height).toBe(36);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('flags controlled vs uncontrolled inputs', () => {
|
|
81
|
-
const state = new EngineState();
|
|
82
|
-
const out = prepare(
|
|
83
|
-
makeCore(),
|
|
84
|
-
state,
|
|
85
|
-
[vnode('input', { value: 'x' }, [], 'i')],
|
|
86
|
-
[1, 0, 0, 1, 0, 0],
|
|
87
|
-
true,
|
|
88
|
-
'root',
|
|
89
|
-
[],
|
|
90
|
-
);
|
|
91
|
-
expect(out[0].props._controlledValue).toBe(true);
|
|
92
|
-
expect(out[0].props._canvasInputId).toBe('input:root/ki');
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// --- text metrics -----------------------------------------------------------
|
|
97
|
-
|
|
98
|
-
describe('prepare · text metrics', () => {
|
|
99
|
-
it('measures the wrapped box instead of assuming one line', () => {
|
|
100
|
-
const state = new EngineState();
|
|
101
|
-
const long = 'a'.repeat(40);
|
|
102
|
-
const out = prepare(
|
|
103
|
-
makeCore(),
|
|
104
|
-
state,
|
|
105
|
-
[
|
|
106
|
-
vnode('text', { text: long, width: 40, fontSize: 10 }, [], 't1'),
|
|
107
|
-
vnode('text', { text: long, width: 40, fontSize: 10, maxLines: 2 }, [], 't2'),
|
|
108
|
-
],
|
|
109
|
-
[1, 0, 0, 1, 0, 0],
|
|
110
|
-
true,
|
|
111
|
-
'root',
|
|
112
|
-
[],
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
const wrapped = out[0].props._textHeight as number;
|
|
116
|
-
const clamped = out[1].props._textHeight as number;
|
|
117
|
-
// Clamping cuts lines, so the same paragraph must report a shorter box.
|
|
118
|
-
expect(clamped).toBeLessThan(wrapped);
|
|
119
|
-
expect(clamped).toBeCloseTo(2 * 10 * TEXT_LINE_HEIGHT);
|
|
120
|
-
expect(out[0].props._textWidth).toBe(40);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('sizes the box to the painted line when no width is declared', () => {
|
|
124
|
-
const state = new EngineState();
|
|
125
|
-
const out = prepare(
|
|
126
|
-
makeCore(),
|
|
127
|
-
state,
|
|
128
|
-
[vnode('text', { text: 'abc', fontSize: 10 }, [], 't')],
|
|
129
|
-
[1, 0, 0, 1, 0, 0],
|
|
130
|
-
true,
|
|
131
|
-
'root',
|
|
132
|
-
[],
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
expect(out[0].props._textWidth).toBe(measureTextWidth('abc', '10px sans-serif'));
|
|
136
|
-
expect(out[0].props._textHeight).toBeCloseTo(10 * TEXT_LINE_HEIGHT);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// --- grid -------------------------------------------------------------------
|
|
141
|
-
|
|
142
|
-
describe('prepare · grid', () => {
|
|
143
|
-
it('lays children into columns and records scroll metrics', () => {
|
|
144
|
-
const state = new EngineState();
|
|
145
|
-
const tree = [
|
|
146
|
-
vnode(
|
|
147
|
-
'grid',
|
|
148
|
-
{ columns: 2, cellHeight: 50, gap: 0, width: 200 },
|
|
149
|
-
[vnode('gridItem', {}, [], 'a'), vnode('gridItem', {}, [], 'b')],
|
|
150
|
-
'g',
|
|
151
|
-
),
|
|
152
|
-
];
|
|
153
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
154
|
-
|
|
155
|
-
expect(out[0].props._gridWidth).toBe(200);
|
|
156
|
-
expect(out[0].props._gridContentHeight).toBe(50);
|
|
157
|
-
expect(out[0].props._scrollTop).toBe(0);
|
|
158
|
-
expect(out[0].children[1].props.x).toBe(100);
|
|
159
|
-
expect(state.gridScrollPositions.get('root/kg')).toBe(0);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('preserves an existing scroll position', () => {
|
|
163
|
-
const state = new EngineState();
|
|
164
|
-
state.gridScrollPositions.set('root/kg', 20);
|
|
165
|
-
const tree = [
|
|
166
|
-
vnode(
|
|
167
|
-
'grid',
|
|
168
|
-
{ columns: 1, cellHeight: 50, gap: 0, width: 100, height: 30 },
|
|
169
|
-
[vnode('gridItem', {}, [], 'a')],
|
|
170
|
-
'g',
|
|
171
|
-
),
|
|
172
|
-
];
|
|
173
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
174
|
-
expect(out[0].props._scrollTop).toBe(20);
|
|
175
|
-
expect(out[0].children[0].props._gridBaseY).toBe(0);
|
|
176
|
-
expect(out[0].children[0].props.y).toBe(-20);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('hands each child the box of the cell it occupies', () => {
|
|
180
|
-
const state = new EngineState();
|
|
181
|
-
const tree = [
|
|
182
|
-
vnode(
|
|
183
|
-
'grid',
|
|
184
|
-
{ columns: 2, cellHeight: 50, gap: 0, width: 200 },
|
|
185
|
-
[
|
|
186
|
-
vnode('rect', {}, [], 'r'),
|
|
187
|
-
vnode(
|
|
188
|
-
'gridItem',
|
|
189
|
-
{ columnSpan: 2, rowSpan: 2 },
|
|
190
|
-
[vnode('text', { text: 'x' }, [], 't')],
|
|
191
|
-
'gi',
|
|
192
|
-
),
|
|
193
|
-
],
|
|
194
|
-
'g',
|
|
195
|
-
),
|
|
196
|
-
];
|
|
197
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
198
|
-
const [cell, spanning] = out[0].children;
|
|
199
|
-
|
|
200
|
-
// A direct child owns exactly one cell (200 / 2 columns × 50).
|
|
201
|
-
expect(cell.props._availableWidth).toBe(100);
|
|
202
|
-
expect(cell.props._availableHeight).toBe(50);
|
|
203
|
-
// A GridItem owns its span-expanded area and shares it with the controls it groups.
|
|
204
|
-
expect(spanning.props._availableWidth).toBe(200);
|
|
205
|
-
expect(spanning.props._availableHeight).toBe(100);
|
|
206
|
-
expect(spanning.children[0].props._availableWidth).toBe(200);
|
|
207
|
-
expect(spanning.children[0].props._availableHeight).toBe(100);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it('wraps and clamps a text child to its cell when it declares nothing', () => {
|
|
211
|
-
const state = new EngineState();
|
|
212
|
-
const tree = [
|
|
213
|
-
vnode(
|
|
214
|
-
'grid',
|
|
215
|
-
{ columns: 2, cellHeight: 50, gap: 0, width: 200 },
|
|
216
|
-
[vnode('text', { text: 'a'.repeat(100), fontSize: 10 }, [], 't')],
|
|
217
|
-
'g',
|
|
218
|
-
),
|
|
219
|
-
];
|
|
220
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
221
|
-
const child = out[0].children[0];
|
|
222
|
-
|
|
223
|
-
expect(child.props._textWidth).toBe(100);
|
|
224
|
-
// Four 12px lines fit the 50px cell; a fifth would hang out of it, so the paragraph stops
|
|
225
|
-
// with an ellipsis instead of bleeding into the next row.
|
|
226
|
-
expect(child.props._textHeight).toBeCloseTo(4 * 10 * TEXT_LINE_HEIGHT);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('lets a declared width and maxLines override the cell box', () => {
|
|
230
|
-
const state = new EngineState();
|
|
231
|
-
const tree = [
|
|
232
|
-
vnode(
|
|
233
|
-
'grid',
|
|
234
|
-
{ columns: 2, cellHeight: 50, gap: 0, width: 200 },
|
|
235
|
-
[
|
|
236
|
-
vnode(
|
|
237
|
-
'text',
|
|
238
|
-
{ text: 'a'.repeat(100), fontSize: 10, width: 40, maxLines: 2 },
|
|
239
|
-
[],
|
|
240
|
-
't',
|
|
241
|
-
),
|
|
242
|
-
],
|
|
243
|
-
'g',
|
|
244
|
-
),
|
|
245
|
-
];
|
|
246
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
247
|
-
const child = out[0].children[0];
|
|
248
|
-
|
|
249
|
-
expect(child.props._textWidth).toBe(40);
|
|
250
|
-
expect(child.props._textHeight).toBeCloseTo(2 * 10 * TEXT_LINE_HEIGHT);
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
// --- dialog -----------------------------------------------------------------
|
|
255
|
-
|
|
256
|
-
describe('prepare · dialog', () => {
|
|
257
|
-
it('centres the panel in the viewport and shifts its children', () => {
|
|
258
|
-
const state = new EngineState();
|
|
259
|
-
const tree = [
|
|
260
|
-
vnode(
|
|
261
|
-
'dialog',
|
|
262
|
-
{ width: 200, height: 100, open: true },
|
|
263
|
-
[vnode('rect', { x: 0, y: 0 }, [], 'c')],
|
|
264
|
-
'd',
|
|
265
|
-
),
|
|
266
|
-
];
|
|
267
|
-
const out = prepare(
|
|
268
|
-
makeCore(400, 200, 1),
|
|
269
|
-
state,
|
|
270
|
-
tree,
|
|
271
|
-
[1, 0, 0, 1, 0, 0],
|
|
272
|
-
true,
|
|
273
|
-
'root',
|
|
274
|
-
[],
|
|
275
|
-
);
|
|
276
|
-
|
|
277
|
-
expect(out[0].props._viewportWidth).toBe(400);
|
|
278
|
-
expect(out[0].props._viewportHeight).toBe(200);
|
|
279
|
-
expect(out[0].props._dialogPanelX).toBe(100);
|
|
280
|
-
expect(out[0].props._dialogPanelY).toBe(50);
|
|
281
|
-
expect(out[0].children[0].props.x).toBe(124);
|
|
282
|
-
expect(out[0].children[0].props.y).toBe(114);
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
// --- tabs -------------------------------------------------------------------
|
|
287
|
-
|
|
288
|
-
describe('prepare · tabs', () => {
|
|
289
|
-
it('selects the first tab by default and marks the rest hidden', () => {
|
|
290
|
-
const state = new EngineState();
|
|
291
|
-
const tree = [
|
|
292
|
-
vnode(
|
|
293
|
-
'tabs',
|
|
294
|
-
{},
|
|
295
|
-
[vnode('tabItem', { id: 't1' }, [], 'a'), vnode('tabItem', { id: 't2' }, [], 'b')],
|
|
296
|
-
't',
|
|
297
|
-
),
|
|
298
|
-
];
|
|
299
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
300
|
-
|
|
301
|
-
expect(out[0].props._activeKey).toBe('t1');
|
|
302
|
-
const items = out[0].props._tabItems as Array<{ selected: boolean }>;
|
|
303
|
-
expect(items[0].selected).toBe(true);
|
|
304
|
-
expect(items[1].selected).toBe(false);
|
|
305
|
-
expect(out[0].children[1].props.visible).toBe(false);
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
it('respects a controlled activeKey', () => {
|
|
309
|
-
const state = new EngineState();
|
|
310
|
-
const tree = [
|
|
311
|
-
vnode(
|
|
312
|
-
'tabs',
|
|
313
|
-
{ activeKey: 't2' },
|
|
314
|
-
[vnode('tabItem', { id: 't1' }, [], 'a'), vnode('tabItem', { id: 't2' }, [], 'b')],
|
|
315
|
-
't',
|
|
316
|
-
),
|
|
317
|
-
];
|
|
318
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
319
|
-
expect(out[0].props._activeKey).toBe('t2');
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
// --- collapse ---------------------------------------------------------------
|
|
324
|
-
|
|
325
|
-
describe('prepare · collapse', () => {
|
|
326
|
-
it('stacks items and expands only the active key', () => {
|
|
327
|
-
const state = new EngineState();
|
|
328
|
-
const tree = [
|
|
329
|
-
vnode(
|
|
330
|
-
'collapse',
|
|
331
|
-
{ defaultActiveKeys: ['a'] },
|
|
332
|
-
[
|
|
333
|
-
vnode('collapseItem', { id: 'a', height: 100 }, [], 'i0'),
|
|
334
|
-
vnode('collapseItem', { id: 'b' }, [], 'i1'),
|
|
335
|
-
],
|
|
336
|
-
'c',
|
|
337
|
-
),
|
|
338
|
-
];
|
|
339
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
340
|
-
|
|
341
|
-
expect(out[0].props._activeKeys).toEqual(['a']);
|
|
342
|
-
expect(out[0].children[0].props._expanded).toBe(true);
|
|
343
|
-
expect(out[0].children[0].props.y).toBe(0);
|
|
344
|
-
// second header sits below header(40) + expanded content(100).
|
|
345
|
-
expect(out[0].children[1].props.y).toBe(140);
|
|
346
|
-
expect(out[0].children[1].props._expanded).toBe(false);
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
it('grows the body past the 100px floor when a wrapped text child needs the room', () => {
|
|
350
|
-
const state = new EngineState();
|
|
351
|
-
const tree = [
|
|
352
|
-
vnode(
|
|
353
|
-
'collapse',
|
|
354
|
-
{ defaultActiveKeys: ['a'] },
|
|
355
|
-
[
|
|
356
|
-
vnode(
|
|
357
|
-
'collapseItem',
|
|
358
|
-
{ id: 'a' },
|
|
359
|
-
[
|
|
360
|
-
vnode(
|
|
361
|
-
'text',
|
|
362
|
-
{ text: 'x'.repeat(200), width: 100, fontSize: 20 },
|
|
363
|
-
[],
|
|
364
|
-
'tx',
|
|
365
|
-
),
|
|
366
|
-
],
|
|
367
|
-
'i0',
|
|
368
|
-
),
|
|
369
|
-
vnode('collapseItem', { id: 'b' }, [], 'i1'),
|
|
370
|
-
],
|
|
371
|
-
'c',
|
|
372
|
-
),
|
|
373
|
-
];
|
|
374
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
375
|
-
|
|
376
|
-
const body = out[0].children[0].props._collapseBodyHeight as number;
|
|
377
|
-
expect(body).toBeGreaterThan(100);
|
|
378
|
-
// The next header starts below header(40) + the space the paragraph actually needs.
|
|
379
|
-
expect(out[0].children[1].props.y).toBe(40 + body);
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
it('keeps the historical 100px body when the content fits inside it', () => {
|
|
383
|
-
const state = new EngineState();
|
|
384
|
-
const tree = [
|
|
385
|
-
vnode(
|
|
386
|
-
'collapse',
|
|
387
|
-
{ defaultActiveKeys: ['a'] },
|
|
388
|
-
[
|
|
389
|
-
vnode(
|
|
390
|
-
'collapseItem',
|
|
391
|
-
{ id: 'a' },
|
|
392
|
-
[vnode('text', { text: 'short', width: 200, fontSize: 16 }, [], 'tx')],
|
|
393
|
-
'i0',
|
|
394
|
-
),
|
|
395
|
-
vnode('collapseItem', { id: 'b' }, [], 'i1'),
|
|
396
|
-
],
|
|
397
|
-
'c',
|
|
398
|
-
),
|
|
399
|
-
];
|
|
400
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
401
|
-
|
|
402
|
-
expect(out[0].children[0].props._collapseBodyHeight).toBe(100);
|
|
403
|
-
expect(out[0].children[1].props.y).toBe(140);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it('keeps only the first key when accordion mode is on', () => {
|
|
407
|
-
const state = new EngineState();
|
|
408
|
-
const active = activeCollapseKeys(state, 'k', {
|
|
409
|
-
accordion: true,
|
|
410
|
-
defaultActiveKeys: ['a', 'b'],
|
|
411
|
-
});
|
|
412
|
-
expect([...active]).toEqual(['a']);
|
|
413
|
-
});
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
describe('layoutCollapseNodes', () => {
|
|
417
|
-
it('re-stacks header positions and toggles child visibility', () => {
|
|
418
|
-
const parent = new SceneNode('collapse', { headerHeight: 40, width: 300 });
|
|
419
|
-
const itemA = new SceneNode('collapseItem', { id: 'a', height: 100 });
|
|
420
|
-
const itemB = new SceneNode('collapseItem', { id: 'b' });
|
|
421
|
-
for (const item of [itemA, itemB]) {
|
|
422
|
-
item.parent = parent;
|
|
423
|
-
parent.children.push(item);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
layoutCollapseNodes(parent, new Set(['a']));
|
|
427
|
-
|
|
428
|
-
expect(itemA.props._expanded).toBe(true);
|
|
429
|
-
expect(itemA.props.y).toBe(0);
|
|
430
|
-
expect(itemA.props._collapseHeaderHeight).toBe(40);
|
|
431
|
-
// header(40) + expanded content(100) pushes the next header down.
|
|
432
|
-
expect(itemB.props.y).toBe(140);
|
|
433
|
-
expect(itemB.props._expanded).toBe(false);
|
|
434
|
-
});
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
// --- carousel ---------------------------------------------------------------
|
|
438
|
-
|
|
439
|
-
describe('prepare · carousel', () => {
|
|
440
|
-
it('seeds index and visibility from the selection state', () => {
|
|
441
|
-
const state = new EngineState();
|
|
442
|
-
const tree = [
|
|
443
|
-
vnode(
|
|
444
|
-
'carousel',
|
|
445
|
-
{},
|
|
446
|
-
[vnode('carouselItem', {}, [], 'i0'), vnode('carouselItem', {}, [], 'i1')],
|
|
447
|
-
'p',
|
|
448
|
-
),
|
|
449
|
-
];
|
|
450
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
451
|
-
|
|
452
|
-
expect(out[0].props._activeIndex).toBe(0);
|
|
453
|
-
expect(out[0].props._itemCount).toBe(2);
|
|
454
|
-
expect(out[0].props._transitionProgress).toBe(1);
|
|
455
|
-
expect(out[0].children[0].props.visible).toBe(true);
|
|
456
|
-
expect(out[0].children[1].props.visible).toBe(false);
|
|
457
|
-
expect(state.carouselSelections.get('root/kp')).toBe(0);
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
it('honours a controlled activeIndex', () => {
|
|
461
|
-
const state = new EngineState();
|
|
462
|
-
const tree = [
|
|
463
|
-
vnode(
|
|
464
|
-
'carousel',
|
|
465
|
-
{ activeIndex: 1 },
|
|
466
|
-
[vnode('carouselItem', {}, [], 'i0'), vnode('carouselItem', {}, [], 'i1')],
|
|
467
|
-
'p',
|
|
468
|
-
),
|
|
469
|
-
];
|
|
470
|
-
const out = prepare(makeCore(), state, tree, [1, 0, 0, 1, 0, 0], true, 'root', []);
|
|
471
|
-
expect(out[0].props._activeIndex).toBe(1);
|
|
472
|
-
expect(out[0].children[1].props.visible).toBe(true);
|
|
473
|
-
});
|
|
474
|
-
});
|
package/engine/carousel.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import type { SceneNode } from '../scene';
|
|
2
|
-
import { number } from '../components/draw-utils';
|
|
3
|
-
import { carouselIndex } from '../components/Carousel';
|
|
4
|
-
import { hasVisibleAncestors, isDescendantOf, setTreeVisibility } from './nodes';
|
|
5
|
-
import { nodeTransform } from './transform';
|
|
6
|
-
import { carouselItemOffset } from './vnodes';
|
|
7
|
-
import { updateTextControlVisibility } from './text-controls';
|
|
8
|
-
import type { CarouselTransition, EngineCore, EngineState } from './state';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The carousel state machine: autoplay timing, index transitions and the
|
|
12
|
-
* per-frame easing that slides items. Driven by {@link advanceCarousels} from the
|
|
13
|
-
* render loop and {@link changeCarousel} from pointer interaction.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export function transitionProgress(transition: CarouselTransition, time: number): number {
|
|
17
|
-
return transition.duration <= 0
|
|
18
|
-
? 1
|
|
19
|
-
: Math.max(0, Math.min(1, (time - transition.startedAt) / transition.duration));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function setCarouselItems(
|
|
23
|
-
node: SceneNode,
|
|
24
|
-
activeIndex: number,
|
|
25
|
-
transition: CarouselTransition | null,
|
|
26
|
-
progress: number,
|
|
27
|
-
): void {
|
|
28
|
-
const width = number(node.props.width, 300);
|
|
29
|
-
const items = node.children.filter((child) => child.type === 'carouselItem');
|
|
30
|
-
items.forEach((item, index) => {
|
|
31
|
-
const visible = transition
|
|
32
|
-
? (index === transition.from || index === transition.to) &&
|
|
33
|
-
item.props._userVisible !== false
|
|
34
|
-
: index === activeIndex && item.props._userVisible !== false;
|
|
35
|
-
const baseX = number(item.props._carouselBaseX, number(item.props.x, 0));
|
|
36
|
-
const offset = carouselItemOffset(index, transition, progress, width);
|
|
37
|
-
item.setProps({ x: baseX + offset });
|
|
38
|
-
setTreeVisibility(item, visible);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function updateCarouselInputLayouts(core: EngineCore, state: EngineState, node: SceneNode): void {
|
|
43
|
-
for (const mounted of state.textControls.values()) {
|
|
44
|
-
if (!isDescendantOf(mounted.node, node)) continue;
|
|
45
|
-
mounted.layout = { ...mounted.layout, transform: nodeTransform(mounted.node) };
|
|
46
|
-
}
|
|
47
|
-
updateTextControlVisibility(core, state);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function changeCarousel(
|
|
51
|
-
core: EngineCore,
|
|
52
|
-
state: EngineState,
|
|
53
|
-
node: SceneNode,
|
|
54
|
-
requestedIndex: number,
|
|
55
|
-
): void {
|
|
56
|
-
const items = node.children.filter((child) => child.type === 'carouselItem');
|
|
57
|
-
if (!items.length) return;
|
|
58
|
-
const currentIndex = number(node.props._activeIndex, 0);
|
|
59
|
-
const nextIndex = carouselIndex(
|
|
60
|
-
requestedIndex,
|
|
61
|
-
currentIndex,
|
|
62
|
-
items.length,
|
|
63
|
-
node.props.loop !== false,
|
|
64
|
-
);
|
|
65
|
-
if (nextIndex === currentIndex) return;
|
|
66
|
-
|
|
67
|
-
const stateKey = String(node.props._stateKey ?? '');
|
|
68
|
-
if (state.carouselTransitions.has(stateKey)) return;
|
|
69
|
-
const duration = Math.max(0, number(node.props.transitionDuration, 450));
|
|
70
|
-
if (duration > 0) {
|
|
71
|
-
const transition: CarouselTransition = {
|
|
72
|
-
from: currentIndex,
|
|
73
|
-
to: nextIndex,
|
|
74
|
-
direction: requestedIndex > currentIndex ? 1 : -1,
|
|
75
|
-
startedAt: state.currentFrameTime,
|
|
76
|
-
duration,
|
|
77
|
-
};
|
|
78
|
-
state.carouselTransitions.set(stateKey, transition);
|
|
79
|
-
state.carouselFrameTimes.set(stateKey, state.currentFrameTime);
|
|
80
|
-
node.setProps({ _activeIndex: nextIndex, _transitionProgress: 0 });
|
|
81
|
-
setCarouselItems(node, nextIndex, transition, 0);
|
|
82
|
-
updateCarouselInputLayouts(core, state, node);
|
|
83
|
-
if (node.props.activeIndex === undefined) state.carouselSelections.set(stateKey, nextIndex);
|
|
84
|
-
(node.props.onChange as ((index: number) => void) | undefined)?.(nextIndex);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
state.carouselFrameTimes.set(stateKey, state.currentFrameTime);
|
|
88
|
-
if (node.props.activeIndex !== undefined) {
|
|
89
|
-
(node.props.onChange as ((index: number) => void) | undefined)?.(nextIndex);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
state.carouselSelections.set(stateKey, nextIndex);
|
|
93
|
-
node.setProps({ _activeIndex: nextIndex });
|
|
94
|
-
items.forEach((item, index) => {
|
|
95
|
-
const active = index === nextIndex && item.props._userVisible !== false;
|
|
96
|
-
setTreeVisibility(item, active);
|
|
97
|
-
});
|
|
98
|
-
updateTextControlVisibility(core, state);
|
|
99
|
-
(node.props.onChange as ((index: number) => void) | undefined)?.(nextIndex);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function advanceCarousels(core: EngineCore, state: EngineState, time: number): void {
|
|
103
|
-
state.currentFrameTime = time;
|
|
104
|
-
const activeKeys = new Set<string>();
|
|
105
|
-
core.scene.traverse((node) => {
|
|
106
|
-
if (node.type !== 'carousel') return;
|
|
107
|
-
const stateKey = String(node.props._stateKey ?? '');
|
|
108
|
-
activeKeys.add(stateKey);
|
|
109
|
-
const transition = state.carouselTransitions.get(stateKey);
|
|
110
|
-
if (transition) {
|
|
111
|
-
if (time - transition.startedAt >= transition.duration) {
|
|
112
|
-
state.carouselTransitions.delete(stateKey);
|
|
113
|
-
node.setProps({ _transitionProgress: 1 });
|
|
114
|
-
setCarouselItems(node, transition.to, null, 1);
|
|
115
|
-
updateCarouselInputLayouts(core, state, node);
|
|
116
|
-
state.carouselFrameTimes.set(stateKey, time);
|
|
117
|
-
} else {
|
|
118
|
-
const progress = transitionProgress(transition, time);
|
|
119
|
-
node.setProps({ _transitionProgress: progress });
|
|
120
|
-
const eased = progress * progress * (3 - 2 * progress);
|
|
121
|
-
setCarouselItems(node, transition.to, transition, eased);
|
|
122
|
-
updateCarouselInputLayouts(core, state, node);
|
|
123
|
-
}
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (
|
|
127
|
-
!hasVisibleAncestors(node) ||
|
|
128
|
-
node.props.autoplay === false ||
|
|
129
|
-
node.children.filter((child) => child.type === 'carouselItem').length < 2
|
|
130
|
-
) {
|
|
131
|
-
state.carouselFrameTimes.delete(stateKey);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const lastTime = state.carouselFrameTimes.get(stateKey);
|
|
135
|
-
if (lastTime === undefined) {
|
|
136
|
-
state.carouselFrameTimes.set(stateKey, time);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
const interval = Math.max(250, number(node.props.interval, 3000));
|
|
140
|
-
if (time - lastTime < interval) return;
|
|
141
|
-
state.carouselFrameTimes.set(stateKey, time);
|
|
142
|
-
changeCarousel(core, state, node, number(node.props._activeIndex, 0) + 1);
|
|
143
|
-
});
|
|
144
|
-
for (const key of state.carouselFrameTimes.keys()) {
|
|
145
|
-
if (!activeKeys.has(key)) state.carouselFrameTimes.delete(key);
|
|
146
|
-
}
|
|
147
|
-
for (const key of state.carouselTransitions.keys()) {
|
|
148
|
-
if (!activeKeys.has(key)) state.carouselTransitions.delete(key);
|
|
149
|
-
}
|
|
150
|
-
}
|
package/engine/events.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { SceneNode } from '../scene';
|
|
2
|
-
import type { CanvasPointerEvent } from '../types';
|
|
3
|
-
import type { CanvasViewPointerEvent } from '../components/CanvasView';
|
|
4
|
-
import { controlSize, eventTarget, isCanvasElement } from './nodes';
|
|
5
|
-
import type { HitResult } from './hit-test';
|
|
6
|
-
import type { EngineCore } from './state';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Translates resolved pointer geometry into the public {@link CanvasUIEvent}
|
|
10
|
-
* stream and the per-node `canvasView` pointer callbacks.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
export function emitPointerEvent(
|
|
14
|
-
core: EngineCore,
|
|
15
|
-
type: 'pointerdown' | 'pointermove' | 'pointerup' | 'pointercancel',
|
|
16
|
-
event: CanvasPointerEvent,
|
|
17
|
-
hit: HitResult | null,
|
|
18
|
-
point: { x: number; y: number },
|
|
19
|
-
): void {
|
|
20
|
-
core.onEvent?.({
|
|
21
|
-
type,
|
|
22
|
-
target: hit ? eventTarget(hit.node) : null,
|
|
23
|
-
x: point.x,
|
|
24
|
-
y: point.y,
|
|
25
|
-
localX: hit?.x,
|
|
26
|
-
localY: hit?.y,
|
|
27
|
-
pointerId: event.pointerId,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function emitCanvasViewPointerEvent(
|
|
32
|
-
type: CanvasViewPointerEvent['type'],
|
|
33
|
-
node: SceneNode,
|
|
34
|
-
x: number,
|
|
35
|
-
y: number,
|
|
36
|
-
event: CanvasPointerEvent,
|
|
37
|
-
): void {
|
|
38
|
-
const source = node.props.source;
|
|
39
|
-
if (!isCanvasElement(source)) return;
|
|
40
|
-
const { width, height } = controlSize(node);
|
|
41
|
-
if (!width || !height || !source.width || !source.height) return;
|
|
42
|
-
const fit = node.props.fit ?? 'contain';
|
|
43
|
-
const scale =
|
|
44
|
-
fit === 'cover'
|
|
45
|
-
? Math.max(width / source.width, height / source.height)
|
|
46
|
-
: fit === 'fill'
|
|
47
|
-
? null
|
|
48
|
-
: Math.min(width / source.width, height / source.height);
|
|
49
|
-
const drawnWidth = scale === null ? width : source.width * scale;
|
|
50
|
-
const drawnHeight = scale === null ? height : source.height * scale;
|
|
51
|
-
const offsetX = (width - drawnWidth) / 2;
|
|
52
|
-
const offsetY = (height - drawnHeight) / 2;
|
|
53
|
-
const sourceX = scale === null ? (x * source.width) / width : (x - offsetX) / scale;
|
|
54
|
-
const sourceY = scale === null ? (y * source.height) / height : (y - offsetY) / scale;
|
|
55
|
-
const callback = node.props.onPointerEvent;
|
|
56
|
-
if (typeof callback !== 'function') return;
|
|
57
|
-
(callback as (pointerEvent: CanvasViewPointerEvent) => void)({
|
|
58
|
-
type,
|
|
59
|
-
source,
|
|
60
|
-
x,
|
|
61
|
-
y,
|
|
62
|
-
sourceX,
|
|
63
|
-
sourceY,
|
|
64
|
-
sourceWidth: source.width,
|
|
65
|
-
sourceHeight: source.height,
|
|
66
|
-
pointerId: event.pointerId,
|
|
67
|
-
pointerType: event.pointerType,
|
|
68
|
-
pressure: event.pressure,
|
|
69
|
-
isPrimary: event.isPrimary,
|
|
70
|
-
button: event.button,
|
|
71
|
-
buttons: event.buttons,
|
|
72
|
-
});
|
|
73
|
-
}
|