This repository was archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (57 loc) · 1.73 KB
/
index.js
File metadata and controls
60 lines (57 loc) · 1.73 KB
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
import interact from 'interact.js'
export default class ImageResizeInteract {
constructor(quill) {
this.quill = quill
this.quill.root.addEventListener('click', this.handleClick.bind(this))
this.quill.on('selection-change', this.handleSelectionChange.bind(this))
this.images = []
}
isImage (event) {
return event.target && event.target.tagName && event.target.tagName.toUpperCase() === 'IMG'
}
isAlreadyInteractable (target) {
return interact.isSet(target)
}
handleSelectionChange (range) {
if (range === null) {
this.removeClasses()
}
}
removeClasses () {
this.images.forEach((image) => {
image.parentNode.classList.remove('ql-resizing')
})
this.images = []
}
handleClick (event) {
if (this.isImage(event)) {
if (this.isAlreadyInteractable(event.target)) {
return
} else {
this.images.push(event.target)
this.convertToResizable(event.target)
}
}
}
convertToResizable(image) {
interact(image)
.resizable({
preserveAspectRatio: true,
edges: { left: true, right: true, bottom: true, top: true }
})
.on('resizeend', (event) => {
let target = event.target
target.parentNode.classList.remove('ql-resizing')
target.parentNode.setAttribute('data-size', '')
})
.on('resizemove', (event) => {
let target = event.target
let roundedWidth = Math.round(event.rect.width)
let roundedHeight = Math.round(event.rect.height)
target.parentNode.classList.add('ql-resizing')
target.parentNode.setAttribute('data-size', `${roundedWidth}x${roundedHeight}`)
target.width = roundedWidth
target.height = roundedHeight
})
}
}