@web-canvas-ui/react 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
|
@@ -1,107 +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 CarouselProps extends TransformProps {
|
|
6
|
-
width: number;
|
|
7
|
-
height: number;
|
|
8
|
-
activeIndex?: number;
|
|
9
|
-
defaultActiveIndex?: number;
|
|
10
|
-
autoplay?: boolean;
|
|
11
|
-
interval?: number;
|
|
12
|
-
transitionDuration?: number;
|
|
13
|
-
loop?: boolean;
|
|
14
|
-
showArrows?: boolean;
|
|
15
|
-
showDots?: boolean;
|
|
16
|
-
backgroundColor?: string;
|
|
17
|
-
arrowColor?: string;
|
|
18
|
-
dotColor?: string;
|
|
19
|
-
activeDotColor?: string;
|
|
20
|
-
children?: unknown;
|
|
21
|
-
onChange?: (index: number) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface CarouselItemProps extends TransformProps {
|
|
25
|
-
children?: unknown;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const Carousel = createComponent<CarouselProps>('carousel');
|
|
29
|
-
export const CarouselItem = createComponent<CarouselItemProps>('carouselItem');
|
|
30
|
-
|
|
31
|
-
/** Public props plus the engine-injected item/active index consumed by the draw layer. */
|
|
32
|
-
export type CarouselDrawProps = CarouselProps & { _activeIndex?: number; _itemCount?: number };
|
|
33
|
-
|
|
34
|
-
export function drawCarousel(context: CanvasRenderingContext2D, props: CarouselDrawProps): void {
|
|
35
|
-
const width = number(props.width, 300);
|
|
36
|
-
const height = number(props.height, 180);
|
|
37
|
-
context.fillStyle = string(props.backgroundColor, 'transparent');
|
|
38
|
-
context.fillRect(0, 0, width, height);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function drawCarouselControls(
|
|
42
|
-
context: CanvasRenderingContext2D,
|
|
43
|
-
props: CarouselDrawProps,
|
|
44
|
-
): void {
|
|
45
|
-
const width = number(props.width, 300);
|
|
46
|
-
const height = number(props.height, 180);
|
|
47
|
-
const count = number(props._itemCount, 0);
|
|
48
|
-
if (count < 2) return;
|
|
49
|
-
|
|
50
|
-
if (props.showArrows !== false) {
|
|
51
|
-
const color = string(
|
|
52
|
-
props.arrowColor,
|
|
53
|
-
themeColor(context, '--canvas-ui-carousel-arrow', '#fff'),
|
|
54
|
-
);
|
|
55
|
-
const centerY = height / 2;
|
|
56
|
-
drawArrow(context, 22, centerY, -1, color);
|
|
57
|
-
drawArrow(context, width - 22, centerY, 1, color);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (props.showDots !== false) {
|
|
61
|
-
const dotColor = string(
|
|
62
|
-
props.dotColor,
|
|
63
|
-
themeColor(context, '--canvas-ui-carousel-dot', 'rgba(255,255,255,0.65)'),
|
|
64
|
-
);
|
|
65
|
-
const activeColor = string(
|
|
66
|
-
props.activeDotColor,
|
|
67
|
-
themeColor(context, '--canvas-ui-carousel-active-dot', '#1677ff'),
|
|
68
|
-
);
|
|
69
|
-
const radius = 4;
|
|
70
|
-
const gap = 14;
|
|
71
|
-
const totalWidth = (count - 1) * gap;
|
|
72
|
-
const startX = (width - totalWidth) / 2;
|
|
73
|
-
const centerY = height - 14;
|
|
74
|
-
context.beginPath();
|
|
75
|
-
for (let index = 0; index < count; index++) {
|
|
76
|
-
context.moveTo(startX + index * gap + radius, centerY);
|
|
77
|
-
context.arc(startX + index * gap, centerY, radius, 0, Math.PI * 2);
|
|
78
|
-
}
|
|
79
|
-
context.fillStyle = dotColor;
|
|
80
|
-
context.fill();
|
|
81
|
-
const selected = Math.max(0, Math.min(count - 1, number(props._activeIndex, 0)));
|
|
82
|
-
context.beginPath();
|
|
83
|
-
context.arc(startX + selected * gap, centerY, radius, 0, Math.PI * 2);
|
|
84
|
-
context.fillStyle = activeColor;
|
|
85
|
-
context.fill();
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function drawArrow(
|
|
90
|
-
context: CanvasRenderingContext2D,
|
|
91
|
-
centerX: number,
|
|
92
|
-
centerY: number,
|
|
93
|
-
direction: -1 | 1,
|
|
94
|
-
color: string,
|
|
95
|
-
): void {
|
|
96
|
-
context.fillStyle = 'rgba(0, 0, 0, 0.3)';
|
|
97
|
-
context.beginPath();
|
|
98
|
-
context.arc(centerX, centerY, 16, 0, Math.PI * 2);
|
|
99
|
-
context.fill();
|
|
100
|
-
context.strokeStyle = color;
|
|
101
|
-
context.lineWidth = 2;
|
|
102
|
-
context.beginPath();
|
|
103
|
-
context.moveTo(centerX - direction * 3, centerY - 6);
|
|
104
|
-
context.lineTo(centerX + direction * 3, centerY);
|
|
105
|
-
context.lineTo(centerX - direction * 3, centerY + 6);
|
|
106
|
-
context.stroke();
|
|
107
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export function carouselControlIndex(
|
|
2
|
-
props: Record<string, unknown>,
|
|
3
|
-
x: number,
|
|
4
|
-
y: number,
|
|
5
|
-
count: number,
|
|
6
|
-
current: number,
|
|
7
|
-
width: number,
|
|
8
|
-
height: number,
|
|
9
|
-
): number | undefined {
|
|
10
|
-
if (count < 2 || x < 0 || x > width || y < 0 || y > height) return undefined;
|
|
11
|
-
if (props.showArrows !== false && Math.abs(y - height / 2) <= 20) {
|
|
12
|
-
if (x <= 42) return current - 1;
|
|
13
|
-
if (x >= width - 42) return current + 1;
|
|
14
|
-
}
|
|
15
|
-
if (props.showDots !== false && y >= height - 26) {
|
|
16
|
-
const gap = 14;
|
|
17
|
-
const startX = (width - (count - 1) * gap) / 2;
|
|
18
|
-
const index = Math.round((x - startX) / gap);
|
|
19
|
-
if (index >= 0 && index < count && Math.abs(x - (startX + index * gap)) <= 9) {
|
|
20
|
-
return index;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function carouselIndex(
|
|
27
|
-
requested: number,
|
|
28
|
-
_current: number,
|
|
29
|
-
count: number,
|
|
30
|
-
loop: boolean,
|
|
31
|
-
): number {
|
|
32
|
-
if (count <= 0) return 0;
|
|
33
|
-
return loop
|
|
34
|
-
? ((requested % count) + count) % count
|
|
35
|
-
: Math.max(0, Math.min(count - 1, requested));
|
|
36
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createComponent } from '../shared';
|
|
2
|
-
import { drawChoiceGroup, type ChoiceGroupProps } from '../ChoiceGroup';
|
|
3
|
-
|
|
4
|
-
export interface CheckboxProps extends ChoiceGroupProps {
|
|
5
|
-
value?: string[];
|
|
6
|
-
defaultValue?: string[];
|
|
7
|
-
onChange?: (value: string[]) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const Checkbox = createComponent<CheckboxProps>('checkbox');
|
|
11
|
-
|
|
12
|
-
export function drawCheckbox(context: CanvasRenderingContext2D, props: CheckboxProps): void {
|
|
13
|
-
drawChoiceGroup(context, props, 'checkbox');
|
|
14
|
-
}
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { measureTextWidth, number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { componentHeight, componentWidth } from '../size';
|
|
4
|
-
|
|
5
|
-
export interface ChoiceOption {
|
|
6
|
-
label: string;
|
|
7
|
-
value: string;
|
|
8
|
-
disabled?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ChoiceGroupProps extends TransformProps {
|
|
12
|
-
size?: { width?: number; height?: number };
|
|
13
|
-
width?: number;
|
|
14
|
-
height?: number;
|
|
15
|
-
options: Array<string | ChoiceOption>;
|
|
16
|
-
value?: string | string[];
|
|
17
|
-
defaultValue?: string | string[];
|
|
18
|
-
disabled?: boolean;
|
|
19
|
-
direction?: 'horizontal' | 'vertical';
|
|
20
|
-
gap?: number;
|
|
21
|
-
optionHeight?: number;
|
|
22
|
-
fontSize?: number;
|
|
23
|
-
color?: string;
|
|
24
|
-
activeColor?: string;
|
|
25
|
-
disabledColor?: string;
|
|
26
|
-
borderColor?: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function choiceOptions(value: unknown): ChoiceOption[] {
|
|
30
|
-
if (!Array.isArray(value)) return [];
|
|
31
|
-
return value.map((option) =>
|
|
32
|
-
typeof option === 'string' ? { label: option, value: option } : (option as ChoiceOption),
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function choiceGroupSize(props: Record<string, unknown>): { width: number; height: number } {
|
|
37
|
-
const options = choiceOptions(props.options);
|
|
38
|
-
const horizontal = props.direction !== 'vertical';
|
|
39
|
-
const gap = number(props.gap, 12);
|
|
40
|
-
const fontSize = number(props.fontSize, 14);
|
|
41
|
-
const optionHeight = number(props.optionHeight, 28);
|
|
42
|
-
const naturalWidth =
|
|
43
|
-
options.reduce((total, option) => total + optionLayoutWidth(option.label, fontSize), 0) +
|
|
44
|
-
gap * Math.max(0, options.length - 1);
|
|
45
|
-
const naturalHeight = options.length * optionHeight + gap * Math.max(0, options.length - 1);
|
|
46
|
-
return {
|
|
47
|
-
width: componentWidth(props, horizontal ? naturalWidth : 200),
|
|
48
|
-
height: componentHeight(props, horizontal ? optionHeight : naturalHeight),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function drawChoiceGroup(
|
|
53
|
-
context: CanvasRenderingContext2D,
|
|
54
|
-
props: ChoiceGroupProps,
|
|
55
|
-
kind: 'radio' | 'checkbox',
|
|
56
|
-
): void {
|
|
57
|
-
const options = choiceOptions(props.options);
|
|
58
|
-
const horizontal = props.direction !== 'vertical';
|
|
59
|
-
const gap = number(props.gap, 12);
|
|
60
|
-
const fontSize = number(props.fontSize, 14);
|
|
61
|
-
const optionHeight = number(props.optionHeight, 28);
|
|
62
|
-
const selected = props.value;
|
|
63
|
-
context.font = `${fontSize}px sans-serif`;
|
|
64
|
-
context.textBaseline = 'middle';
|
|
65
|
-
context.lineWidth = 1;
|
|
66
|
-
|
|
67
|
-
let offsetX = 0;
|
|
68
|
-
let offsetY = 0;
|
|
69
|
-
for (const option of options) {
|
|
70
|
-
const isSelected = Array.isArray(selected)
|
|
71
|
-
? selected.includes(option.value)
|
|
72
|
-
: selected === option.value;
|
|
73
|
-
const centerY = offsetY + optionHeight / 2;
|
|
74
|
-
const controlX = offsetX + 9;
|
|
75
|
-
// Per-option disabling stays here; a whole-control `disabled` is handled uniformly by the
|
|
76
|
-
// renderer's opacity fade so it must not double-apply on top of the normal colours.
|
|
77
|
-
const disabled = option.disabled === true;
|
|
78
|
-
context.strokeStyle = disabled
|
|
79
|
-
? string(
|
|
80
|
-
props.disabledColor,
|
|
81
|
-
themeColor(context, '--canvas-ui-choice-disabled', '#bfbfbf'),
|
|
82
|
-
)
|
|
83
|
-
: string(
|
|
84
|
-
props.borderColor,
|
|
85
|
-
themeColor(context, '--canvas-ui-choice-border', '#d9d9d9'),
|
|
86
|
-
);
|
|
87
|
-
context.fillStyle = disabled
|
|
88
|
-
? themeColor(context, '--canvas-ui-choice-disabled-background', '#f5f5f5')
|
|
89
|
-
: '#fff';
|
|
90
|
-
|
|
91
|
-
if (kind === 'radio') {
|
|
92
|
-
context.beginPath();
|
|
93
|
-
context.arc(controlX, centerY, 8, 0, Math.PI * 2);
|
|
94
|
-
context.fill();
|
|
95
|
-
context.stroke();
|
|
96
|
-
if (isSelected) {
|
|
97
|
-
context.beginPath();
|
|
98
|
-
context.arc(controlX, centerY, 4, 0, Math.PI * 2);
|
|
99
|
-
context.fillStyle = string(
|
|
100
|
-
props.activeColor,
|
|
101
|
-
themeColor(context, '--canvas-ui-choice-active', '#1677ff'),
|
|
102
|
-
);
|
|
103
|
-
context.fill();
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
context.beginPath();
|
|
107
|
-
context.roundRect(offsetX + 1, centerY - 8, 16, 16, 3);
|
|
108
|
-
context.fill();
|
|
109
|
-
context.stroke();
|
|
110
|
-
if (isSelected) {
|
|
111
|
-
context.strokeStyle = string(
|
|
112
|
-
props.activeColor,
|
|
113
|
-
themeColor(context, '--canvas-ui-choice-active', '#1677ff'),
|
|
114
|
-
);
|
|
115
|
-
context.lineWidth = 2;
|
|
116
|
-
context.beginPath();
|
|
117
|
-
context.moveTo(offsetX + 5, centerY);
|
|
118
|
-
context.lineTo(offsetX + 8, centerY + 3);
|
|
119
|
-
context.lineTo(offsetX + 13, centerY - 4);
|
|
120
|
-
context.stroke();
|
|
121
|
-
context.lineWidth = 1;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
context.fillStyle = disabled
|
|
126
|
-
? string(
|
|
127
|
-
props.disabledColor,
|
|
128
|
-
themeColor(context, '--canvas-ui-choice-disabled', '#bfbfbf'),
|
|
129
|
-
)
|
|
130
|
-
: string(props.color, themeColor(context, '--canvas-ui-choice-color', '#333'));
|
|
131
|
-
context.fillText(option.label, offsetX + 23, centerY);
|
|
132
|
-
if (horizontal) {
|
|
133
|
-
offsetX += optionLayoutWidth(option.label, fontSize) + gap;
|
|
134
|
-
} else {
|
|
135
|
-
offsetY += optionHeight + gap;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function choiceAt(
|
|
141
|
-
props: Record<string, unknown>,
|
|
142
|
-
x: number,
|
|
143
|
-
y: number,
|
|
144
|
-
): ChoiceOption | undefined {
|
|
145
|
-
const options = choiceOptions(props.options);
|
|
146
|
-
const horizontal = props.direction !== 'vertical';
|
|
147
|
-
const gap = number(props.gap, 12);
|
|
148
|
-
const fontSize = number(props.fontSize, 14);
|
|
149
|
-
const optionHeight = number(props.optionHeight, 28);
|
|
150
|
-
const size = choiceGroupSize(props);
|
|
151
|
-
if (horizontal) {
|
|
152
|
-
let offsetX = 0;
|
|
153
|
-
for (const option of options) {
|
|
154
|
-
const optionWidth = optionLayoutWidth(option.label, fontSize);
|
|
155
|
-
if (x >= offsetX && x <= offsetX + optionWidth && y >= 0 && y <= size.height)
|
|
156
|
-
return option;
|
|
157
|
-
offsetX += optionWidth + gap;
|
|
158
|
-
}
|
|
159
|
-
return undefined;
|
|
160
|
-
}
|
|
161
|
-
const row = Math.floor(y / (optionHeight + gap));
|
|
162
|
-
const rowOffset = y - row * (optionHeight + gap);
|
|
163
|
-
return rowOffset <= optionHeight && x >= 0 && x <= size.width ? options[row] : undefined;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* The horizontal footprint an option occupies (control + label + trailing padding).
|
|
168
|
-
* Uses the shared real `measureText` so draw, hit-test and layout never diverge.
|
|
169
|
-
*/
|
|
170
|
-
function optionLayoutWidth(label: string, fontSize: number): number {
|
|
171
|
-
return Math.max(24, measureTextWidth(label, `${fontSize}px sans-serif`)) + 24;
|
|
172
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { ChoiceOption } from './choiceGroup';
|
|
2
|
-
|
|
3
|
-
export function radioSelection(current: unknown, option: ChoiceOption): string | undefined {
|
|
4
|
-
return current === option.value ? undefined : option.value;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function checkboxSelection(current: unknown, option: ChoiceOption): string[] {
|
|
8
|
-
const selected = Array.isArray(current)
|
|
9
|
-
? current.filter((value): value is string => typeof value === 'string')
|
|
10
|
-
: [];
|
|
11
|
-
return selected.includes(option.value)
|
|
12
|
-
? selected.filter((value) => value !== option.value)
|
|
13
|
-
: [...selected, option.value];
|
|
14
|
-
}
|
|
@@ -1,31 +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 CircleProps extends TransformProps {
|
|
6
|
-
radius?: number;
|
|
7
|
-
color?: string;
|
|
8
|
-
stroke?: string;
|
|
9
|
-
strokeWidth?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const Circle = createComponent<CircleProps>('circle');
|
|
13
|
-
|
|
14
|
-
export function drawCircle(context: CanvasRenderingContext2D, props: CircleProps): void {
|
|
15
|
-
const radius = number(props.radius, 10);
|
|
16
|
-
context.beginPath();
|
|
17
|
-
context.arc(0, 0, radius, 0, Math.PI * 2);
|
|
18
|
-
context.fillStyle = string(
|
|
19
|
-
props.color,
|
|
20
|
-
themeColor(context, '--canvas-ui-circle-background', '#e74c3c'),
|
|
21
|
-
);
|
|
22
|
-
context.fill();
|
|
23
|
-
if (props.stroke) {
|
|
24
|
-
context.strokeStyle = string(
|
|
25
|
-
props.stroke,
|
|
26
|
-
themeColor(context, '--canvas-ui-circle-border', '#000'),
|
|
27
|
-
);
|
|
28
|
-
context.lineWidth = number(props.strokeWidth, 1);
|
|
29
|
-
context.stroke();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { fitText, number, string, themeColor } from '../draw-utils';
|
|
3
|
-
import { createComponent } from '../shared';
|
|
4
|
-
|
|
5
|
-
export interface CollapseProps extends TransformProps {
|
|
6
|
-
width: number;
|
|
7
|
-
activeKeys?: string[];
|
|
8
|
-
defaultActiveKeys?: string[];
|
|
9
|
-
accordion?: boolean;
|
|
10
|
-
headerHeight?: number;
|
|
11
|
-
backgroundColor?: string;
|
|
12
|
-
textColor?: string;
|
|
13
|
-
borderColor?: string;
|
|
14
|
-
children?: unknown;
|
|
15
|
-
onChange?: (keys: string[]) => void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface CollapseItemProps extends TransformProps {
|
|
19
|
-
id: string;
|
|
20
|
-
title: string;
|
|
21
|
-
height?: number;
|
|
22
|
-
disabled?: boolean;
|
|
23
|
-
backgroundColor?: string;
|
|
24
|
-
textColor?: string;
|
|
25
|
-
borderColor?: string;
|
|
26
|
-
children?: unknown;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const Collapse = createComponent<CollapseProps>('collapse');
|
|
30
|
-
export const CollapseItem = createComponent<CollapseItemProps>('collapseItem');
|
|
31
|
-
|
|
32
|
-
/** Public props plus the engine-injected layout/expand state consumed by the draw layer. */
|
|
33
|
-
export type CollapseItemDrawProps = CollapseItemProps & {
|
|
34
|
-
_collapseWidth?: number;
|
|
35
|
-
_collapseHeaderHeight?: number;
|
|
36
|
-
_expanded?: boolean;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export function drawCollapseItem(
|
|
40
|
-
context: CanvasRenderingContext2D,
|
|
41
|
-
props: CollapseItemDrawProps,
|
|
42
|
-
): void {
|
|
43
|
-
const width = number(props._collapseWidth, 300);
|
|
44
|
-
const headerHeight = number(props._collapseHeaderHeight, 40);
|
|
45
|
-
context.fillStyle = string(
|
|
46
|
-
props.backgroundColor,
|
|
47
|
-
themeColor(context, '--canvas-ui-collapse-header-background', '#fafafa'),
|
|
48
|
-
);
|
|
49
|
-
context.fillRect(0, 0, width, headerHeight);
|
|
50
|
-
context.strokeStyle = string(
|
|
51
|
-
props.borderColor,
|
|
52
|
-
themeColor(context, '--canvas-ui-collapse-border', '#d9d9d9'),
|
|
53
|
-
);
|
|
54
|
-
context.strokeRect(0, 0, width, headerHeight);
|
|
55
|
-
context.font = '14px sans-serif';
|
|
56
|
-
context.textBaseline = 'middle';
|
|
57
|
-
context.fillStyle = props.disabled
|
|
58
|
-
? themeColor(context, '--canvas-ui-collapse-disabled-color', '#bfbfbf')
|
|
59
|
-
: string(props.textColor, themeColor(context, '--canvas-ui-collapse-color', '#262626'));
|
|
60
|
-
context.fillText(
|
|
61
|
-
fitText(
|
|
62
|
-
context,
|
|
63
|
-
`${props._expanded ? '−' : '+'} ${String(props.title ?? '')}`,
|
|
64
|
-
Math.max(0, width - 24),
|
|
65
|
-
),
|
|
66
|
-
12,
|
|
67
|
-
headerHeight / 2,
|
|
68
|
-
);
|
|
69
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export function toggleCollapseKeys(
|
|
2
|
-
current: readonly string[],
|
|
3
|
-
key: string,
|
|
4
|
-
accordion: boolean,
|
|
5
|
-
): string[] {
|
|
6
|
-
const active = new Set(current);
|
|
7
|
-
if (active.has(key)) {
|
|
8
|
-
active.delete(key);
|
|
9
|
-
} else {
|
|
10
|
-
if (accordion) active.clear();
|
|
11
|
-
active.add(key);
|
|
12
|
-
}
|
|
13
|
-
return [...active];
|
|
14
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { TransformProps } from '../../types';
|
|
2
|
-
import { createComponent } from '../shared';
|
|
3
|
-
|
|
4
|
-
export interface CustomDrawSize {
|
|
5
|
-
readonly width: number;
|
|
6
|
-
readonly height: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface CustomDrawClickEvent extends CustomDrawSize {
|
|
10
|
-
readonly x: number;
|
|
11
|
-
readonly y: number;
|
|
12
|
-
readonly canvasX: number;
|
|
13
|
-
readonly canvasY: number;
|
|
14
|
-
readonly pointerId: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type CustomDrawHitTest = (x: number, y: number, size: CustomDrawSize) => boolean;
|
|
18
|
-
|
|
19
|
-
export interface CustomDrawProps extends TransformProps {
|
|
20
|
-
width: number;
|
|
21
|
-
height: number;
|
|
22
|
-
draw: (context: CanvasRenderingContext2D, size: CustomDrawSize) => void;
|
|
23
|
-
onClick?: (event: CustomDrawClickEvent) => void;
|
|
24
|
-
hitTest?: CustomDrawHitTest;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const CustomDraw = createComponent<CustomDrawProps>('customDraw');
|
|
28
|
-
|
|
29
|
-
export function drawCustomDraw(context: CanvasRenderingContext2D, props: CustomDrawProps): void {
|
|
30
|
-
const draw = props.draw;
|
|
31
|
-
if (typeof draw !== 'function') {
|
|
32
|
-
throw new TypeError('<CustomDraw> requires a draw(context, size) function.');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const width = finiteDimension(props.width, 'width');
|
|
36
|
-
const height = finiteDimension(props.height, 'height');
|
|
37
|
-
context.save();
|
|
38
|
-
try {
|
|
39
|
-
draw(context, { width, height });
|
|
40
|
-
} finally {
|
|
41
|
-
context.restore();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function finiteDimension(value: unknown, name: string): number {
|
|
46
|
-
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
|
47
|
-
throw new RangeError(`<CustomDraw> ${name} must be a non-negative finite number.`);
|
|
48
|
-
}
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
@@ -1,70 +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 DatePickerProps extends TransformProps {
|
|
6
|
-
width?: number;
|
|
7
|
-
height?: number;
|
|
8
|
-
value?: string;
|
|
9
|
-
defaultValue?: string;
|
|
10
|
-
placeholder?: string;
|
|
11
|
-
fontSize?: number;
|
|
12
|
-
color?: string;
|
|
13
|
-
bgColor?: string;
|
|
14
|
-
borderColor?: string;
|
|
15
|
-
focusBorderColor?: string;
|
|
16
|
-
borderRadius?: number;
|
|
17
|
-
disabled?: boolean;
|
|
18
|
-
onChange?: (value: string) => void;
|
|
19
|
-
onFocus?: () => void;
|
|
20
|
-
onBlur?: () => void;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const DatePicker = createComponent<DatePickerProps>('datePicker');
|
|
24
|
-
|
|
25
|
-
/** Public props plus the engine-injected focus state consumed by the draw layer. */
|
|
26
|
-
export type DatePickerDrawProps = DatePickerProps & { focused?: boolean };
|
|
27
|
-
|
|
28
|
-
export function drawDatePicker(
|
|
29
|
-
context: CanvasRenderingContext2D,
|
|
30
|
-
props: DatePickerDrawProps,
|
|
31
|
-
): void {
|
|
32
|
-
const width = number(props.width, 200);
|
|
33
|
-
const height = number(props.height, 36);
|
|
34
|
-
const radius = Math.max(0, Math.min(number(props.borderRadius, 4), width / 2, height / 2));
|
|
35
|
-
const value = String(props.value ?? '');
|
|
36
|
-
context.beginPath();
|
|
37
|
-
context.roundRect(0, 0, width, height, radius);
|
|
38
|
-
context.fillStyle = string(
|
|
39
|
-
props.bgColor,
|
|
40
|
-
themeColor(context, '--canvas-ui-input-background', '#fff'),
|
|
41
|
-
);
|
|
42
|
-
context.fill();
|
|
43
|
-
context.strokeStyle = props.focused
|
|
44
|
-
? string(
|
|
45
|
-
props.focusBorderColor,
|
|
46
|
-
themeColor(context, '--canvas-ui-input-focus-border', '#1677ff'),
|
|
47
|
-
)
|
|
48
|
-
: string(props.borderColor, themeColor(context, '--canvas-ui-input-border', '#d9d9d9'));
|
|
49
|
-
context.stroke();
|
|
50
|
-
context.font = `${number(props.fontSize, 14)}px sans-serif`;
|
|
51
|
-
context.textBaseline = 'middle';
|
|
52
|
-
context.fillStyle = value
|
|
53
|
-
? string(props.color, themeColor(context, '--canvas-ui-input-color', '#333'))
|
|
54
|
-
: themeColor(context, '--canvas-ui-input-placeholder-color', '#999');
|
|
55
|
-
context.fillText(value || string(props.placeholder, 'Select date'), 8, height / 2);
|
|
56
|
-
context.fillStyle = string(props.color, themeColor(context, '--canvas-ui-input-color', '#666'));
|
|
57
|
-
context.strokeStyle = context.fillStyle;
|
|
58
|
-
context.lineWidth = 1.4;
|
|
59
|
-
const iconX = width - 20;
|
|
60
|
-
const iconY = height / 2 - 7;
|
|
61
|
-
context.strokeRect(iconX, iconY + 2, 13, 12);
|
|
62
|
-
context.beginPath();
|
|
63
|
-
context.moveTo(iconX, iconY + 5);
|
|
64
|
-
context.lineTo(iconX + 13, iconY + 5);
|
|
65
|
-
context.moveTo(iconX + 3, iconY);
|
|
66
|
-
context.lineTo(iconX + 3, iconY + 4);
|
|
67
|
-
context.moveTo(iconX + 10, iconY);
|
|
68
|
-
context.lineTo(iconX + 10, iconY + 4);
|
|
69
|
-
context.stroke();
|
|
70
|
-
}
|