forked from kensnyder/quill-image-resize-module
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageResize.js
200 lines (160 loc) · 5.63 KB
/
ImageResize.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
import defaultsDeep from 'lodash/defaultsDeep';
import DefaultOptions from './DefaultOptions';
import { DisplaySize } from './modules/DisplaySize';
import { Toolbar } from './modules/Toolbar';
import { Resize } from './modules/Resize';
const knownModules = { DisplaySize, Toolbar, Resize };
/**
* Custom module for quilljs to allow user to resize <img> elements
* (Works on Chrome, Edge, Safari and replaces Firefox's native resize behavior)
* @see https://quilljs.com/blog/building-a-custom-module/
*/
export default class ImageResize {
constructor(quill, options = {}) {
// save the quill reference and options
this.quill = quill;
// Apply the options to our defaults, and stash them for later
// defaultsDeep doesn't do arrays as you'd expect, so we'll need to apply the classes array from options separately
let moduleClasses = false;
if (options.modules) {
moduleClasses = options.modules.slice();
}
// Apply options to default options
this.options = defaultsDeep({}, options, DefaultOptions);
// (see above about moduleClasses)
if (moduleClasses !== false) {
this.options.modules = moduleClasses;
}
// disable native image resizing on firefox
document.execCommand('enableObjectResizing', false, 'false');
// respond to clicks inside the editor
this.quill.root.addEventListener('click', this.handleClick, false);
this.quill.root.parentNode.style.position = this.quill.root.parentNode.style.position || 'relative';
// setup modules
this.moduleClasses = this.options.modules;
this.modules = [];
}
initializeModules = () => {
this.removeModules();
this.modules = this.moduleClasses.map(
ModuleClass => new (knownModules[ModuleClass] || ModuleClass)(this),
);
this.modules.forEach(
(module) => {
module.onCreate();
},
);
this.onUpdate();
};
onUpdate = () => {
this.repositionElements();
this.modules.forEach(
(module) => {
module.onUpdate();
},
);
};
removeModules = () => {
this.modules.forEach(
(module) => {
module.onDestroy();
},
);
this.modules = [];
};
handleClick = (evt) => {
if (evt.target && evt.target.tagName && evt.target.tagName.toUpperCase() === 'IMG') {
if (this.img === evt.target) {
// we are already focused on this image
return;
}
if (this.img) {
// we were just focused on another image
this.hide();
}
// clicked on an image inside the editor
this.show(evt.target);
} else if (this.img) {
// clicked on a non image
this.hide();
}
};
show = (img) => {
// keep track of this img element
this.img = img;
this.showOverlay();
this.initializeModules();
};
showOverlay = () => {
if (this.overlay) {
this.hideOverlay();
}
this.quill.setSelection(null);
// prevent spurious text selection
this.setUserSelect('none');
// listen for the image being deleted or moved
document.addEventListener('keyup', this.checkImage, true);
this.quill.root.addEventListener('input', this.checkImage, true);
// Create and add the overlay
this.overlay = document.createElement('div');
Object.assign(this.overlay.style, this.options.overlayStyles);
this.quill.root.parentNode.appendChild(this.overlay);
this.repositionElements();
};
hideOverlay = () => {
if (!this.overlay) {
return;
}
// Remove the overlay
this.quill.root.parentNode.removeChild(this.overlay);
this.overlay = undefined;
// stop listening for image deletion or movement
document.removeEventListener('keyup', this.checkImage);
this.quill.root.removeEventListener('input', this.checkImage);
// reset user-select
this.setUserSelect('');
};
repositionElements = () => {
if (!this.overlay || !this.img) {
return;
}
// position the overlay over the image
const parent = this.quill.root.parentNode;
const imgRect = this.img.getBoundingClientRect();
const containerRect = parent.getBoundingClientRect();
Object.assign(this.overlay.style, {
left: `${imgRect.left - containerRect.left - 1 + parent.scrollLeft}px`,
top: `${imgRect.top - containerRect.top + parent.scrollTop}px`,
width: `${imgRect.width}px`,
height: `${imgRect.height}px`,
});
};
hide = () => {
this.hideOverlay();
this.removeModules();
this.img = undefined;
};
setUserSelect = (value) => {
[
'userSelect',
'mozUserSelect',
'webkitUserSelect',
'msUserSelect',
].forEach((prop) => {
// set on contenteditable element and <html>
this.quill.root.style[prop] = value;
document.documentElement.style[prop] = value;
});
};
checkImage = (evt) => {
if (this.img) {
if (evt.keyCode == 46 || evt.keyCode == 8) {
window.Quill.find(this.img).deleteAt(0);
}
this.hide();
}
};
}
if (window.Quill) {
window.Quill.register('modules/imageResize', ImageResize);
}