@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.
Files changed (165) hide show
  1. package/README.md +415 -205
  2. package/index.js +3660 -0
  3. package/package.json +36 -27
  4. package/types/canvas-handle.d.ts +22 -0
  5. package/types/components/Button/button.d.ts +22 -0
  6. package/types/components/Button/logic.d.ts +3 -0
  7. package/types/components/CanvasView/canvasView.d.ts +28 -0
  8. package/types/components/CanvasView/index.d.ts +2 -0
  9. package/types/components/Carousel/carousel.d.ts +31 -0
  10. package/types/components/Carousel/logic.d.ts +2 -0
  11. package/types/components/Checkbox/checkbox.d.ts +8 -0
  12. package/types/components/ChoiceGroup/choiceGroup.d.ts +38 -0
  13. package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +1 -1
  14. package/types/components/ChoiceGroup/logic.d.ts +3 -0
  15. package/types/components/Circle/circle.d.ts +9 -0
  16. package/types/components/Collapse/collapse.d.ts +32 -0
  17. package/types/components/Collapse/logic.d.ts +1 -0
  18. package/types/components/CustomDraw/customDraw.d.ts +22 -0
  19. package/types/components/CustomDraw/index.d.ts +2 -0
  20. package/types/components/DatePicker/datePicker.d.ts +24 -0
  21. package/types/components/Dialog/dialog.d.ts +20 -0
  22. package/types/components/Dropdown/dropdown.d.ts +30 -0
  23. package/types/components/Dropdown/logic.d.ts +5 -0
  24. package/types/components/Form/form.d.ts +42 -0
  25. package/types/components/Form/index.d.ts +2 -0
  26. package/{components/Grid/grid.ts → types/components/Grid/grid.d.ts} +3 -4
  27. package/{components/GridItem/gridItem.ts → types/components/GridItem/gridItem.d.ts} +1 -4
  28. package/{components/Group/group.ts → types/components/Group/group.d.ts} +1 -4
  29. package/types/components/Icon/icon.d.ts +14 -0
  30. package/types/components/Image/image.d.ts +12 -0
  31. package/types/components/Input/input.d.ts +32 -0
  32. package/types/components/Progress/progress.d.ts +15 -0
  33. package/types/components/Radio/radio.d.ts +8 -0
  34. package/types/components/Rect/rect.d.ts +11 -0
  35. package/types/components/Slider/logic.d.ts +1 -0
  36. package/types/components/Slider/slider.d.ts +17 -0
  37. package/types/components/Switch/logic.d.ts +2 -0
  38. package/types/components/Switch/switch.d.ts +14 -0
  39. package/types/components/Tabs/logic.d.ts +2 -0
  40. package/types/components/Tabs/tabs.d.ts +35 -0
  41. package/types/components/Text/text.d.ts +39 -0
  42. package/types/components/Textarea/textarea.d.ts +10 -0
  43. package/types/components/Upload/index.d.ts +2 -0
  44. package/types/components/Upload/upload.d.ts +30 -0
  45. package/types/components/Video/index.d.ts +4 -0
  46. package/types/components/Video/logic.d.ts +56 -0
  47. package/types/components/Video/video.d.ts +54 -0
  48. package/types/components/draw-utils.d.ts +48 -0
  49. package/types/components/shared.d.ts +7 -0
  50. package/types/components/size.d.ts +6 -0
  51. package/{components/types.ts → types/components/types.d.ts} +5 -13
  52. package/types/engine/accessibility.d.ts +5 -0
  53. package/types/engine/carousel.d.ts +10 -0
  54. package/types/engine/events.d.ts +14 -0
  55. package/types/engine/forms.d.ts +21 -0
  56. package/types/engine/hit-test.d.ts +31 -0
  57. package/types/engine/images.d.ts +8 -0
  58. package/types/engine/interaction.d.ts +12 -0
  59. package/types/engine/layout.d.ts +6 -0
  60. package/types/engine/nodes.d.ts +19 -0
  61. package/types/engine/state.d.ts +91 -0
  62. package/types/engine/text-controls.d.ts +10 -0
  63. package/types/engine/transform.d.ts +13 -0
  64. package/types/engine/uploads.d.ts +5 -0
  65. package/types/engine/videos.d.ts +28 -0
  66. package/types/engine/vnodes.d.ts +36 -0
  67. package/types/engine.d.ts +38 -0
  68. package/types/image-assets.d.ts +2 -0
  69. package/types/input-bridge.d.ts +54 -0
  70. package/types/react/Canvas.d.ts +4 -0
  71. package/types/react/components.d.ts +31 -0
  72. package/types/react/index.d.ts +5 -0
  73. package/types/renderer.d.ts +20 -0
  74. package/types/scene.d.ts +15 -0
  75. package/{types.ts → types/types.d.ts} +48 -142
  76. package/types/video-assets.d.ts +28 -0
  77. package/Canvas.tsx +0 -142
  78. package/__tests__/pure-logic.test.ts +0 -153
  79. package/__tests__/scene-and-node.test.ts +0 -131
  80. package/__tests__/scratch-space.test.ts +0 -30
  81. package/__tests__/textarea-caret.test.ts +0 -96
  82. package/__tests__/utils.test.ts +0 -114
  83. package/canvas-handle.ts +0 -114
  84. package/components/Button/button.ts +0 -57
  85. package/components/Button/logic.ts +0 -11
  86. package/components/CanvasView/canvasView.ts +0 -103
  87. package/components/CanvasView/index.ts +0 -7
  88. package/components/Carousel/carousel.ts +0 -107
  89. package/components/Carousel/logic.ts +0 -36
  90. package/components/Checkbox/checkbox.ts +0 -14
  91. package/components/ChoiceGroup/choiceGroup.ts +0 -172
  92. package/components/ChoiceGroup/logic.ts +0 -14
  93. package/components/Circle/circle.ts +0 -31
  94. package/components/Collapse/collapse.ts +0 -69
  95. package/components/Collapse/logic.ts +0 -14
  96. package/components/CustomDraw/customDraw.ts +0 -50
  97. package/components/CustomDraw/index.ts +0 -7
  98. package/components/DatePicker/datePicker.ts +0 -70
  99. package/components/Dialog/dialog.ts +0 -102
  100. package/components/Dropdown/dropdown.ts +0 -125
  101. package/components/Dropdown/logic.ts +0 -29
  102. package/components/Icon/icon.ts +0 -669
  103. package/components/Image/image.ts +0 -69
  104. package/components/Input/input.ts +0 -139
  105. package/components/Progress/progress.ts +0 -59
  106. package/components/Radio/radio.ts +0 -14
  107. package/components/Rect/rect.ts +0 -35
  108. package/components/Slider/logic.ts +0 -19
  109. package/components/Slider/slider.ts +0 -58
  110. package/components/Switch/logic.ts +0 -8
  111. package/components/Switch/switch.ts +0 -37
  112. package/components/Tabs/logic.ts +0 -14
  113. package/components/Tabs/tabs.ts +0 -78
  114. package/components/Text/text.ts +0 -95
  115. package/components/Textarea/textarea.ts +0 -16
  116. package/components/Upload/index.ts +0 -2
  117. package/components/Upload/upload.ts +0 -66
  118. package/components/draw-utils.ts +0 -230
  119. package/components/shared.ts +0 -15
  120. package/components/size.ts +0 -20
  121. package/components.ts +0 -98
  122. package/engine/__tests__/carousel.test.ts +0 -218
  123. package/engine/__tests__/hit-test.test.ts +0 -282
  124. package/engine/__tests__/layout.test.ts +0 -474
  125. package/engine/carousel.ts +0 -150
  126. package/engine/events.ts +0 -73
  127. package/engine/hit-test.ts +0 -321
  128. package/engine/images.ts +0 -41
  129. package/engine/interaction.ts +0 -442
  130. package/engine/layout.ts +0 -355
  131. package/engine/nodes.ts +0 -166
  132. package/engine/state.ts +0 -75
  133. package/engine/text-controls.ts +0 -122
  134. package/engine/transform.ts +0 -61
  135. package/engine/vnodes.ts +0 -204
  136. package/engine.ts +0 -162
  137. package/image-assets.ts +0 -37
  138. package/index.ts +0 -78
  139. package/input-bridge.ts +0 -285
  140. package/renderer.ts +0 -303
  141. package/scene.ts +0 -122
  142. package/theme.scss +0 -234
  143. package/web-canvas-ui-react-0.1.1.tgz +0 -0
  144. /package/{components/Button/index.ts → types/components/Button/index.d.ts} +0 -0
  145. /package/{components/Carousel/index.ts → types/components/Carousel/index.d.ts} +0 -0
  146. /package/{components/Checkbox/index.ts → types/components/Checkbox/index.d.ts} +0 -0
  147. /package/{components/Circle/index.ts → types/components/Circle/index.d.ts} +0 -0
  148. /package/{components/Collapse/index.ts → types/components/Collapse/index.d.ts} +0 -0
  149. /package/{components/DatePicker/index.ts → types/components/DatePicker/index.d.ts} +0 -0
  150. /package/{components/Dialog/index.ts → types/components/Dialog/index.d.ts} +0 -0
  151. /package/{components/Dropdown/index.ts → types/components/Dropdown/index.d.ts} +0 -0
  152. /package/{components/Grid/index.ts → types/components/Grid/index.d.ts} +0 -0
  153. /package/{components/GridItem/index.ts → types/components/GridItem/index.d.ts} +0 -0
  154. /package/{components/Group/index.ts → types/components/Group/index.d.ts} +0 -0
  155. /package/{components/Icon/index.ts → types/components/Icon/index.d.ts} +0 -0
  156. /package/{components/Image/index.ts → types/components/Image/index.d.ts} +0 -0
  157. /package/{components/Input/index.ts → types/components/Input/index.d.ts} +0 -0
  158. /package/{components/Progress/index.ts → types/components/Progress/index.d.ts} +0 -0
  159. /package/{components/Radio/index.ts → types/components/Radio/index.d.ts} +0 -0
  160. /package/{components/Rect/index.ts → types/components/Rect/index.d.ts} +0 -0
  161. /package/{components/Slider/index.ts → types/components/Slider/index.d.ts} +0 -0
  162. /package/{components/Switch/index.ts → types/components/Switch/index.d.ts} +0 -0
  163. /package/{components/Tabs/index.ts → types/components/Tabs/index.d.ts} +0 -0
  164. /package/{components/Text/index.ts → types/components/Text/index.d.ts} +0 -0
  165. /package/{components/Textarea/index.ts → types/components/Textarea/index.d.ts} +0 -0
@@ -1,122 +0,0 @@
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
- }
@@ -1,61 +0,0 @@
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 DELETED
@@ -1,204 +0,0 @@
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 DELETED
@@ -1,162 +0,0 @@
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 DELETED
@@ -1,37 +0,0 @@
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 DELETED
@@ -1,78 +0,0 @@
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';