@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.
- package/README.md +415 -205
- package/index.js +3660 -0
- package/package.json +36 -27
- package/types/canvas-handle.d.ts +22 -0
- package/types/components/Button/button.d.ts +22 -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 +38 -0
- package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +1 -1
- 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/types/components/Form/form.d.ts +42 -0
- package/types/components/Form/index.d.ts +2 -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/index.d.ts +2 -0
- package/types/components/Upload/upload.d.ts +30 -0
- package/types/components/Video/index.d.ts +4 -0
- package/types/components/Video/logic.d.ts +56 -0
- package/types/components/Video/video.d.ts +54 -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} +5 -13
- package/types/engine/accessibility.d.ts +5 -0
- package/types/engine/carousel.d.ts +10 -0
- package/types/engine/events.d.ts +14 -0
- package/types/engine/forms.d.ts +21 -0
- package/types/engine/hit-test.d.ts +31 -0
- package/types/engine/images.d.ts +8 -0
- package/types/engine/interaction.d.ts +12 -0
- package/types/engine/layout.d.ts +6 -0
- package/types/engine/nodes.d.ts +19 -0
- package/types/engine/state.d.ts +91 -0
- package/types/engine/text-controls.d.ts +10 -0
- package/types/engine/transform.d.ts +13 -0
- package/types/engine/uploads.d.ts +5 -0
- package/types/engine/videos.d.ts +28 -0
- package/types/engine/vnodes.d.ts +36 -0
- package/types/engine.d.ts +38 -0
- package/types/image-assets.d.ts +2 -0
- package/types/input-bridge.d.ts +54 -0
- package/types/react/Canvas.d.ts +4 -0
- package/types/react/components.d.ts +31 -0
- package/types/react/index.d.ts +5 -0
- package/types/renderer.d.ts +20 -0
- package/types/scene.d.ts +15 -0
- package/{types.ts → types/types.d.ts} +48 -142
- package/types/video-assets.d.ts +28 -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/index.ts +0 -2
- 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/state.ts +0 -75
- 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/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
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { fitText, number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { drawIcon, type IconName } from '../Icon';
|
|
4
|
-
import { createComponent } from '../shared';
|
|
5
|
-
|
|
6
|
-
export interface UploadProps extends TransformProps {
|
|
7
|
-
width?: number;
|
|
8
|
-
height?: number;
|
|
9
|
-
text?: string;
|
|
10
|
-
icon?: IconName;
|
|
11
|
-
iconSize?: number;
|
|
12
|
-
accept?: string;
|
|
13
|
-
multiple?: boolean;
|
|
14
|
-
disabled?: boolean;
|
|
15
|
-
backgroundColor?: string;
|
|
16
|
-
textColor?: string;
|
|
17
|
-
borderColor?: string;
|
|
18
|
-
borderRadius?: number;
|
|
19
|
-
fontSize?: number;
|
|
20
|
-
onChange?: (files: File[]) => void;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const Upload = createComponent<UploadProps>('upload');
|
|
24
|
-
|
|
25
|
-
export function drawUpload(context: CanvasRenderingContext2D, props: UploadProps): void {
|
|
26
|
-
const width = number(props.width, 120);
|
|
27
|
-
const height = number(props.height, 40);
|
|
28
|
-
const radius = number(props.borderRadius, 6);
|
|
29
|
-
context.beginPath();
|
|
30
|
-
context.roundRect(0, 0, width, height, radius);
|
|
31
|
-
context.fillStyle = string(
|
|
32
|
-
props.backgroundColor,
|
|
33
|
-
themeColor(context, '--canvas-ui-button-background', '#1677ff'),
|
|
34
|
-
);
|
|
35
|
-
context.fill();
|
|
36
|
-
if (props.borderColor) {
|
|
37
|
-
context.strokeStyle = string(props.borderColor, '#1677ff');
|
|
38
|
-
context.stroke();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const label = string(props.text, 'Choose File');
|
|
42
|
-
const color = string(props.textColor, themeColor(context, '--canvas-ui-button-color', '#fff'));
|
|
43
|
-
const fontSize = number(props.fontSize, 15);
|
|
44
|
-
const iconSize = number(props.iconSize, Math.round(fontSize * 1.1));
|
|
45
|
-
const gap = 6;
|
|
46
|
-
|
|
47
|
-
context.font = `${fontSize}px sans-serif`;
|
|
48
|
-
context.textBaseline = 'middle';
|
|
49
|
-
context.textAlign = 'left';
|
|
50
|
-
const fittedLabel = fitText(context, label, Math.max(0, width - 12 - iconSize - gap));
|
|
51
|
-
// A built-in glyph (defaults to the "upload" arrow; pass `icon` to customise) plus the
|
|
52
|
-
// label, kept as one centred group that never spills outside the button box.
|
|
53
|
-
const textWidth = fittedLabel ? context.measureText(fittedLabel).width : 0;
|
|
54
|
-
const contentWidth = iconSize + (fittedLabel ? gap : 0) + textWidth;
|
|
55
|
-
let cursorX = (width - contentWidth) / 2;
|
|
56
|
-
const centerY = height / 2;
|
|
57
|
-
|
|
58
|
-
context.save();
|
|
59
|
-
context.translate(cursorX, centerY - iconSize / 2);
|
|
60
|
-
drawIcon(context, { name: props.icon ?? 'upload', size: iconSize, color });
|
|
61
|
-
context.restore();
|
|
62
|
-
cursorX += iconSize + (fittedLabel ? gap : 0);
|
|
63
|
-
|
|
64
|
-
context.fillStyle = color;
|
|
65
|
-
context.fillText(fittedLabel, cursorX, centerY);
|
|
66
|
-
}
|
package/components/draw-utils.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
export function number(value: unknown, fallback: number): number {
|
|
2
|
-
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function string(value: unknown, fallback: string): string {
|
|
6
|
-
return typeof value === 'string' ? value : fallback;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/** Measures rendered text width; drawing passes a context, layout passes `measureTextWidth`. */
|
|
10
|
-
export type TextMeasurer = (text: string) => number;
|
|
11
|
-
|
|
12
|
-
/** Baseline-to-baseline multiplier, shared by Text drawing and the pass that sizes it. */
|
|
13
|
-
export const TEXT_LINE_HEIGHT = 1.2;
|
|
14
|
-
|
|
15
|
-
const ELLIPSIS = '…';
|
|
16
|
-
|
|
17
|
-
// CJK ideographs, kana and Hangul syllables: each of them is one em wide, and a run of them may be
|
|
18
|
-
// broken between any two characters - unlike a Latin word, which moves to the next line whole.
|
|
19
|
-
const WIDE_CHARACTER = /[\u2e80-\u9fff\uac00-\ud7af]/u;
|
|
20
|
-
|
|
21
|
-
function byContext(context: CanvasRenderingContext2D): TextMeasurer {
|
|
22
|
-
return (text) => context.measureText(text).width;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function breakIntoLines(measure: TextMeasurer, text: string, width: number): string[] {
|
|
26
|
-
const lines: string[] = [];
|
|
27
|
-
for (const paragraph of text.split('\n')) {
|
|
28
|
-
let line = '';
|
|
29
|
-
for (const word of paragraph.split(/(\s+)/).filter(Boolean)) {
|
|
30
|
-
const candidate = line + word;
|
|
31
|
-
if (line && measure(candidate) <= width) {
|
|
32
|
-
line = candidate;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
if (!line && /^\s+$/.test(word)) continue;
|
|
36
|
-
if (!line && measure(word) <= width) {
|
|
37
|
-
line = word;
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
if (line && !WIDE_CHARACTER.test(word)) {
|
|
41
|
-
// A Latin word that no longer fits is moved down whole; a CJK run instead keeps
|
|
42
|
-
// filling this line, because that is what the browser does to the text of a
|
|
43
|
-
// textarea, and a caret offset is resolved against those very line breaks.
|
|
44
|
-
lines.push(line.trimEnd());
|
|
45
|
-
line = '';
|
|
46
|
-
}
|
|
47
|
-
for (const character of Array.from(word)) {
|
|
48
|
-
if (line && measure(line + character) > width) {
|
|
49
|
-
lines.push(line.trimEnd());
|
|
50
|
-
line = '';
|
|
51
|
-
}
|
|
52
|
-
line += character;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
lines.push(line);
|
|
56
|
-
}
|
|
57
|
-
return lines;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Longest prefix of `text` that still fits `maxWidth` once the ellipsis is appended to it. */
|
|
61
|
-
function ellipsizedTo(measure: TextMeasurer, text: string, maxWidth: number): string {
|
|
62
|
-
const characters = Array.from(text);
|
|
63
|
-
let low = 0;
|
|
64
|
-
let high = characters.length;
|
|
65
|
-
while (low < high) {
|
|
66
|
-
const middle = Math.ceil((low + high) / 2);
|
|
67
|
-
const candidate = characters.slice(0, middle).join('') + ELLIPSIS;
|
|
68
|
-
if (measure(candidate) <= maxWidth) low = middle;
|
|
69
|
-
else high = middle - 1;
|
|
70
|
-
}
|
|
71
|
-
return characters.slice(0, low).join('') + ELLIPSIS;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function truncateTo(measure: TextMeasurer, text: string, maxWidth: number): string {
|
|
75
|
-
if (!text) return text;
|
|
76
|
-
if (measure(text) <= maxWidth) return text;
|
|
77
|
-
return ellipsizedTo(measure, text, maxWidth);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function clampToLines(
|
|
81
|
-
measure: TextMeasurer,
|
|
82
|
-
text: string,
|
|
83
|
-
width: number,
|
|
84
|
-
maxLines: number,
|
|
85
|
-
): string[] {
|
|
86
|
-
const lines = breakIntoLines(measure, text, width);
|
|
87
|
-
if (lines.length <= maxLines) return lines;
|
|
88
|
-
const visible = lines.slice(0, maxLines);
|
|
89
|
-
// Shorten the last visible line rather than dropping it: the ellipsis has to borrow room from
|
|
90
|
-
// the very line it terminates.
|
|
91
|
-
visible[maxLines - 1] = ellipsizedTo(measure, visible[maxLines - 1], width);
|
|
92
|
-
return visible;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Wraps `text` into lines at most `width` wide, keeping at most `maxLines` of them; a clamped
|
|
97
|
-
* block ends with an ellipsis instead of silently losing the rest. `maxLines` <= 0 means "no
|
|
98
|
-
* limit"; any positive limit keeps at least one line. Callers supply the measurer so that drawing
|
|
99
|
-
* (live context) and layout ({@link measureTextWidth}) share one algorithm and therefore one result.
|
|
100
|
-
*/
|
|
101
|
-
export function measureLines(
|
|
102
|
-
measure: TextMeasurer,
|
|
103
|
-
text: string,
|
|
104
|
-
width: number,
|
|
105
|
-
maxLines = 0,
|
|
106
|
-
): string[] {
|
|
107
|
-
if (!(width > 0)) return [text];
|
|
108
|
-
if (!(maxLines > 0)) return breakIntoLines(measure, text, width);
|
|
109
|
-
return clampToLines(measure, text, width, Math.max(1, Math.round(maxLines)));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function wrapText(context: CanvasRenderingContext2D, text: string, width: number): string[] {
|
|
113
|
-
return breakIntoLines(byContext(context), text, width);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Truncates `text` with a trailing ellipsis so it fits within `maxWidth` for the
|
|
118
|
-
* context's current font. Display-only labels (buttons, tabs, dropdown rows…) use
|
|
119
|
-
* this to stay inside their box; editable controls clip instead.
|
|
120
|
-
*/
|
|
121
|
-
export function fitText(context: CanvasRenderingContext2D, text: string, maxWidth: number): string {
|
|
122
|
-
return truncateTo(byContext(context), text, maxWidth);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
let cachedMeasureContext: CanvasRenderingContext2D | null = null;
|
|
126
|
-
let measureContextResolved = false;
|
|
127
|
-
// Assigning `font` re-resolves the font list, which is the expensive half of a measurement. Runs of
|
|
128
|
-
// calls almost always share one font, so the assignment is skipped while the string is unchanged.
|
|
129
|
-
let appliedMeasureFont = '';
|
|
130
|
-
|
|
131
|
-
function getMeasureContext(): CanvasRenderingContext2D | null {
|
|
132
|
-
if (!measureContextResolved) {
|
|
133
|
-
measureContextResolved = true;
|
|
134
|
-
if (typeof document !== 'undefined') {
|
|
135
|
-
cachedMeasureContext = document.createElement('canvas').getContext('2d');
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return cachedMeasureContext;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Measures the rendered width of `text` for a given CSS `font` string using a
|
|
143
|
-
* shared offscreen context. Draw, hit-test and layout code must all go through
|
|
144
|
-
* this helper so their width calculations stay byte-for-byte identical.
|
|
145
|
-
* Falls back to a deterministic estimate when no canvas is available (SSR/tests).
|
|
146
|
-
*/
|
|
147
|
-
export function measureTextWidth(text: string, font: string): number {
|
|
148
|
-
const context = getMeasureContext();
|
|
149
|
-
if (!context) return estimateTextWidth(text, font);
|
|
150
|
-
if (appliedMeasureFont !== font) {
|
|
151
|
-
context.font = font;
|
|
152
|
-
appliedMeasureFont = font;
|
|
153
|
-
}
|
|
154
|
-
return context.measureText(text).width;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function estimateTextWidth(text: string, font: string): number {
|
|
158
|
-
const fontSize = parseFloat(font) || 14;
|
|
159
|
-
return Array.from(text).reduce((total, character) => {
|
|
160
|
-
const isWide = WIDE_CHARACTER.test(character);
|
|
161
|
-
return total + fontSize * (isWide ? 1 : 0.62);
|
|
162
|
-
}, 0);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
let cachedScrollbarWidth: number | null = null;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Width a classic vertical scrollbar occupies, 0 on platforms that float scrollbars over the
|
|
169
|
-
* content. Measured once from a throwaway scroller and cached, the same way the measurement context
|
|
170
|
-
* is; without a document (SSR, unit tests) there is nothing to reserve.
|
|
171
|
-
*/
|
|
172
|
-
export function verticalScrollbarWidth(): number {
|
|
173
|
-
if (cachedScrollbarWidth !== null) return cachedScrollbarWidth;
|
|
174
|
-
cachedScrollbarWidth = 0;
|
|
175
|
-
const body = typeof document === 'undefined' ? null : document.body;
|
|
176
|
-
if (!body) return cachedScrollbarWidth;
|
|
177
|
-
const host = document.createElement('div');
|
|
178
|
-
host.style.position = 'absolute';
|
|
179
|
-
host.style.top = '-9999px';
|
|
180
|
-
host.style.width = '100px';
|
|
181
|
-
host.style.height = '100px';
|
|
182
|
-
host.style.overflowY = 'scroll';
|
|
183
|
-
const inner = document.createElement('div');
|
|
184
|
-
inner.style.width = '10px';
|
|
185
|
-
inner.style.height = '10px';
|
|
186
|
-
host.appendChild(inner);
|
|
187
|
-
body.appendChild(host);
|
|
188
|
-
cachedScrollbarWidth = Math.max(0, host.offsetWidth - host.clientWidth);
|
|
189
|
-
host.remove();
|
|
190
|
-
return cachedScrollbarWidth;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
interface CachedThemeColor {
|
|
194
|
-
generation: number;
|
|
195
|
-
value: string;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Reads of CSS custom properties go through getComputedStyle, which is expensive enough that
|
|
199
|
-
// calling it for every colour of every node on every frame measurably slows rendering. Results are
|
|
200
|
-
// memoised per canvas element (a WeakMap so a destroyed canvas never leaks) and tagged with a
|
|
201
|
-
// global generation counter; bumping the generation invalidates every entry at once.
|
|
202
|
-
let themeGeneration = 0;
|
|
203
|
-
const themeColorCache = new WeakMap<HTMLCanvasElement, Map<string, CachedThemeColor>>();
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Drops every cached theme colour so the next read recomputes it. Call this whenever the theme can
|
|
207
|
-
* change (for example from a MutationObserver watching the canvas / ancestor `class` or `style`).
|
|
208
|
-
*/
|
|
209
|
-
export function invalidateThemeColors(): void {
|
|
210
|
-
themeGeneration += 1;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function themeColor(
|
|
214
|
-
context: CanvasRenderingContext2D,
|
|
215
|
-
token: string,
|
|
216
|
-
fallback: string,
|
|
217
|
-
): string {
|
|
218
|
-
if (typeof getComputedStyle !== 'function') return fallback;
|
|
219
|
-
const canvas = context.canvas;
|
|
220
|
-
let byToken = themeColorCache.get(canvas);
|
|
221
|
-
if (!byToken) {
|
|
222
|
-
byToken = new Map();
|
|
223
|
-
themeColorCache.set(canvas, byToken);
|
|
224
|
-
}
|
|
225
|
-
const cached = byToken.get(token);
|
|
226
|
-
if (cached && cached.generation === themeGeneration) return cached.value;
|
|
227
|
-
const value = getComputedStyle(canvas).getPropertyValue(token).trim() || fallback;
|
|
228
|
-
byToken.set(token, { generation: themeGeneration, value });
|
|
229
|
-
return value;
|
|
230
|
-
}
|
package/components/shared.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ComponentKind } from '../types';
|
|
2
|
-
|
|
3
|
-
export const kindSymbol = Symbol.for('rnative55.web-canvas-ui.kind');
|
|
4
|
-
|
|
5
|
-
export type MarkedComponent<Props = Record<string, unknown>> = ((props: Props) => null) & {
|
|
6
|
-
displayName?: string;
|
|
7
|
-
[kindSymbol]?: ComponentKind;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export function createComponent<Props>(kind: ComponentKind): MarkedComponent<Props> {
|
|
11
|
-
const component = (() => null) as MarkedComponent<Props>;
|
|
12
|
-
component.displayName = kind[0].toUpperCase() + kind.slice(1);
|
|
13
|
-
component[kindSymbol] = kind;
|
|
14
|
-
return component;
|
|
15
|
-
}
|
package/components/size.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { number } from './draw-utils';
|
|
2
|
-
|
|
3
|
-
export interface ComponentSize {
|
|
4
|
-
width?: number;
|
|
5
|
-
height?: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function componentWidth(props: Record<string, unknown>, fallback: number): number {
|
|
9
|
-
return number(sizeObject(props)?.width, number(props.width, fallback));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function componentHeight(props: Record<string, unknown>, fallback: number): number {
|
|
13
|
-
return number(sizeObject(props)?.height, number(props.height, fallback));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function sizeObject(props: Record<string, unknown>): ComponentSize | undefined {
|
|
17
|
-
const size = props.size;
|
|
18
|
-
if (typeof size !== 'object' || size === null || Array.isArray(size)) return undefined;
|
|
19
|
-
return size as ComponentSize;
|
|
20
|
-
}
|
package/components.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { kindSymbol, type MarkedComponent } from './components/shared.ts';
|
|
3
|
-
import type { VNode } from './types.ts';
|
|
4
|
-
|
|
5
|
-
export type * from './components/types.ts';
|
|
6
|
-
|
|
7
|
-
export { Group } from './components/Group';
|
|
8
|
-
export { Grid } from './components/Grid';
|
|
9
|
-
export { GridItem } from './components/GridItem';
|
|
10
|
-
export { Rect } from './components/Rect';
|
|
11
|
-
export { Circle } from './components/Circle';
|
|
12
|
-
export { Text } from './components/Text';
|
|
13
|
-
export { Input } from './components/Input';
|
|
14
|
-
export { Textarea } from './components/Textarea';
|
|
15
|
-
export { Dropdown } from './components/Dropdown';
|
|
16
|
-
export { Slider } from './components/Slider';
|
|
17
|
-
export { Switch } from './components/Switch';
|
|
18
|
-
export { DatePicker } from './components/DatePicker';
|
|
19
|
-
export { Radio } from './components/Radio';
|
|
20
|
-
export { Checkbox } from './components/Checkbox';
|
|
21
|
-
export { Upload } from './components/Upload';
|
|
22
|
-
export { Carousel, CarouselItem } from './components/Carousel';
|
|
23
|
-
export type { ChoiceOption } from './components/ChoiceGroup';
|
|
24
|
-
export { Button } from './components/Button';
|
|
25
|
-
export { Image } from './components/Image';
|
|
26
|
-
export { Icon } from './components/Icon';
|
|
27
|
-
export { Dialog } from './components/Dialog';
|
|
28
|
-
export { Progress } from './components/Progress';
|
|
29
|
-
export { Tabs, TabItem } from './components/Tabs';
|
|
30
|
-
export { Collapse, CollapseItem } from './components/Collapse';
|
|
31
|
-
export { CustomDraw } from './components/CustomDraw';
|
|
32
|
-
export { CanvasView } from './components/CanvasView';
|
|
33
|
-
|
|
34
|
-
function compileChild(child: unknown, path: string): VNode[] {
|
|
35
|
-
if (child === null || child === undefined || typeof child === 'boolean') return [];
|
|
36
|
-
if (Array.isArray(child)) {
|
|
37
|
-
return child.flatMap((item, index) => compileChild(item, `${path}.${index}`));
|
|
38
|
-
}
|
|
39
|
-
if (typeof child === 'string' || typeof child === 'number') return [];
|
|
40
|
-
if (!React.isValidElement(child)) {
|
|
41
|
-
throw new Error(`Unsupported child inside <Canvas> at ${path}. Use Canvas UI components.`);
|
|
42
|
-
}
|
|
43
|
-
if (child.type === React.Fragment) {
|
|
44
|
-
return compileChild((child.props as { children?: React.ReactNode }).children, path);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const kind = (child.type as MarkedComponent)[kindSymbol];
|
|
48
|
-
if (!kind) {
|
|
49
|
-
throw new Error(
|
|
50
|
-
`Unsupported component inside <Canvas> at ${path}. Use Canvas UI components.`,
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const props = { ...(child.props as Record<string, unknown>) };
|
|
55
|
-
const children = props.children as React.ReactNode;
|
|
56
|
-
delete props.children;
|
|
57
|
-
const key = child.key ?? (typeof props.key === 'string' ? props.key : null);
|
|
58
|
-
delete props.key;
|
|
59
|
-
|
|
60
|
-
if (kind === 'text') {
|
|
61
|
-
if (props.text === undefined) {
|
|
62
|
-
props.text =
|
|
63
|
-
typeof children === 'string' || typeof children === 'number'
|
|
64
|
-
? String(children)
|
|
65
|
-
: '';
|
|
66
|
-
}
|
|
67
|
-
return [{ type: kind, key, props, children: [] }];
|
|
68
|
-
}
|
|
69
|
-
if (kind === 'button' && props.text === undefined) {
|
|
70
|
-
props.text =
|
|
71
|
-
typeof children === 'string' || typeof children === 'number' ? String(children) : '';
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return [
|
|
75
|
-
{
|
|
76
|
-
type: kind,
|
|
77
|
-
key,
|
|
78
|
-
props,
|
|
79
|
-
children:
|
|
80
|
-
kind === 'group' ||
|
|
81
|
-
kind === 'grid' ||
|
|
82
|
-
kind === 'gridItem' ||
|
|
83
|
-
kind === 'dialog' ||
|
|
84
|
-
kind === 'tabs' ||
|
|
85
|
-
kind === 'tabItem' ||
|
|
86
|
-
kind === 'collapse' ||
|
|
87
|
-
kind === 'collapseItem' ||
|
|
88
|
-
kind === 'carousel' ||
|
|
89
|
-
kind === 'carouselItem'
|
|
90
|
-
? compileChild(children, path)
|
|
91
|
-
: [],
|
|
92
|
-
},
|
|
93
|
-
];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function compileChildren(children: unknown): VNode[] {
|
|
97
|
-
return compileChild(children, 'canvas');
|
|
98
|
-
}
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { SceneNode } from '../../scene';
|
|
3
|
-
import { advanceCarousels, changeCarousel, transitionProgress } from '../carousel';
|
|
4
|
-
import { EngineState } from '../state';
|
|
5
|
-
import type { CarouselTransition, EngineCore } from '../state';
|
|
6
|
-
|
|
7
|
-
// --- fixtures ---------------------------------------------------------------
|
|
8
|
-
|
|
9
|
-
function makeCore(scene: SceneNode): EngineCore {
|
|
10
|
-
// The carousel helpers only touch `scene` (traverse); text-control visibility
|
|
11
|
-
// is exercised with an empty overlay map, so the bridge is never called.
|
|
12
|
-
return { scene, inputBridge: {} } as unknown as EngineCore;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function carouselFixture(
|
|
16
|
-
carouselProps: Record<string, unknown> = {},
|
|
17
|
-
itemCount = 2,
|
|
18
|
-
): { scene: SceneNode; carousel: SceneNode; items: SceneNode[] } {
|
|
19
|
-
const scene = new SceneNode('root');
|
|
20
|
-
const carousel = new SceneNode('carousel', { _stateKey: 'c1', ...carouselProps });
|
|
21
|
-
carousel.parent = scene;
|
|
22
|
-
scene.children.push(carousel);
|
|
23
|
-
const items: SceneNode[] = [];
|
|
24
|
-
for (let index = 0; index < itemCount; index++) {
|
|
25
|
-
const item = new SceneNode('carouselItem', {});
|
|
26
|
-
item.parent = carousel;
|
|
27
|
-
carousel.children.push(item);
|
|
28
|
-
items.push(item);
|
|
29
|
-
}
|
|
30
|
-
return { scene, carousel, items };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const transition = (over: Partial<CarouselTransition> = {}): CarouselTransition => ({
|
|
34
|
-
from: 0,
|
|
35
|
-
to: 1,
|
|
36
|
-
direction: 1,
|
|
37
|
-
startedAt: 0,
|
|
38
|
-
duration: 100,
|
|
39
|
-
...over,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// --- transitionProgress -----------------------------------------------------
|
|
43
|
-
|
|
44
|
-
describe('transitionProgress', () => {
|
|
45
|
-
it('reports 1 immediately for a zero-duration transition', () => {
|
|
46
|
-
expect(transitionProgress(transition({ duration: 0 }), 999)).toBe(1);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('clamps elapsed fraction into [0, 1]', () => {
|
|
50
|
-
const t = transition({ startedAt: 100, duration: 200 });
|
|
51
|
-
expect(transitionProgress(t, 100)).toBe(0);
|
|
52
|
-
expect(transitionProgress(t, 200)).toBe(0.5);
|
|
53
|
-
expect(transitionProgress(t, 300)).toBe(1);
|
|
54
|
-
expect(transitionProgress(t, 50)).toBe(0); // before start → 0
|
|
55
|
-
expect(transitionProgress(t, 500)).toBe(1); // after end → 1
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// --- changeCarousel ---------------------------------------------------------
|
|
60
|
-
|
|
61
|
-
describe('changeCarousel', () => {
|
|
62
|
-
it('starts an eased transition and records the pending state', () => {
|
|
63
|
-
const state = new EngineState();
|
|
64
|
-
state.currentFrameTime = 1000;
|
|
65
|
-
const seen: number[] = [];
|
|
66
|
-
const { core, carousel } = (() => {
|
|
67
|
-
const f = carouselFixture({
|
|
68
|
-
width: 300,
|
|
69
|
-
_activeIndex: 0,
|
|
70
|
-
onChange: (i: number) => seen.push(i),
|
|
71
|
-
});
|
|
72
|
-
return { core: makeCore(f.scene), carousel: f.carousel };
|
|
73
|
-
})();
|
|
74
|
-
|
|
75
|
-
changeCarousel(core, state, carousel, 1);
|
|
76
|
-
|
|
77
|
-
expect(carousel.props._activeIndex).toBe(1);
|
|
78
|
-
expect(carousel.props._transitionProgress).toBe(0);
|
|
79
|
-
expect(state.carouselTransitions.has('c1')).toBe(true);
|
|
80
|
-
expect(state.carouselFrameTimes.get('c1')).toBe(1000);
|
|
81
|
-
expect(state.carouselSelections.get('c1')).toBe(1);
|
|
82
|
-
expect(seen).toEqual([1]);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('applies an instant change when the transition duration is 0', () => {
|
|
86
|
-
const state = new EngineState();
|
|
87
|
-
const seen: number[] = [];
|
|
88
|
-
const { scene, carousel, items } = carouselFixture({
|
|
89
|
-
transitionDuration: 0,
|
|
90
|
-
_activeIndex: 0,
|
|
91
|
-
onChange: (i: number) => seen.push(i),
|
|
92
|
-
});
|
|
93
|
-
carousel.setProps({ visible: true });
|
|
94
|
-
|
|
95
|
-
changeCarousel(makeCore(scene), state, carousel, 1);
|
|
96
|
-
|
|
97
|
-
expect(carousel.props._activeIndex).toBe(1);
|
|
98
|
-
expect(state.carouselTransitions.has('c1')).toBe(false);
|
|
99
|
-
expect(items[0].props.visible).toBe(false);
|
|
100
|
-
expect(items[1].props.visible).toBe(true);
|
|
101
|
-
expect(seen).toEqual([1]);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('is a no-op when the resolved index equals the current one', () => {
|
|
105
|
-
const state = new EngineState();
|
|
106
|
-
const seen: number[] = [];
|
|
107
|
-
const { scene, carousel } = carouselFixture({
|
|
108
|
-
_activeIndex: 1,
|
|
109
|
-
onChange: (i: number) => seen.push(i),
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
changeCarousel(makeCore(scene), state, carousel, 1);
|
|
113
|
-
|
|
114
|
-
expect(seen).toEqual([]);
|
|
115
|
-
expect(state.carouselTransitions.has('c1')).toBe(false);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('is a no-op while a transition for the node is already running', () => {
|
|
119
|
-
const state = new EngineState();
|
|
120
|
-
state.carouselTransitions.set('c1', transition());
|
|
121
|
-
const seen: number[] = [];
|
|
122
|
-
const { scene, carousel } = carouselFixture({
|
|
123
|
-
_activeIndex: 0,
|
|
124
|
-
onChange: (i: number) => seen.push(i),
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
changeCarousel(makeCore(scene), state, carousel, 1);
|
|
128
|
-
|
|
129
|
-
expect(carousel.props._activeIndex).toBe(0);
|
|
130
|
-
expect(seen).toEqual([]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('is a no-op for an empty carousel', () => {
|
|
134
|
-
const state = new EngineState();
|
|
135
|
-
const seen: number[] = [];
|
|
136
|
-
const { scene, carousel } = carouselFixture({ onChange: (i: number) => seen.push(i) }, 0);
|
|
137
|
-
|
|
138
|
-
changeCarousel(makeCore(scene), state, carousel, 1);
|
|
139
|
-
|
|
140
|
-
expect(seen).toEqual([]);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// --- advanceCarousels -------------------------------------------------------
|
|
145
|
-
|
|
146
|
-
describe('advanceCarousels', () => {
|
|
147
|
-
it('seeds the frame clock then auto-advances past the interval', () => {
|
|
148
|
-
const state = new EngineState();
|
|
149
|
-
const { scene, carousel } = carouselFixture({ interval: 100, _activeIndex: 0 });
|
|
150
|
-
const core = makeCore(scene);
|
|
151
|
-
|
|
152
|
-
advanceCarousels(core, state, 0);
|
|
153
|
-
expect(state.carouselFrameTimes.get('c1')).toBe(0);
|
|
154
|
-
expect(carousel.props._activeIndex).toBe(0);
|
|
155
|
-
|
|
156
|
-
// interval is floored at 250ms, so 300ms elapsed triggers a change.
|
|
157
|
-
advanceCarousels(core, state, 300);
|
|
158
|
-
expect(state.currentFrameTime).toBe(300);
|
|
159
|
-
expect(carousel.props._activeIndex).toBe(1);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('does not advance before the interval elapses', () => {
|
|
163
|
-
const state = new EngineState();
|
|
164
|
-
const { scene, carousel } = carouselFixture({ interval: 1000, _activeIndex: 0 });
|
|
165
|
-
const core = makeCore(scene);
|
|
166
|
-
advanceCarousels(core, state, 0);
|
|
167
|
-
advanceCarousels(core, state, 500);
|
|
168
|
-
expect(carousel.props._activeIndex).toBe(0);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('completes an in-flight transition and clears it', () => {
|
|
172
|
-
const state = new EngineState();
|
|
173
|
-
state.carouselTransitions.set(
|
|
174
|
-
'c1',
|
|
175
|
-
transition({ from: 0, to: 1, startedAt: 0, duration: 100 }),
|
|
176
|
-
);
|
|
177
|
-
const { scene, carousel } = carouselFixture({ _activeIndex: 1 });
|
|
178
|
-
carousel.setProps({ _transitionProgress: 0.4 });
|
|
179
|
-
|
|
180
|
-
advanceCarousels(makeCore(scene), state, 200);
|
|
181
|
-
|
|
182
|
-
expect(state.carouselTransitions.has('c1')).toBe(false);
|
|
183
|
-
expect(carousel.props._transitionProgress).toBe(1);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it('progresses a transition mid-flight without finishing it', () => {
|
|
187
|
-
const state = new EngineState();
|
|
188
|
-
state.carouselTransitions.set('c1', transition({ startedAt: 0, duration: 200 }));
|
|
189
|
-
const { scene, carousel } = carouselFixture();
|
|
190
|
-
|
|
191
|
-
advanceCarousels(makeCore(scene), state, 100);
|
|
192
|
-
|
|
193
|
-
expect(state.carouselTransitions.has('c1')).toBe(true);
|
|
194
|
-
expect(carousel.props._transitionProgress).toBeCloseTo(0.5);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('stops the clock for non-autoplaying carousels', () => {
|
|
198
|
-
const state = new EngineState();
|
|
199
|
-
state.carouselFrameTimes.set('c1', 0);
|
|
200
|
-
const { scene } = carouselFixture({ autoplay: false });
|
|
201
|
-
|
|
202
|
-
advanceCarousels(makeCore(scene), state, 5000);
|
|
203
|
-
|
|
204
|
-
expect(state.carouselFrameTimes.has('c1')).toBe(false);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('prunes timing bookkeeping for carousels that left the tree', () => {
|
|
208
|
-
const state = new EngineState();
|
|
209
|
-
state.carouselFrameTimes.set('gone', 1);
|
|
210
|
-
state.carouselTransitions.set('gone', transition());
|
|
211
|
-
const { scene } = carouselFixture({ interval: 100 });
|
|
212
|
-
|
|
213
|
-
advanceCarousels(makeCore(scene), state, 0);
|
|
214
|
-
|
|
215
|
-
expect(state.carouselFrameTimes.has('gone')).toBe(false);
|
|
216
|
-
expect(state.carouselTransitions.has('gone')).toBe(false);
|
|
217
|
-
});
|
|
218
|
-
});
|