@web-canvas-ui/react 0.0.0-stage → 0.1.2
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/Canvas.tsx +142 -0
- package/README.md +988 -2
- package/__tests__/pure-logic.test.ts +153 -0
- package/__tests__/scene-and-node.test.ts +131 -0
- package/__tests__/scratch-space.test.ts +30 -0
- package/__tests__/textarea-caret.test.ts +96 -0
- package/__tests__/utils.test.ts +114 -0
- package/canvas-handle.ts +114 -0
- package/components/Button/button.ts +57 -0
- package/components/Button/index.ts +3 -0
- package/components/Button/logic.ts +11 -0
- package/components/CanvasView/canvasView.ts +103 -0
- package/components/CanvasView/index.ts +7 -0
- package/components/Carousel/carousel.ts +107 -0
- package/components/Carousel/index.ts +3 -0
- package/components/Carousel/logic.ts +36 -0
- package/components/Checkbox/checkbox.ts +14 -0
- package/components/Checkbox/index.ts +2 -0
- package/components/ChoiceGroup/choiceGroup.ts +172 -0
- package/components/ChoiceGroup/index.ts +3 -0
- package/components/ChoiceGroup/logic.ts +14 -0
- package/components/Circle/circle.ts +31 -0
- package/components/Circle/index.ts +2 -0
- package/components/Collapse/collapse.ts +69 -0
- package/components/Collapse/index.ts +3 -0
- package/components/Collapse/logic.ts +14 -0
- package/components/CustomDraw/customDraw.ts +50 -0
- package/components/CustomDraw/index.ts +7 -0
- package/components/DatePicker/datePicker.ts +70 -0
- package/components/DatePicker/index.ts +2 -0
- package/components/Dialog/dialog.ts +102 -0
- package/components/Dialog/index.ts +2 -0
- package/components/Dropdown/dropdown.ts +125 -0
- package/components/Dropdown/index.ts +3 -0
- package/components/Dropdown/logic.ts +29 -0
- package/components/Grid/grid.ts +15 -0
- package/components/Grid/index.ts +2 -0
- package/components/GridItem/gridItem.ts +11 -0
- package/components/GridItem/index.ts +2 -0
- package/components/Group/group.ts +8 -0
- package/components/Group/index.ts +2 -0
- package/components/Icon/icon.ts +669 -0
- package/components/Icon/index.ts +2 -0
- package/components/Image/image.ts +69 -0
- package/components/Image/index.ts +2 -0
- package/components/Input/index.ts +2 -0
- package/components/Input/input.ts +139 -0
- package/components/Progress/index.ts +2 -0
- package/components/Progress/progress.ts +59 -0
- package/components/Radio/index.ts +2 -0
- package/components/Radio/radio.ts +14 -0
- package/components/Rect/index.ts +2 -0
- package/components/Rect/rect.ts +35 -0
- package/components/Slider/index.ts +3 -0
- package/components/Slider/logic.ts +19 -0
- package/components/Slider/slider.ts +58 -0
- package/components/Switch/index.ts +3 -0
- package/components/Switch/logic.ts +8 -0
- package/components/Switch/switch.ts +37 -0
- package/components/Tabs/index.ts +3 -0
- package/components/Tabs/logic.ts +14 -0
- package/components/Tabs/tabs.ts +78 -0
- package/components/Text/index.ts +2 -0
- package/components/Text/text.ts +95 -0
- package/components/Textarea/index.ts +2 -0
- package/components/Textarea/textarea.ts +16 -0
- package/components/Upload/index.ts +2 -0
- package/components/Upload/upload.ts +66 -0
- package/components/draw-utils.ts +230 -0
- package/components/shared.ts +15 -0
- package/components/size.ts +20 -0
- package/components/types.ts +37 -0
- package/components.ts +98 -0
- package/engine/__tests__/carousel.test.ts +218 -0
- package/engine/__tests__/hit-test.test.ts +282 -0
- package/engine/__tests__/layout.test.ts +474 -0
- package/engine/carousel.ts +150 -0
- package/engine/events.ts +73 -0
- package/engine/hit-test.ts +321 -0
- package/engine/images.ts +41 -0
- package/engine/interaction.ts +442 -0
- package/engine/layout.ts +355 -0
- package/engine/nodes.ts +166 -0
- package/engine/state.ts +75 -0
- package/engine/text-controls.ts +122 -0
- package/engine/transform.ts +61 -0
- package/engine/vnodes.ts +204 -0
- package/engine.ts +162 -0
- package/image-assets.ts +37 -0
- package/index.ts +78 -0
- package/input-bridge.ts +285 -0
- package/package.json +27 -4
- package/renderer.ts +303 -0
- package/scene.ts +122 -0
- package/theme.css +327 -0
- package/theme.scss +234 -0
- package/types.ts +256 -0
- package/web-canvas-ui-react-0.1.1.tgz +0 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { SceneNode } from '../scene';
|
|
2
|
+
import type { InputEvents, TextControlProps } from '../types';
|
|
3
|
+
import { isTextControlVisible } from './nodes';
|
|
4
|
+
import type { EngineCore, EngineState, TextControlDescription } from './state';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Keeps the off-canvas DOM input overlay (text inputs / textareas / date pickers)
|
|
8
|
+
* in sync with the scene graph: mount/update/teardown bridges and push value,
|
|
9
|
+
* focus and scroll state back into the corresponding {@link SceneNode}s.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function syncTextControls(
|
|
13
|
+
core: EngineCore,
|
|
14
|
+
state: EngineState,
|
|
15
|
+
descriptions: TextControlDescription[],
|
|
16
|
+
): void {
|
|
17
|
+
const nodes = new Map<string, SceneNode>();
|
|
18
|
+
core.scene.traverse((node) => {
|
|
19
|
+
const id = node.props._canvasInputId;
|
|
20
|
+
if (typeof id === 'string') nodes.set(id, node);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const activeIds = new Set(descriptions.map((item) => item.id));
|
|
24
|
+
for (const [id, mounted] of state.textControls) {
|
|
25
|
+
if (!activeIds.has(id) || nodes.get(id) !== mounted.node) {
|
|
26
|
+
mounted.dispose();
|
|
27
|
+
state.textControls.delete(id);
|
|
28
|
+
state.inputValues.delete(id);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
for (const description of descriptions) {
|
|
33
|
+
const node = nodes.get(description.id);
|
|
34
|
+
if (!node) continue;
|
|
35
|
+
const layout = {
|
|
36
|
+
...description.layout,
|
|
37
|
+
visible: isTextControlVisible(node, description.layout),
|
|
38
|
+
};
|
|
39
|
+
const value =
|
|
40
|
+
description.props.value ??
|
|
41
|
+
state.inputValues.get(description.id) ??
|
|
42
|
+
description.props.defaultValue ??
|
|
43
|
+
'';
|
|
44
|
+
node.setProps({
|
|
45
|
+
...node.props,
|
|
46
|
+
...description.props,
|
|
47
|
+
value,
|
|
48
|
+
_canvasInputId: description.id,
|
|
49
|
+
_controlledValue: description.props.value !== undefined,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const mounted = state.textControls.get(description.id);
|
|
53
|
+
if (mounted) {
|
|
54
|
+
mounted.layout = layout;
|
|
55
|
+
core.inputBridge.update(description.id, { ...description.props, value }, layout);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const events: InputEvents = {
|
|
60
|
+
input: (nextValue, cursor) => {
|
|
61
|
+
if (node.props._controlledValue !== true)
|
|
62
|
+
state.inputValues.set(description.id, nextValue);
|
|
63
|
+
node.setProps({ value: nextValue, cursorPos: cursor });
|
|
64
|
+
const callback = node.props.onChange;
|
|
65
|
+
if (typeof callback === 'function') callback(nextValue);
|
|
66
|
+
},
|
|
67
|
+
selection: (cursor) => node.setProps({ cursorPos: cursor }),
|
|
68
|
+
focus: () => {
|
|
69
|
+
node.setProps({ focused: true });
|
|
70
|
+
const callback = node.props.onFocus;
|
|
71
|
+
if (typeof callback === 'function') callback();
|
|
72
|
+
},
|
|
73
|
+
blur: () => {
|
|
74
|
+
node.setProps({ focused: false });
|
|
75
|
+
const callback = node.props.onBlur;
|
|
76
|
+
if (typeof callback === 'function') callback();
|
|
77
|
+
},
|
|
78
|
+
scroll: (scrollTop, scrollHeight, clientHeight) => {
|
|
79
|
+
if (
|
|
80
|
+
node.props._scrollTop === scrollTop &&
|
|
81
|
+
node.props._scrollHeight === scrollHeight &&
|
|
82
|
+
node.props._scrollClientHeight === clientHeight
|
|
83
|
+
)
|
|
84
|
+
return;
|
|
85
|
+
node.setProps({
|
|
86
|
+
_scrollTop: scrollTop,
|
|
87
|
+
_scrollHeight: scrollHeight,
|
|
88
|
+
_scrollClientHeight: clientHeight,
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
const dispose = core.inputBridge.mount(
|
|
93
|
+
description.id,
|
|
94
|
+
{ ...description.props, value },
|
|
95
|
+
layout,
|
|
96
|
+
events,
|
|
97
|
+
);
|
|
98
|
+
state.textControls.set(description.id, { node, dispose, layout });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function updateTextControlVisibility(core: EngineCore, state: EngineState): void {
|
|
103
|
+
for (const mounted of state.textControls.values()) {
|
|
104
|
+
const visible = isTextControlVisible(mounted.node, mounted.layout);
|
|
105
|
+
mounted.layout = { ...mounted.layout, visible };
|
|
106
|
+
core.inputBridge.update(
|
|
107
|
+
String(mounted.node.props._canvasInputId),
|
|
108
|
+
mounted.node.props as unknown as TextControlProps,
|
|
109
|
+
mounted.layout,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function removeTextControls(state: EngineState, node: SceneNode): void {
|
|
115
|
+
node.children.forEach((child) => removeTextControls(state, child));
|
|
116
|
+
const id = node.props._canvasInputId;
|
|
117
|
+
if (typeof id === 'string') {
|
|
118
|
+
state.textControls.get(id)?.dispose();
|
|
119
|
+
state.textControls.delete(id);
|
|
120
|
+
state.inputValues.delete(id);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { SceneNode } from '../scene';
|
|
2
|
+
import type { Transform } from '../types';
|
|
3
|
+
import { IDENTITY_TRANSFORM } from '../types';
|
|
4
|
+
import { number } from '../components/draw-utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pure 2D affine-transform math shared by hit-testing, layout and input routing.
|
|
8
|
+
* A transform is the `[a, b, c, d, e, f]` matrix tuple used by the Canvas API.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function multiplyTransform(parent: Transform, props: Record<string, unknown>): Transform {
|
|
12
|
+
const rotation = number(props.rotation, 0);
|
|
13
|
+
const scaleX = number(props.scaleX, 1);
|
|
14
|
+
const scaleY = number(props.scaleY, 1);
|
|
15
|
+
const cos = Math.cos(rotation);
|
|
16
|
+
const sin = Math.sin(rotation);
|
|
17
|
+
const a = cos * scaleX;
|
|
18
|
+
const b = sin * scaleX;
|
|
19
|
+
const c = -sin * scaleY;
|
|
20
|
+
const d = cos * scaleY;
|
|
21
|
+
const x = number(props.x, 0);
|
|
22
|
+
const y = number(props.y, 0);
|
|
23
|
+
const [pa, pb, pc, pd, pe, pf] = parent;
|
|
24
|
+
return [
|
|
25
|
+
pa * a + pc * b,
|
|
26
|
+
pb * a + pd * b,
|
|
27
|
+
pa * c + pc * d,
|
|
28
|
+
pb * c + pd * d,
|
|
29
|
+
pa * x + pc * y + pe,
|
|
30
|
+
pb * x + pd * y + pf,
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function invertPoint(
|
|
35
|
+
x: number,
|
|
36
|
+
y: number,
|
|
37
|
+
transform: Transform,
|
|
38
|
+
): { x: number; y: number } | null {
|
|
39
|
+
const [a, b, c, d, e, f] = transform;
|
|
40
|
+
const determinant = a * d - b * c;
|
|
41
|
+
if (!determinant) return null;
|
|
42
|
+
const dx = x - e;
|
|
43
|
+
const dy = y - f;
|
|
44
|
+
return {
|
|
45
|
+
x: (d * dx - c * dy) / determinant,
|
|
46
|
+
y: (-b * dx + a * dy) / determinant,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Accumulated world transform of a node from the scene root down to it. */
|
|
51
|
+
export function nodeTransform(node: SceneNode): Transform {
|
|
52
|
+
const ancestors: SceneNode[] = [];
|
|
53
|
+
let current: SceneNode | null = node;
|
|
54
|
+
while (current) {
|
|
55
|
+
ancestors.push(current);
|
|
56
|
+
current = current.parent;
|
|
57
|
+
}
|
|
58
|
+
return ancestors
|
|
59
|
+
.reverse()
|
|
60
|
+
.reduce((transform, item) => multiplyTransform(transform, item.props), IDENTITY_TRANSFORM);
|
|
61
|
+
}
|
package/engine/vnodes.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { VNode } from '../types';
|
|
2
|
+
import { number } from '../components/draw-utils';
|
|
3
|
+
import type { CarouselTransition } from './state';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Pure helpers that rewrite a VNode tree during the `prepare` normalization pass,
|
|
7
|
+
* plus metrics used by grid scrolling. They never touch engine state.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function layoutGridChildren(vnode: VNode): {
|
|
11
|
+
children: VNode[];
|
|
12
|
+
contentHeight: number;
|
|
13
|
+
width: number;
|
|
14
|
+
} {
|
|
15
|
+
const columns = vnode.props.columns;
|
|
16
|
+
const explicitCellWidth = vnode.props.cellWidth;
|
|
17
|
+
const gridWidth = vnode.props.width;
|
|
18
|
+
const cellHeight = vnode.props.cellHeight;
|
|
19
|
+
const gap = vnode.props.gap ?? 0;
|
|
20
|
+
if (!Number.isInteger(columns) || (columns as number) < 1) {
|
|
21
|
+
throw new RangeError('Grid columns must be a positive integer.');
|
|
22
|
+
}
|
|
23
|
+
if (typeof cellHeight !== 'number' || !Number.isFinite(cellHeight) || cellHeight <= 0) {
|
|
24
|
+
throw new RangeError('Grid cellHeight must be a positive finite number.');
|
|
25
|
+
}
|
|
26
|
+
if (typeof gap !== 'number' || !Number.isFinite(gap) || gap < 0) {
|
|
27
|
+
throw new RangeError('Grid gap must be a non-negative finite number.');
|
|
28
|
+
}
|
|
29
|
+
if (
|
|
30
|
+
gridWidth !== undefined &&
|
|
31
|
+
(typeof gridWidth !== 'number' || !Number.isFinite(gridWidth) || gridWidth <= 0)
|
|
32
|
+
) {
|
|
33
|
+
throw new RangeError('Grid width must be a positive finite number when provided.');
|
|
34
|
+
}
|
|
35
|
+
if (
|
|
36
|
+
vnode.props.height !== undefined &&
|
|
37
|
+
(typeof vnode.props.height !== 'number' ||
|
|
38
|
+
!Number.isFinite(vnode.props.height) ||
|
|
39
|
+
vnode.props.height <= 0)
|
|
40
|
+
) {
|
|
41
|
+
throw new RangeError('Grid height must be a positive finite number when provided.');
|
|
42
|
+
}
|
|
43
|
+
const cellWidth =
|
|
44
|
+
explicitCellWidth === undefined
|
|
45
|
+
? typeof gridWidth === 'number' && Number.isFinite(gridWidth) && gridWidth > 0
|
|
46
|
+
? (gridWidth - gap * ((columns as number) - 1)) / (columns as number)
|
|
47
|
+
: NaN
|
|
48
|
+
: explicitCellWidth;
|
|
49
|
+
if (typeof cellWidth !== 'number' || !Number.isFinite(cellWidth) || cellWidth <= 0) {
|
|
50
|
+
throw new RangeError(
|
|
51
|
+
'Grid requires a positive finite cellWidth, or a positive width large enough for its columns and gaps.',
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const width =
|
|
56
|
+
typeof gridWidth === 'number' && Number.isFinite(gridWidth)
|
|
57
|
+
? gridWidth
|
|
58
|
+
: cellWidth * (columns as number) + gap * ((columns as number) - 1);
|
|
59
|
+
const occupied = new Set<string>();
|
|
60
|
+
let cursor = 0;
|
|
61
|
+
let contentHeight = 0;
|
|
62
|
+
const children = vnode.children.map((child) => {
|
|
63
|
+
const isGridItem = child.type === 'gridItem';
|
|
64
|
+
const columnSpan = isGridItem ? (child.props.columnSpan ?? 1) : 1;
|
|
65
|
+
const rowSpan = isGridItem ? (child.props.rowSpan ?? 1) : 1;
|
|
66
|
+
if (
|
|
67
|
+
!Number.isInteger(columnSpan) ||
|
|
68
|
+
(columnSpan as number) < 1 ||
|
|
69
|
+
(columnSpan as number) > (columns as number)
|
|
70
|
+
) {
|
|
71
|
+
throw new RangeError(
|
|
72
|
+
'GridItem columnSpan must be a positive integer no greater than Grid columns.',
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (!Number.isInteger(rowSpan) || (rowSpan as number) < 1) {
|
|
76
|
+
throw new RangeError('GridItem rowSpan must be a positive integer.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let row = Math.floor(cursor / (columns as number));
|
|
80
|
+
let column = cursor % (columns as number);
|
|
81
|
+
while (
|
|
82
|
+
column + (columnSpan as number) > (columns as number) ||
|
|
83
|
+
!gridAreaIsFree(occupied, row, column, columnSpan as number, rowSpan as number)
|
|
84
|
+
) {
|
|
85
|
+
cursor++;
|
|
86
|
+
row = Math.floor(cursor / (columns as number));
|
|
87
|
+
column = cursor % (columns as number);
|
|
88
|
+
}
|
|
89
|
+
for (let itemRow = row; itemRow < row + (rowSpan as number); itemRow++) {
|
|
90
|
+
for (
|
|
91
|
+
let itemColumn = column;
|
|
92
|
+
itemColumn < column + (columnSpan as number);
|
|
93
|
+
itemColumn++
|
|
94
|
+
) {
|
|
95
|
+
occupied.add(`${itemRow}:${itemColumn}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
contentHeight = Math.max(
|
|
99
|
+
contentHeight,
|
|
100
|
+
row * ((cellHeight as number) + (gap as number)) +
|
|
101
|
+
(rowSpan as number) * (cellHeight as number) +
|
|
102
|
+
((rowSpan as number) - 1) * (gap as number),
|
|
103
|
+
);
|
|
104
|
+
cursor++;
|
|
105
|
+
|
|
106
|
+
// The box this child has to fit in: one cell, or the span-expanded area for a GridItem.
|
|
107
|
+
// Content nodes read it as their wrap width and line budget unless they declare their own,
|
|
108
|
+
// so `cellWidth`/`cellHeight` no longer have to be copied into every child by hand.
|
|
109
|
+
const areaWidth = cellWidth * (columnSpan as number) + gap * ((columnSpan as number) - 1);
|
|
110
|
+
const areaHeight = cellHeight * (rowSpan as number) + gap * ((rowSpan as number) - 1);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
...child,
|
|
114
|
+
props: {
|
|
115
|
+
...child.props,
|
|
116
|
+
x: number(child.props.x, 0) + column * (cellWidth + gap),
|
|
117
|
+
y: number(child.props.y, 0) + row * (cellHeight + gap),
|
|
118
|
+
_availableWidth: areaWidth,
|
|
119
|
+
_availableHeight: areaHeight,
|
|
120
|
+
...(isGridItem ? { _gridItemWidth: areaWidth, _gridItemHeight: areaHeight } : {}),
|
|
121
|
+
},
|
|
122
|
+
// A GridItem paints nothing itself, so the controls it groups inherit its box. Only one
|
|
123
|
+
// level deep: a nested Group has no size of its own to hand down.
|
|
124
|
+
children: isGridItem
|
|
125
|
+
? stampAvailable(child.children, areaWidth, areaHeight)
|
|
126
|
+
: child.children,
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
return { children, contentHeight, width };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Rewrites `children` with the content box of the container they live in. */
|
|
133
|
+
function stampAvailable(children: VNode[], width: number, height: number): VNode[] {
|
|
134
|
+
return children.map((child) => ({
|
|
135
|
+
...child,
|
|
136
|
+
props: { ...child.props, _availableWidth: width, _availableHeight: height },
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function gridAreaIsFree(
|
|
141
|
+
occupied: Set<string>,
|
|
142
|
+
row: number,
|
|
143
|
+
column: number,
|
|
144
|
+
columnSpan: number,
|
|
145
|
+
rowSpan: number,
|
|
146
|
+
): boolean {
|
|
147
|
+
for (let itemRow = row; itemRow < row + rowSpan; itemRow++) {
|
|
148
|
+
for (let itemColumn = column; itemColumn < column + columnSpan; itemColumn++) {
|
|
149
|
+
if (occupied.has(`${itemRow}:${itemColumn}`)) return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function translateChildren(children: VNode[], offsetX: number, offsetY: number): VNode[] {
|
|
156
|
+
return children.map((child) => ({
|
|
157
|
+
...child,
|
|
158
|
+
props: {
|
|
159
|
+
...child.props,
|
|
160
|
+
x: number(child.props.x, 0) + offsetX,
|
|
161
|
+
y: number(child.props.y, 0) + offsetY,
|
|
162
|
+
},
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function setVNodeVisibility(children: VNode[], parentVisible: boolean): VNode[] {
|
|
167
|
+
return children.map((child) => {
|
|
168
|
+
const userVisible =
|
|
169
|
+
typeof child.props._userVisible === 'boolean'
|
|
170
|
+
? child.props._userVisible
|
|
171
|
+
: child.props.visible !== false;
|
|
172
|
+
const visible = parentVisible && userVisible;
|
|
173
|
+
return {
|
|
174
|
+
...child,
|
|
175
|
+
props: { ...child.props, visible, _userVisible: userVisible },
|
|
176
|
+
children: setVNodeVisibility(child.children, visible),
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function carouselItemOffset(
|
|
182
|
+
index: number,
|
|
183
|
+
transition: CarouselTransition | undefined | null,
|
|
184
|
+
progress: number,
|
|
185
|
+
width: number,
|
|
186
|
+
): number {
|
|
187
|
+
if (!transition) return 0;
|
|
188
|
+
if (index === transition.from) return -transition.direction * width * progress;
|
|
189
|
+
if (index === transition.to) return transition.direction * width * (1 - progress);
|
|
190
|
+
return 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function gridScrollbarMetrics(props: Record<string, unknown>) {
|
|
194
|
+
const height = number(props.height, 0);
|
|
195
|
+
const contentHeight = number(props._gridContentHeight, height);
|
|
196
|
+
const width = number(props._gridWidth, number(props.width, 0));
|
|
197
|
+
const maxScroll = Math.max(0, contentHeight - height);
|
|
198
|
+
const thumbHeight =
|
|
199
|
+
maxScroll > 0 ? Math.min(height, Math.max(18, (height * height) / contentHeight)) : height;
|
|
200
|
+
const trackX = Math.max(0, width - 6);
|
|
201
|
+
const thumbY =
|
|
202
|
+
maxScroll > 0 ? ((height - thumbHeight) * number(props._scrollTop, 0)) / maxScroll : 0;
|
|
203
|
+
return { width, height, maxScroll, thumbHeight, trackX, thumbY };
|
|
204
|
+
}
|
package/engine.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { SceneNode } from './scene';
|
|
2
|
+
import { CanvasRenderer } from './renderer';
|
|
3
|
+
import type {
|
|
4
|
+
CanvasPointerEvent,
|
|
5
|
+
CanvasUIEvent,
|
|
6
|
+
CanvasWheelEvent,
|
|
7
|
+
VNode,
|
|
8
|
+
WebInputBridge,
|
|
9
|
+
} from './types';
|
|
10
|
+
import { IDENTITY_TRANSFORM } from './types';
|
|
11
|
+
import { hasVisibleOpenDialog } from './engine/nodes';
|
|
12
|
+
import { removeTextControls, syncTextControls } from './engine/text-controls';
|
|
13
|
+
import { syncImages } from './engine/images';
|
|
14
|
+
import { prepare } from './engine/layout';
|
|
15
|
+
import { advanceCarousels } from './engine/carousel';
|
|
16
|
+
import { pointerDown, pointerMove, pointerUp, wheel } from './engine/interaction';
|
|
17
|
+
import { invalidateThemeColors } from './components/draw-utils';
|
|
18
|
+
import type { TextControlDescription } from './engine/state';
|
|
19
|
+
import { EngineState } from './engine/state';
|
|
20
|
+
import type { EngineCore } from './engine/state';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Public entry point of the canvas rendering engine.
|
|
24
|
+
*
|
|
25
|
+
* <p>{@code CanvasEngine} is intentionally a thin orchestrator: it owns the
|
|
26
|
+
* {@link EngineCore} services and the {@link EngineState} container, wires them into
|
|
27
|
+
* a render loop, and delegates every concrete behaviour to a cohesive module under
|
|
28
|
+
* {@code ./engine} (layout normalization, hit-testing, interaction, carousel,
|
|
29
|
+
* text controls, images, events). The public surface is unchanged from when this
|
|
30
|
+
* was a single God class.</p>
|
|
31
|
+
*/
|
|
32
|
+
export class CanvasEngine {
|
|
33
|
+
readonly scene = new SceneNode('root');
|
|
34
|
+
readonly renderer = new CanvasRenderer();
|
|
35
|
+
private readonly state = new EngineState();
|
|
36
|
+
private readonly context: CanvasRenderingContext2D;
|
|
37
|
+
private readonly core: EngineCore;
|
|
38
|
+
private animationFrame: number | null = null;
|
|
39
|
+
private themeObserver: MutationObserver | null = null;
|
|
40
|
+
private running = false;
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
context: CanvasRenderingContext2D,
|
|
44
|
+
inputBridge: WebInputBridge,
|
|
45
|
+
onFrame?: (time: number) => void,
|
|
46
|
+
pixelRatio = 1,
|
|
47
|
+
onEvent?: (event: CanvasUIEvent) => void,
|
|
48
|
+
) {
|
|
49
|
+
this.context = context;
|
|
50
|
+
this.core = {
|
|
51
|
+
scene: this.scene,
|
|
52
|
+
renderer: this.renderer,
|
|
53
|
+
context,
|
|
54
|
+
inputBridge,
|
|
55
|
+
pixelRatio,
|
|
56
|
+
onFrame,
|
|
57
|
+
onEvent,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
update(tree: VNode[]): void {
|
|
62
|
+
const descriptions: TextControlDescription[] = [];
|
|
63
|
+
const normalized = prepare(
|
|
64
|
+
this.core,
|
|
65
|
+
this.state,
|
|
66
|
+
tree,
|
|
67
|
+
IDENTITY_TRANSFORM,
|
|
68
|
+
true,
|
|
69
|
+
'root',
|
|
70
|
+
descriptions,
|
|
71
|
+
);
|
|
72
|
+
this.scene.syncChildren(normalized, (node) => removeTextControls(this.state, node));
|
|
73
|
+
this.core.inputBridge.setOverlayVisible(!hasVisibleOpenDialog(this.scene, true));
|
|
74
|
+
syncTextControls(this.core, this.state, descriptions);
|
|
75
|
+
syncImages(this.core, this.state, tree);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
render(): void {
|
|
79
|
+
this.renderer.render(this.context, this.scene);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
requestRender(): void {
|
|
83
|
+
this.scene.markDirty();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
renderTo(context: CanvasRenderingContext2D, scaleX: number, scaleY: number): void {
|
|
87
|
+
context.save();
|
|
88
|
+
context.scale(scaleX, scaleY);
|
|
89
|
+
this.renderer.render(context, this.scene);
|
|
90
|
+
context.restore();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
start(): void {
|
|
94
|
+
if (this.running) return;
|
|
95
|
+
this.running = true;
|
|
96
|
+
const Observer = this.context.canvas.ownerDocument.defaultView?.MutationObserver;
|
|
97
|
+
if (Observer) {
|
|
98
|
+
this.themeObserver = new Observer(() => {
|
|
99
|
+
// A class/style change may flip any CSS custom property, so drop the memoised
|
|
100
|
+
// theme colours before repainting; the next draw recomputes them once per token.
|
|
101
|
+
invalidateThemeColors();
|
|
102
|
+
this.scene.markDirty();
|
|
103
|
+
});
|
|
104
|
+
let element: Element | null = this.context.canvas;
|
|
105
|
+
while (element) {
|
|
106
|
+
this.themeObserver.observe(element, {
|
|
107
|
+
attributes: true,
|
|
108
|
+
attributeFilter: ['class', 'style'],
|
|
109
|
+
});
|
|
110
|
+
element = element.parentElement;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const frame = (time: number) => {
|
|
114
|
+
if (!this.running) return;
|
|
115
|
+
advanceCarousels(this.core, this.state, time);
|
|
116
|
+
if (this.scene.dirty) this.render();
|
|
117
|
+
this.core.onFrame?.(time);
|
|
118
|
+
if (this.running) this.animationFrame = requestAnimationFrame(frame);
|
|
119
|
+
};
|
|
120
|
+
this.animationFrame = requestAnimationFrame(frame);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
pointerDown(event: CanvasPointerEvent): void {
|
|
124
|
+
pointerDown(this.core, this.state, event);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
pointerMove(event: CanvasPointerEvent): void {
|
|
128
|
+
pointerMove(this.core, this.state, event);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pointerUp(event: CanvasPointerEvent, cancelled = false): void {
|
|
132
|
+
pointerUp(this.core, this.state, event, cancelled);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
wheel(event: CanvasWheelEvent): void {
|
|
136
|
+
wheel(this.core, this.state, event);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
destroy(): void {
|
|
140
|
+
this.running = false;
|
|
141
|
+
if (this.animationFrame !== null) cancelAnimationFrame(this.animationFrame);
|
|
142
|
+
this.animationFrame = null;
|
|
143
|
+
this.themeObserver?.disconnect();
|
|
144
|
+
this.themeObserver = null;
|
|
145
|
+
for (const mounted of this.state.textControls.values()) mounted.dispose();
|
|
146
|
+
this.state.textControls.clear();
|
|
147
|
+
this.state.inputValues.clear();
|
|
148
|
+
this.state.imageSubscriptions.forEach((dispose) => dispose());
|
|
149
|
+
this.state.imageSubscriptions.clear();
|
|
150
|
+
this.state.tabSelections.clear();
|
|
151
|
+
this.state.carouselSelections.clear();
|
|
152
|
+
this.state.carouselFrameTimes.clear();
|
|
153
|
+
this.state.carouselTransitions.clear();
|
|
154
|
+
this.state.collapseSelections.clear();
|
|
155
|
+
this.state.gridScrollPositions.clear();
|
|
156
|
+
this.state.pointerSequences.clear();
|
|
157
|
+
this.state.activeSlider = null;
|
|
158
|
+
this.state.activeButton = null;
|
|
159
|
+
this.state.activeGridScroll = null;
|
|
160
|
+
this.core.inputBridge.destroy();
|
|
161
|
+
}
|
|
162
|
+
}
|
package/image-assets.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
interface ImageAsset {
|
|
2
|
+
image: HTMLImageElement;
|
|
3
|
+
error: Error | null;
|
|
4
|
+
listeners: Set<(error: Error | null) => void>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const assets = new Map<string, ImageAsset>();
|
|
8
|
+
|
|
9
|
+
function loadAsset(src: string): ImageAsset {
|
|
10
|
+
const existing = assets.get(src);
|
|
11
|
+
if (existing) return existing;
|
|
12
|
+
const image = new Image();
|
|
13
|
+
const asset: ImageAsset = { image, error: null, listeners: new Set() };
|
|
14
|
+
assets.set(src, asset);
|
|
15
|
+
image.onload = () => asset.listeners.forEach((listener) => listener(null));
|
|
16
|
+
image.onerror = () => {
|
|
17
|
+
asset.error = new Error(`Canvas image failed to load: ${src}`);
|
|
18
|
+
asset.listeners.forEach((listener) => listener(asset.error));
|
|
19
|
+
};
|
|
20
|
+
image.src = src;
|
|
21
|
+
return asset;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getCanvasImage(src: string): HTMLImageElement | null {
|
|
25
|
+
const asset = loadAsset(src);
|
|
26
|
+
return asset.image.complete && asset.image.naturalWidth > 0 ? asset.image : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function subscribeCanvasImage(
|
|
30
|
+
src: string,
|
|
31
|
+
listener: (error: Error | null) => void,
|
|
32
|
+
): () => void {
|
|
33
|
+
const asset = loadAsset(src);
|
|
34
|
+
asset.listeners.add(listener);
|
|
35
|
+
if (asset.image.complete) listener(asset.error);
|
|
36
|
+
return () => asset.listeners.delete(listener);
|
|
37
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export { Canvas } from './Canvas';
|
|
2
|
+
export type { CanvasExportOptions, CanvasHandle } from './Canvas';
|
|
3
|
+
export {
|
|
4
|
+
Group,
|
|
5
|
+
Grid,
|
|
6
|
+
GridItem,
|
|
7
|
+
Rect,
|
|
8
|
+
Circle,
|
|
9
|
+
Text,
|
|
10
|
+
Input,
|
|
11
|
+
DatePicker,
|
|
12
|
+
Radio,
|
|
13
|
+
Checkbox,
|
|
14
|
+
Textarea,
|
|
15
|
+
Dropdown,
|
|
16
|
+
Slider,
|
|
17
|
+
Switch,
|
|
18
|
+
Button,
|
|
19
|
+
Upload,
|
|
20
|
+
Carousel,
|
|
21
|
+
CarouselItem,
|
|
22
|
+
Image,
|
|
23
|
+
Icon,
|
|
24
|
+
Dialog,
|
|
25
|
+
Progress,
|
|
26
|
+
Tabs,
|
|
27
|
+
TabItem,
|
|
28
|
+
Collapse,
|
|
29
|
+
CollapseItem,
|
|
30
|
+
CustomDraw,
|
|
31
|
+
CanvasView,
|
|
32
|
+
} from './components';
|
|
33
|
+
export type {
|
|
34
|
+
CanvasProps,
|
|
35
|
+
GroupProps,
|
|
36
|
+
GridProps,
|
|
37
|
+
GridItemProps,
|
|
38
|
+
ImageProps,
|
|
39
|
+
IconName,
|
|
40
|
+
IconProps,
|
|
41
|
+
IconVariant,
|
|
42
|
+
DialogProps,
|
|
43
|
+
ProgressProps,
|
|
44
|
+
TabsProps,
|
|
45
|
+
TabItemProps,
|
|
46
|
+
CollapseProps,
|
|
47
|
+
CollapseItemProps,
|
|
48
|
+
RectProps,
|
|
49
|
+
CircleProps,
|
|
50
|
+
TextProps,
|
|
51
|
+
InputProps,
|
|
52
|
+
DatePickerProps,
|
|
53
|
+
RadioProps,
|
|
54
|
+
CheckboxProps,
|
|
55
|
+
UploadProps,
|
|
56
|
+
CarouselProps,
|
|
57
|
+
CarouselItemProps,
|
|
58
|
+
ChoiceOption,
|
|
59
|
+
TextareaProps,
|
|
60
|
+
DropdownOption,
|
|
61
|
+
DropdownProps,
|
|
62
|
+
SliderProps,
|
|
63
|
+
SwitchProps,
|
|
64
|
+
ButtonProps,
|
|
65
|
+
ComponentSize,
|
|
66
|
+
CustomDrawProps,
|
|
67
|
+
CustomDrawSize,
|
|
68
|
+
CustomDrawClickEvent,
|
|
69
|
+
CustomDrawHitTest,
|
|
70
|
+
CanvasViewFit,
|
|
71
|
+
CanvasViewPointerEvent,
|
|
72
|
+
CanvasViewPointerEventHandler,
|
|
73
|
+
CanvasViewProps,
|
|
74
|
+
CanvasUIEvent,
|
|
75
|
+
CanvasUIPointerEvent,
|
|
76
|
+
CanvasUIWheelEvent,
|
|
77
|
+
CanvasUIEventTarget,
|
|
78
|
+
} from './types';
|