-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
253 lines (210 loc) · 6.35 KB
/
index.ts
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
import Konva from 'konva'
import { convertHtmlToText } from '../../../utils/html-to-text'
import { Board } from '../../../Board'
import { ShapeModel } from '../../ShapeModel'
import { isBrowser, isNode } from '../../../utils/detect-environment'
import { rotateAroundCenter } from '../../../utils/rotate-around-center'
import { DrawType, LabelConfig } from '../../../types'
export class LabelModel extends ShapeModel<Konva.Label, Konva.LabelConfig> {
/**
* The configurations of the label
*/
public config: LabelConfig
/**
* Represents whether the label is editing (inline edit) or not
*/
private isEditingEnabled = false
constructor(board: Board, node: Konva.Label, config: LabelConfig = {}) {
super(board, node, config)
this.config = config
node.on('transform', this.transform.bind(this))
node.on('dblclick', this.inlineEdit.bind(this))
}
/**
* @inheritdoc
*/
public get type(): string {
return 'label'
}
/**
* Returns the label is in inline editing mode or not
*/
public get isEditing() {
return this.isEditingEnabled
}
/**
* Returns the text node of the label
*/
public get textNode() {
return this.node.getText() as Konva.Text
}
/**
* Returns the tag node of the label
*/
public get tagNode() {
return this.node.getTag() as Konva.Tag
}
/**
* @inheritdoc
* @override
*/
public rotate(theta: number) {
rotateAroundCenter(this.node, theta)
this.board.events.emit('shape:rotate', {
shapes: [this]
})
}
/**
* Updates attributes of the label's tag
*
* @param attributes The list of attributes
*/
public updateTag(attributes: Partial<Konva.TagConfig>) {
this.board.history.create(this.board.layer, this.tagNode)
this.tagNode.setAttrs(attributes)
}
/**
* Updates attributes of the label's tag
*
* @param attributes The list of attributes
*/
public updateText(attributes: Partial<Konva.TextConfig>) {
this.board.history.create(this.board.layer, this.textNode)
this.textNode.setAttrs(attributes)
this.updateTransformer()
}
/**
* Controls transforming the label
* This function rescales the text node and label node
* to avoid from stretching that
*/
private transform() {
if (
this.config.keepScale === false ||
this.board.selection.transformer.getActiveAnchor() === 'rotater'
) {
return
}
this.textNode.setAttrs({
width: this.node.width() * this.node.scaleX(),
scaleX: this.node.scaleY()
})
this.node.scaleX(this.textNode.scaleY())
this.tagNode.scaleX(this.node.scaleY())
}
/**
* Enables inline editing of the label with double clicking on the node
*
* @param e The [[MouseEvent | Mouse Event]
*/
private inlineEdit(e: Konva.KonvaEventObject<MouseEvent>) {
if (isNode() || this.node.isCached()) {
return
}
e.cancelBubble = true
this.isEditingEnabled = true
this.board.setActiveDrawing(DrawType.Text)
const position = e.target.absolutePosition()
const textBeforeEdit = this.textNode.getAttr('text')
// hide node
this.node.hide()
this.node?.draggable(false)
// deselect all selected nodes
this.board.selection.deselectAll()
const input = document.createElement('span')
this.board.container
?.getElementsByClassName(this.board.settings.containerClassName!)[0]
?.append(input)
if (isBrowser()) {
this.setInputFocus(input)
}
input.setAttribute('contenteditable', '')
input.setAttribute('role', 'textbox')
input.innerText = this.textNode.getAttr('text')
Object.assign(input.style, {
position: 'absolute',
display: 'inline-block',
left: `${position.x - this.textNode.padding()}px`,
top: `${position.y - this.textNode.padding()}px`,
width: `${this.textNode.width() * this.node.scaleX()}px`,
minWidth: `${this.textNode.padding() * 2}px`,
maxWidth: `${this.textNode.width() * this.node.scaleX()}px`,
minHeight: `${this.textNode.height() * this.node.scaleY()}px`,
fontSize: `${this.textNode.fontSize() * this.node.scaleY()}px`,
border: 'none',
padding: `${this.textNode.padding()}px`,
margin: `${this.textNode.padding()}px`,
overflow: 'hidden',
background: this.tagNode.fill(),
borderRadius: `${this.tagNode.cornerRadius()}px`,
outline: 'none',
resize: 'none',
lineHeight: this.textNode.lineHeight(),
fontFamily: this.textNode.fontFamily(),
transformOrigin: 'left top',
textAlign: this.textNode.align(),
color: this.textNode.fill(),
transform: `${input.style.transform} rotateZ(${this.node.rotation()}deg)`
})
input.addEventListener('blur', (e: Event) => {
this.isEditingEnabled = false
input.parentNode?.removeChild(input)
this.board.setActiveDrawing(null)
const newText = convertHtmlToText((<HTMLSpanElement>e.target).innerHTML)
if (newText !== textBeforeEdit) {
this.board.history.create(this.board.layer, [], {
undo: () => this.changeText(textBeforeEdit),
redo: () => this.changeText(newText)
})
}
// update label's text
this.textNode.setText(newText)
this.node.show()
this.node.setAttrs({
draggable: this.board.settings.selection?.interactive,
width: this.textNode.width()
})
// select node
this.board.selection.add(this)
})
}
/**
* Changes the text value of the label
*
* @param value The text value
*/
private changeText(value: string) {
this.textNode.setAttrs({
text: value
})
this.updateTransformer()
this.board.events.emit('label:update-text', {
shapes: [this],
data: {
text: value
}
})
}
/**
* updates transformer after changing text
*/
private updateTransformer() {
if (this.board.selection.isVisible) {
this.board.selection.transformer.forceUpdate()
}
}
/**
* Focuses on the input to start editing that
*
* @param input The [[HTMLSpanElement]]
*/
private async setInputFocus(input: HTMLSpanElement) {
await new Promise(resolve => setTimeout(resolve, 50))
const range = document.createRange()
range.selectNodeContents(input)
range.collapse(false)
const selection = window.getSelection()!
selection.removeAllRanges()
selection.addRange(range)
}
}