This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
/
text-tool.js
301 lines (257 loc) · 10.7 KB
/
text-tool.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const React = require("react");
const PropTypes = require("prop-types");
const { Localized } = require("fluent-react/compat");
const sendEvent = require("../../browser-send-event.js");
const { Selection } = require("../../../shared/selection");
const { ColorPicker } = require("./color-picker");
// Text input height is determined by text size and padding
// See frame.scss mixin type-box($size)
const TEXT_INPUT_PADDING = 4;
// Padding used by draggable div
const DRAG_DIV_PADDING = 10;
// Editor header height in pixel
const EDITOR_HEADER_HEIGHT = 100;
const FONT_STYLE = "sans-serif";
const FONT_WEIGHT = 900;
const INIT_FONT_SIZE = 36;
// Text Input drag limit from the edge of the canvas
const TEXT_DRAG_EDGE_LIMIT = 5;
let previousTextInputWidth;
let hasFirstInput;
let dragMouseDown = false;
let prevDragMousePos = null;
exports.TextTool = class TextTool extends React.Component {
constructor(props) {
super(props);
this.el = React.createRef();
this.textInput = React.createRef();
this.canvasCssWidth = props.canvasCssWidth;
this.canvasCssHeight = props.canvasCssHeight;
const VISIBLE_HEIGHT = this.canvasCssHeight > window.innerHeight ? window.innerHeight - EDITOR_HEADER_HEIGHT :
this.canvasCssHeight;
const INIT_LEFT = Math.floor((this.canvasCssWidth / 2) - (INIT_FONT_SIZE / 2));
const INIT_TOP = window.scrollY + Math.floor((VISIBLE_HEIGHT / 2) - ((INIT_FONT_SIZE) / 2) - TEXT_INPUT_PADDING);
this.state = {
left: INIT_LEFT,
top: INIT_TOP,
color: this.props.color,
colorName: this.props.colorName,
textSize: "large-text"
};
}
componentDidMount() {
// Set hidden div to placeholder text and has first edit happened flag to false. hasFirstInput
// is used in adjustX to avoid setting hidden div textContent to empty textInput value
hasFirstInput = false;
this.textInput.current.nextSibling.textContent = this.textInput.current.placeholder;
this.textInput.current.focus();
this.textInput.current.nextSibling.style.maxWidth = `${this.canvasCssWidth - 2 * TEXT_DRAG_EDGE_LIMIT}px`;
this.adjustWidth();
previousTextInputWidth = this.textInput.current.clientWidth;
if (this.props.toolbarOverrideCallback) {
this.props.toolbarOverrideCallback();
}
}
componentDidUpdate(oldProps, oldState) {
if (oldState.textSize !== this.state.textSize) {
this.props.toolbarOverrideCallback();
this.adjustX();
}
}
componentWillUnmount() {
if (this.props.toolbarOverrideCallback) {
this.props.toolbarOverrideCallback();
}
}
render() {
const textToolBox = this.renderTextBox();
return <div
ref={this.el}
className={`text-tool-container centered`}
style={{height: this.props.baseCanvas.style.height, width: this.props.baseCanvas.style.width}}>
{textToolBox}
</div>;
}
renderTextBox() {
const dragDivTopPx = `${this.state.top - DRAG_DIV_PADDING}px`;
const dragDivLeftPx = `${this.state.left - DRAG_DIV_PADDING}px`;
const dragDivPadding = `${DRAG_DIV_PADDING}px`;
const dragDivStyles = {
position: "absolute",
top: dragDivTopPx,
left: dragDivLeftPx,
padding: dragDivPadding,
cursor: "move"
};
// Styles used by hidden div that is used to compute width of input
// text for respective font-size
const hiddenDivStyles = {
position: "absolute",
zIndex: "-999",
visibility: "hidden"
};
return [
<div key="drag" style={dragDivStyles} onMouseDown={this.onDragMouseDown.bind(this)}>
<Localized id="textToolInputPlaceholder">
<input type="text" id="text-input" ref={this.textInput} key="text" maxLength="1000" placeholder="Hello"
onInput={this.onInput.bind(this)} className={`${this.state.textSize} ${this.state.colorName} text`}>
</input>
</Localized>
<div id="text-width" style={hiddenDivStyles} className={`${this.state.textSize} text`} key="text-width"></div>
</div>
];
}
renderToolbar() {
return <div className="editor-header default-color-scheme"><div className="annotation-tools">
<Localized id="annotationTextSize">
<select className={`text-select`} title="Text Size" onChange={this.onChangeTextSize.bind(this)} value={this.state.textSize}>
<Localized id="textSizeSmall"><option value="small-text">Small</option></Localized>
<Localized id="textSizeMedium"><option value="medium-text">Medium</option></Localized>
<Localized id="textSizeLarge"><option value="large-text">Large</option></Localized>
</select>
</Localized>
<ColorPicker activeTool={this.state.tool}
setColorCallback={this.setColor.bind(this)}
color={this.state.color} />
<span className="annotation-divider"></span>
<Localized id="annotationTextConfirmButton">
<button className={`button transparent confirm-text`} id="confirm-text" onClick={this.onClickConfirm.bind(this)} title="Confirm Text">Confirm</button>
</Localized>
<Localized id="annotationTextCancelButton">
<button className={`button transparent cancel-text`} id="cancel-text" onClick={this.onClickCancel.bind(this)} title="Cancel Text">Cancel</button>
</Localized>
</div></div>;
}
setColor(color, colorName) {
this.setState({color, colorName});
this.textInput.current.focus();
}
onDragMouseDown(e) {
if (e.target === this.textInput.current) {
return;
}
dragMouseDown = true;
prevDragMousePos = this.captureMousePosition(e);
}
onMouseMove(e) {
if (!dragMouseDown) {
return;
}
const mousePos = this.captureMousePosition(e);
const xDelta = mousePos.x - prevDragMousePos.x;
const yDelta = mousePos.y - prevDragMousePos.y;
const maxLeft = this.canvasCssWidth - this.textInput.current.clientWidth - TEXT_DRAG_EDGE_LIMIT;
const maxTop = this.canvasCssHeight - this.textInput.current.clientHeight - TEXT_DRAG_EDGE_LIMIT;
const newLeft = clamp(this.state.left + xDelta, TEXT_DRAG_EDGE_LIMIT, maxLeft);
const newTop = clamp(this.state.top + yDelta, TEXT_DRAG_EDGE_LIMIT, maxTop);
this.setState({
left: newLeft,
top: newTop
});
prevDragMousePos = mousePos;
}
onMouseUp(e) {
dragMouseDown = false;
prevDragMousePos = null;
}
// TODO This also exist in drawing-tool.js and crop-tool.js. Move it to a shared
// space.
captureMousePosition(e) {
const boundingRect = this.el.current.getBoundingClientRect();
return {
x: e.clientX - boundingRect.left,
y: e.clientY - boundingRect.top
};
}
onClickConfirm(e) {
// Exit if user doesn't enter any text
if (!this.textInput.current.value) {
if (this.props.cancelTextHandler) {
this.props.cancelTextHandler();
}
sendEvent("cancel-text", "text-toolbar");
return;
}
const styles = window.getComputedStyle(this.textInput.current);
const FONT_SIZE = parseInt(styles["font-size"], 10);
// Due to line-height differences of how fonts are rendered across platforms
// adjust text y position to one-third of difference of line-height and font-size
const ADJUST_VERTICAL_SHIFT = (parseFloat(styles["line-height"]) - FONT_SIZE) / 3;
const x = this.state.left + parseFloat(styles["padding-left"]);
const y = this.state.top + TEXT_INPUT_PADDING + parseFloat(styles["line-height"]) / 2 + ADJUST_VERTICAL_SHIFT;
const textCanvas = document.createElement("canvas");
textCanvas.width = this.props.baseCanvas.width;
textCanvas.height = this.props.baseCanvas.height;
const drawingContext = textCanvas.getContext("2d");
drawingContext.scale(this.props.canvasPixelRatio, this.props.canvasPixelRatio);
drawingContext.textBaseline = "middle";
drawingContext.fillStyle = styles.backgroundColor;
drawingContext.fillRect(this.state.left,
this.state.top,
this.textInput.current.clientWidth,
this.textInput.current.clientHeight);
drawingContext.fillStyle = styles.color;
drawingContext.font = `${FONT_WEIGHT} ${FONT_SIZE}px ${FONT_STYLE}`;
drawingContext.fillText(this.textInput.current.value, x, y);
const textSelection = new Selection(this.state.left,
this.state.top,
this.state.left + this.textInput.current.clientWidth,
this.state.top + this.textInput.current.clientHeight);
if (this.props.confirmTextHandler) {
this.props.confirmTextHandler(textSelection, textCanvas);
}
sendEvent("confirm-text", "text-toolbar");
}
onClickCancel(e) {
if (this.props.cancelTextHandler) {
this.props.cancelTextHandler();
}
sendEvent("cancel-text", "text-toolbar");
}
onChangeTextSize(event) {
const size = event.target.value;
this.setState({textSize: size});
this.textInput.current.focus();
}
onInput() {
hasFirstInput = true;
this.adjustX();
}
adjustWidth() {
const width = this.textInput.current.nextSibling.clientWidth;
this.textInput.current.style.width = `${width}px`;
}
adjustX() {
const styles = window.getComputedStyle(this.textInput.current);
// Check for text input width not to exceed canvas width, if yes constraint
// text input value to hidden div text content to stop additional text in the input box
if ((this.textInput.current.value.length > this.textInput.current.nextSibling.textContent.length) &&
(this.textInput.current.nextSibling.clientWidth >=
this.canvasCssWidth - (2 * parseFloat(styles.paddingLeft)))) {
this.textInput.current.value = this.textInput.current.nextSibling.textContent;
} else if (hasFirstInput) {
this.textInput.current.nextSibling.textContent = this.textInput.current.value;
}
this.adjustWidth();
const widthDiff = this.textInput.current.clientWidth - previousTextInputWidth;
const maxLeft = this.canvasCssWidth - this.textInput.current.clientWidth + TEXT_DRAG_EDGE_LIMIT;
const newLeft = clamp(this.state.left - widthDiff / 2, TEXT_DRAG_EDGE_LIMIT, maxLeft);
this.setState({left: newLeft});
previousTextInputWidth = this.textInput.current.clientWidth;
}
};
exports.TextTool.propTypes = {
baseCanvas: PropTypes.object,
color: PropTypes.string,
colorName: PropTypes.string,
toolbarOverrideCallback: PropTypes.func,
confirmTextHandler: PropTypes.func,
cancelTextHandler: PropTypes.func,
canvasPixelRatio: PropTypes.number,
canvasCssWidth: PropTypes.number,
canvasCssHeight: PropTypes.number,
};
// TODO This also exist in crop-tool.js. Move it to a shared space.
function clamp(val, min, max) {
return Math.min(Math.max(val, min), max);
}