-
Notifications
You must be signed in to change notification settings - Fork 689
/
Text.js
170 lines (152 loc) · 4.81 KB
/
Text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import reduceCSSCalc from 'reduce-css-calc';
import getStringWidth from './util/getStringWidth';
class Text extends Component {
constructor(props) {
super(props);
this.state = {
wordsByLines: []
};
}
componentWillMount() {
this.updateWordsByLines(this.props, true);
}
componentWillReceiveProps(nextProps) {
const needCalculate =
this.props.children !== nextProps.children || this.props.style !== nextProps.style;
this.updateWordsByLines(nextProps, needCalculate);
}
updateWordsByLines(props, needCalculate) {
// Only perform calculations if using features that require them (multiline, scaleToFit)
if (props.width || props.scaleToFit) {
if (needCalculate) {
const words = props.children ? props.children.toString().split(/(?:(?!\u00A0+)\s+)/) : [];
this.wordsWithComputedWidth = words.map(word => ({
word,
width: getStringWidth(word, props.style)
}));
this.spaceWidth = getStringWidth('\u00A0', props.style);
}
const wordsByLines = this.calculateWordsByLines(
this.wordsWithComputedWidth,
this.spaceWidth,
props.width
);
this.setState({ wordsByLines });
} else {
this.updateWordsWithoutCalculate(props);
}
}
updateWordsWithoutCalculate(props) {
const words = props.children ? props.children.toString().split(/(?:(?!\u00A0+)\s+)/) : [];
this.setState({ wordsByLines: [{ words }] });
}
calculateWordsByLines(wordsWithComputedWidth, spaceWidth, lineWidth) {
const { scaleToFit } = this.props;
return wordsWithComputedWidth.reduce((result, { word, width }) => {
const currentLine = result[result.length - 1];
if (
currentLine &&
(lineWidth == null || scaleToFit || currentLine.width + width + spaceWidth < lineWidth)
) {
// Word can be added to an existing line
currentLine.words.push(word);
currentLine.width += width + spaceWidth;
} else {
// Add first word to line or word is too long to scaleToFit on existing line
const newLine = { words: [word], width };
result.push(newLine);
}
return result;
}, []);
}
render() {
const {
dx,
dy,
textAnchor,
verticalAnchor,
scaleToFit,
angle,
lineHeight,
capHeight,
innerRef,
...textProps
} = this.props;
const { wordsByLines } = this.state;
const { x, y } = textProps;
let startDy;
switch (verticalAnchor) {
case 'start':
startDy = reduceCSSCalc(`calc(${capHeight})`);
break;
case 'middle':
startDy = reduceCSSCalc(
`calc(${(wordsByLines.length - 1) / 2} * -${lineHeight} + (${capHeight} / 2))`
);
break;
default:
startDy = reduceCSSCalc(`calc(${wordsByLines.length - 1} * -${lineHeight})`);
break;
}
const transforms = [];
if (scaleToFit && wordsByLines.length) {
const lineWidth = wordsByLines[0].width;
const sx = this.props.width / lineWidth;
const sy = sx;
const originX = x - sx * x;
const originY = y - sy * y;
transforms.push(`matrix(${sx}, 0, 0, ${sy}, ${originX}, ${originY})`);
}
if (angle) {
transforms.push(`rotate(${angle}, ${x}, ${y})`);
}
if (transforms.length) {
textProps.transform = transforms.join(' ');
}
return (
<svg
ref={innerRef}
x={dx}
y={dy}
fontSize={textProps.fontSize}
style={{ overflow: 'visible' }}
>
<text {...textProps} textAnchor={textAnchor}>
{wordsByLines.map((line, index) => (
<tspan x={x} dy={index === 0 ? startDy : lineHeight} key={index}>
{line.words.join(' ')}
</tspan>
))}
</text>
</svg>
);
}
}
Text.defaultProps = {
x: 0,
y: 0,
dx: 0,
dy: 0,
lineHeight: '1em',
capHeight: '0.71em', // Magic number from d3
scaleToFit: false,
textAnchor: 'start',
verticalAnchor: 'end' // default SVG behavior
};
Text.propTypes = {
scaleToFit: PropTypes.bool,
angle: PropTypes.number,
textAnchor: PropTypes.oneOf(['start', 'middle', 'end', 'inherit']),
verticalAnchor: PropTypes.oneOf(['start', 'middle', 'end']),
style: PropTypes.object,
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
x: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
y: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
dx: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
dy: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
lineHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
capHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
};
export default Text;