@web-canvas-ui/react 0.0.0-stage → 0.1.1
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/Canvas.tsx +142 -0
- package/README.md +988 -2
- package/__tests__/pure-logic.test.ts +153 -0
- package/__tests__/scene-and-node.test.ts +131 -0
- package/__tests__/scratch-space.test.ts +30 -0
- package/__tests__/textarea-caret.test.ts +96 -0
- package/__tests__/utils.test.ts +114 -0
- package/canvas-handle.ts +114 -0
- package/components/Button/button.ts +57 -0
- package/components/Button/index.ts +3 -0
- package/components/Button/logic.ts +11 -0
- package/components/CanvasView/canvasView.ts +103 -0
- package/components/CanvasView/index.ts +7 -0
- package/components/Carousel/carousel.ts +107 -0
- package/components/Carousel/index.ts +3 -0
- package/components/Carousel/logic.ts +36 -0
- package/components/Checkbox/checkbox.ts +14 -0
- package/components/Checkbox/index.ts +2 -0
- package/components/ChoiceGroup/choiceGroup.ts +172 -0
- package/components/ChoiceGroup/index.ts +3 -0
- package/components/ChoiceGroup/logic.ts +14 -0
- package/components/Circle/circle.ts +31 -0
- package/components/Circle/index.ts +2 -0
- package/components/Collapse/collapse.ts +69 -0
- package/components/Collapse/index.ts +3 -0
- package/components/Collapse/logic.ts +14 -0
- package/components/CustomDraw/customDraw.ts +50 -0
- package/components/CustomDraw/index.ts +7 -0
- package/components/DatePicker/datePicker.ts +70 -0
- package/components/DatePicker/index.ts +2 -0
- package/components/Dialog/dialog.ts +102 -0
- package/components/Dialog/index.ts +2 -0
- package/components/Dropdown/dropdown.ts +125 -0
- package/components/Dropdown/index.ts +3 -0
- package/components/Dropdown/logic.ts +29 -0
- package/components/Grid/grid.ts +15 -0
- package/components/Grid/index.ts +2 -0
- package/components/GridItem/gridItem.ts +11 -0
- package/components/GridItem/index.ts +2 -0
- package/components/Group/group.ts +8 -0
- package/components/Group/index.ts +2 -0
- package/components/Icon/icon.ts +669 -0
- package/components/Icon/index.ts +2 -0
- package/components/Image/image.ts +69 -0
- package/components/Image/index.ts +2 -0
- package/components/Input/index.ts +2 -0
- package/components/Input/input.ts +139 -0
- package/components/Progress/index.ts +2 -0
- package/components/Progress/progress.ts +59 -0
- package/components/Radio/index.ts +2 -0
- package/components/Radio/radio.ts +14 -0
- package/components/Rect/index.ts +2 -0
- package/components/Rect/rect.ts +35 -0
- package/components/Slider/index.ts +3 -0
- package/components/Slider/logic.ts +19 -0
- package/components/Slider/slider.ts +58 -0
- package/components/Switch/index.ts +3 -0
- package/components/Switch/logic.ts +8 -0
- package/components/Switch/switch.ts +37 -0
- package/components/Tabs/index.ts +3 -0
- package/components/Tabs/logic.ts +14 -0
- package/components/Tabs/tabs.ts +78 -0
- package/components/Text/index.ts +2 -0
- package/components/Text/text.ts +95 -0
- package/components/Textarea/index.ts +2 -0
- package/components/Textarea/textarea.ts +16 -0
- package/components/Upload/index.ts +2 -0
- package/components/Upload/upload.ts +66 -0
- package/components/draw-utils.ts +230 -0
- package/components/shared.ts +15 -0
- package/components/size.ts +20 -0
- package/components/types.ts +37 -0
- package/components.ts +98 -0
- package/engine/__tests__/carousel.test.ts +218 -0
- package/engine/__tests__/hit-test.test.ts +282 -0
- package/engine/__tests__/layout.test.ts +474 -0
- package/engine/carousel.ts +150 -0
- package/engine/events.ts +73 -0
- package/engine/hit-test.ts +321 -0
- package/engine/images.ts +41 -0
- package/engine/interaction.ts +442 -0
- package/engine/layout.ts +355 -0
- package/engine/nodes.ts +166 -0
- package/engine/state.ts +75 -0
- package/engine/text-controls.ts +122 -0
- package/engine/transform.ts +61 -0
- package/engine/vnodes.ts +204 -0
- package/engine.ts +162 -0
- package/image-assets.ts +37 -0
- package/index.ts +78 -0
- package/input-bridge.ts +285 -0
- package/package.json +27 -4
- package/renderer.ts +303 -0
- package/scene.ts +122 -0
- package/theme.css +327 -0
- package/theme.scss +234 -0
- package/types.ts +256 -0
- package/web-canvas-ui-react-0.1.1.tgz +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { TransformProps } from '../../types';
|
|
2
|
+
import { fitText, number, string, themeColor } from '../draw-utils';
|
|
3
|
+
import { createComponent } from '../shared';
|
|
4
|
+
|
|
5
|
+
export interface ButtonProps extends TransformProps {
|
|
6
|
+
children?: string | number;
|
|
7
|
+
text?: string;
|
|
8
|
+
width?: number;
|
|
9
|
+
height?: number;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
backgroundColor?: string;
|
|
12
|
+
fontColor?: string;
|
|
13
|
+
borderColor?: string;
|
|
14
|
+
borderRadius?: number;
|
|
15
|
+
fontSize?: number;
|
|
16
|
+
onClick?: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const Button = createComponent<ButtonProps>('button');
|
|
20
|
+
|
|
21
|
+
/** Public props plus the engine-injected press state consumed by the draw layer. */
|
|
22
|
+
export type ButtonDrawProps = ButtonProps & { pressed?: boolean };
|
|
23
|
+
|
|
24
|
+
export function drawButton(context: CanvasRenderingContext2D, props: ButtonDrawProps): void {
|
|
25
|
+
const width = number(props.width, 120);
|
|
26
|
+
const height = number(props.height, 40);
|
|
27
|
+
const radius = number(props.borderRadius, 6);
|
|
28
|
+
context.beginPath();
|
|
29
|
+
context.roundRect(0, 0, width, height, radius);
|
|
30
|
+
context.fillStyle = props.pressed
|
|
31
|
+
? themeColor(context, '--canvas-ui-button-pressed-background', '#0958d9')
|
|
32
|
+
: string(
|
|
33
|
+
props.backgroundColor,
|
|
34
|
+
themeColor(context, '--canvas-ui-button-background', '#1677ff'),
|
|
35
|
+
);
|
|
36
|
+
context.fill();
|
|
37
|
+
if (props.borderColor) {
|
|
38
|
+
context.strokeStyle = string(
|
|
39
|
+
props.borderColor,
|
|
40
|
+
themeColor(context, '--canvas-ui-button-border', '#1677ff'),
|
|
41
|
+
);
|
|
42
|
+
context.stroke();
|
|
43
|
+
}
|
|
44
|
+
context.font = `${number(props.fontSize, 15)}px sans-serif`;
|
|
45
|
+
context.textBaseline = 'middle';
|
|
46
|
+
context.textAlign = 'center';
|
|
47
|
+
context.fillStyle = string(
|
|
48
|
+
props.fontColor,
|
|
49
|
+
themeColor(context, '--canvas-ui-button-color', '#fff'),
|
|
50
|
+
);
|
|
51
|
+
context.fillText(
|
|
52
|
+
fitText(context, String(props.text ?? ''), Math.max(0, width - 16)),
|
|
53
|
+
width / 2,
|
|
54
|
+
height / 2,
|
|
55
|
+
);
|
|
56
|
+
context.textAlign = 'left';
|
|
57
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SceneNode } from '../../scene';
|
|
2
|
+
|
|
3
|
+
export function setButtonPressed(button: SceneNode, pressed: boolean): void {
|
|
4
|
+
button.setProps({ pressed });
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function activateButton(button: SceneNode): void {
|
|
8
|
+
if (button.props.disabled === true) return;
|
|
9
|
+
const onClick = button.props.onClick;
|
|
10
|
+
if (typeof onClick === 'function') onClick();
|
|
11
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { TransformProps } from '../../types';
|
|
2
|
+
import { createComponent } from '../shared';
|
|
3
|
+
import type { CustomDrawSize } from '../CustomDraw';
|
|
4
|
+
|
|
5
|
+
export type CanvasViewFit = 'contain' | 'cover' | 'fill';
|
|
6
|
+
|
|
7
|
+
export interface CanvasViewPointerEvent {
|
|
8
|
+
readonly type: 'pointerdown' | 'pointermove' | 'pointerup' | 'pointercancel' | 'click';
|
|
9
|
+
readonly source: HTMLCanvasElement;
|
|
10
|
+
readonly x: number;
|
|
11
|
+
readonly y: number;
|
|
12
|
+
readonly sourceX: number;
|
|
13
|
+
readonly sourceY: number;
|
|
14
|
+
readonly sourceWidth: number;
|
|
15
|
+
readonly sourceHeight: number;
|
|
16
|
+
readonly pointerId: number;
|
|
17
|
+
readonly pointerType?: string;
|
|
18
|
+
readonly pressure?: number;
|
|
19
|
+
readonly isPrimary?: boolean;
|
|
20
|
+
readonly button?: number;
|
|
21
|
+
readonly buttons?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type CanvasViewPointerEventHandler = (event: CanvasViewPointerEvent) => void;
|
|
25
|
+
|
|
26
|
+
export interface CanvasViewProps extends TransformProps {
|
|
27
|
+
source: HTMLCanvasElement;
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
fit?: CanvasViewFit;
|
|
31
|
+
onPointerEvent?: CanvasViewPointerEventHandler;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const CanvasView = createComponent<CanvasViewProps>('canvasView');
|
|
35
|
+
|
|
36
|
+
export function drawCanvasView(context: CanvasRenderingContext2D, props: CanvasViewProps): void {
|
|
37
|
+
const source = props.source;
|
|
38
|
+
if (!isHTMLCanvasElement(source)) {
|
|
39
|
+
throw new TypeError('<CanvasView> requires an HTMLCanvasElement source.');
|
|
40
|
+
}
|
|
41
|
+
if (source === context.canvas) {
|
|
42
|
+
throw new TypeError('<CanvasView> cannot draw the destination Canvas into itself.');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const width = dimension(props.width, 'width');
|
|
46
|
+
const height = dimension(props.height, 'height');
|
|
47
|
+
if (!source.width || !source.height || !width || !height) return;
|
|
48
|
+
|
|
49
|
+
const size: CustomDrawSize = { width, height };
|
|
50
|
+
const fit = props.fit ?? 'contain';
|
|
51
|
+
context.save();
|
|
52
|
+
try {
|
|
53
|
+
context.beginPath();
|
|
54
|
+
context.rect(0, 0, size.width, size.height);
|
|
55
|
+
context.clip();
|
|
56
|
+
if (fit === 'fill') {
|
|
57
|
+
context.drawImage(source, 0, 0, size.width, size.height);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (fit !== 'contain' && fit !== 'cover') {
|
|
62
|
+
throw new RangeError('<CanvasView> fit must be "contain", "cover", or "fill".');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const scale =
|
|
66
|
+
fit === 'cover'
|
|
67
|
+
? Math.max(size.width / source.width, size.height / source.height)
|
|
68
|
+
: Math.min(size.width / source.width, size.height / source.height);
|
|
69
|
+
const drawWidth = source.width * scale;
|
|
70
|
+
const drawHeight = source.height * scale;
|
|
71
|
+
context.drawImage(
|
|
72
|
+
source,
|
|
73
|
+
(size.width - drawWidth) / 2,
|
|
74
|
+
(size.height - drawHeight) / 2,
|
|
75
|
+
drawWidth,
|
|
76
|
+
drawHeight,
|
|
77
|
+
);
|
|
78
|
+
} finally {
|
|
79
|
+
context.restore();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function dimension(value: unknown, name: string): number {
|
|
84
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
|
85
|
+
throw new RangeError(`<CanvasView> ${name} must be a non-negative finite number.`);
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function isHTMLCanvasElement(value: unknown): value is HTMLCanvasElement {
|
|
91
|
+
return (
|
|
92
|
+
typeof value === 'object' &&
|
|
93
|
+
value !== null &&
|
|
94
|
+
'nodeName' in value &&
|
|
95
|
+
value.nodeName === 'CANVAS' &&
|
|
96
|
+
'width' in value &&
|
|
97
|
+
typeof value.width === 'number' &&
|
|
98
|
+
'height' in value &&
|
|
99
|
+
typeof value.height === 'number' &&
|
|
100
|
+
'getContext' in value &&
|
|
101
|
+
typeof value.getContext === 'function'
|
|
102
|
+
);
|
|
103
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|