@hpcc-js/react 2.55.2 → 2.55.3

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/src/text.tsx CHANGED
@@ -1,238 +1,238 @@
1
- import { Utility } from "@hpcc-js/common";
2
- import * as React from "@hpcc-js/preact-shim";
3
- import { Icon } from "./icon";
4
- import { Rectangle } from "./shape";
5
-
6
- interface TextLine {
7
- text: string;
8
- height?: number;
9
- anchor?: string;
10
- baseline?: string;
11
- fontFamily?: string;
12
- fill?: string;
13
- }
14
-
15
- export const TextLine: React.FunctionComponent<TextLine> = ({
16
- text,
17
- height = 12,
18
- anchor = "middle",
19
- baseline = "middle",
20
- fontFamily = "Verdana",
21
- fill = "black"
22
- }) => {
23
- return <text
24
- font-family={fontFamily}
25
- font-size={`${height}px`}
26
- text-anchor={anchor}
27
- dominant-baseline={baseline}
28
- fill={fill}
29
- >{text}</text>;
30
- };
31
-
32
- interface Text {
33
- text: string;
34
- height?: number;
35
- fontFamily?: string;
36
- fill?: string;
37
- onSizeUpdate?: (size: { width: number, height: number }) => void;
38
- }
39
-
40
- export const Text: React.FunctionComponent<Text> = ({
41
- text,
42
- height = 12,
43
- fontFamily = "Verdana",
44
- fill = "black",
45
- onSizeUpdate
46
- }) => {
47
- const [totalWidth, setTotalWidthUpdate] = React.useState(0);
48
- const [totalHeight, setTotalHeightUpdate] = React.useState(0);
49
-
50
- React.useEffect(() => {
51
- onSizeUpdate && onSizeUpdate({ width: totalWidth, height: totalHeight });
52
- }, [totalWidth, totalHeight, onSizeUpdate]);
53
-
54
- const parts = React.useMemo(() => {
55
- return text.split("\n");
56
- }, [text]);
57
-
58
- const ts = React.useMemo(() => {
59
- return Utility.textSize(parts, fontFamily, height);
60
- }, [fontFamily, height, parts]);
61
-
62
- setTotalWidthUpdate(ts.width);
63
- setTotalHeightUpdate(parts.length * (height + 2) - 2);
64
-
65
- const yOffset = -(totalHeight / 2) + (height / 2);
66
- const TextLines = React.useMemo(() => {
67
- return parts.map((p, i) => {
68
- return <g key={i} transform={`translate(0 ${yOffset + i * (height + 2)})`}>
69
- <TextLine
70
- text={p}
71
- height={height}
72
- fontFamily={fontFamily}
73
- fill={fill}
74
- />
75
- </g>;
76
- });
77
- }, [fill, fontFamily, height, parts, yOffset]);
78
-
79
- return <>{TextLines}</>;
80
- };
81
-
82
- export interface TextBox {
83
- text: string;
84
- height?: number;
85
- fontFamily?: string;
86
- padding?: number;
87
- fill?: string;
88
- stroke?: string;
89
- textFill?: string;
90
- strokeWidth?: number;
91
- cornerRadius?: number;
92
- textOffsetY?: number;
93
- onSizeUpdate?: (size: { width: number, height: number }) => void;
94
- }
95
-
96
- export const TextBox: React.FunctionComponent<TextBox> = ({
97
- text,
98
- height = 12,
99
- fontFamily = "Verdana",
100
- padding = 4,
101
- fill = "whitesmoke",
102
- stroke = "lightgray",
103
- textFill = "black",
104
- strokeWidth = 1,
105
- cornerRadius = 0,
106
- onSizeUpdate
107
- }) => {
108
- const [textWidth, setTextWidthUpdate] = React.useState(0);
109
- const [textHeight, setTextHeightUpdate] = React.useState(0);
110
-
111
- React.useEffect(() => {
112
- onSizeUpdate && onSizeUpdate({ width: textWidth, height: textHeight });
113
- }, [textWidth, textHeight, onSizeUpdate]);
114
-
115
- const onTextSizeUpdate = React.useCallback(size => {
116
- setTextWidthUpdate(size.width);
117
- setTextHeightUpdate(size.height);
118
- }, []);
119
-
120
- const w = textWidth + padding * 2 + strokeWidth;
121
- const h = textHeight + padding * 2 + strokeWidth;
122
- const textOffsetY = Math.floor(height / 10);
123
-
124
- return <>
125
- <Rectangle
126
- width={w}
127
- height={h}
128
- fill={fill}
129
- stroke={stroke}
130
- strokeWidth={strokeWidth}
131
- cornerRadius={cornerRadius}
132
- />
133
- <g transform={`translate(0 ${textOffsetY})`}>
134
- <Text
135
- text={text}
136
- fontFamily={fontFamily}
137
- height={height}
138
- fill={textFill}
139
- onSizeUpdate={onTextSizeUpdate}
140
- />
141
- </g>
142
- </>;
143
- };
144
-
145
- export interface LabelledRect extends TextBox {
146
- width?: number;
147
- fontSize?: number;
148
- }
149
-
150
- export interface IconLabelledRect extends LabelledRect {
151
- icon: string;
152
- iconFontFamily: string;
153
- iconFontSize: number;
154
- }
155
-
156
- export const LabelledRect: React.FunctionComponent<LabelledRect> = ({
157
- text,
158
- height = 12,
159
- width = 12,
160
- fontFamily = "Verdana",
161
- fontSize = 10,
162
- padding = 3,
163
- fill = "whitesmoke",
164
- stroke = "lightgray",
165
- textFill = "black",
166
- strokeWidth = 1,
167
- cornerRadius = 0,
168
- onSizeUpdate = (size: { width: number, height: number }) => { }
169
- }) => {
170
- return <>
171
- <Rectangle
172
- width={width}
173
- height={height}
174
- fill={fill}
175
- stroke={stroke}
176
- strokeWidth={strokeWidth}
177
- cornerRadius={cornerRadius}
178
- />
179
- <g transform={`translate(${-(width / 2) + padding} ${-(height / 2) + padding})`}>
180
- <TextLine
181
- text={text}
182
- fontFamily={fontFamily}
183
- height={fontSize}
184
- fill={textFill}
185
- anchor="start"
186
- baseline="hanging"
187
- />
188
- </g>
189
- </>;
190
- };
191
-
192
- export const IconLabelledRect: React.FunctionComponent<IconLabelledRect> = ({
193
- icon,
194
- iconFontFamily,
195
- text,
196
- height = 12,
197
- width = 12,
198
- fontFamily = "Verdana",
199
- fontSize = 10,
200
- padding = 3,
201
- fill = "whitesmoke",
202
- stroke = "lightgray",
203
- textFill = "black",
204
- strokeWidth = 1,
205
- cornerRadius = 0,
206
- onSizeUpdate = (size: { width: number, height: number }) => { }
207
- }) => {
208
- return <>
209
- <Rectangle
210
- width={width}
211
- height={height}
212
- fill={fill}
213
- stroke={stroke}
214
- strokeWidth={strokeWidth}
215
- cornerRadius={cornerRadius}
216
- />
217
- <g transform={`translate(${-(width / 2) + padding} ${-(height / 2) + padding})`}>
218
- <Icon
219
- shape="square"
220
- imageFontFamily={iconFontFamily}
221
- imageChar={icon}
222
- height={height}
223
- fill={fill}
224
- imageCharFill={textFill}
225
- />
226
- </g>
227
- <g transform={`translate(${-(width / 2) + (padding * 2) + height} ${-(height / 2) + padding})`}>
228
- <TextLine
229
- text={text}
230
- fontFamily={fontFamily}
231
- height={fontSize}
232
- fill={textFill}
233
- anchor="start"
234
- baseline="hanging"
235
- />
236
- </g>
237
- </>;
238
- };
1
+ import { Utility } from "@hpcc-js/common";
2
+ import * as React from "@hpcc-js/preact-shim";
3
+ import { Icon } from "./icon";
4
+ import { Rectangle } from "./shape";
5
+
6
+ interface TextLine {
7
+ text: string;
8
+ height?: number;
9
+ anchor?: string;
10
+ baseline?: string;
11
+ fontFamily?: string;
12
+ fill?: string;
13
+ }
14
+
15
+ export const TextLine: React.FunctionComponent<TextLine> = ({
16
+ text,
17
+ height = 12,
18
+ anchor = "middle",
19
+ baseline = "middle",
20
+ fontFamily = "Verdana",
21
+ fill = "black"
22
+ }) => {
23
+ return <text
24
+ font-family={fontFamily}
25
+ font-size={`${height}px`}
26
+ text-anchor={anchor}
27
+ dominant-baseline={baseline}
28
+ fill={fill}
29
+ >{text}</text>;
30
+ };
31
+
32
+ interface Text {
33
+ text: string;
34
+ height?: number;
35
+ fontFamily?: string;
36
+ fill?: string;
37
+ onSizeUpdate?: (size: { width: number, height: number }) => void;
38
+ }
39
+
40
+ export const Text: React.FunctionComponent<Text> = ({
41
+ text,
42
+ height = 12,
43
+ fontFamily = "Verdana",
44
+ fill = "black",
45
+ onSizeUpdate
46
+ }) => {
47
+ const [totalWidth, setTotalWidthUpdate] = React.useState(0);
48
+ const [totalHeight, setTotalHeightUpdate] = React.useState(0);
49
+
50
+ React.useEffect(() => {
51
+ onSizeUpdate && onSizeUpdate({ width: totalWidth, height: totalHeight });
52
+ }, [totalWidth, totalHeight, onSizeUpdate]);
53
+
54
+ const parts = React.useMemo(() => {
55
+ return text.split("\n");
56
+ }, [text]);
57
+
58
+ const ts = React.useMemo(() => {
59
+ return Utility.textSize(parts, fontFamily, height);
60
+ }, [fontFamily, height, parts]);
61
+
62
+ setTotalWidthUpdate(ts.width);
63
+ setTotalHeightUpdate(parts.length * (height + 2) - 2);
64
+
65
+ const yOffset = -(totalHeight / 2) + (height / 2);
66
+ const TextLines = React.useMemo(() => {
67
+ return parts.map((p, i) => {
68
+ return <g key={i} transform={`translate(0 ${yOffset + i * (height + 2)})`}>
69
+ <TextLine
70
+ text={p}
71
+ height={height}
72
+ fontFamily={fontFamily}
73
+ fill={fill}
74
+ />
75
+ </g>;
76
+ });
77
+ }, [fill, fontFamily, height, parts, yOffset]);
78
+
79
+ return <>{TextLines}</>;
80
+ };
81
+
82
+ export interface TextBox {
83
+ text: string;
84
+ height?: number;
85
+ fontFamily?: string;
86
+ padding?: number;
87
+ fill?: string;
88
+ stroke?: string;
89
+ textFill?: string;
90
+ strokeWidth?: number;
91
+ cornerRadius?: number;
92
+ textOffsetY?: number;
93
+ onSizeUpdate?: (size: { width: number, height: number }) => void;
94
+ }
95
+
96
+ export const TextBox: React.FunctionComponent<TextBox> = ({
97
+ text,
98
+ height = 12,
99
+ fontFamily = "Verdana",
100
+ padding = 4,
101
+ fill = "whitesmoke",
102
+ stroke = "lightgray",
103
+ textFill = "black",
104
+ strokeWidth = 1,
105
+ cornerRadius = 0,
106
+ onSizeUpdate
107
+ }) => {
108
+ const [textWidth, setTextWidthUpdate] = React.useState(0);
109
+ const [textHeight, setTextHeightUpdate] = React.useState(0);
110
+
111
+ React.useEffect(() => {
112
+ onSizeUpdate && onSizeUpdate({ width: textWidth, height: textHeight });
113
+ }, [textWidth, textHeight, onSizeUpdate]);
114
+
115
+ const onTextSizeUpdate = React.useCallback(size => {
116
+ setTextWidthUpdate(size.width);
117
+ setTextHeightUpdate(size.height);
118
+ }, []);
119
+
120
+ const w = textWidth + padding * 2 + strokeWidth;
121
+ const h = textHeight + padding * 2 + strokeWidth;
122
+ const textOffsetY = Math.floor(height / 10);
123
+
124
+ return <>
125
+ <Rectangle
126
+ width={w}
127
+ height={h}
128
+ fill={fill}
129
+ stroke={stroke}
130
+ strokeWidth={strokeWidth}
131
+ cornerRadius={cornerRadius}
132
+ />
133
+ <g transform={`translate(0 ${textOffsetY})`}>
134
+ <Text
135
+ text={text}
136
+ fontFamily={fontFamily}
137
+ height={height}
138
+ fill={textFill}
139
+ onSizeUpdate={onTextSizeUpdate}
140
+ />
141
+ </g>
142
+ </>;
143
+ };
144
+
145
+ export interface LabelledRect extends TextBox {
146
+ width?: number;
147
+ fontSize?: number;
148
+ }
149
+
150
+ export interface IconLabelledRect extends LabelledRect {
151
+ icon: string;
152
+ iconFontFamily: string;
153
+ iconFontSize: number;
154
+ }
155
+
156
+ export const LabelledRect: React.FunctionComponent<LabelledRect> = ({
157
+ text,
158
+ height = 12,
159
+ width = 12,
160
+ fontFamily = "Verdana",
161
+ fontSize = 10,
162
+ padding = 3,
163
+ fill = "whitesmoke",
164
+ stroke = "lightgray",
165
+ textFill = "black",
166
+ strokeWidth = 1,
167
+ cornerRadius = 0,
168
+ onSizeUpdate = (size: { width: number, height: number }) => { }
169
+ }) => {
170
+ return <>
171
+ <Rectangle
172
+ width={width}
173
+ height={height}
174
+ fill={fill}
175
+ stroke={stroke}
176
+ strokeWidth={strokeWidth}
177
+ cornerRadius={cornerRadius}
178
+ />
179
+ <g transform={`translate(${-(width / 2) + padding} ${-(height / 2) + padding})`}>
180
+ <TextLine
181
+ text={text}
182
+ fontFamily={fontFamily}
183
+ height={fontSize}
184
+ fill={textFill}
185
+ anchor="start"
186
+ baseline="hanging"
187
+ />
188
+ </g>
189
+ </>;
190
+ };
191
+
192
+ export const IconLabelledRect: React.FunctionComponent<IconLabelledRect> = ({
193
+ icon,
194
+ iconFontFamily,
195
+ text,
196
+ height = 12,
197
+ width = 12,
198
+ fontFamily = "Verdana",
199
+ fontSize = 10,
200
+ padding = 3,
201
+ fill = "whitesmoke",
202
+ stroke = "lightgray",
203
+ textFill = "black",
204
+ strokeWidth = 1,
205
+ cornerRadius = 0,
206
+ onSizeUpdate = (size: { width: number, height: number }) => { }
207
+ }) => {
208
+ return <>
209
+ <Rectangle
210
+ width={width}
211
+ height={height}
212
+ fill={fill}
213
+ stroke={stroke}
214
+ strokeWidth={strokeWidth}
215
+ cornerRadius={cornerRadius}
216
+ />
217
+ <g transform={`translate(${-(width / 2) + padding} ${-(height / 2) + padding})`}>
218
+ <Icon
219
+ shape="square"
220
+ imageFontFamily={iconFontFamily}
221
+ imageChar={icon}
222
+ height={height}
223
+ fill={fill}
224
+ imageCharFill={textFill}
225
+ />
226
+ </g>
227
+ <g transform={`translate(${-(width / 2) + (padding * 2) + height} ${-(height / 2) + padding})`}>
228
+ <TextLine
229
+ text={text}
230
+ fontFamily={fontFamily}
231
+ height={fontSize}
232
+ fill={textFill}
233
+ anchor="start"
234
+ baseline="hanging"
235
+ />
236
+ </g>
237
+ </>;
238
+ };