@web-canvas-ui/react 0.1.1 → 0.2.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 (157) hide show
  1. package/README.md +172 -76
  2. package/index.js +2682 -0
  3. package/package.json +36 -27
  4. package/types/canvas-handle.d.ts +22 -0
  5. package/types/components/Button/button.d.ts +20 -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 +33 -0
  13. package/types/components/ChoiceGroup/logic.d.ts +3 -0
  14. package/types/components/Circle/circle.d.ts +9 -0
  15. package/types/components/Collapse/collapse.d.ts +32 -0
  16. package/types/components/Collapse/logic.d.ts +1 -0
  17. package/types/components/CustomDraw/customDraw.d.ts +22 -0
  18. package/types/components/CustomDraw/index.d.ts +2 -0
  19. package/types/components/DatePicker/datePicker.d.ts +24 -0
  20. package/types/components/Dialog/dialog.d.ts +20 -0
  21. package/types/components/Dropdown/dropdown.d.ts +30 -0
  22. package/types/components/Dropdown/logic.d.ts +5 -0
  23. package/{components/Grid/grid.ts → types/components/Grid/grid.d.ts} +3 -4
  24. package/{components/GridItem/gridItem.ts → types/components/GridItem/gridItem.d.ts} +1 -4
  25. package/{components/Group/group.ts → types/components/Group/group.d.ts} +1 -4
  26. package/types/components/Icon/icon.d.ts +14 -0
  27. package/types/components/Image/image.d.ts +12 -0
  28. package/types/components/Input/input.d.ts +32 -0
  29. package/types/components/Progress/progress.d.ts +15 -0
  30. package/types/components/Radio/radio.d.ts +8 -0
  31. package/types/components/Rect/rect.d.ts +11 -0
  32. package/types/components/Slider/logic.d.ts +1 -0
  33. package/types/components/Slider/slider.d.ts +17 -0
  34. package/types/components/Switch/logic.d.ts +2 -0
  35. package/types/components/Switch/switch.d.ts +14 -0
  36. package/types/components/Tabs/logic.d.ts +2 -0
  37. package/types/components/Tabs/tabs.d.ts +35 -0
  38. package/types/components/Text/text.d.ts +39 -0
  39. package/types/components/Textarea/textarea.d.ts +10 -0
  40. package/types/components/Upload/upload.d.ts +20 -0
  41. package/types/components/Video/index.d.ts +2 -0
  42. package/types/components/Video/video.d.ts +20 -0
  43. package/types/components/draw-utils.d.ts +48 -0
  44. package/types/components/shared.d.ts +7 -0
  45. package/types/components/size.d.ts +6 -0
  46. package/{components/types.ts → types/components/types.d.ts} +3 -12
  47. package/types/engine/carousel.d.ts +10 -0
  48. package/types/engine/events.d.ts +14 -0
  49. package/types/engine/hit-test.d.ts +31 -0
  50. package/types/engine/images.d.ts +8 -0
  51. package/types/engine/interaction.d.ts +11 -0
  52. package/types/engine/layout.d.ts +6 -0
  53. package/types/engine/nodes.d.ts +18 -0
  54. package/{engine/state.ts → types/engine/state.d.ts} +27 -22
  55. package/types/engine/text-controls.d.ts +10 -0
  56. package/types/engine/transform.d.ts +13 -0
  57. package/types/engine/videos.d.ts +14 -0
  58. package/types/engine/vnodes.d.ts +36 -0
  59. package/types/engine.d.ts +36 -0
  60. package/types/image-assets.d.ts +2 -0
  61. package/types/input-bridge.d.ts +40 -0
  62. package/types/react/Canvas.d.ts +4 -0
  63. package/types/react/components.d.ts +30 -0
  64. package/types/react/index.d.ts +4 -0
  65. package/types/renderer.d.ts +20 -0
  66. package/types/scene.d.ts +15 -0
  67. package/{types.ts → types/types.d.ts} +10 -142
  68. package/types/video-assets.d.ts +11 -0
  69. package/Canvas.tsx +0 -142
  70. package/__tests__/pure-logic.test.ts +0 -153
  71. package/__tests__/scene-and-node.test.ts +0 -131
  72. package/__tests__/scratch-space.test.ts +0 -30
  73. package/__tests__/textarea-caret.test.ts +0 -96
  74. package/__tests__/utils.test.ts +0 -114
  75. package/canvas-handle.ts +0 -114
  76. package/components/Button/button.ts +0 -57
  77. package/components/Button/logic.ts +0 -11
  78. package/components/CanvasView/canvasView.ts +0 -103
  79. package/components/CanvasView/index.ts +0 -7
  80. package/components/Carousel/carousel.ts +0 -107
  81. package/components/Carousel/logic.ts +0 -36
  82. package/components/Checkbox/checkbox.ts +0 -14
  83. package/components/ChoiceGroup/choiceGroup.ts +0 -172
  84. package/components/ChoiceGroup/logic.ts +0 -14
  85. package/components/Circle/circle.ts +0 -31
  86. package/components/Collapse/collapse.ts +0 -69
  87. package/components/Collapse/logic.ts +0 -14
  88. package/components/CustomDraw/customDraw.ts +0 -50
  89. package/components/CustomDraw/index.ts +0 -7
  90. package/components/DatePicker/datePicker.ts +0 -70
  91. package/components/Dialog/dialog.ts +0 -102
  92. package/components/Dropdown/dropdown.ts +0 -125
  93. package/components/Dropdown/logic.ts +0 -29
  94. package/components/Icon/icon.ts +0 -669
  95. package/components/Image/image.ts +0 -69
  96. package/components/Input/input.ts +0 -139
  97. package/components/Progress/progress.ts +0 -59
  98. package/components/Radio/radio.ts +0 -14
  99. package/components/Rect/rect.ts +0 -35
  100. package/components/Slider/logic.ts +0 -19
  101. package/components/Slider/slider.ts +0 -58
  102. package/components/Switch/logic.ts +0 -8
  103. package/components/Switch/switch.ts +0 -37
  104. package/components/Tabs/logic.ts +0 -14
  105. package/components/Tabs/tabs.ts +0 -78
  106. package/components/Text/text.ts +0 -95
  107. package/components/Textarea/textarea.ts +0 -16
  108. package/components/Upload/upload.ts +0 -66
  109. package/components/draw-utils.ts +0 -230
  110. package/components/shared.ts +0 -15
  111. package/components/size.ts +0 -20
  112. package/components.ts +0 -98
  113. package/engine/__tests__/carousel.test.ts +0 -218
  114. package/engine/__tests__/hit-test.test.ts +0 -282
  115. package/engine/__tests__/layout.test.ts +0 -474
  116. package/engine/carousel.ts +0 -150
  117. package/engine/events.ts +0 -73
  118. package/engine/hit-test.ts +0 -321
  119. package/engine/images.ts +0 -41
  120. package/engine/interaction.ts +0 -442
  121. package/engine/layout.ts +0 -355
  122. package/engine/nodes.ts +0 -166
  123. package/engine/text-controls.ts +0 -122
  124. package/engine/transform.ts +0 -61
  125. package/engine/vnodes.ts +0 -204
  126. package/engine.ts +0 -162
  127. package/image-assets.ts +0 -37
  128. package/index.ts +0 -78
  129. package/input-bridge.ts +0 -285
  130. package/renderer.ts +0 -303
  131. package/scene.ts +0 -122
  132. package/theme.scss +0 -234
  133. package/web-canvas-ui-react-0.1.1.tgz +0 -0
  134. /package/{components/Button/index.ts → types/components/Button/index.d.ts} +0 -0
  135. /package/{components/Carousel/index.ts → types/components/Carousel/index.d.ts} +0 -0
  136. /package/{components/Checkbox/index.ts → types/components/Checkbox/index.d.ts} +0 -0
  137. /package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +0 -0
  138. /package/{components/Circle/index.ts → types/components/Circle/index.d.ts} +0 -0
  139. /package/{components/Collapse/index.ts → types/components/Collapse/index.d.ts} +0 -0
  140. /package/{components/DatePicker/index.ts → types/components/DatePicker/index.d.ts} +0 -0
  141. /package/{components/Dialog/index.ts → types/components/Dialog/index.d.ts} +0 -0
  142. /package/{components/Dropdown/index.ts → types/components/Dropdown/index.d.ts} +0 -0
  143. /package/{components/Grid/index.ts → types/components/Grid/index.d.ts} +0 -0
  144. /package/{components/GridItem/index.ts → types/components/GridItem/index.d.ts} +0 -0
  145. /package/{components/Group/index.ts → types/components/Group/index.d.ts} +0 -0
  146. /package/{components/Icon/index.ts → types/components/Icon/index.d.ts} +0 -0
  147. /package/{components/Image/index.ts → types/components/Image/index.d.ts} +0 -0
  148. /package/{components/Input/index.ts → types/components/Input/index.d.ts} +0 -0
  149. /package/{components/Progress/index.ts → types/components/Progress/index.d.ts} +0 -0
  150. /package/{components/Radio/index.ts → types/components/Radio/index.d.ts} +0 -0
  151. /package/{components/Rect/index.ts → types/components/Rect/index.d.ts} +0 -0
  152. /package/{components/Slider/index.ts → types/components/Slider/index.d.ts} +0 -0
  153. /package/{components/Switch/index.ts → types/components/Switch/index.d.ts} +0 -0
  154. /package/{components/Tabs/index.ts → types/components/Tabs/index.d.ts} +0 -0
  155. /package/{components/Text/index.ts → types/components/Text/index.d.ts} +0 -0
  156. /package/{components/Textarea/index.ts → types/components/Textarea/index.d.ts} +0 -0
  157. /package/{components/Upload/index.ts → types/components/Upload/index.d.ts} +0 -0
@@ -1,321 +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
- import { normalizeDropdownOptions } from '../components/Dropdown';
6
- import { carouselControlIndex } from '../components/Carousel';
7
- import {
8
- controlSize,
9
- hasVisibleAncestors,
10
- isCanvasElement,
11
- isDescendantOf,
12
- isInteractive,
13
- } from './nodes';
14
- import { invertPoint, multiplyTransform } from './transform';
15
- import { gridScrollbarMetrics } from './vnodes';
16
- import type { EngineCore } from './state';
17
-
18
- /**
19
- * Geometry: resolve client (viewport) coordinates to canvas / node-local space and
20
- * determine which interactive node, if any, is under a pointer, honouring modal
21
- * dialogs and grid/carousel clipping. Pure apart from reading the live scene graph.
22
- */
23
-
24
- export interface HitResult {
25
- node: SceneNode;
26
- x: number;
27
- y: number;
28
- }
29
-
30
- export function canvasPoint(
31
- core: EngineCore,
32
- canvas: HTMLCanvasElement,
33
- clientX: number,
34
- clientY: number,
35
- ): { x: number; y: number } | null {
36
- const rect = canvas.getBoundingClientRect();
37
- if (!rect.width || !rect.height) return null;
38
- return {
39
- x: ((clientX - rect.left) * canvas.width) / rect.width / core.pixelRatio,
40
- y: ((clientY - rect.top) * canvas.height) / rect.height / core.pixelRatio,
41
- };
42
- }
43
-
44
- export function localPoint(
45
- core: EngineCore,
46
- canvas: HTMLCanvasElement,
47
- clientX: number,
48
- clientY: number,
49
- target: SceneNode,
50
- ): { x: number; y: number } | null {
51
- const rect = canvas.getBoundingClientRect();
52
- if (!rect.width || !rect.height) return null;
53
- const x = ((clientX - rect.left) * canvas.width) / rect.width / core.pixelRatio;
54
- const y = ((clientY - rect.top) * canvas.height) / rect.height / core.pixelRatio;
55
- let result: { x: number; y: number } | null = null;
56
- const find = (node: SceneNode, parent: Transform): boolean => {
57
- const transform = multiplyTransform(parent, node.props);
58
- if (node === target) {
59
- result = invertPoint(x, y, transform);
60
- return true;
61
- }
62
- for (const child of node.children) {
63
- if (find(child, transform)) return true;
64
- }
65
- return false;
66
- };
67
- find(core.scene, IDENTITY_TRANSFORM);
68
- return result;
69
- }
70
-
71
- export function isWithinClip(
72
- core: EngineCore,
73
- canvas: HTMLCanvasElement,
74
- clientX: number,
75
- clientY: number,
76
- node: SceneNode,
77
- ): boolean {
78
- let parent = node.parent;
79
- while (parent) {
80
- if (
81
- (parent.type === 'grid' && typeof parent.props.height === 'number') ||
82
- parent.type === 'carousel'
83
- ) {
84
- const point = localPoint(core, canvas, clientX, clientY, parent);
85
- const width =
86
- parent.type === 'grid'
87
- ? number(parent.props._gridWidth, number(parent.props.width, 0))
88
- : number(parent.props.width, 300);
89
- const height = number(parent.props.height, parent.type === 'carousel' ? 180 : 0);
90
- if (!point || point.x < 0 || point.x > width || point.y < 0 || point.y > height) {
91
- return false;
92
- }
93
- }
94
- parent = parent.parent;
95
- }
96
- return true;
97
- }
98
-
99
- export function findHit(
100
- core: EngineCore,
101
- canvas: HTMLCanvasElement,
102
- clientX: number,
103
- clientY: number,
104
- ): HitResult | null {
105
- const rect = canvas.getBoundingClientRect();
106
- if (!rect.width || !rect.height) return null;
107
- const pointX = ((clientX - rect.left) * canvas.width) / rect.width / core.pixelRatio;
108
- const pointY = ((clientY - rect.top) * canvas.height) / rect.height / core.pixelRatio;
109
- const candidates: Array<{ node: SceneNode; transform: Transform }> = [];
110
- const collect = (node: SceneNode, parent: Transform, parentVisible: boolean) => {
111
- const visible = parentVisible && node.props.visible !== false;
112
- if (!visible) return;
113
- // Dialogs render in viewport space, outside their enclosing group transforms.
114
- const transform =
115
- node.type === 'dialog'
116
- ? multiplyTransform(IDENTITY_TRANSFORM, node.props)
117
- : multiplyTransform(parent, node.props);
118
- if (isInteractive(node.type) || (node.type === 'dialog' && node.props.open === true)) {
119
- candidates.push({ node, transform });
120
- }
121
- node.children.forEach((child) => collect(child, transform, visible));
122
- };
123
- collect(core.scene, IDENTITY_TRANSFORM, true);
124
- const modal = [...candidates].reverse().find((candidate) => candidate.node.type === 'dialog');
125
-
126
- for (let index = candidates.length - 1; index >= 0; index--) {
127
- const candidate = candidates[index];
128
- if (modal && candidate.node !== modal.node && !isDescendantOf(candidate.node, modal.node))
129
- continue;
130
- if (
131
- candidate.node.type !== 'dropdown' ||
132
- !candidate.node.props.open ||
133
- candidate.node.props.disabled
134
- )
135
- continue;
136
- if (!isWithinClip(core, canvas, clientX, clientY, candidate.node)) continue;
137
- const local = invertPoint(pointX, pointY, candidate.transform);
138
- if (!local) continue;
139
- const { width, height } = controlSize(candidate.node);
140
- const popupHeight = normalizeDropdownOptions(candidate.node.props.options).length * height;
141
- if (
142
- local.x >= 0 &&
143
- local.x <= width &&
144
- local.y >= height &&
145
- local.y <= height + popupHeight
146
- ) {
147
- return { node: candidate.node, x: local.x, y: local.y };
148
- }
149
- }
150
-
151
- for (let index = candidates.length - 1; index >= 0; index--) {
152
- const candidate = candidates[index];
153
- if (modal && candidate.node !== modal.node && !isDescendantOf(candidate.node, modal.node))
154
- continue;
155
- if (!isWithinClip(core, canvas, clientX, clientY, candidate.node)) continue;
156
- const local = invertPoint(pointX, pointY, candidate.transform);
157
- if (!local) continue;
158
- if (candidate.node.type === 'customDraw') {
159
- const { width, height } = controlSize(candidate.node);
160
- if (local.x < 0 || local.x > width || local.y < 0 || local.y > height) continue;
161
- const hitTest = candidate.node.props.hitTest;
162
- if (
163
- typeof hitTest === 'function' &&
164
- !(
165
- hitTest as (
166
- x: number,
167
- y: number,
168
- size: { width: number; height: number },
169
- ) => boolean
170
- )(local.x, local.y, { width, height })
171
- )
172
- continue;
173
- return { node: candidate.node, x: local.x, y: local.y };
174
- }
175
- if (candidate.node.type === 'canvasView') {
176
- const source = candidate.node.props.source;
177
- if (!isCanvasElement(source) || !source.width || !source.height) continue;
178
- const { width, height } = controlSize(candidate.node);
179
- if (local.x < 0 || local.x > width || local.y < 0 || local.y > height) continue;
180
- const fit = candidate.node.props.fit ?? 'contain';
181
- if (fit === 'contain') {
182
- const scale = Math.min(width / source.width, height / source.height);
183
- const drawnWidth = source.width * scale;
184
- const drawnHeight = source.height * scale;
185
- const offsetX = (width - drawnWidth) / 2;
186
- const offsetY = (height - drawnHeight) / 2;
187
- if (
188
- local.x < offsetX ||
189
- local.x > offsetX + drawnWidth ||
190
- local.y < offsetY ||
191
- local.y > offsetY + drawnHeight
192
- )
193
- continue;
194
- }
195
- return { node: candidate.node, x: local.x, y: local.y };
196
- }
197
- if (candidate.node.type === 'dialog') {
198
- return { node: candidate.node, x: local.x, y: local.y };
199
- }
200
- if (candidate.node.type === 'tabs') {
201
- const width = number(candidate.node.props.width, 300);
202
- const height = number(candidate.node.props.height, 200);
203
- if (local.x >= 0 && local.x <= width && local.y >= 0 && local.y <= height) {
204
- return { node: candidate.node, x: local.x, y: local.y };
205
- }
206
- continue;
207
- }
208
- if (candidate.node.type === 'carousel') {
209
- const width = number(candidate.node.props.width, 300);
210
- const height = number(candidate.node.props.height, 180);
211
- if (local.x >= 0 && local.x <= width && local.y >= 0 && local.y <= height) {
212
- return { node: candidate.node, x: local.x, y: local.y };
213
- }
214
- continue;
215
- }
216
- if (candidate.node.type === 'grid') {
217
- if (typeof candidate.node.props.height !== 'number') continue;
218
- const width = number(
219
- candidate.node.props._gridWidth,
220
- number(candidate.node.props.width, 0),
221
- );
222
- const height = number(candidate.node.props.height, 0);
223
- if (local.x >= 0 && local.x <= width && local.y >= 0 && local.y <= height) {
224
- return { node: candidate.node, x: local.x, y: local.y };
225
- }
226
- continue;
227
- }
228
- if (candidate.node.type === 'collapse') {
229
- const width = number(candidate.node.props.width, 300);
230
- const height = candidate.node.children.reduce(
231
- (total, child) =>
232
- total +
233
- number(child.props._collapseHeaderHeight, 40) +
234
- (child.props._expanded ? number(child.props.height, 100) : 0),
235
- 0,
236
- );
237
- if (local.x >= 0 && local.x <= width && local.y >= 0 && local.y <= height) {
238
- return { node: candidate.node, x: local.x, y: local.y };
239
- }
240
- continue;
241
- }
242
- const { width, height } = controlSize(candidate.node);
243
- const popupHeight =
244
- candidate.node.type === 'dropdown' && candidate.node.props.open
245
- ? normalizeDropdownOptions(candidate.node.props.options).length * height
246
- : 0;
247
- if (local.x >= 0 && local.x <= width && local.y >= 0 && local.y <= height + popupHeight) {
248
- return { node: candidate.node, x: local.x, y: local.y };
249
- }
250
- }
251
- return null;
252
- }
253
-
254
- export function findCarouselControlHit(
255
- core: EngineCore,
256
- canvas: HTMLCanvasElement,
257
- clientX: number,
258
- clientY: number,
259
- ): { node: SceneNode; index: number } | null {
260
- const carousels: SceneNode[] = [];
261
- const dialogs: SceneNode[] = [];
262
- core.scene.traverse((node) => {
263
- if (node.type === 'dialog' && node.props.open === true && hasVisibleAncestors(node)) {
264
- dialogs.push(node);
265
- }
266
- if (node.type === 'carousel' && hasVisibleAncestors(node)) carousels.push(node);
267
- });
268
- const modal = dialogs[dialogs.length - 1];
269
- for (const node of carousels.reverse()) {
270
- if (modal && !isDescendantOf(node, modal)) continue;
271
- const point = localPoint(core, canvas, clientX, clientY, node);
272
- if (!point) continue;
273
- const width = number(node.props.width, 300);
274
- const height = number(node.props.height, 180);
275
- const count = node.children.filter((child) => child.type === 'carouselItem').length;
276
- const current = number(node.props._activeIndex, 0);
277
- const index = carouselControlIndex(
278
- node.props,
279
- point.x,
280
- point.y,
281
- count,
282
- current,
283
- width,
284
- height,
285
- );
286
- if (index !== undefined) return { node, index };
287
- }
288
- return null;
289
- }
290
-
291
- export function findGridScrollbarHit(
292
- core: EngineCore,
293
- canvas: HTMLCanvasElement,
294
- clientX: number,
295
- clientY: number,
296
- ): { node: SceneNode; pointerY: number; scrollTop: number } | null {
297
- const grids: SceneNode[] = [];
298
- core.scene.traverse((node) => {
299
- if (node.type === 'grid' && typeof node.props.height === 'number') grids.push(node);
300
- });
301
- for (const grid of grids.reverse()) {
302
- if (!isWithinClip(core, canvas, clientX, clientY, grid)) continue;
303
- const point = localPoint(core, canvas, clientX, clientY, grid);
304
- if (!point) continue;
305
- const metrics = gridScrollbarMetrics(grid.props);
306
- if (
307
- metrics.maxScroll <= 0 ||
308
- point.x < metrics.trackX ||
309
- point.x > metrics.width ||
310
- point.y < metrics.thumbY ||
311
- point.y > metrics.thumbY + metrics.thumbHeight
312
- )
313
- continue;
314
- return {
315
- node: grid,
316
- pointerY: point.y,
317
- scrollTop: number(grid.props._scrollTop, 0),
318
- };
319
- }
320
- return null;
321
- }
package/engine/images.ts DELETED
@@ -1,41 +0,0 @@
1
- import { subscribeCanvasImage } from '../image-assets';
2
- import type { VNode } from '../types';
3
- import type { EngineCore, EngineState } from './state';
4
-
5
- /**
6
- * Subscribes the engine to the shared image cache for every `image` source in the
7
- * tree, tearing down subscriptions for sources that disappear. Load errors mark the
8
- * scene dirty and forward to each image node's `onError` callback.
9
- */
10
-
11
- export function syncImages(core: EngineCore, state: EngineState, tree: VNode[]): void {
12
- const sources = new Set<string>();
13
- const visit = (nodes: VNode[]) =>
14
- nodes.forEach((node) => {
15
- if (node.type === 'image' && typeof node.props.src === 'string' && node.props.src) {
16
- sources.add(node.props.src);
17
- }
18
- visit(node.children);
19
- });
20
- visit(tree);
21
- for (const [source, dispose] of state.imageSubscriptions) {
22
- if (!sources.has(source)) {
23
- dispose();
24
- state.imageSubscriptions.delete(source);
25
- }
26
- }
27
- for (const source of sources) {
28
- if (state.imageSubscriptions.has(source)) continue;
29
- const dispose = subscribeCanvasImage(source, (error) => {
30
- core.scene.markDirty();
31
- if (error) {
32
- core.scene.traverse((node) => {
33
- if (node.type === 'image' && node.props.src === source) {
34
- (node.props.onError as ((error: Error) => void) | undefined)?.(error);
35
- }
36
- });
37
- }
38
- });
39
- state.imageSubscriptions.set(source, dispose);
40
- }
41
- }