@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.
- package/README.md +172 -76
- package/index.js +2682 -0
- package/package.json +36 -27
- package/types/canvas-handle.d.ts +22 -0
- package/types/components/Button/button.d.ts +20 -0
- package/types/components/Button/logic.d.ts +3 -0
- package/types/components/CanvasView/canvasView.d.ts +28 -0
- package/types/components/CanvasView/index.d.ts +2 -0
- package/types/components/Carousel/carousel.d.ts +31 -0
- package/types/components/Carousel/logic.d.ts +2 -0
- package/types/components/Checkbox/checkbox.d.ts +8 -0
- package/types/components/ChoiceGroup/choiceGroup.d.ts +33 -0
- package/types/components/ChoiceGroup/logic.d.ts +3 -0
- package/types/components/Circle/circle.d.ts +9 -0
- package/types/components/Collapse/collapse.d.ts +32 -0
- package/types/components/Collapse/logic.d.ts +1 -0
- package/types/components/CustomDraw/customDraw.d.ts +22 -0
- package/types/components/CustomDraw/index.d.ts +2 -0
- package/types/components/DatePicker/datePicker.d.ts +24 -0
- package/types/components/Dialog/dialog.d.ts +20 -0
- package/types/components/Dropdown/dropdown.d.ts +30 -0
- package/types/components/Dropdown/logic.d.ts +5 -0
- package/{components/Grid/grid.ts → types/components/Grid/grid.d.ts} +3 -4
- package/{components/GridItem/gridItem.ts → types/components/GridItem/gridItem.d.ts} +1 -4
- package/{components/Group/group.ts → types/components/Group/group.d.ts} +1 -4
- package/types/components/Icon/icon.d.ts +14 -0
- package/types/components/Image/image.d.ts +12 -0
- package/types/components/Input/input.d.ts +32 -0
- package/types/components/Progress/progress.d.ts +15 -0
- package/types/components/Radio/radio.d.ts +8 -0
- package/types/components/Rect/rect.d.ts +11 -0
- package/types/components/Slider/logic.d.ts +1 -0
- package/types/components/Slider/slider.d.ts +17 -0
- package/types/components/Switch/logic.d.ts +2 -0
- package/types/components/Switch/switch.d.ts +14 -0
- package/types/components/Tabs/logic.d.ts +2 -0
- package/types/components/Tabs/tabs.d.ts +35 -0
- package/types/components/Text/text.d.ts +39 -0
- package/types/components/Textarea/textarea.d.ts +10 -0
- package/types/components/Upload/upload.d.ts +20 -0
- package/types/components/Video/index.d.ts +2 -0
- package/types/components/Video/video.d.ts +20 -0
- package/types/components/draw-utils.d.ts +48 -0
- package/types/components/shared.d.ts +7 -0
- package/types/components/size.d.ts +6 -0
- package/{components/types.ts → types/components/types.d.ts} +3 -12
- package/types/engine/carousel.d.ts +10 -0
- package/types/engine/events.d.ts +14 -0
- package/types/engine/hit-test.d.ts +31 -0
- package/types/engine/images.d.ts +8 -0
- package/types/engine/interaction.d.ts +11 -0
- package/types/engine/layout.d.ts +6 -0
- package/types/engine/nodes.d.ts +18 -0
- package/{engine/state.ts → types/engine/state.d.ts} +27 -22
- package/types/engine/text-controls.d.ts +10 -0
- package/types/engine/transform.d.ts +13 -0
- package/types/engine/videos.d.ts +14 -0
- package/types/engine/vnodes.d.ts +36 -0
- package/types/engine.d.ts +36 -0
- package/types/image-assets.d.ts +2 -0
- package/types/input-bridge.d.ts +40 -0
- package/types/react/Canvas.d.ts +4 -0
- package/types/react/components.d.ts +30 -0
- package/types/react/index.d.ts +4 -0
- package/types/renderer.d.ts +20 -0
- package/types/scene.d.ts +15 -0
- package/{types.ts → types/types.d.ts} +10 -142
- package/types/video-assets.d.ts +11 -0
- package/Canvas.tsx +0 -142
- package/__tests__/pure-logic.test.ts +0 -153
- package/__tests__/scene-and-node.test.ts +0 -131
- package/__tests__/scratch-space.test.ts +0 -30
- package/__tests__/textarea-caret.test.ts +0 -96
- package/__tests__/utils.test.ts +0 -114
- package/canvas-handle.ts +0 -114
- package/components/Button/button.ts +0 -57
- package/components/Button/logic.ts +0 -11
- package/components/CanvasView/canvasView.ts +0 -103
- package/components/CanvasView/index.ts +0 -7
- package/components/Carousel/carousel.ts +0 -107
- package/components/Carousel/logic.ts +0 -36
- package/components/Checkbox/checkbox.ts +0 -14
- package/components/ChoiceGroup/choiceGroup.ts +0 -172
- package/components/ChoiceGroup/logic.ts +0 -14
- package/components/Circle/circle.ts +0 -31
- package/components/Collapse/collapse.ts +0 -69
- package/components/Collapse/logic.ts +0 -14
- package/components/CustomDraw/customDraw.ts +0 -50
- package/components/CustomDraw/index.ts +0 -7
- package/components/DatePicker/datePicker.ts +0 -70
- package/components/Dialog/dialog.ts +0 -102
- package/components/Dropdown/dropdown.ts +0 -125
- package/components/Dropdown/logic.ts +0 -29
- package/components/Icon/icon.ts +0 -669
- package/components/Image/image.ts +0 -69
- package/components/Input/input.ts +0 -139
- package/components/Progress/progress.ts +0 -59
- package/components/Radio/radio.ts +0 -14
- package/components/Rect/rect.ts +0 -35
- package/components/Slider/logic.ts +0 -19
- package/components/Slider/slider.ts +0 -58
- package/components/Switch/logic.ts +0 -8
- package/components/Switch/switch.ts +0 -37
- package/components/Tabs/logic.ts +0 -14
- package/components/Tabs/tabs.ts +0 -78
- package/components/Text/text.ts +0 -95
- package/components/Textarea/textarea.ts +0 -16
- package/components/Upload/upload.ts +0 -66
- package/components/draw-utils.ts +0 -230
- package/components/shared.ts +0 -15
- package/components/size.ts +0 -20
- package/components.ts +0 -98
- package/engine/__tests__/carousel.test.ts +0 -218
- package/engine/__tests__/hit-test.test.ts +0 -282
- package/engine/__tests__/layout.test.ts +0 -474
- package/engine/carousel.ts +0 -150
- package/engine/events.ts +0 -73
- package/engine/hit-test.ts +0 -321
- package/engine/images.ts +0 -41
- package/engine/interaction.ts +0 -442
- package/engine/layout.ts +0 -355
- package/engine/nodes.ts +0 -166
- package/engine/text-controls.ts +0 -122
- package/engine/transform.ts +0 -61
- package/engine/vnodes.ts +0 -204
- package/engine.ts +0 -162
- package/image-assets.ts +0 -37
- package/index.ts +0 -78
- package/input-bridge.ts +0 -285
- package/renderer.ts +0 -303
- package/scene.ts +0 -122
- package/theme.scss +0 -234
- package/web-canvas-ui-react-0.1.1.tgz +0 -0
- /package/{components/Button/index.ts → types/components/Button/index.d.ts} +0 -0
- /package/{components/Carousel/index.ts → types/components/Carousel/index.d.ts} +0 -0
- /package/{components/Checkbox/index.ts → types/components/Checkbox/index.d.ts} +0 -0
- /package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +0 -0
- /package/{components/Circle/index.ts → types/components/Circle/index.d.ts} +0 -0
- /package/{components/Collapse/index.ts → types/components/Collapse/index.d.ts} +0 -0
- /package/{components/DatePicker/index.ts → types/components/DatePicker/index.d.ts} +0 -0
- /package/{components/Dialog/index.ts → types/components/Dialog/index.d.ts} +0 -0
- /package/{components/Dropdown/index.ts → types/components/Dropdown/index.d.ts} +0 -0
- /package/{components/Grid/index.ts → types/components/Grid/index.d.ts} +0 -0
- /package/{components/GridItem/index.ts → types/components/GridItem/index.d.ts} +0 -0
- /package/{components/Group/index.ts → types/components/Group/index.d.ts} +0 -0
- /package/{components/Icon/index.ts → types/components/Icon/index.d.ts} +0 -0
- /package/{components/Image/index.ts → types/components/Image/index.d.ts} +0 -0
- /package/{components/Input/index.ts → types/components/Input/index.d.ts} +0 -0
- /package/{components/Progress/index.ts → types/components/Progress/index.d.ts} +0 -0
- /package/{components/Radio/index.ts → types/components/Radio/index.d.ts} +0 -0
- /package/{components/Rect/index.ts → types/components/Rect/index.d.ts} +0 -0
- /package/{components/Slider/index.ts → types/components/Slider/index.d.ts} +0 -0
- /package/{components/Switch/index.ts → types/components/Switch/index.d.ts} +0 -0
- /package/{components/Tabs/index.ts → types/components/Tabs/index.d.ts} +0 -0
- /package/{components/Text/index.ts → types/components/Text/index.d.ts} +0 -0
- /package/{components/Textarea/index.ts → types/components/Textarea/index.d.ts} +0 -0
- /package/{components/Upload/index.ts → types/components/Upload/index.d.ts} +0 -0
package/input-bridge.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
InputEvents,
|
|
3
|
-
InputLayout,
|
|
4
|
-
TextControlProps,
|
|
5
|
-
WebInputBridge as WebInputBridgeContract,
|
|
6
|
-
} from './types';
|
|
7
|
-
|
|
8
|
-
interface MountedInput {
|
|
9
|
-
element: HTMLInputElement | HTMLTextAreaElement;
|
|
10
|
-
events: InputEvents;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The CSS that makes the transparent element lay its text out where the Canvas drawing puts it: same
|
|
15
|
-
* font metrics, same horizontal inset. Left to the browser default font, a click is converted into a
|
|
16
|
-
* character index for one layout while the caret is painted for another, so the caret lands on the
|
|
17
|
-
* wrong glyph (and typing looks one character behind). Pure so it can be checked without a DOM.
|
|
18
|
-
*/
|
|
19
|
-
export function overlayTextStyle(
|
|
20
|
-
props: TextControlProps,
|
|
21
|
-
multiline: boolean,
|
|
22
|
-
): Record<string, string> {
|
|
23
|
-
// DatePicker declares neither `padding` nor `fontFamily`; its drawing hard-codes 8px / sans-serif.
|
|
24
|
-
const padding = 'padding' in props && typeof props.padding === 'number' ? props.padding : 8;
|
|
25
|
-
const fontSize = typeof props.fontSize === 'number' ? props.fontSize : 14;
|
|
26
|
-
const declaredFont =
|
|
27
|
-
'fontFamily' in props && typeof props.fontFamily === 'string' ? props.fontFamily : '';
|
|
28
|
-
const fontFamily = declaredFont || 'sans-serif';
|
|
29
|
-
return {
|
|
30
|
-
fontFamily,
|
|
31
|
-
fontSize: `${fontSize}px`,
|
|
32
|
-
paddingLeft: `${padding}px`,
|
|
33
|
-
paddingRight: `${padding}px`,
|
|
34
|
-
// A textarea also needs the vertical inset and the 1.35 leading the Canvas line box uses.
|
|
35
|
-
...(multiline
|
|
36
|
-
? {
|
|
37
|
-
paddingTop: `${padding}px`,
|
|
38
|
-
paddingBottom: `${padding}px`,
|
|
39
|
-
lineHeight: '1.35',
|
|
40
|
-
overflowY: 'auto',
|
|
41
|
-
overflowX: 'hidden',
|
|
42
|
-
}
|
|
43
|
-
: {}),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export class WebInputBridge implements WebInputBridgeContract {
|
|
48
|
-
private readonly mounted = new Map<string, MountedInput>();
|
|
49
|
-
private readonly fileInputs = new Set<HTMLInputElement>();
|
|
50
|
-
private readonly originalDisplay: string;
|
|
51
|
-
private overlayHidden = false;
|
|
52
|
-
private selectionTracked = 0;
|
|
53
|
-
private readonly overlay: HTMLElement;
|
|
54
|
-
|
|
55
|
-
constructor(overlay: HTMLElement) {
|
|
56
|
-
this.overlay = overlay;
|
|
57
|
-
this.originalDisplay = overlay.style.display;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
mount(
|
|
61
|
-
id: string,
|
|
62
|
-
props: TextControlProps,
|
|
63
|
-
layout: InputLayout,
|
|
64
|
-
events: InputEvents,
|
|
65
|
-
): () => void {
|
|
66
|
-
this.unmount(id);
|
|
67
|
-
const element = document.createElement(id.startsWith('textarea:') ? 'textarea' : 'input');
|
|
68
|
-
element.setAttribute('aria-label', props.placeholder || 'Canvas input');
|
|
69
|
-
if (element instanceof HTMLInputElement) element.autocomplete = 'off';
|
|
70
|
-
Object.assign(element.style, {
|
|
71
|
-
position: 'absolute',
|
|
72
|
-
left: '0',
|
|
73
|
-
top: '0',
|
|
74
|
-
margin: '0',
|
|
75
|
-
padding: '0',
|
|
76
|
-
border: '0',
|
|
77
|
-
outline: '0',
|
|
78
|
-
opacity: '0',
|
|
79
|
-
color: 'transparent',
|
|
80
|
-
caretColor: 'transparent',
|
|
81
|
-
background: 'transparent',
|
|
82
|
-
boxSizing: 'border-box',
|
|
83
|
-
transformOrigin: '0 0',
|
|
84
|
-
pointerEvents: 'auto',
|
|
85
|
-
});
|
|
86
|
-
element.addEventListener('input', this.handleInput);
|
|
87
|
-
element.addEventListener('select', this.handleSelection);
|
|
88
|
-
element.addEventListener('focus', this.handleFocus);
|
|
89
|
-
element.addEventListener('blur', this.handleBlur);
|
|
90
|
-
if (element instanceof HTMLTextAreaElement) {
|
|
91
|
-
element.addEventListener('scroll', this.handleTextareaScroll);
|
|
92
|
-
}
|
|
93
|
-
this.trackSelections();
|
|
94
|
-
this.mounted.set(id, { element, events });
|
|
95
|
-
this.overlay.appendChild(element);
|
|
96
|
-
this.update(id, props, layout);
|
|
97
|
-
return () => this.unmount(id);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
update(id: string, props: TextControlProps, layout: InputLayout): void {
|
|
101
|
-
const mounted = this.mounted.get(id);
|
|
102
|
-
if (!mounted) return;
|
|
103
|
-
const { element } = mounted;
|
|
104
|
-
const value = props.value ?? props.defaultValue ?? '';
|
|
105
|
-
if (element instanceof HTMLInputElement) {
|
|
106
|
-
element.type = id.startsWith('datePicker:')
|
|
107
|
-
? 'date'
|
|
108
|
-
: 'type' in props
|
|
109
|
-
? (props.type ?? 'text')
|
|
110
|
-
: 'text';
|
|
111
|
-
}
|
|
112
|
-
element.placeholder = props.placeholder ?? '';
|
|
113
|
-
element.disabled = props.disabled ?? false;
|
|
114
|
-
const maxLength = 'maxLength' in props ? props.maxLength : undefined;
|
|
115
|
-
if (typeof maxLength === 'number' && Number.isInteger(maxLength) && maxLength >= 0) {
|
|
116
|
-
element.maxLength = maxLength;
|
|
117
|
-
} else {
|
|
118
|
-
element.removeAttribute('maxLength');
|
|
119
|
-
}
|
|
120
|
-
if (element.value !== value) element.value = value;
|
|
121
|
-
const multiline = element instanceof HTMLTextAreaElement;
|
|
122
|
-
Object.assign(element.style, {
|
|
123
|
-
display: layout.visible ? 'block' : 'none',
|
|
124
|
-
width: `${layout.width}px`,
|
|
125
|
-
height: `${layout.height}px`,
|
|
126
|
-
transform: `matrix(${layout.transform.join(',')})`,
|
|
127
|
-
resize: 'none',
|
|
128
|
-
whiteSpace: multiline ? 'pre-wrap' : 'normal',
|
|
129
|
-
...overlayTextStyle(props, multiline),
|
|
130
|
-
});
|
|
131
|
-
if (multiline) {
|
|
132
|
-
element.wrap = 'soft';
|
|
133
|
-
mounted.events.scroll(element.scrollTop, element.scrollHeight, element.clientHeight);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
pickFiles(
|
|
138
|
-
accept: string | undefined,
|
|
139
|
-
multiple: boolean,
|
|
140
|
-
onChange: (files: File[]) => void,
|
|
141
|
-
): void {
|
|
142
|
-
const input = document.createElement('input');
|
|
143
|
-
input.type = 'file';
|
|
144
|
-
input.accept = accept ?? '';
|
|
145
|
-
input.multiple = multiple;
|
|
146
|
-
Object.assign(input.style, {
|
|
147
|
-
position: 'fixed',
|
|
148
|
-
width: '1px',
|
|
149
|
-
height: '1px',
|
|
150
|
-
opacity: '0',
|
|
151
|
-
pointerEvents: 'none',
|
|
152
|
-
});
|
|
153
|
-
const cleanup = () => {
|
|
154
|
-
input.removeEventListener('change', handleChange);
|
|
155
|
-
input.removeEventListener('cancel', cleanup);
|
|
156
|
-
input.remove();
|
|
157
|
-
this.fileInputs.delete(input);
|
|
158
|
-
};
|
|
159
|
-
const handleChange = () => {
|
|
160
|
-
const files = Array.from(input.files ?? []);
|
|
161
|
-
cleanup();
|
|
162
|
-
if (files.length) onChange(files);
|
|
163
|
-
};
|
|
164
|
-
input.addEventListener('change', handleChange, { once: true });
|
|
165
|
-
input.addEventListener('cancel', cleanup, { once: true });
|
|
166
|
-
this.fileInputs.add(input);
|
|
167
|
-
this.overlay.ownerDocument.body.appendChild(input);
|
|
168
|
-
input.click();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
focus(id: string): void {
|
|
172
|
-
this.mounted.get(id)?.element.focus();
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
setOverlayVisible(visible: boolean): void {
|
|
176
|
-
if (visible) {
|
|
177
|
-
if (!this.overlayHidden) return;
|
|
178
|
-
this.overlay.style.display = this.originalDisplay;
|
|
179
|
-
this.overlayHidden = false;
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
if (this.overlayHidden) return;
|
|
183
|
-
|
|
184
|
-
const activeElement = this.overlay.ownerDocument.activeElement;
|
|
185
|
-
if (activeElement instanceof HTMLElement && this.overlay.contains(activeElement)) {
|
|
186
|
-
activeElement.blur();
|
|
187
|
-
}
|
|
188
|
-
this.overlay.style.display = 'none';
|
|
189
|
-
this.overlayHidden = true;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
private unmount(id: string): void {
|
|
193
|
-
const mounted = this.mounted.get(id);
|
|
194
|
-
if (!mounted) return;
|
|
195
|
-
const { element } = mounted;
|
|
196
|
-
element.removeEventListener('input', this.handleInput);
|
|
197
|
-
element.removeEventListener('select', this.handleSelection);
|
|
198
|
-
element.removeEventListener('focus', this.handleFocus);
|
|
199
|
-
element.removeEventListener('blur', this.handleBlur);
|
|
200
|
-
if (element instanceof HTMLTextAreaElement) {
|
|
201
|
-
element.removeEventListener('scroll', this.handleTextareaScroll);
|
|
202
|
-
}
|
|
203
|
-
this.untrackSelections();
|
|
204
|
-
element.remove();
|
|
205
|
-
this.mounted.delete(id);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
destroy(): void {
|
|
209
|
-
this.setOverlayVisible(true);
|
|
210
|
-
for (const id of this.mounted.keys()) this.unmount(id);
|
|
211
|
-
this.fileInputs.forEach((input) => input.remove());
|
|
212
|
-
this.fileInputs.clear();
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
private find(target: EventTarget | null): MountedInput | undefined {
|
|
216
|
-
for (const mounted of this.mounted.values()) {
|
|
217
|
-
if (mounted.element === target) return mounted;
|
|
218
|
-
}
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
private readonly handleInput = (event: Event) => {
|
|
223
|
-
const mounted = this.find(event.currentTarget);
|
|
224
|
-
if (mounted) {
|
|
225
|
-
mounted.events.input(
|
|
226
|
-
mounted.element.value,
|
|
227
|
-
mounted.element.selectionStart ?? mounted.element.value.length,
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
private readonly handleSelection = (event: Event) => {
|
|
233
|
-
const mounted = this.find(event.currentTarget);
|
|
234
|
-
if (!mounted) return;
|
|
235
|
-
mounted.events.selection(mounted.element.selectionStart ?? mounted.element.value.length);
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* `select` only fires when a range is created, so the interactions that just move the caret —
|
|
240
|
-
* clicking into the text, arrow keys, Home/End — report nothing, and the drawn caret stays pinned
|
|
241
|
-
* to wherever the last edit left it. `selectionchange` is the event that actually tracks the
|
|
242
|
-
* caret; it is document-scoped, hence one shared listener refcounted over the mounted controls
|
|
243
|
-
* (re-adding the same callback would be a no-op, and unmounting one must not stop the others).
|
|
244
|
-
*/
|
|
245
|
-
private trackSelections(): void {
|
|
246
|
-
if (this.selectionTracked++ === 0) {
|
|
247
|
-
this.overlay.ownerDocument.addEventListener(
|
|
248
|
-
'selectionchange',
|
|
249
|
-
this.handleSelectionChange,
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
private untrackSelections(): void {
|
|
255
|
-
if (--this.selectionTracked === 0) {
|
|
256
|
-
this.overlay.ownerDocument.removeEventListener(
|
|
257
|
-
'selectionchange',
|
|
258
|
-
this.handleSelectionChange,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
private readonly handleSelectionChange = () => {
|
|
264
|
-
const active = this.overlay.ownerDocument.activeElement;
|
|
265
|
-
if (!(active instanceof HTMLInputElement) && !(active instanceof HTMLTextAreaElement)) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
const mounted = this.find(active);
|
|
269
|
-
if (!mounted) return;
|
|
270
|
-
mounted.events.selection(mounted.element.selectionStart ?? mounted.element.value.length);
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
private readonly handleFocus = (event: Event) => this.find(event.currentTarget)?.events.focus();
|
|
274
|
-
private readonly handleBlur = (event: Event) => this.find(event.currentTarget)?.events.blur();
|
|
275
|
-
private readonly handleTextareaScroll = (event: Event) => {
|
|
276
|
-
const mounted = this.find(event.currentTarget);
|
|
277
|
-
if (mounted?.element instanceof HTMLTextAreaElement) {
|
|
278
|
-
mounted.events.scroll(
|
|
279
|
-
mounted.element.scrollTop,
|
|
280
|
-
mounted.element.scrollHeight,
|
|
281
|
-
mounted.element.clientHeight,
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
}
|
package/renderer.ts
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import { drawButton } from './components/Button';
|
|
2
|
-
import { drawDialog } from './components/Dialog';
|
|
3
|
-
import { drawCircle } from './components/Circle';
|
|
4
|
-
import { drawCustomDraw } from './components/CustomDraw';
|
|
5
|
-
import { drawCanvasView } from './components/CanvasView';
|
|
6
|
-
import { drawCollapseItem } from './components/Collapse';
|
|
7
|
-
import { drawDropdown, drawDropdownOptions } from './components/Dropdown';
|
|
8
|
-
import { drawImage } from './components/Image';
|
|
9
|
-
import { drawProgress } from './components/Progress';
|
|
10
|
-
import { drawInput } from './components/Input';
|
|
11
|
-
import { drawIcon } from './components/Icon';
|
|
12
|
-
import { drawDatePicker } from './components/DatePicker';
|
|
13
|
-
import { drawRadio } from './components/Radio';
|
|
14
|
-
import { drawCheckbox } from './components/Checkbox';
|
|
15
|
-
import { drawUpload } from './components/Upload';
|
|
16
|
-
import { drawCarousel, drawCarouselControls } from './components/Carousel';
|
|
17
|
-
import { drawRect } from './components/Rect';
|
|
18
|
-
import { drawSlider } from './components/Slider';
|
|
19
|
-
import { drawSwitch } from './components/Switch';
|
|
20
|
-
import { drawTabs } from './components/Tabs';
|
|
21
|
-
import { drawText } from './components/Text';
|
|
22
|
-
import { drawTextarea } from './components/Textarea';
|
|
23
|
-
import { SceneNode } from './scene';
|
|
24
|
-
import type { ComponentKind } from './types';
|
|
25
|
-
import type { RectProps } from './components/Rect';
|
|
26
|
-
import type { CircleProps } from './components/Circle';
|
|
27
|
-
import type { CustomDrawProps } from './components/CustomDraw';
|
|
28
|
-
import type { CanvasViewProps } from './components/CanvasView';
|
|
29
|
-
import type { TextProps } from './components/Text';
|
|
30
|
-
import type { InputDrawProps } from './components/Input';
|
|
31
|
-
import type { DatePickerDrawProps } from './components/DatePicker';
|
|
32
|
-
import type { RadioProps } from './components/Radio';
|
|
33
|
-
import type { CheckboxProps } from './components/Checkbox';
|
|
34
|
-
import type { UploadProps } from './components/Upload';
|
|
35
|
-
import type { CarouselDrawProps } from './components/Carousel';
|
|
36
|
-
import type { TextareaDrawProps } from './components/Textarea';
|
|
37
|
-
import type { DropdownDrawProps } from './components/Dropdown';
|
|
38
|
-
import type { SliderProps } from './components/Slider';
|
|
39
|
-
import type { SwitchProps } from './components/Switch';
|
|
40
|
-
import type { ButtonDrawProps } from './components/Button';
|
|
41
|
-
import type { ImageProps } from './components/Image';
|
|
42
|
-
import type { IconProps } from './components/Icon';
|
|
43
|
-
import type { ProgressProps } from './components/Progress';
|
|
44
|
-
import type { TabsDrawProps } from './components/Tabs';
|
|
45
|
-
import type { CollapseItemDrawProps } from './components/Collapse';
|
|
46
|
-
|
|
47
|
-
/** Maps every drawable component kind to the exact props type its draw function consumes. */
|
|
48
|
-
interface DrawPropsMap {
|
|
49
|
-
rect: RectProps;
|
|
50
|
-
circle: CircleProps;
|
|
51
|
-
customDraw: CustomDrawProps;
|
|
52
|
-
canvasView: CanvasViewProps;
|
|
53
|
-
text: TextProps;
|
|
54
|
-
input: InputDrawProps;
|
|
55
|
-
datePicker: DatePickerDrawProps;
|
|
56
|
-
radio: RadioProps;
|
|
57
|
-
checkbox: CheckboxProps;
|
|
58
|
-
upload: UploadProps;
|
|
59
|
-
carousel: CarouselDrawProps;
|
|
60
|
-
textarea: TextareaDrawProps;
|
|
61
|
-
dropdown: DropdownDrawProps;
|
|
62
|
-
slider: SliderProps;
|
|
63
|
-
switch: SwitchProps;
|
|
64
|
-
button: ButtonDrawProps;
|
|
65
|
-
image: ImageProps;
|
|
66
|
-
icon: IconProps;
|
|
67
|
-
progress: ProgressProps;
|
|
68
|
-
tabs: TabsDrawProps;
|
|
69
|
-
collapseItem: CollapseItemDrawProps;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
type DrawRegistry = {
|
|
73
|
-
[K in keyof DrawPropsMap]: (context: CanvasRenderingContext2D, props: DrawPropsMap[K]) => void;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
// Every entry is checked against DrawPropsMap, so a kind can never be paired with a draw
|
|
77
|
-
// function that expects a different props shape.
|
|
78
|
-
const drawComponent: DrawRegistry = {
|
|
79
|
-
rect: drawRect,
|
|
80
|
-
circle: drawCircle,
|
|
81
|
-
customDraw: drawCustomDraw,
|
|
82
|
-
canvasView: drawCanvasView,
|
|
83
|
-
text: drawText,
|
|
84
|
-
input: drawInput,
|
|
85
|
-
datePicker: drawDatePicker,
|
|
86
|
-
radio: drawRadio,
|
|
87
|
-
checkbox: drawCheckbox,
|
|
88
|
-
upload: drawUpload,
|
|
89
|
-
carousel: drawCarousel,
|
|
90
|
-
textarea: drawTextarea,
|
|
91
|
-
dropdown: drawDropdown,
|
|
92
|
-
slider: drawSlider,
|
|
93
|
-
switch: drawSwitch,
|
|
94
|
-
button: drawButton,
|
|
95
|
-
image: drawImage,
|
|
96
|
-
icon: drawIcon,
|
|
97
|
-
progress: drawProgress,
|
|
98
|
-
tabs: drawTabs,
|
|
99
|
-
collapseItem: drawCollapseItem,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
type LooseDraw = (context: CanvasRenderingContext2D, props: Record<string, unknown>) => void;
|
|
103
|
-
|
|
104
|
-
// Boundary hop between the scene and the draw layer: scene nodes carry props as
|
|
105
|
-
// `Record<string, unknown>`, while draw functions declare precise props types. `DrawRegistry`
|
|
106
|
-
// above already proves each kind is paired with a function accepting that kind's props, so these
|
|
107
|
-
// two helpers are the only place that intentionally erases that guarantee in order to dispatch by
|
|
108
|
-
// a runtime `node.type`. Keep casts out of individual draw functions and route everything here.
|
|
109
|
-
const looseDrawComponent = drawComponent as unknown as Partial<Record<ComponentKind, LooseDraw>>;
|
|
110
|
-
|
|
111
|
-
function eraseDraw<P>(draw: (context: CanvasRenderingContext2D, props: P) => void): LooseDraw {
|
|
112
|
-
return draw as unknown as LooseDraw;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Single source of truth for the disabled look: every whole-control `disabled` state is rendered
|
|
116
|
-
// the same way here, by fading the node subtree, instead of each component inventing its own graying
|
|
117
|
-
// (which left Switch/Input/Textarea/DatePicker/Dropdown with no disabled feedback at all).
|
|
118
|
-
const DISABLED_OPACITY = 0.5;
|
|
119
|
-
|
|
120
|
-
export class CanvasRenderer {
|
|
121
|
-
render(context: CanvasRenderingContext2D, root: SceneNode): void {
|
|
122
|
-
const canvas = context.canvas;
|
|
123
|
-
context.save();
|
|
124
|
-
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
125
|
-
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
126
|
-
context.restore();
|
|
127
|
-
this.drawNode(context, root);
|
|
128
|
-
this.drawDialogs(context, root);
|
|
129
|
-
this.drawDropdownOverlays(context, root);
|
|
130
|
-
root.traverse((node) => {
|
|
131
|
-
node.dirty = false;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Runs a single node's draw function behind a save/restore pair and a try/catch so that a
|
|
137
|
-
* throwing draw (an `<Icon>` with an unknown name, a `<CustomDraw>`/`<CanvasView>` with invalid
|
|
138
|
-
* props, or a faulty user draw callback) only skips that one node instead of aborting the whole
|
|
139
|
-
* frame. The save/restore also contains any partial context mutation the draw made before it
|
|
140
|
-
* threw, so sibling nodes and later overlays still render from a clean state.
|
|
141
|
-
*/
|
|
142
|
-
private safeDraw(
|
|
143
|
-
context: CanvasRenderingContext2D,
|
|
144
|
-
draw: LooseDraw | undefined,
|
|
145
|
-
node: SceneNode,
|
|
146
|
-
): void {
|
|
147
|
-
if (!draw) return;
|
|
148
|
-
context.save();
|
|
149
|
-
try {
|
|
150
|
-
draw(context, node.props);
|
|
151
|
-
} catch (error) {
|
|
152
|
-
console.error(
|
|
153
|
-
`[canvas-ui] Skipped drawing <${node.type}> after a render error:`,
|
|
154
|
-
error,
|
|
155
|
-
);
|
|
156
|
-
} finally {
|
|
157
|
-
context.restore();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private drawNode(context: CanvasRenderingContext2D, node: SceneNode): void {
|
|
162
|
-
if (node.props.visible === false) return;
|
|
163
|
-
if (node.type === 'dialog') return;
|
|
164
|
-
context.save();
|
|
165
|
-
context.translate(number(node.props.x, 0), number(node.props.y, 0));
|
|
166
|
-
context.rotate(number(node.props.rotation, 0));
|
|
167
|
-
context.scale(number(node.props.scaleX, 1), number(node.props.scaleY, 1));
|
|
168
|
-
if (node.props.disabled === true) context.globalAlpha *= DISABLED_OPACITY;
|
|
169
|
-
|
|
170
|
-
if (node.type === 'gridItem' && typeof node.props.backgroundColor === 'string') {
|
|
171
|
-
drawGridItemBackground(context, node);
|
|
172
|
-
}
|
|
173
|
-
if (node.type !== 'root') this.safeDraw(context, looseDrawComponent[node.type], node);
|
|
174
|
-
if (node.type === 'grid' && typeof node.props.height === 'number') {
|
|
175
|
-
context.save();
|
|
176
|
-
context.beginPath();
|
|
177
|
-
context.rect(
|
|
178
|
-
0,
|
|
179
|
-
0,
|
|
180
|
-
number(node.props._gridWidth, number(node.props.width, 0)),
|
|
181
|
-
number(node.props.height, 0),
|
|
182
|
-
);
|
|
183
|
-
context.clip();
|
|
184
|
-
node.children.forEach((child) => this.drawNode(context, child));
|
|
185
|
-
context.restore();
|
|
186
|
-
drawGridScrollbar(context, node);
|
|
187
|
-
} else if (node.type === 'tabs') {
|
|
188
|
-
const width = number(node.props.width, 300);
|
|
189
|
-
const height = number(node.props.height, 200);
|
|
190
|
-
const headerHeight = number(node.props.headerHeight, 40);
|
|
191
|
-
context.save();
|
|
192
|
-
context.beginPath();
|
|
193
|
-
context.rect(0, headerHeight, width, Math.max(0, height - headerHeight));
|
|
194
|
-
context.clip();
|
|
195
|
-
node.children.forEach((child) => this.drawNode(context, child));
|
|
196
|
-
context.restore();
|
|
197
|
-
} else if (node.type === 'carousel') {
|
|
198
|
-
context.save();
|
|
199
|
-
context.beginPath();
|
|
200
|
-
context.rect(0, 0, number(node.props.width, 300), number(node.props.height, 180));
|
|
201
|
-
context.clip();
|
|
202
|
-
node.children.forEach((child) => this.drawNode(context, child));
|
|
203
|
-
context.restore();
|
|
204
|
-
this.safeDraw(context, eraseDraw(drawCarouselControls), node);
|
|
205
|
-
} else {
|
|
206
|
-
node.children.forEach((child) => this.drawNode(context, child));
|
|
207
|
-
}
|
|
208
|
-
context.restore();
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private drawDialogs(context: CanvasRenderingContext2D, node: SceneNode): void {
|
|
212
|
-
if (node.props.visible === false) return;
|
|
213
|
-
if (node.type === 'dialog') {
|
|
214
|
-
if (!node.props.open) return;
|
|
215
|
-
context.save();
|
|
216
|
-
context.translate(number(node.props.x, 0), number(node.props.y, 0));
|
|
217
|
-
context.rotate(number(node.props.rotation, 0));
|
|
218
|
-
context.scale(number(node.props.scaleX, 1), number(node.props.scaleY, 1));
|
|
219
|
-
if (node.props.disabled === true) context.globalAlpha *= DISABLED_OPACITY;
|
|
220
|
-
this.safeDraw(context, eraseDraw(drawDialog), node);
|
|
221
|
-
const x = number(node.props._dialogPanelX, 0);
|
|
222
|
-
const y = number(node.props._dialogPanelY, 0);
|
|
223
|
-
const width = number(node.props.width, 420);
|
|
224
|
-
const height = number(node.props.height, 240);
|
|
225
|
-
context.save();
|
|
226
|
-
context.beginPath();
|
|
227
|
-
context.rect(x + 12, y + 56, Math.max(0, width - 24), Math.max(0, height - 112));
|
|
228
|
-
context.clip();
|
|
229
|
-
node.children.forEach((child) => this.drawNode(context, child));
|
|
230
|
-
context.restore();
|
|
231
|
-
context.restore();
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
node.children.forEach((child) => this.drawDialogs(context, child));
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
private drawDropdownOverlays(context: CanvasRenderingContext2D, node: SceneNode): void {
|
|
238
|
-
if (node.props.visible === false) return;
|
|
239
|
-
context.save();
|
|
240
|
-
context.translate(number(node.props.x, 0), number(node.props.y, 0));
|
|
241
|
-
context.rotate(number(node.props.rotation, 0));
|
|
242
|
-
context.scale(number(node.props.scaleX, 1), number(node.props.scaleY, 1));
|
|
243
|
-
if (node.props.disabled === true) context.globalAlpha *= DISABLED_OPACITY;
|
|
244
|
-
|
|
245
|
-
if (node.type === 'dropdown' && node.props.open) {
|
|
246
|
-
this.safeDraw(context, eraseDraw(drawDropdownOptions), node);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
(node.type === 'grid' && typeof node.props.height === 'number') ||
|
|
251
|
-
node.type === 'carousel'
|
|
252
|
-
) {
|
|
253
|
-
context.save();
|
|
254
|
-
context.beginPath();
|
|
255
|
-
context.rect(
|
|
256
|
-
0,
|
|
257
|
-
0,
|
|
258
|
-
node.type === 'grid'
|
|
259
|
-
? number(node.props._gridWidth, number(node.props.width, 0))
|
|
260
|
-
: number(node.props.width, 300),
|
|
261
|
-
number(node.props.height, node.type === 'carousel' ? 180 : 0),
|
|
262
|
-
);
|
|
263
|
-
context.clip();
|
|
264
|
-
node.children.forEach((child) => this.drawDropdownOverlays(context, child));
|
|
265
|
-
context.restore();
|
|
266
|
-
} else {
|
|
267
|
-
node.children.forEach((child) => this.drawDropdownOverlays(context, child));
|
|
268
|
-
}
|
|
269
|
-
context.restore();
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function drawGridItemBackground(context: CanvasRenderingContext2D, node: SceneNode): void {
|
|
274
|
-
context.fillStyle = node.props.backgroundColor as string;
|
|
275
|
-
context.fillRect(
|
|
276
|
-
0,
|
|
277
|
-
0,
|
|
278
|
-
number(node.props._gridItemWidth, 0),
|
|
279
|
-
number(node.props._gridItemHeight, 0),
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function drawGridScrollbar(context: CanvasRenderingContext2D, node: SceneNode): void {
|
|
284
|
-
const height = number(node.props.height, 0);
|
|
285
|
-
const contentHeight = number(node.props._gridContentHeight, height);
|
|
286
|
-
if (height <= 0 || contentHeight <= height) return;
|
|
287
|
-
const width = number(node.props._gridWidth, number(node.props.width, 0));
|
|
288
|
-
const trackWidth = 6;
|
|
289
|
-
const trackX = Math.max(0, width - trackWidth);
|
|
290
|
-
const thumbHeight = Math.min(height, Math.max(18, (height * height) / contentHeight));
|
|
291
|
-
const maxScroll = contentHeight - height;
|
|
292
|
-
const thumbY = ((height - thumbHeight) * number(node.props._scrollTop, 0)) / maxScroll;
|
|
293
|
-
context.fillStyle = 'rgba(0, 0, 0, 0.08)';
|
|
294
|
-
context.fillRect(trackX, 0, trackWidth, height);
|
|
295
|
-
context.fillStyle = 'rgba(0, 0, 0, 0.35)';
|
|
296
|
-
context.beginPath();
|
|
297
|
-
context.roundRect(trackX + 1, thumbY, trackWidth - 2, thumbHeight, 3);
|
|
298
|
-
context.fill();
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function number(value: unknown, fallback: number): number {
|
|
302
|
-
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
303
|
-
}
|