|
1 | | -import React, { VFC, CSSProperties } from 'react'; |
2 | | - |
3 | | -type Position = { |
4 | | - top?: number; // px |
5 | | - right?: number; // px |
6 | | - bottom?: number; // px |
7 | | - left?: number; // px |
8 | | -} |
9 | | - |
10 | | -type TextWidgetProps = { |
11 | | - text: string; |
12 | | - textColor?: string; |
13 | | - backgroundColor?: string; |
14 | | - edgeWeight?: number; // px |
15 | | - edgeColor?: string; |
16 | | - width?: number; // px |
17 | | - height?: number; // px |
18 | | - padding?: string; // px |
19 | | - position?: Position; |
20 | | -}; |
21 | | - |
22 | | -const calcTextShadow = (weight, color) => { |
23 | | - const edge = [ |
24 | | - `${weight}px ${weight}px 0 ${color}`, |
25 | | - `-${weight}px -${weight}px 0 ${color}`, |
26 | | - `-${weight}px ${weight}px 0 ${color}`, |
27 | | - `${weight}px -${weight}px 0 ${color}`, |
28 | | - `0px ${weight}px 0 ${color}`, |
29 | | - `0-${weight}px 0 ${color}`, |
30 | | - `-${weight}px 0 0 ${color}`, |
31 | | - `${weight}px 0 0 ${color}` |
32 | | - ].join(', '); |
33 | | - return edge; |
34 | | -}; |
35 | | - |
36 | | -const defaultStyle: CSSProperties = { |
37 | | - boxSizing: 'border-box', |
38 | | - whiteSpace: 'pre-wrap', |
39 | | - overflow: 'hidden', |
40 | | - color: 'white', |
41 | | - backgroundColor: 'rgba(0, 0, 0 0.1)', |
42 | | - textShadow: calcTextShadow(1, 'black'), |
43 | | - width: 320, |
44 | | - height: 540, |
45 | | - padding: '4px 8px', |
46 | | - position: 'absolute', |
47 | | -}; |
48 | | - |
49 | | -const TextWidget: VFC<TextWidgetProps> = ({ |
50 | | - text, |
51 | | - textColor, |
52 | | - backgroundColor, |
53 | | - edgeWeight, |
54 | | - edgeColor, |
55 | | - width, |
56 | | - height, |
57 | | - padding, |
58 | | - position, |
59 | | -}) => { |
60 | | - const edge = calcTextShadow(edgeWeight || 1, edgeColor || 'black'); |
61 | | - |
62 | | - const style: CSSProperties = { |
63 | | - ...defaultStyle, |
64 | | - width: `${width || 320}px`, |
65 | | - height: `${height | 540}px`, |
66 | | - padding: padding || '4px 8px', |
67 | | - color: textColor || 'white', |
68 | | - textShadow: edge, |
69 | | - backgroundColor: backgroundColor || 'rgba(0, 0, 0, 0.1)', |
70 | | - }; |
71 | | - |
72 | | - if (position?.top || position?.right || position?.bottom || position?.left) { |
73 | | - if (position?.top) style.top = position.top; |
74 | | - if (position?.right) style.right = position.right; |
75 | | - if (position?.bottom) style.bottom = position.bottom; |
76 | | - if (position?.left) style.left = position.left; |
77 | | - } else { |
78 | | - style.right = 0; |
79 | | - } |
80 | | - |
81 | | - console.log(style); |
82 | | - |
83 | | - return <div style={style}>{text}</div>; |
84 | | -}; |
85 | | - |
86 | | -export { TextWidget } |
| 1 | +export { TextWidget } from './widget'; |
| 2 | +export { TextWidgetEditor } from './editor'; |
0 commit comments