@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,69 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { createComponent } from '../shared';
|
|
3
|
-
import { getCanvasImage } from '../../image-assets';
|
|
4
|
-
import { number } from '../draw-utils';
|
|
5
|
-
|
|
6
|
-
export interface ImageProps extends TransformProps {
|
|
7
|
-
src: string;
|
|
8
|
-
width: number;
|
|
9
|
-
height: number;
|
|
10
|
-
objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
|
|
11
|
-
onError?: (error: Error) => void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const Image = createComponent<ImageProps>('image');
|
|
15
|
-
|
|
16
|
-
export function drawImage(context: CanvasRenderingContext2D, props: ImageProps): void {
|
|
17
|
-
const src = props.src;
|
|
18
|
-
if (typeof src !== 'string' || !src) return;
|
|
19
|
-
const image = getCanvasImage(src);
|
|
20
|
-
if (!image) return;
|
|
21
|
-
const width = number(props.width, image.naturalWidth);
|
|
22
|
-
const height = number(props.height, image.naturalHeight);
|
|
23
|
-
const fit = props.objectFit ?? 'contain';
|
|
24
|
-
context.save();
|
|
25
|
-
context.beginPath();
|
|
26
|
-
context.rect(0, 0, width, height);
|
|
27
|
-
context.clip();
|
|
28
|
-
if (fit === 'fill') {
|
|
29
|
-
context.drawImage(image, 0, 0, width, height);
|
|
30
|
-
context.restore();
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const scale =
|
|
35
|
-
fit === 'cover'
|
|
36
|
-
? Math.max(width / image.naturalWidth, height / image.naturalHeight)
|
|
37
|
-
: fit === 'none'
|
|
38
|
-
? 1
|
|
39
|
-
: fit === 'scale-down'
|
|
40
|
-
? Math.min(1, width / image.naturalWidth, height / image.naturalHeight)
|
|
41
|
-
: Math.min(width / image.naturalWidth, height / image.naturalHeight);
|
|
42
|
-
const drawWidth = image.naturalWidth * scale;
|
|
43
|
-
const drawHeight = image.naturalHeight * scale;
|
|
44
|
-
if (fit === 'cover') {
|
|
45
|
-
const sourceWidth = width / scale;
|
|
46
|
-
const sourceHeight = height / scale;
|
|
47
|
-
context.drawImage(
|
|
48
|
-
image,
|
|
49
|
-
(image.naturalWidth - sourceWidth) / 2,
|
|
50
|
-
(image.naturalHeight - sourceHeight) / 2,
|
|
51
|
-
sourceWidth,
|
|
52
|
-
sourceHeight,
|
|
53
|
-
0,
|
|
54
|
-
0,
|
|
55
|
-
width,
|
|
56
|
-
height,
|
|
57
|
-
);
|
|
58
|
-
context.restore();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
context.drawImage(
|
|
62
|
-
image,
|
|
63
|
-
(width - drawWidth) / 2,
|
|
64
|
-
(height - drawHeight) / 2,
|
|
65
|
-
drawWidth,
|
|
66
|
-
drawHeight,
|
|
67
|
-
);
|
|
68
|
-
context.restore();
|
|
69
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { number, string, themeColor, verticalScrollbarWidth, wrapText } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface InputProps extends TransformProps {
|
|
6
|
-
width?: number;
|
|
7
|
-
height?: number;
|
|
8
|
-
value?: string;
|
|
9
|
-
defaultValue?: string;
|
|
10
|
-
placeholder?: string;
|
|
11
|
-
fontSize?: number;
|
|
12
|
-
fontFamily?: string;
|
|
13
|
-
color?: string;
|
|
14
|
-
bgColor?: string;
|
|
15
|
-
borderColor?: string;
|
|
16
|
-
focusBorderColor?: string;
|
|
17
|
-
borderRadius?: number;
|
|
18
|
-
padding?: number;
|
|
19
|
-
disabled?: boolean;
|
|
20
|
-
maxLength?: number;
|
|
21
|
-
type?: 'text' | 'password' | 'number';
|
|
22
|
-
onChange?: (value: string) => void;
|
|
23
|
-
onFocus?: () => void;
|
|
24
|
-
onBlur?: () => void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const Input = createComponent<InputProps>('input');
|
|
28
|
-
|
|
29
|
-
/** Public props plus the engine-injected caret/scroll state consumed by the draw layer. */
|
|
30
|
-
export type InputDrawProps = InputProps & {
|
|
31
|
-
focused?: boolean;
|
|
32
|
-
cursorPos?: number;
|
|
33
|
-
_scrollTop?: number;
|
|
34
|
-
_scrollHeight?: number;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export function drawInput(
|
|
38
|
-
context: CanvasRenderingContext2D,
|
|
39
|
-
props: InputDrawProps,
|
|
40
|
-
multiline = false,
|
|
41
|
-
): void {
|
|
42
|
-
const width = number(props.width, 200);
|
|
43
|
-
const height = number(props.height, 36);
|
|
44
|
-
const radius = Math.max(0, Math.min(number(props.borderRadius, 4), width / 2, height / 2));
|
|
45
|
-
const padding = number(props.padding, 8);
|
|
46
|
-
const value = String(props.value ?? '');
|
|
47
|
-
const placeholder = String(props.placeholder ?? '');
|
|
48
|
-
const text = props.type === 'password' ? '•'.repeat(value.length) : value;
|
|
49
|
-
|
|
50
|
-
context.beginPath();
|
|
51
|
-
context.roundRect(0, 0, width, height, radius);
|
|
52
|
-
context.fillStyle = string(
|
|
53
|
-
props.bgColor,
|
|
54
|
-
themeColor(context, '--canvas-ui-input-background', '#fff'),
|
|
55
|
-
);
|
|
56
|
-
context.fill();
|
|
57
|
-
context.strokeStyle = props.focused
|
|
58
|
-
? string(
|
|
59
|
-
props.focusBorderColor,
|
|
60
|
-
themeColor(context, '--canvas-ui-input-focus-border', '#1677ff'),
|
|
61
|
-
)
|
|
62
|
-
: string(props.borderColor, themeColor(context, '--canvas-ui-input-border', '#d9d9d9'));
|
|
63
|
-
context.lineWidth = 1;
|
|
64
|
-
context.stroke();
|
|
65
|
-
|
|
66
|
-
context.save();
|
|
67
|
-
context.beginPath();
|
|
68
|
-
context.rect(0, 0, width, height);
|
|
69
|
-
context.clip();
|
|
70
|
-
const fontSize = number(props.fontSize, 14);
|
|
71
|
-
context.font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
|
|
72
|
-
context.textBaseline = multiline ? 'top' : 'middle';
|
|
73
|
-
context.fillStyle = value
|
|
74
|
-
? string(props.color, themeColor(context, '--canvas-ui-input-color', '#333'))
|
|
75
|
-
: themeColor(context, '--canvas-ui-input-placeholder-color', '#999');
|
|
76
|
-
const lineHeight = fontSize * 1.35;
|
|
77
|
-
// The text being drawn is really the transparent DOM element's text, and that element wraps at its
|
|
78
|
-
// own content width: as soon as the value overflows, its vertical scrollbar takes room away from
|
|
79
|
-
// the text. Wrapping a few pixels wider here breaks the same paragraph at other words, and the
|
|
80
|
-
// caret - resolved from a character index the element reported - then lands on a line the reader
|
|
81
|
-
// never clicked. Reserve the gutter the element reserves to keep the two wrappings identical.
|
|
82
|
-
const gutter =
|
|
83
|
-
multiline && number(props._scrollHeight, 0) > height ? verticalScrollbarWidth() : 0;
|
|
84
|
-
const contentWidth = Math.max(1, width - padding * 2 - gutter);
|
|
85
|
-
const lines = multiline ? wrapText(context, text || placeholder, contentWidth) : [];
|
|
86
|
-
const contentHeight = multiline
|
|
87
|
-
? Math.max(number(props._scrollHeight, 0), lines.length * lineHeight + padding * 2)
|
|
88
|
-
: 0;
|
|
89
|
-
const scrollTop = multiline
|
|
90
|
-
? Math.max(0, Math.min(contentHeight - height, number(props._scrollTop, 0)))
|
|
91
|
-
: 0;
|
|
92
|
-
if (multiline) {
|
|
93
|
-
lines.forEach((line, index) => {
|
|
94
|
-
context.fillText(line, padding, padding + index * lineHeight - scrollTop);
|
|
95
|
-
});
|
|
96
|
-
if (contentHeight > height)
|
|
97
|
-
drawTextareaScrollbar(context, width, height, contentHeight, scrollTop);
|
|
98
|
-
} else {
|
|
99
|
-
context.fillText(text || placeholder, padding, height / 2);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (props.focused && !props.disabled) {
|
|
103
|
-
const cursorPosition = number(props.cursorPos, value.length);
|
|
104
|
-
const cursorLines = multiline
|
|
105
|
-
? wrapText(context, text.slice(0, cursorPosition), contentWidth)
|
|
106
|
-
: text.slice(0, cursorPosition).split('\n');
|
|
107
|
-
const cursorWidth = context.measureText(cursorLines[cursorLines.length - 1] ?? '').width;
|
|
108
|
-
const cursorY = multiline
|
|
109
|
-
? padding + (cursorLines.length - 1) * lineHeight - scrollTop
|
|
110
|
-
: (height - fontSize) / 2;
|
|
111
|
-
context.fillStyle = string(
|
|
112
|
-
props.color,
|
|
113
|
-
themeColor(context, '--canvas-ui-input-color', '#333'),
|
|
114
|
-
);
|
|
115
|
-
if (!multiline || (cursorY + fontSize >= 0 && cursorY <= height)) {
|
|
116
|
-
context.fillRect(padding + cursorWidth, cursorY, 1, fontSize);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
context.restore();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function drawTextareaScrollbar(
|
|
123
|
-
context: CanvasRenderingContext2D,
|
|
124
|
-
width: number,
|
|
125
|
-
height: number,
|
|
126
|
-
contentHeight: number,
|
|
127
|
-
scrollTop: number,
|
|
128
|
-
): void {
|
|
129
|
-
const trackWidth = 5;
|
|
130
|
-
const thumbHeight = Math.min(height, Math.max(16, (height * height) / contentHeight));
|
|
131
|
-
const maxScroll = contentHeight - height;
|
|
132
|
-
const thumbY = maxScroll > 0 ? ((height - thumbHeight) * scrollTop) / maxScroll : 0;
|
|
133
|
-
context.fillStyle = 'rgba(0, 0, 0, 0.08)';
|
|
134
|
-
context.fillRect(width - trackWidth, 0, trackWidth, height);
|
|
135
|
-
context.fillStyle = 'rgba(0, 0, 0, 0.35)';
|
|
136
|
-
context.beginPath();
|
|
137
|
-
context.roundRect(width - trackWidth + 1, thumbY, trackWidth - 2, thumbHeight, 2);
|
|
138
|
-
context.fill();
|
|
139
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface ProgressProps extends TransformProps {
|
|
6
|
-
value: number;
|
|
7
|
-
min?: number;
|
|
8
|
-
max?: number;
|
|
9
|
-
width?: number;
|
|
10
|
-
height?: number;
|
|
11
|
-
color?: string;
|
|
12
|
-
trackColor?: string;
|
|
13
|
-
textColor?: string;
|
|
14
|
-
borderRadius?: number;
|
|
15
|
-
showText?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const Progress = createComponent<ProgressProps>('progress');
|
|
19
|
-
|
|
20
|
-
export function drawProgress(context: CanvasRenderingContext2D, props: ProgressProps): void {
|
|
21
|
-
const width = number(props.width, 200);
|
|
22
|
-
const height = number(props.height, 12);
|
|
23
|
-
const min = number(props.min, 0);
|
|
24
|
-
const max = number(props.max, 100);
|
|
25
|
-
const value = Math.max(min, Math.min(max, number(props.value, min)));
|
|
26
|
-
const ratio = max > min ? (value - min) / (max - min) : 0;
|
|
27
|
-
const radius = Math.max(
|
|
28
|
-
0,
|
|
29
|
-
Math.min(number(props.borderRadius, height / 2), height / 2, width / 2),
|
|
30
|
-
);
|
|
31
|
-
context.beginPath();
|
|
32
|
-
context.roundRect(0, 0, width, height, radius);
|
|
33
|
-
context.fillStyle = string(
|
|
34
|
-
props.trackColor,
|
|
35
|
-
themeColor(context, '--canvas-ui-progress-track', '#f0f0f0'),
|
|
36
|
-
);
|
|
37
|
-
context.fill();
|
|
38
|
-
if (ratio > 0) {
|
|
39
|
-
context.save();
|
|
40
|
-
context.beginPath();
|
|
41
|
-
context.roundRect(0, 0, width, height, radius);
|
|
42
|
-
context.clip();
|
|
43
|
-
context.fillStyle = string(
|
|
44
|
-
props.color,
|
|
45
|
-
themeColor(context, '--canvas-ui-progress-active', '#1677ff'),
|
|
46
|
-
);
|
|
47
|
-
context.fillRect(0, 0, width * ratio, height);
|
|
48
|
-
context.restore();
|
|
49
|
-
}
|
|
50
|
-
if (props.showText) {
|
|
51
|
-
context.font = `${Math.max(10, height)}px sans-serif`;
|
|
52
|
-
context.textBaseline = 'middle';
|
|
53
|
-
context.fillStyle = string(
|
|
54
|
-
props.textColor,
|
|
55
|
-
themeColor(context, '--canvas-ui-progress-text', '#333'),
|
|
56
|
-
);
|
|
57
|
-
context.fillText(`${Math.round(ratio * 100)}%`, width + 8, height / 2);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createComponent } from '../shared';
|
|
2
|
-
import { drawChoiceGroup, type ChoiceGroupProps } from '../ChoiceGroup';
|
|
3
|
-
|
|
4
|
-
export interface RadioProps extends ChoiceGroupProps {
|
|
5
|
-
value?: string;
|
|
6
|
-
defaultValue?: string;
|
|
7
|
-
onChange?: (value: string) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const Radio = createComponent<RadioProps>('radio');
|
|
11
|
-
|
|
12
|
-
export function drawRadio(context: CanvasRenderingContext2D, props: RadioProps): void {
|
|
13
|
-
drawChoiceGroup(context, props, 'radio');
|
|
14
|
-
}
|
package/components/Rect/rect.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface RectProps extends TransformProps {
|
|
6
|
-
width?: number;
|
|
7
|
-
height?: number;
|
|
8
|
-
color?: string;
|
|
9
|
-
radius?: number;
|
|
10
|
-
stroke?: string;
|
|
11
|
-
strokeWidth?: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const Rect = createComponent<RectProps>('rect');
|
|
15
|
-
|
|
16
|
-
export function drawRect(context: CanvasRenderingContext2D, props: RectProps): void {
|
|
17
|
-
const width = number(props.width, 100);
|
|
18
|
-
const height = number(props.height, 40);
|
|
19
|
-
const radius = Math.max(0, Math.min(number(props.radius, 0), width / 2, height / 2));
|
|
20
|
-
context.beginPath();
|
|
21
|
-
context.roundRect(0, 0, width, height, radius);
|
|
22
|
-
context.fillStyle = string(
|
|
23
|
-
props.color,
|
|
24
|
-
themeColor(context, '--canvas-ui-rect-background', 'transparent'),
|
|
25
|
-
);
|
|
26
|
-
context.fill();
|
|
27
|
-
if (props.stroke) {
|
|
28
|
-
context.strokeStyle = string(
|
|
29
|
-
props.stroke,
|
|
30
|
-
themeColor(context, '--canvas-ui-rect-border', '#000'),
|
|
31
|
-
);
|
|
32
|
-
context.lineWidth = number(props.strokeWidth, 1);
|
|
33
|
-
context.stroke();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { number } from '../draw-utils';
|
|
2
|
-
|
|
3
|
-
export function sliderValueAt(
|
|
4
|
-
props: Record<string, unknown>,
|
|
5
|
-
width: number,
|
|
6
|
-
localX: number,
|
|
7
|
-
): number | undefined {
|
|
8
|
-
const thumbRadius = Math.min(8, number(props.height, 32) / 3);
|
|
9
|
-
const min = number(props.min, 0);
|
|
10
|
-
const max = number(props.max, 100);
|
|
11
|
-
if (max <= min) return undefined;
|
|
12
|
-
const step = Math.max(Number.EPSILON, number(props.step, 1));
|
|
13
|
-
const ratio = Math.max(
|
|
14
|
-
0,
|
|
15
|
-
Math.min(1, (localX - thumbRadius) / Math.max(1, width - thumbRadius * 2)),
|
|
16
|
-
);
|
|
17
|
-
const rawValue = min + ratio * (max - min);
|
|
18
|
-
return Math.max(min, Math.min(max, min + Math.round((rawValue - min) / step) * step));
|
|
19
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface SliderProps extends TransformProps {
|
|
6
|
-
width?: number;
|
|
7
|
-
height?: number;
|
|
8
|
-
min?: number;
|
|
9
|
-
max?: number;
|
|
10
|
-
step?: number;
|
|
11
|
-
value?: number;
|
|
12
|
-
defaultValue?: number;
|
|
13
|
-
disabled?: boolean;
|
|
14
|
-
trackColor?: string;
|
|
15
|
-
activeColor?: string;
|
|
16
|
-
thumbColor?: string;
|
|
17
|
-
onChange?: (value: number) => void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const Slider = createComponent<SliderProps>('slider');
|
|
21
|
-
|
|
22
|
-
export function drawSlider(context: CanvasRenderingContext2D, props: SliderProps): void {
|
|
23
|
-
const width = number(props.width, 160);
|
|
24
|
-
const height = number(props.height, 32);
|
|
25
|
-
const min = number(props.min, 0);
|
|
26
|
-
const max = number(props.max, 100);
|
|
27
|
-
const value = Math.max(min, Math.min(max, number(props.value, min)));
|
|
28
|
-
const ratio = max === min ? 0 : (value - min) / (max - min);
|
|
29
|
-
const centerY = height / 2;
|
|
30
|
-
const radius = Math.min(8, height / 3);
|
|
31
|
-
const thumbX = radius + ratio * Math.max(0, width - radius * 2);
|
|
32
|
-
context.beginPath();
|
|
33
|
-
context.roundRect(radius, centerY - 3, Math.max(0, width - radius * 2), 6, 3);
|
|
34
|
-
context.fillStyle = string(
|
|
35
|
-
props.trackColor,
|
|
36
|
-
themeColor(context, '--canvas-ui-slider-track', '#f0f0f0'),
|
|
37
|
-
);
|
|
38
|
-
context.fill();
|
|
39
|
-
context.beginPath();
|
|
40
|
-
context.roundRect(radius, centerY - 3, Math.max(0, thumbX - radius), 6, 3);
|
|
41
|
-
context.fillStyle = string(
|
|
42
|
-
props.activeColor,
|
|
43
|
-
themeColor(context, '--canvas-ui-slider-active', '#1677ff'),
|
|
44
|
-
);
|
|
45
|
-
context.fill();
|
|
46
|
-
context.beginPath();
|
|
47
|
-
context.arc(thumbX, centerY, radius, 0, Math.PI * 2);
|
|
48
|
-
context.fillStyle = string(
|
|
49
|
-
props.thumbColor,
|
|
50
|
-
themeColor(context, '--canvas-ui-slider-thumb', '#fff'),
|
|
51
|
-
);
|
|
52
|
-
context.fill();
|
|
53
|
-
context.strokeStyle = string(
|
|
54
|
-
props.activeColor,
|
|
55
|
-
themeColor(context, '--canvas-ui-slider-active', '#1677ff'),
|
|
56
|
-
);
|
|
57
|
-
context.stroke();
|
|
58
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { SceneNode } from '../../scene';
|
|
2
|
-
|
|
3
|
-
export function toggleSwitch(node: SceneNode): void {
|
|
4
|
-
const checked = !node.props.checked;
|
|
5
|
-
node.setProps({ checked });
|
|
6
|
-
const onChange = node.props.onChange;
|
|
7
|
-
if (typeof onChange === 'function') onChange(checked);
|
|
8
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface SwitchProps extends TransformProps {
|
|
6
|
-
checked?: boolean;
|
|
7
|
-
defaultChecked?: boolean;
|
|
8
|
-
disabled?: boolean;
|
|
9
|
-
width?: number;
|
|
10
|
-
height?: number;
|
|
11
|
-
onColor?: string;
|
|
12
|
-
offColor?: string;
|
|
13
|
-
thumbColor?: string;
|
|
14
|
-
onChange?: (checked: boolean) => void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const Switch = createComponent<SwitchProps>('switch');
|
|
18
|
-
|
|
19
|
-
export function drawSwitch(context: CanvasRenderingContext2D, props: SwitchProps): void {
|
|
20
|
-
const width = number(props.width, 44);
|
|
21
|
-
const height = number(props.height, 24);
|
|
22
|
-
const radius = height / 2;
|
|
23
|
-
const checked = Boolean(props.checked);
|
|
24
|
-
context.beginPath();
|
|
25
|
-
context.roundRect(0, 0, width, height, radius);
|
|
26
|
-
context.fillStyle = checked
|
|
27
|
-
? string(props.onColor, themeColor(context, '--canvas-ui-switch-on', '#1677ff'))
|
|
28
|
-
: string(props.offColor, themeColor(context, '--canvas-ui-switch-off', '#d9d9d9'));
|
|
29
|
-
context.fill();
|
|
30
|
-
context.beginPath();
|
|
31
|
-
context.arc(checked ? width - radius : radius, radius, Math.max(1, radius - 3), 0, Math.PI * 2);
|
|
32
|
-
context.fillStyle = string(
|
|
33
|
-
props.thumbColor,
|
|
34
|
-
themeColor(context, '--canvas-ui-switch-thumb', '#fff'),
|
|
35
|
-
);
|
|
36
|
-
context.fill();
|
|
37
|
-
}
|
package/components/Tabs/logic.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { SceneNode } from '../../scene';
|
|
2
|
-
|
|
3
|
-
export function tabKeyAt(
|
|
4
|
-
items: readonly SceneNode[],
|
|
5
|
-
x: number,
|
|
6
|
-
y: number,
|
|
7
|
-
width: number,
|
|
8
|
-
headerHeight: number,
|
|
9
|
-
): string | undefined {
|
|
10
|
-
if (y >= headerHeight || items.length === 0) return undefined;
|
|
11
|
-
const item = items[Math.floor(x / (width / items.length))];
|
|
12
|
-
if (!item || item.props.disabled || typeof item.props.id !== 'string') return undefined;
|
|
13
|
-
return item.props.id;
|
|
14
|
-
}
|
package/components/Tabs/tabs.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { fitText, number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
import type { ComponentSize } from '../size';
|
|
5
|
-
|
|
6
|
-
export interface TabsProps extends TransformProps {
|
|
7
|
-
width: number;
|
|
8
|
-
height: number;
|
|
9
|
-
activeKey?: string;
|
|
10
|
-
defaultActiveKey?: string;
|
|
11
|
-
headerHeight?: number;
|
|
12
|
-
backgroundColor?: string;
|
|
13
|
-
textColor?: string;
|
|
14
|
-
activeColor?: string;
|
|
15
|
-
indicatorColor?: string;
|
|
16
|
-
children?: unknown;
|
|
17
|
-
onChange?: (key: string) => void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface TabItemProps extends TransformProps {
|
|
21
|
-
id: string;
|
|
22
|
-
title: string;
|
|
23
|
-
disabled?: boolean;
|
|
24
|
-
size?: ComponentSize;
|
|
25
|
-
children?: unknown;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const Tabs = createComponent<TabsProps>('tabs');
|
|
29
|
-
export const TabItem = createComponent<TabItemProps>('tabItem');
|
|
30
|
-
|
|
31
|
-
/** A tab header entry injected by the engine for the draw layer. */
|
|
32
|
-
export interface TabItemView {
|
|
33
|
-
title: string;
|
|
34
|
-
disabled?: boolean;
|
|
35
|
-
selected?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Public props plus the engine-injected header model consumed by the draw layer. */
|
|
39
|
-
export type TabsDrawProps = TabsProps & { _tabItems?: TabItemView[] };
|
|
40
|
-
|
|
41
|
-
export function drawTabs(context: CanvasRenderingContext2D, props: TabsDrawProps): void {
|
|
42
|
-
const width = number(props.width, 300);
|
|
43
|
-
const headerHeight = number(props.headerHeight, 40);
|
|
44
|
-
const items = props._tabItems ?? [];
|
|
45
|
-
if (!items.length) return;
|
|
46
|
-
const itemWidth = width / items.length;
|
|
47
|
-
context.fillStyle = string(
|
|
48
|
-
props.backgroundColor,
|
|
49
|
-
themeColor(context, '--canvas-ui-tabs-background', '#fff'),
|
|
50
|
-
);
|
|
51
|
-
context.fillRect(0, 0, width, headerHeight);
|
|
52
|
-
context.font = '14px sans-serif';
|
|
53
|
-
context.textAlign = 'center';
|
|
54
|
-
context.textBaseline = 'middle';
|
|
55
|
-
items.forEach((item, index) => {
|
|
56
|
-
context.fillStyle = item.disabled
|
|
57
|
-
? themeColor(context, '--canvas-ui-tabs-disabled-color', '#bfbfbf')
|
|
58
|
-
: item.selected
|
|
59
|
-
? string(
|
|
60
|
-
props.activeColor,
|
|
61
|
-
themeColor(context, '--canvas-ui-tabs-active-color', '#1677ff'),
|
|
62
|
-
)
|
|
63
|
-
: string(props.textColor, themeColor(context, '--canvas-ui-tabs-color', '#595959'));
|
|
64
|
-
context.fillText(
|
|
65
|
-
fitText(context, item.title, Math.max(0, itemWidth - 16)),
|
|
66
|
-
itemWidth * (index + 0.5),
|
|
67
|
-
headerHeight / 2,
|
|
68
|
-
);
|
|
69
|
-
if (item.selected) {
|
|
70
|
-
context.fillStyle = string(
|
|
71
|
-
props.indicatorColor,
|
|
72
|
-
themeColor(context, '--canvas-ui-tabs-indicator', '#1677ff'),
|
|
73
|
-
);
|
|
74
|
-
context.fillRect(itemWidth * index, headerHeight - 2, itemWidth, 2);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
context.textAlign = 'left';
|
|
78
|
-
}
|
package/components/Text/text.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import {
|
|
3
|
-
TEXT_LINE_HEIGHT,
|
|
4
|
-
measureLines,
|
|
5
|
-
measureTextWidth,
|
|
6
|
-
number,
|
|
7
|
-
string,
|
|
8
|
-
themeColor,
|
|
9
|
-
type TextMeasurer,
|
|
10
|
-
} from '../draw-utils';
|
|
11
|
-
import { createComponent } from '../shared';
|
|
12
|
-
|
|
13
|
-
export interface TextProps extends TransformProps {
|
|
14
|
-
text?: string;
|
|
15
|
-
children?: string | number;
|
|
16
|
-
fontSize?: number;
|
|
17
|
-
/** Wrap width. Falls back to the box the parent container hands down when unset. */
|
|
18
|
-
width?: number;
|
|
19
|
-
/**
|
|
20
|
-
* Maximum number of wrapped lines; the last one is cut with an ellipsis. Falls back to the number
|
|
21
|
-
* of lines that fit the container's box, and to "no limit" when neither is declared.
|
|
22
|
-
*/
|
|
23
|
-
maxLines?: number;
|
|
24
|
-
fontFamily?: string;
|
|
25
|
-
color?: string;
|
|
26
|
-
baseline?: CanvasTextBaseline;
|
|
27
|
-
textAlign?: CanvasTextAlign;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface TextBox {
|
|
31
|
-
lines: string[];
|
|
32
|
-
width: number;
|
|
33
|
-
height: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Public props plus the content box a parent container hands down to this node. */
|
|
37
|
-
export type TextDrawProps = TextProps & {
|
|
38
|
-
_availableWidth?: number;
|
|
39
|
-
_availableHeight?: number;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const DEFAULT_FONT_SIZE = 16;
|
|
43
|
-
|
|
44
|
-
export const Text = createComponent<TextProps>('text');
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Whole lines that fit `height`, floored so a half-visible last line is never painted. Returns 0 when
|
|
48
|
-
* no height is known, which {@link measureLines} reads as "no limit".
|
|
49
|
-
*/
|
|
50
|
-
function lineBudget(height: number, fontSize: number): number {
|
|
51
|
-
if (!(height > 0)) return 0;
|
|
52
|
-
return Math.max(1, Math.floor(height / (fontSize * TEXT_LINE_HEIGHT)));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Resolves `props` into the lines it renders and the box they occupy. {@link drawText} paints these
|
|
57
|
-
* lines and the layout pass sizes the node from the same result, so a Text node's reported height can
|
|
58
|
-
* never disagree with the glyphs it actually draws.
|
|
59
|
-
*
|
|
60
|
-
* A box handed down by the container is the fallback, not the override: declaring `width` or
|
|
61
|
-
* `maxLines` keeps the author in charge, so a node can stay narrower than its cell on purpose.
|
|
62
|
-
*/
|
|
63
|
-
export function measureTextBox(props: TextDrawProps): TextBox {
|
|
64
|
-
const fontSize = number(props.fontSize, DEFAULT_FONT_SIZE);
|
|
65
|
-
const font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
|
|
66
|
-
const measure: TextMeasurer = (text: string) => measureTextWidth(text, font);
|
|
67
|
-
const text = String(props.text ?? '');
|
|
68
|
-
const width = number(props.width, number(props._availableWidth, 0));
|
|
69
|
-
const maxLines = number(
|
|
70
|
-
props.maxLines,
|
|
71
|
-
lineBudget(number(props._availableHeight, 0), fontSize),
|
|
72
|
-
);
|
|
73
|
-
const lines = measureLines(measure, text, width, maxLines);
|
|
74
|
-
return {
|
|
75
|
-
lines,
|
|
76
|
-
width: width > 0 ? width : measure(text),
|
|
77
|
-
height: lines.length * fontSize * TEXT_LINE_HEIGHT,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function drawText(context: CanvasRenderingContext2D, props: TextDrawProps): void {
|
|
82
|
-
const fontSize = number(props.fontSize, DEFAULT_FONT_SIZE);
|
|
83
|
-
context.font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
|
|
84
|
-
context.textBaseline = props.baseline ?? 'top';
|
|
85
|
-
const textAlign =
|
|
86
|
-
props.textAlign === 'center' || props.textAlign === 'right' ? props.textAlign : 'left';
|
|
87
|
-
context.textAlign = textAlign;
|
|
88
|
-
context.fillStyle = string(props.color, themeColor(context, '--canvas-ui-text-color', '#333'));
|
|
89
|
-
const box = measureTextBox(props);
|
|
90
|
-
const anchorX = textAlign === 'center' ? box.width / 2 : textAlign === 'right' ? box.width : 0;
|
|
91
|
-
box.lines.forEach((line, index) => {
|
|
92
|
-
context.fillText(line, anchorX, index * fontSize * TEXT_LINE_HEIGHT);
|
|
93
|
-
});
|
|
94
|
-
context.textAlign = 'left';
|
|
95
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { InputProps, InputDrawProps } from '../Input';
|
|
2
|
-
import { drawInput } from '../Input';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface TextareaProps extends Omit<InputProps, 'type'> {
|
|
6
|
-
rows?: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const Textarea = createComponent<TextareaProps>('textarea');
|
|
10
|
-
|
|
11
|
-
/** Public props plus the engine-injected caret/scroll state consumed by the draw layer. */
|
|
12
|
-
export type TextareaDrawProps = Omit<InputDrawProps, 'type'> & { rows?: number };
|
|
13
|
-
|
|
14
|
-
export function drawTextarea(context: CanvasRenderingContext2D, props: TextareaDrawProps): void {
|
|
15
|
-
drawInput(context, props, true);
|
|
16
|
-
}
|