|
|
@@ -1,34 +1,41 @@
|
|
1
|
1
|
import { uniqueId } from 'utils/utils'
|
|
2
|
|
-import vec from 'utils/vec'
|
|
3
|
2
|
import { TextShape, ShapeType, FontSize } from 'types'
|
|
4
|
3
|
import { registerShapeUtils } from './index'
|
|
5
|
|
-import { defaultStyle, getFontStyle, getShapeStyle } from 'lib/shape-styles'
|
|
|
4
|
+import {
|
|
|
5
|
+ defaultStyle,
|
|
|
6
|
+ getFontSize,
|
|
|
7
|
+ getFontStyle,
|
|
|
8
|
+ getShapeStyle,
|
|
|
9
|
+} from 'lib/shape-styles'
|
|
6
|
10
|
import styled from 'styles'
|
|
7
|
11
|
import state from 'state'
|
|
8
|
|
-import { useEffect, useRef } from 'react'
|
|
9
|
|
-
|
|
10
|
|
-// A div used for measurement
|
|
|
12
|
+import React from 'react'
|
|
11
|
13
|
|
|
12
|
14
|
if (document.getElementById('__textMeasure')) {
|
|
13
|
15
|
document.getElementById('__textMeasure').remove()
|
|
14
|
16
|
}
|
|
15
|
17
|
|
|
|
18
|
+// A div used for measurement
|
|
16
|
19
|
const mdiv = document.createElement('pre')
|
|
17
|
20
|
mdiv.id = '__textMeasure'
|
|
18
|
21
|
mdiv.style.whiteSpace = 'pre'
|
|
19
|
22
|
mdiv.style.width = 'auto'
|
|
20
|
23
|
mdiv.style.border = '1px solid red'
|
|
21
|
24
|
mdiv.style.padding = '4px'
|
|
22
|
|
-mdiv.style.lineHeight = '1'
|
|
23
|
25
|
mdiv.style.margin = '0px'
|
|
24
|
26
|
mdiv.style.opacity = '0'
|
|
25
|
27
|
mdiv.style.position = 'absolute'
|
|
26
|
28
|
mdiv.style.top = '-500px'
|
|
27
|
29
|
mdiv.style.left = '0px'
|
|
28
|
30
|
mdiv.style.zIndex = '9999'
|
|
|
31
|
+mdiv.tabIndex = -1
|
|
29
|
32
|
mdiv.setAttribute('readonly', 'true')
|
|
30
|
33
|
document.body.appendChild(mdiv)
|
|
31
|
34
|
|
|
|
35
|
+function normalizeText(text: string) {
|
|
|
36
|
+ return text.replace(/\t/g, ' ').replace(/\r?\n|\r/g, '\n')
|
|
|
37
|
+}
|
|
|
38
|
+
|
|
32
|
39
|
const text = registerShapeUtils<TextShape>({
|
|
33
|
40
|
isForeignObject: true,
|
|
34
|
41
|
canChangeAspectRatio: false,
|
|
|
@@ -66,6 +73,51 @@ const text = registerShapeUtils<TextShape>({
|
|
66
|
73
|
|
|
67
|
74
|
const bounds = this.getBounds(shape)
|
|
68
|
75
|
|
|
|
76
|
+ function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
|
|
|
77
|
+ state.send('EDITED_SHAPE', {
|
|
|
78
|
+ change: { text: normalizeText(e.currentTarget.value) },
|
|
|
79
|
+ })
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ function handleKeyDown(e: React.KeyboardEvent) {
|
|
|
83
|
+ e.stopPropagation()
|
|
|
84
|
+ if (e.key === 'Tab') {
|
|
|
85
|
+ e.preventDefault()
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ function handleBlur() {
|
|
|
90
|
+ state.send('BLURRED_EDITING_SHAPE')
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ function handleFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
|
|
|
94
|
+ e.currentTarget.select()
|
|
|
95
|
+ state.send('FOCUSED_EDITING_SHAPE')
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ const lineHeight = getFontSize(shape.fontSize) * shape.scale
|
|
|
99
|
+ const gap = lineHeight * 0.4
|
|
|
100
|
+
|
|
|
101
|
+ if (!isEditing) {
|
|
|
102
|
+ return (
|
|
|
103
|
+ <g id={id} pointerEvents="none">
|
|
|
104
|
+ {text.split('\n').map((str, i) => (
|
|
|
105
|
+ <text
|
|
|
106
|
+ key={i}
|
|
|
107
|
+ x={4}
|
|
|
108
|
+ y={4 + gap / 2 + i * (lineHeight + gap)}
|
|
|
109
|
+ style={{ font }}
|
|
|
110
|
+ width={bounds.width}
|
|
|
111
|
+ height={bounds.height}
|
|
|
112
|
+ dominant-baseline="hanging"
|
|
|
113
|
+ >
|
|
|
114
|
+ {str}
|
|
|
115
|
+ </text>
|
|
|
116
|
+ ))}
|
|
|
117
|
+ </g>
|
|
|
118
|
+ )
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
69
|
121
|
return (
|
|
70
|
122
|
<foreignObject
|
|
71
|
123
|
id={id}
|
|
|
@@ -82,22 +134,17 @@ const text = registerShapeUtils<TextShape>({
|
|
82
|
134
|
font,
|
|
83
|
135
|
color: styles.stroke,
|
|
84
|
136
|
}}
|
|
|
137
|
+ tabIndex={0}
|
|
85
|
138
|
value={text}
|
|
86
|
139
|
autoComplete="false"
|
|
87
|
140
|
autoCapitalize="false"
|
|
88
|
141
|
autoCorrect="false"
|
|
89
|
|
- onFocus={(e) => {
|
|
90
|
|
- e.currentTarget.select()
|
|
91
|
|
- state.send('FOCUSED_EDITING_SHAPE')
|
|
92
|
|
- }}
|
|
93
|
|
- onBlur={() => {
|
|
94
|
|
- state.send('BLURRED_EDITING_SHAPE')
|
|
95
|
|
- }}
|
|
96
|
|
- onChange={(e) => {
|
|
97
|
|
- state.send('EDITED_SHAPE', {
|
|
98
|
|
- change: { text: e.currentTarget.value },
|
|
99
|
|
- })
|
|
100
|
|
- }}
|
|
|
142
|
+ spellCheck="false"
|
|
|
143
|
+ onFocus={handleFocus}
|
|
|
144
|
+ onBlur={handleBlur}
|
|
|
145
|
+ onKeyDown={handleKeyDown}
|
|
|
146
|
+ onChange={handleChange}
|
|
|
147
|
+ dir="auto"
|
|
101
|
148
|
/>
|
|
102
|
149
|
) : (
|
|
103
|
150
|
<StyledText
|
|
|
@@ -115,7 +162,7 @@ const text = registerShapeUtils<TextShape>({
|
|
115
|
162
|
|
|
116
|
163
|
getBounds(shape) {
|
|
117
|
164
|
if (!this.boundsCache.has(shape)) {
|
|
118
|
|
- mdiv.innerHTML = shape.text || ' ' // + ' '
|
|
|
165
|
+ mdiv.innerHTML = shape.text + '‍'
|
|
119
|
166
|
mdiv.style.font = getFontStyle(shape.fontSize, shape.scale, shape.style)
|
|
120
|
167
|
|
|
121
|
168
|
const [minX, minY] = shape.point
|
|
|
@@ -180,14 +227,17 @@ const StyledText = styled('div', {
|
|
180
|
227
|
border: 'none',
|
|
181
|
228
|
padding: '4px',
|
|
182
|
229
|
whiteSpace: 'pre',
|
|
183
|
|
- resize: 'none',
|
|
184
|
230
|
minHeight: 1,
|
|
185
|
231
|
minWidth: 1,
|
|
186
|
|
- outline: 'none',
|
|
|
232
|
+ outline: 0,
|
|
187
|
233
|
backgroundColor: 'transparent',
|
|
188
|
234
|
overflow: 'hidden',
|
|
189
|
235
|
pointerEvents: 'none',
|
|
190
|
236
|
userSelect: 'none',
|
|
|
237
|
+ backfaceVisibility: 'hidden',
|
|
|
238
|
+ display: 'inline-block',
|
|
|
239
|
+ position: 'relative',
|
|
|
240
|
+ zIndex: 0,
|
|
191
|
241
|
})
|
|
192
|
242
|
|
|
193
|
243
|
const StyledTextArea = styled('textarea', {
|
|
|
@@ -199,8 +249,10 @@ const StyledTextArea = styled('textarea', {
|
|
199
|
249
|
resize: 'none',
|
|
200
|
250
|
minHeight: 1,
|
|
201
|
251
|
minWidth: 1,
|
|
202
|
|
- outline: 'none',
|
|
203
|
|
- overflow: 'hidden',
|
|
|
252
|
+ outline: 0,
|
|
204
|
253
|
backgroundColor: '$boundsBg',
|
|
|
254
|
+ overflow: 'hidden',
|
|
205
|
255
|
pointerEvents: 'all',
|
|
|
256
|
+ backfaceVisibility: 'hidden',
|
|
|
257
|
+ display: 'inline-block',
|
|
206
|
258
|
})
|