-
Notifications
You must be signed in to change notification settings - Fork 0
/
paste-template.js
305 lines (282 loc) · 14.1 KB
/
paste-template.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
302
303
304
305
const DRAG_ANCHOR = {
NONE: 0,
TOP_LEFT: 1,
LEFT: 2,
BOTTOM_LEFT: 3,
BOTTOM: 4,
BOTTOM_RIGHT: 5,
RIGHT: 6,
TOP_RIGHT: 7,
TOP: 8,
}
class PasteToolbar extends HTMLElement {
constructor() {
super();
this.addEventListener('mouseenter', () => this.classList.remove('hidden'));
this.addEventListener('mouseleave', () => this.classList.add('hidden'));
this.cropButton_ = new CropButton('Crop');
this.resizeButton_ = new ResizeButton('Resize');
this.classList.add('hidden');
this.append(this.cropButton_, this.resizeButton_);
}
}
window.customElements.define('paste-toolbar', PasteToolbar);
class PasteToolbarButton extends HTMLElement {
constructor(buttonContents) {
super();
this.append(buttonContents);
this.addEventListener('click', () => {
document.querySelector('body-overlay').classList.remove('hidden');
document.querySelector('paste-overlay').classList.remove('hidden');
});
}
}
window.customElements.define('paste-toolbar-button', PasteToolbarButton);
class CropButton extends PasteToolbarButton {
constructor(buttonContents) {
super(buttonContents);
this.addEventListener('click', () => document.cropMode = true);
}
}
window.customElements.define('crop-button', CropButton);
class ResizeButton extends PasteToolbarButton {
constructor(buttonContents) {
super(buttonContents);
this.addEventListener('click', () => document.resizeMode = true);
}
}
window.customElements.define('resize-button', ResizeButton);
class PasteOverlay extends HTMLElement {
constructor(left, top, width, height) {
super();
this.style.left = left + 'px';
this.style.top = top + 'px';
this.style.width = width + 'px';
this.style.height = height + 'px';
this.classList.add('hidden');
this.dragAnchor_ = DRAG_ANCHOR.NONE;
}
get dragAnchor() {
return this.dragAnchor_;
}
updateDragAnchor(mousePosX, mousePosY) {
const TOLERANCE = 5;
let boundingRect = this.getBoundingClientRect();
let left = Math.round(boundingRect.left);
let right = Math.round(boundingRect.right);
let top = Math.round(boundingRect.top);
let bottom = Math.round(boundingRect.bottom);
if (Math.abs(left - mousePosX) <= TOLERANCE && Math.abs(top - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.TOP_LEFT;
} else if (Math.abs(left - mousePosX) <= TOLERANCE && Math.abs(bottom - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.BOTTOM_LEFT;
} else if (Math.abs(right - mousePosX) <= TOLERANCE && Math.abs(bottom - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.BOTTOM_RIGHT;
} else if (Math.abs(right - mousePosX) <= TOLERANCE && Math.abs(top - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.TOP_RIGHT;
} else if (Math.abs(left - mousePosX) <= TOLERANCE && top - TOLERANCE < mousePosY && mousePosY < bottom + TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.LEFT;
} else if (left - TOLERANCE < mousePosX && mousePosX < right + TOLERANCE && Math.abs(bottom - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.BOTTOM;
} else if (Math.abs(right - mousePosX) <= TOLERANCE && top - TOLERANCE < mousePosY && mousePosY < bottom + TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.RIGHT;
} else if (left - TOLERANCE < mousePosX && mousePosX < right + TOLERANCE && Math.abs(top - mousePosY) <= TOLERANCE) {
this.dragAnchor_ = DRAG_ANCHOR.TOP;
} else {
this.dragAnchor_ = DRAG_ANCHOR.NONE;
}
}
resetDragAnchor() {
this.dragAnchor_ = DRAG_ANCHOR.NONE;
}
adjustForDrag(mousePosX, mousePosY) {
switch (this.dragAnchor_) {
case DRAG_ANCHOR.TOP_LEFT: {
let xDiff = mousePosX - this.offsetLeft;
let yDiff = mousePosY - this.offsetTop;
this.style.width = (this.offsetWidth - xDiff -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
this.style.height = (this.offsetHeight - yDiff -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
this.style.left = mousePosX + 'px';
this.style.top = mousePosY + 'px';
break;
}
case DRAG_ANCHOR.BOTTOM_LEFT: {
let xDiff = mousePosX - this.offsetLeft;
let yRemainder = mousePosY - this.offsetTop;
this.style.width = (this.offsetWidth - xDiff -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
this.style.height = (yRemainder -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
this.style.left = mousePosX + 'px';
break;
}
case DRAG_ANCHOR.BOTTOM_RIGHT: {
let xRemainder = mousePosX - this.offsetLeft;
let yRemainder = mousePosY - this.offsetTop;
this.style.width = (xRemainder -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
this.style.height = (yRemainder -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
break;
}
case DRAG_ANCHOR.TOP_RIGHT: {
let xRemainder = mousePosX - this.offsetLeft;
let yDiff = mousePosY - this.offsetTop;
this.style.width = (xRemainder -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
this.style.height = (this.offsetHeight - yDiff -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
this.style.top = mousePosY + 'px';
break;
}
case DRAG_ANCHOR.LEFT: {
let xDiff = mousePosX - this.offsetLeft;
this.style.width = (this.offsetWidth - xDiff -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
this.style.left = mousePosX + 'px';
break;
}
case DRAG_ANCHOR.TOP: {
let yDiff = mousePosY - this.offsetTop;
this.style.height = (this.offsetHeight - yDiff -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
this.style.top = mousePosY + 'px';
break;
}
case DRAG_ANCHOR.RIGHT: {
let xRemainder = mousePosX - this.offsetLeft;
this.style.width = (xRemainder -
window.getComputedStyle(this).paddingLeft.replace('px', '') -
window.getComputedStyle(this).paddingRight.replace('px', '') -
window.getComputedStyle(this).borderLeftWidth.replace('px', '') -
window.getComputedStyle(this).borderRightWidth.replace('px', '')) + 'px';
break;
}
case DRAG_ANCHOR.BOTTOM: {
let yRemainder = mousePosY - this.offsetTop;
this.style.height = (yRemainder -
window.getComputedStyle(this).paddingTop.replace('px', '') -
window.getComputedStyle(this).paddingBottom.replace('px', '') -
window.getComputedStyle(this).borderTopWidth.replace('px', '') -
window.getComputedStyle(this).borderBottomWidth.replace('px', '')) + 'px';
break;
}
}
}
}
window.customElements.define('paste-overlay', PasteOverlay);
class BodyOverlay extends HTMLElement {
constructor() {
super();
this.classList.add('hidden');
}
}
window.customElements.define('body-overlay', BodyOverlay);
document.addEventListener('keydown', (event) => {
switch (event.key) {
case 'Enter': {
if (document.cropMode) {
// TODO: Broken for crop from top and left.
let pasteOverlay = document.querySelector('paste-overlay');
document.pasteContainer.style.position = 'absolute';
document.pasteContainer.style.left = pasteOverlay.offsetLeft + 'px';
document.pasteContainer.style.top = pasteOverlay.offsetTop + 'px';
document.pasteContainer.style.width = pasteOverlay.offsetWidth + 'px';
document.pasteContainer.style.height = pasteOverlay.offsetHeight + 'px';
}
// fallthrough
}
case 'Escape': {
document.querySelector('body-overlay').classList.add('hidden');
document.querySelector('paste-overlay').classList.add('hidden');
document.cropMode = false;
document.resizeMode = false;
break;
}
}
});
function pasteCallback(pasteEvent) {
pasteEvent.preventDefault();
let selection = window.getSelection();
let range = selection.getRangeAt(0);
range.deleteContents();
document.pasteContainer = getPasteHTML();
range.insertNode(document.pasteContainer);
range.collapse();
selection.removeAllRanges();
selection.addRange(range);
let pasteOverlay = new PasteOverlay(document.pasteContainer.offsetLeft, document.pasteContainer.offsetTop,
document.pasteContainer.offsetWidth, document.pasteContainer.offsetHeight);
document.body.append(pasteOverlay);
document.pasteContainer.addEventListener('mouseenter', (mouseEvent) => {
let pasteToolbar = document.querySelector('paste-toolbar');
pasteToolbar.classList.remove('hidden');
pasteToolbar.style.left = mouseEvent.clientX + 'px';
pasteToolbar.style.top = mouseEvent.clientY + 'px';
});
document.pasteContainer.addEventListener('mouseleave', () => {
let pasteToolbar = document.querySelector('paste-toolbar');
pasteToolbar.classList.add('hidden');
});
document.addEventListener('mousedown', (mouseEvent) => {
let pasteOverlay = document.querySelector('paste-overlay');
pasteOverlay.updateDragAnchor(mouseEvent.clientX, mouseEvent.clientY);
});
document.addEventListener('mousemove', (mouseEvent) => {
let pasteOverlay = document.querySelector('paste-overlay');
pasteOverlay.adjustForDrag(mouseEvent.clientX, mouseEvent.clientY);
if (document.resizeMode) {
// TODO: Broken for resize from top and left.
if (pasteOverlay.dragAnchor != DRAG_ANCHOR.NONE) {
let xScaleFactor = pasteOverlay.offsetWidth / document.pasteContainer.offsetWidth;
let yScaleFactor = pasteOverlay.offsetHeight / document.pasteContainer.offsetHeight;
document.pasteContainer.style.transformOrigin = 'top left';
document.pasteContainer.style.transform = 'scale(' + xScaleFactor + ',' + yScaleFactor + ')';
}
}
});
document.addEventListener('mouseup', () => {
document.querySelector('paste-overlay').resetDragAnchor();
});
}
let header = document.createElement('h3');
header.append('Paste from Smart Copy into container below:');
let ceContainer = document.createElement('div');
ceContainer.setAttribute('id', 'ceContainer');
ceContainer.setAttribute('contenteditable', 'true');
ceContainer.addEventListener('paste', pasteCallback);
let pasteToolbar = new PasteToolbar();
let bodyOverlay = new BodyOverlay();
document.body.append(header, ceContainer, pasteToolbar, bodyOverlay);