@web-canvas-ui/react 0.1.2 → 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
@@ -0,0 +1,15 @@
1
+ import type { ComponentKind, VNode } from './types';
2
+ export declare class SceneNode {
3
+ readonly children: SceneNode[];
4
+ parent: SceneNode | null;
5
+ dirty: boolean;
6
+ readonly type: ComponentKind | 'root';
7
+ props: Record<string, unknown>;
8
+ constructor(type: ComponentKind | 'root', props?: Record<string, unknown>);
9
+ setProps(props: Record<string, unknown>): void;
10
+ replaceProps(props: Record<string, unknown>): void;
11
+ markDirty(): void;
12
+ syncChildren(vnodes: VNode[], onRemove: (node: SceneNode) => void): void;
13
+ traverse(visitor: (node: SceneNode) => void): void;
14
+ }
15
+ export declare function descriptorKey(vnode: VNode, index: number, parentPath: string): string;
@@ -1,66 +1,6 @@
1
- import type {
2
- ButtonProps,
3
- CarouselItemProps,
4
- CarouselProps,
5
- CheckboxProps,
6
- CircleProps,
7
- CollapseItemProps,
8
- CollapseProps,
9
- CustomDrawProps,
10
- CanvasViewProps,
11
- DatePickerProps,
12
- DialogProps,
13
- DropdownProps,
14
- GridItemProps,
15
- GridProps,
16
- GroupProps,
17
- IconProps,
18
- ImageProps,
19
- InputProps,
20
- ProgressProps,
21
- RadioProps,
22
- RectProps,
23
- SliderProps,
24
- SwitchProps,
25
- TabItemProps,
26
- TabsProps,
27
- TextProps,
28
- TextareaProps,
29
- UploadProps,
30
- } from './components/types';
31
-
32
- export type ComponentKind =
33
- | 'group'
34
- | 'grid'
35
- | 'gridItem'
36
- | 'rect'
37
- | 'circle'
38
- | 'text'
39
- | 'input'
40
- | 'datePicker'
41
- | 'radio'
42
- | 'checkbox'
43
- | 'upload'
44
- | 'carousel'
45
- | 'carouselItem'
46
- | 'textarea'
47
- | 'dropdown'
48
- | 'slider'
49
- | 'switch'
50
- | 'button'
51
- | 'image'
52
- | 'icon'
53
- | 'dialog'
54
- | 'progress'
55
- | 'tabs'
56
- | 'tabItem'
57
- | 'collapse'
58
- | 'collapseItem'
59
- | 'customDraw'
60
- | 'canvasView';
61
-
1
+ import type { ButtonProps, CarouselItemProps, CarouselProps, CheckboxProps, CircleProps, CollapseItemProps, CollapseProps, CustomDrawProps, CanvasViewProps, DatePickerProps, DialogProps, DropdownProps, GridItemProps, GridProps, GroupProps, IconProps, ImageProps, InputProps, ProgressProps, RadioProps, RectProps, SliderProps, SwitchProps, TabItemProps, TabsProps, TextProps, TextareaProps, UploadProps, VideoProps } from './components/types';
2
+ export type ComponentKind = 'group' | 'grid' | 'gridItem' | 'rect' | 'circle' | 'text' | 'input' | 'datePicker' | 'radio' | 'checkbox' | 'upload' | 'carousel' | 'carouselItem' | 'textarea' | 'dropdown' | 'slider' | 'switch' | 'button' | 'image' | 'icon' | 'dialog' | 'progress' | 'tabs' | 'tabItem' | 'collapse' | 'collapseItem' | 'customDraw' | 'canvasView' | 'video';
62
3
  export type Transform = readonly [number, number, number, number, number, number];
63
-
64
4
  export interface TransformProps {
65
5
  x?: number;
66
6
  y?: number;
@@ -69,7 +9,6 @@ export interface TransformProps {
69
9
  scaleY?: number;
70
10
  visible?: boolean;
71
11
  }
72
-
73
12
  export interface CanvasWheelEvent {
74
13
  currentTarget: HTMLCanvasElement;
75
14
  clientX: number;
@@ -77,56 +16,14 @@ export interface CanvasWheelEvent {
77
16
  deltaY: number;
78
17
  preventDefault(): void;
79
18
  }
80
-
81
- export type {
82
- ButtonProps,
83
- CarouselItemProps,
84
- CarouselProps,
85
- CheckboxProps,
86
- ChoiceOption,
87
- CircleProps,
88
- CollapseItemProps,
89
- CollapseProps,
90
- ComponentSize,
91
- CustomDrawProps,
92
- CustomDrawSize,
93
- CustomDrawClickEvent,
94
- CustomDrawHitTest,
95
- CanvasViewFit,
96
- CanvasViewPointerEvent,
97
- CanvasViewPointerEventHandler,
98
- CanvasViewProps,
99
- DatePickerProps,
100
- DialogProps,
101
- DropdownOption,
102
- DropdownProps,
103
- GridItemProps,
104
- GridProps,
105
- GroupProps,
106
- IconName,
107
- IconProps,
108
- IconVariant,
109
- ImageProps,
110
- InputProps,
111
- ProgressProps,
112
- RadioProps,
113
- RectProps,
114
- SliderProps,
115
- SwitchProps,
116
- TabItemProps,
117
- TabsProps,
118
- TextProps,
119
- TextareaProps,
120
- UploadProps,
121
- } from './components/types';
122
-
19
+ export type { ButtonProps, CarouselItemProps, CarouselProps, CheckboxProps, ChoiceOption, CircleProps, CollapseItemProps, CollapseProps, ComponentSize, CustomDrawProps, CustomDrawSize, CustomDrawClickEvent, CustomDrawHitTest, CanvasViewFit, CanvasViewPointerEvent, CanvasViewPointerEventHandler, CanvasViewProps, DatePickerProps, DialogProps, DropdownOption, DropdownProps, GridItemProps, GridProps, GroupProps, IconName, IconProps, IconVariant, ImageProps, InputProps, ProgressProps, RadioProps, RectProps, SliderProps, SwitchProps, TabItemProps, TabsProps, TextProps, TextareaProps, UploadProps, VideoProps, } from './components/types';
123
20
  export interface CanvasComponentProps {
124
21
  group: Omit<GroupProps, 'children'>;
125
22
  grid: Omit<GridProps, 'children'>;
126
23
  gridItem: Omit<GridItemProps, 'children'>;
127
24
  rect: RectProps;
128
25
  circle: CircleProps;
129
- text: Omit<TextProps, 'children'>;
26
+ text: TextProps;
130
27
  input: InputProps;
131
28
  datePicker: DatePickerProps;
132
29
  radio: RadioProps;
@@ -149,61 +46,39 @@ export interface CanvasComponentProps {
149
46
  collapseItem: Omit<CollapseItemProps, 'children'>;
150
47
  customDraw: CustomDrawProps;
151
48
  canvasView: CanvasViewProps;
49
+ video: VideoProps;
152
50
  }
153
-
154
51
  type KeysOfUnion<T> = T extends T ? keyof T : never;
155
-
156
52
  export type CanvasNodeProps = CanvasComponentProps[ComponentKind];
157
53
  export type CanvasNodePropKey = KeysOfUnion<CanvasNodeProps>;
158
- export type CanvasNodePropValue<Key extends CanvasNodePropKey> = CanvasNodeProps extends infer Props
159
- ? Props extends object
160
- ? Key extends keyof Props
161
- ? Props[Key]
162
- : never
163
- : never
164
- : never;
165
-
54
+ export type CanvasNodePropValue<Key extends CanvasNodePropKey> = CanvasNodeProps extends infer Props ? Props extends object ? Key extends keyof Props ? Props[Key] : never : never : never;
166
55
  export interface VNode {
167
56
  type: ComponentKind;
168
57
  key: string | null;
169
58
  props: Record<string, unknown>;
170
59
  children: VNode[];
171
60
  }
172
-
173
61
  export interface InputLayout {
174
62
  width: number;
175
63
  height: number;
176
64
  transform: Transform;
177
65
  visible: boolean;
178
66
  }
179
-
180
67
  export type TextControlProps = InputProps | DatePickerProps | TextareaProps;
181
-
182
68
  export interface InputEvents {
183
69
  input: (value: string, cursor: number) => void;
184
70
  selection: (cursor: number) => void;
185
71
  focus: () => void;
186
72
  blur: () => void;
187
- scroll: (scrollTop: number, scrollHeight: number, clientHeight: number) => void;
73
+ scroll: (scrollTop: number, scrollHeight: number, clientHeight: number, clientWidth: number) => void;
188
74
  }
189
-
190
75
  export interface WebInputBridge {
191
- mount(
192
- id: string,
193
- props: TextControlProps,
194
- layout: InputLayout,
195
- events: InputEvents,
196
- ): () => void;
76
+ mount(id: string, props: TextControlProps, layout: InputLayout, events: InputEvents): () => void;
197
77
  update(id: string, props: TextControlProps, layout: InputLayout): void;
198
- pickFiles(
199
- accept: string | undefined,
200
- multiple: boolean,
201
- onChange: (files: File[]) => void,
202
- ): void;
78
+ pickFiles(accept: string | undefined, multiple: boolean, onChange: (files: File[]) => void): void;
203
79
  setOverlayVisible(visible: boolean): void;
204
80
  destroy(): void;
205
81
  }
206
-
207
82
  export interface CanvasProps {
208
83
  width: number;
209
84
  height: number;
@@ -214,12 +89,10 @@ export interface CanvasProps {
214
89
  onEvent?: (event: CanvasUIEvent) => void;
215
90
  children?: unknown;
216
91
  }
217
-
218
92
  export interface CanvasUIEventTarget {
219
93
  readonly type: ComponentKind;
220
94
  readonly key: string | null;
221
95
  }
222
-
223
96
  interface CanvasUIEventPosition {
224
97
  readonly target: CanvasUIEventTarget | null;
225
98
  readonly x: number;
@@ -227,19 +100,15 @@ interface CanvasUIEventPosition {
227
100
  readonly localX?: number;
228
101
  readonly localY?: number;
229
102
  }
230
-
231
103
  export type CanvasUIPointerEvent = CanvasUIEventPosition & {
232
104
  readonly type: 'pointerdown' | 'pointermove' | 'pointerup' | 'pointercancel' | 'click';
233
105
  readonly pointerId: number;
234
106
  };
235
-
236
107
  export type CanvasUIWheelEvent = CanvasUIEventPosition & {
237
108
  readonly type: 'wheel';
238
109
  readonly deltaY: number;
239
110
  };
240
-
241
111
  export type CanvasUIEvent = CanvasUIPointerEvent | CanvasUIWheelEvent;
242
-
243
112
  export interface CanvasPointerEvent {
244
113
  currentTarget: HTMLCanvasElement;
245
114
  clientX: number;
@@ -252,5 +121,4 @@ export interface CanvasPointerEvent {
252
121
  buttons?: number;
253
122
  preventDefault(): void;
254
123
  }
255
-
256
- export const IDENTITY_TRANSFORM: Transform = [1, 0, 0, 1, 0, 0];
124
+ export declare const IDENTITY_TRANSFORM: Transform;
@@ -0,0 +1,11 @@
1
+ export type VideoLoadListener = (error: Error | null) => void;
2
+ export type VideoTimeListener = (currentTime: number, duration: number) => void;
3
+ /** The pooled element for `src`; loading happens as a side effect. */
4
+ export declare function acquireCanvasVideoElement(src: string): HTMLVideoElement;
5
+ /** The pooled element only once its first frame is decodable; null triggers a redraw on ready. */
6
+ export declare function getCanvasVideo(src: string): HTMLVideoElement | null;
7
+ /** The pooled element if one exists, without acquiring a reference or starting a load. */
8
+ export declare function peekCanvasVideo(src: string): HTMLVideoElement | null;
9
+ export declare function subscribeCanvasVideo(src: string, onLoad: VideoLoadListener, onTime: VideoTimeListener): () => void;
10
+ /** Drops one node reference; unloads the shared element once no node holds it. */
11
+ export declare function releaseCanvasVideo(src: string): void;
package/Canvas.tsx DELETED
@@ -1,142 +0,0 @@
1
- import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from 'react';
2
- import { compileChildren } from './components';
3
- import { CanvasEngine } from './engine';
4
- import { WebInputBridge } from './input-bridge';
5
- import {
6
- createExportCanvas,
7
- exportCanvasBlob,
8
- exportCanvasDataURL,
9
- getDevicePixelRatio,
10
- normalizeExportOptions,
11
- } from './canvas-handle';
12
- import type { CanvasExportOptions, CanvasHandle } from './canvas-handle';
13
- import type { CanvasProps } from './types';
14
-
15
- export type { CanvasExportOptions, CanvasHandle } from './canvas-handle';
16
-
17
- export const Canvas = forwardRef<CanvasHandle, CanvasProps>(function Canvas(
18
- { width, height, style, className, ariaLabel = 'Canvas', onFrame, onEvent, children },
19
- ref,
20
- ) {
21
- const canvasRef = useRef<HTMLCanvasElement>(null);
22
- const overlayRef = useRef<HTMLDivElement>(null);
23
- const containerRef = useRef<HTMLDivElement>(null);
24
- const engineRef = useRef<CanvasEngine | null>(null);
25
- const pixelRatioRef = useRef(1);
26
- const tree = useMemo(() => compileChildren(children), [children]);
27
- const onFrameRef = useRef(onFrame);
28
- onFrameRef.current = onFrame;
29
- const onEventRef = useRef<CanvasProps['onEvent']>(undefined);
30
- onEventRef.current = onEvent;
31
-
32
- useImperativeHandle(
33
- ref,
34
- () => ({
35
- getCanvas: () => canvasRef.current,
36
- requestRender: () => engineRef.current?.requestRender(),
37
- toDataURL: (optionsOrType: CanvasExportOptions | string = {}, legacyQuality) => {
38
- const canvas = canvasRef.current;
39
- if (!canvas) throw new Error('Canvas is not mounted.');
40
- const options = normalizeExportOptions(optionsOrType, legacyQuality);
41
- return exportCanvasDataURL(
42
- createExportCanvas(canvas, engineRef.current, options, pixelRatioRef.current),
43
- options.type,
44
- options.quality,
45
- );
46
- },
47
- toBlob: (optionsOrType: CanvasExportOptions | string = {}, legacyQuality) =>
48
- new Promise((resolve, reject) => {
49
- const canvas = canvasRef.current;
50
- if (!canvas) {
51
- reject(new Error('Canvas is not mounted.'));
52
- return;
53
- }
54
- const options = normalizeExportOptions(optionsOrType, legacyQuality);
55
- exportCanvasBlob(
56
- createExportCanvas(
57
- canvas,
58
- engineRef.current,
59
- options,
60
- pixelRatioRef.current,
61
- ),
62
- options.type,
63
- options.quality,
64
- ).then(resolve, reject);
65
- }),
66
- }),
67
- [],
68
- );
69
-
70
- useEffect(() => {
71
- const canvas = canvasRef.current;
72
- const overlay = overlayRef.current;
73
- const container = containerRef.current;
74
- if (!canvas || !overlay || !container) return;
75
-
76
- const ratio = getDevicePixelRatio();
77
- pixelRatioRef.current = ratio;
78
- canvas.width = Math.round(width * ratio);
79
- canvas.height = Math.round(height * ratio);
80
- const context = canvas.getContext('2d');
81
- if (!context) throw new Error('Canvas 2D context is not available.');
82
- context.scale(ratio, ratio);
83
-
84
- const engine = new CanvasEngine(
85
- context,
86
- new WebInputBridge(overlay),
87
- (time) => onFrameRef.current?.(time),
88
- ratio,
89
- (event) => onEventRef.current?.(event),
90
- );
91
- engineRef.current = engine;
92
- engine.update(tree);
93
- engine.start();
94
- const wheel = (event: WheelEvent) => {
95
- engine.wheel({
96
- currentTarget: canvas,
97
- clientX: event.clientX,
98
- clientY: event.clientY,
99
- deltaY: event.deltaY,
100
- preventDefault: () => event.preventDefault(),
101
- });
102
- };
103
- container.addEventListener('wheel', wheel, { passive: false });
104
-
105
- return () => {
106
- container.removeEventListener('wheel', wheel);
107
- engine.destroy();
108
- engineRef.current = null;
109
- };
110
- }, [width, height]);
111
-
112
- useEffect(() => {
113
- engineRef.current?.update(tree);
114
- }, [tree]);
115
-
116
- return (
117
- <div
118
- ref={containerRef}
119
- className={className}
120
- style={{ ...style, position: 'relative', width, height }}
121
- >
122
- <canvas
123
- ref={canvasRef}
124
- aria-label={ariaLabel}
125
- style={{ display: 'block', width: '100%', height: '100%' }}
126
- onPointerDown={(event) => engineRef.current?.pointerDown(event)}
127
- onPointerMove={(event) => engineRef.current?.pointerMove(event)}
128
- onPointerUp={(event) => engineRef.current?.pointerUp(event)}
129
- onPointerCancel={(event) => engineRef.current?.pointerUp(event, true)}
130
- />
131
- <div
132
- ref={overlayRef}
133
- style={{
134
- position: 'absolute',
135
- inset: 0,
136
- overflow: 'hidden',
137
- pointerEvents: 'none',
138
- }}
139
- />
140
- </div>
141
- );
142
- });
@@ -1,153 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { toggleCollapseKeys } from '../components/Collapse/logic';
3
- import { checkboxSelection, radioSelection } from '../components/ChoiceGroup/logic';
4
- import { dropdownOptionAt, normalizeDropdownOptions } from '../components/Dropdown/logic';
5
- import { carouselControlIndex, carouselIndex } from '../components/Carousel/logic';
6
- import { sliderValueAt } from '../components/Slider/logic';
7
- import { overlayTextStyle } from '../input-bridge';
8
-
9
- describe('toggleCollapseKeys', () => {
10
- it('adds a new key and toggles it off when already active', () => {
11
- expect(toggleCollapseKeys(['a'], 'b', false)).toEqual(['a', 'b']);
12
- expect(toggleCollapseKeys(['a', 'b'], 'a', false)).toEqual(['b']);
13
- });
14
-
15
- it('clears other keys in accordion mode', () => {
16
- expect(toggleCollapseKeys(['a'], 'b', true)).toEqual(['b']);
17
- });
18
- });
19
-
20
- describe('radioSelection', () => {
21
- const option = { label: 'X', value: 'x' };
22
- it('deselects when re-picking the active value', () => {
23
- expect(radioSelection('x', option)).toBeUndefined();
24
- });
25
- it('selects the new value otherwise', () => {
26
- expect(radioSelection('y', option)).toBe('x');
27
- expect(radioSelection(undefined, option)).toBe('x');
28
- });
29
- });
30
-
31
- describe('checkboxSelection', () => {
32
- const option = { label: 'X', value: 'x' };
33
- it('removes an already-selected value', () => {
34
- expect(checkboxSelection(['x'], option)).toEqual([]);
35
- });
36
- it('adds a value and filters out non-strings', () => {
37
- expect(checkboxSelection(['y'], option)).toEqual(['y', 'x']);
38
- expect(checkboxSelection(['y', 1], option)).toEqual(['y', 'x']);
39
- expect(checkboxSelection(undefined, option)).toEqual(['x']);
40
- });
41
- });
42
-
43
- describe('normalizeDropdownOptions / dropdownOptionAt', () => {
44
- it('expands string options and keeps object options', () => {
45
- expect(normalizeDropdownOptions(['a', { label: 'L', value: 'V' }])).toEqual([
46
- { label: 'a', value: 'a' },
47
- { label: 'L', value: 'V', disabled: false },
48
- ]);
49
- });
50
-
51
- it('preserves the disabled flag', () => {
52
- expect(
53
- normalizeDropdownOptions([{ label: 'L', value: 'V', disabled: true }])[0].disabled,
54
- ).toBe(true);
55
- });
56
-
57
- it('drops invalid entries and non-arrays', () => {
58
- expect(normalizeDropdownOptions([{ label: 1, value: 2 }])).toEqual([]);
59
- expect(normalizeDropdownOptions(['a', null, 5])).toEqual([{ label: 'a', value: 'a' }]);
60
- expect(normalizeDropdownOptions('nope')).toEqual([]);
61
- });
62
-
63
- it('indexes into the normalized list', () => {
64
- expect(dropdownOptionAt(['a', 'b'], 1)).toEqual({ label: 'b', value: 'b' });
65
- expect(dropdownOptionAt(['a'], 5)).toBeUndefined();
66
- });
67
- });
68
-
69
- describe('carouselIndex', () => {
70
- it('wraps around when looping', () => {
71
- expect(carouselIndex(5, 0, 3, true)).toBe(2);
72
- expect(carouselIndex(-1, 0, 3, true)).toBe(2);
73
- });
74
- it('clamps when not looping', () => {
75
- expect(carouselIndex(5, 0, 3, false)).toBe(2);
76
- expect(carouselIndex(-1, 0, 3, false)).toBe(0);
77
- });
78
- it('returns 0 for an empty carousel', () => {
79
- expect(carouselIndex(2, 0, 0, true)).toBe(0);
80
- });
81
- });
82
-
83
- describe('carouselControlIndex', () => {
84
- const props = {};
85
- it('ignores interaction for fewer than two items', () => {
86
- expect(carouselControlIndex(props, 10, 90, 1, 0, 300, 180)).toBeUndefined();
87
- });
88
- it('hits the previous arrow on the left edge', () => {
89
- expect(carouselControlIndex(props, 10, 90, 3, 0, 300, 180)).toBe(-1);
90
- });
91
- it('hits the next arrow on the right edge', () => {
92
- expect(carouselControlIndex(props, 290, 90, 3, 0, 300, 180)).toBe(1);
93
- });
94
- it('hits a dot near the bottom', () => {
95
- // startX = (300 - (3-1)*14) / 2 = 136, first dot at x = 136.
96
- expect(carouselControlIndex({ showDots: true }, 136, 160, 3, 1, 300, 180)).toBe(0);
97
- });
98
- });
99
-
100
- describe('sliderValueAt', () => {
101
- const props = { min: 0, max: 100, step: 1, height: 32 };
102
- it('maps the midpoint to the middle value', () => {
103
- // thumbRadius = 8, width = 160 -> ratio (80-8)/144 = 0.5
104
- expect(sliderValueAt(props, 160, 80)).toBe(50);
105
- });
106
- it('clamps to the ends', () => {
107
- expect(sliderValueAt(props, 160, 0)).toBe(0);
108
- expect(sliderValueAt(props, 160, 160)).toBe(100);
109
- });
110
- it('snaps to the configured step', () => {
111
- expect(sliderValueAt({ min: 0, max: 100, step: 10, height: 32 }, 160, 80)).toBe(50);
112
- });
113
- it('returns undefined for an inverted range', () => {
114
- expect(sliderValueAt({ min: 10, max: 10 }, 160, 80)).toBeUndefined();
115
- });
116
- });
117
-
118
- describe('overlayTextStyle', () => {
119
- it('mirrors the font and inset the Canvas draws with', () => {
120
- expect(
121
- overlayTextStyle({ fontSize: 20, fontFamily: 'monospace', padding: 12 }, false),
122
- ).toMatchObject({
123
- fontFamily: 'monospace',
124
- fontSize: '20px',
125
- paddingLeft: '12px',
126
- paddingRight: '12px',
127
- });
128
- });
129
-
130
- it('falls back to the drawing defaults when nothing is declared', () => {
131
- const styles = overlayTextStyle({}, false);
132
- expect(styles.fontSize).toBe('14px');
133
- expect(styles.fontFamily).toBe('sans-serif');
134
- expect(styles.paddingLeft).toBe('8px');
135
- // A single-line control is vertically centred by the element itself, like the Canvas middle baseline.
136
- expect(styles.paddingTop).toBeUndefined();
137
- });
138
-
139
- it('keeps the 1.35 leading and vertical padding for a textarea', () => {
140
- expect(overlayTextStyle({ padding: 6 }, true)).toMatchObject({
141
- paddingTop: '6px',
142
- paddingBottom: '6px',
143
- lineHeight: '1.35',
144
- });
145
- });
146
-
147
- it('tolerates a datePicker, which declares neither padding nor fontFamily', () => {
148
- expect(overlayTextStyle({ value: '2024-01-02' }, false)).toMatchObject({
149
- paddingLeft: '8px',
150
- fontFamily: 'sans-serif',
151
- });
152
- });
153
- });