forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangleSelection.ts
More file actions
448 lines (382 loc) · 13 KB
/
rectangleSelection.ts
File metadata and controls
448 lines (382 loc) · 13 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* @class RectangleSelection
* @classdesc Manages Block selection with mouse
*
* @module RectangleSelection
* @version 1.0.0
*/
import Module from '../__module';
import $ from '../dom';
import SelectionUtils from '../selection';
import Block from '../block';
export default class RectangleSelection extends Module {
/**
* CSS classes for the Block
* @return {{wrapper: string, content: string}}
*/
static get CSS() {
return {
overlay: 'codex-editor-overlay',
overlayContainer: 'codex-editor-overlay__container',
rect: 'codex-editor-overlay__rectangle',
topScrollZone: 'codex-editor-overlay__scroll-zone--top',
bottomScrollZone: 'codex-editor-overlay__scroll-zone--bottom',
};
}
/**
* Using the selection rectangle
* @type {boolean}
*/
private isRectSelectionActivated: boolean = false;
/**
* Speed of Scrolling
*/
private readonly SCROLL_SPEED: number = 3;
/**
* Height of scroll zone on boundary of screen
*/
private readonly HEIGHT_OF_SCROLL_ZONE = 40;
/**
* Scroll zone type indicators
*/
private readonly BOTTOM_SCROLL_ZONE = 1;
private readonly TOP_SCROLL_ZONE = 2;
/**
* Id of main button for event.button
*/
private readonly MAIN_MOUSE_BUTTON = 0;
/**
* Mouse is clamped
*/
private mousedown: boolean = false;
/**
* Is scrolling now
*/
private isScrolling: boolean = false;
/**
* Mouse is in scroll zone
*/
private inScrollZone: number | null = null;
/**
* Coords of rect
*/
private startX: number = 0;
private startY: number = 0;
private mouseX: number = 0;
private mouseY: number = 0;
/**
* Selected blocks
*/
private stackOfSelected: number[] = [];
/**
* Does the rectangle intersect blocks
*/
private rectCrossesBlocks: boolean;
/**
* Selection rectangle
*/
private overlayRectangle: HTMLDivElement;
/**
* Module Preparation
* Creating rect and hang handlers
*/
public prepare(): void {
const {Listeners} = this.Editor;
const {container} = this.genHTML();
Listeners.on(container, 'mousedown', (event: MouseEvent) => {
if (event.button !== this.MAIN_MOUSE_BUTTON) {
return;
}
this.startSelection(event.pageX, event.pageY);
}, false);
Listeners.on(document.body, 'mousemove', (event: MouseEvent) => {
this.changingRectangle(event);
this.scrollByZones(event.clientY);
}, false);
Listeners.on(document.body, 'mouseleave', () => {
this.clearSelection();
this.endSelection();
});
Listeners.on(window, 'scroll', (event) => {
this.changingRectangle(event);
}, false);
Listeners.on(document.body, 'mouseup', () => {
this.endSelection();
}, false);
}
/**
* Init rect params
* @param {number} pageX - X coord of mouse
* @param {number} pageY - Y coord of mouse
*/
public startSelection(pageX, pageY) {
const elemWhereSelectionStart = document.elementFromPoint(pageX - window.pageXOffset, pageY - window.pageYOffset);
/**
* Don't clear selected block by clicks on the Block settings
* because we need to keep highlighting working block
*/
const startsInsideToolbar = elemWhereSelectionStart.closest(`.${this.Editor.Toolbar.CSS.toolbar}`);
if (!startsInsideToolbar) {
this.Editor.BlockSelection.allBlocksSelected = false;
this.clearSelection();
this.stackOfSelected = [];
}
const selectorsToAvoid = [
`.${Block.CSS.content}`,
`.${this.Editor.Toolbar.CSS.toolbar}`,
`.${this.Editor.InlineToolbar.CSS.inlineToolbar}`,
];
const startsInsideEditor = elemWhereSelectionStart.closest('.' + this.Editor.UI.CSS.editorWrapper);
const startsInSelectorToAvoid = selectorsToAvoid.some(((selector) => !!elemWhereSelectionStart.closest(selector)));
/**
* If selection starts outside of the editor or inside the blocks or on Editor UI elements, do not handle it
*/
if (!startsInsideEditor || startsInSelectorToAvoid) {
return;
}
this.mousedown = true;
this.startX = pageX;
this.startY = pageY;
}
/**
* Clear all params to end selection
*/
public endSelection() {
this.mousedown = false;
this.startX = 0;
this.startY = 0;
this.overlayRectangle.style.display = 'none';
}
/**
* is RectSelection Activated
*/
public isRectActivated() {
return this.isRectSelectionActivated;
}
/**
* Mark that selection is end
*/
public clearSelection() {
this.isRectSelectionActivated = false;
}
/**
* Scroll If mouse in scroll zone
* @param {number} clientY - Y coord of mouse
*/
private scrollByZones(clientY) {
this.inScrollZone = null;
if (clientY <= this.HEIGHT_OF_SCROLL_ZONE) {
this.inScrollZone = this.TOP_SCROLL_ZONE;
}
if (document.documentElement.clientHeight - clientY <= this.HEIGHT_OF_SCROLL_ZONE) {
this.inScrollZone = this.BOTTOM_SCROLL_ZONE;
}
if (!this.inScrollZone) {
this.isScrolling = false;
return;
}
if (!this.isScrolling) {
this.scrollVertical(this.inScrollZone === this.TOP_SCROLL_ZONE ? -this.SCROLL_SPEED : this.SCROLL_SPEED);
this.isScrolling = true;
}
}
private genHTML() {
const {UI} = this.Editor;
const container = UI.nodes.holder.querySelector('.' + UI.CSS.editorWrapper);
const overlay = $.make('div', RectangleSelection.CSS.overlay, {});
const overlayContainer = $.make('div', RectangleSelection.CSS.overlayContainer, {});
const overlayRectangle = $.make('div', RectangleSelection.CSS.rect, {});
overlayContainer.appendChild(overlayRectangle);
overlay.appendChild(overlayContainer);
container.appendChild(overlay);
this.overlayRectangle = overlayRectangle as HTMLDivElement;
return {
container,
overlay,
};
}
/**
* Activates scrolling if blockSelection is active and mouse is in scroll zone
* @param {number} speed - speed of scrolling
*/
private scrollVertical(speed) {
if (!(this.inScrollZone && this.mousedown)) {
return;
}
const lastOffset = window.pageYOffset;
window.scrollBy(0, speed);
this.mouseY += window.pageYOffset - lastOffset;
setTimeout(() => {
this.scrollVertical(speed);
}, 0);
}
/**
* Handles the change in the rectangle and its effect
* @param {MouseEvent} event
*/
private changingRectangle(event) {
if (!this.mousedown) {
return;
}
if (event.pageY !== undefined) {
this.mouseX = event.pageX;
this.mouseY = event.pageY;
}
const {rightPos, leftPos, index} = this.genInfoForMouseSelection();
// There is not new block in selection
const rectIsOnRighSideOfredactor = this.startX > rightPos && this.mouseX > rightPos;
const rectISOnLeftSideOfRedactor = this.startX < leftPos && this.mouseX < leftPos;
this.rectCrossesBlocks = !(rectIsOnRighSideOfredactor || rectISOnLeftSideOfRedactor);
if (!this.isRectSelectionActivated) {
this.rectCrossesBlocks = false;
this.isRectSelectionActivated = true;
this.shrinkRectangleToPoint();
this.overlayRectangle.style.display = 'block';
}
this.updateRectangleSize();
if (index === undefined) {
return;
}
this.trySelectNextBlock(index);
// For case, when rect is out from blocks
this.inverseSelection();
SelectionUtils.get().removeAllRanges();
event.preventDefault();
}
/**
* Shrink rect to singular point
*/
private shrinkRectangleToPoint() {
this.overlayRectangle.style.left = `${this.startX - window.pageXOffset}px`;
this.overlayRectangle.style.top = `${this.startY - window.pageYOffset}px`;
this.overlayRectangle.style.bottom = `calc(100% - ${this.startY - window.pageYOffset}px`;
this.overlayRectangle.style.right = `calc(100% - ${this.startX - window.pageXOffset}px`;
}
/**
* Select or unselect all of blocks in array if rect is out or in selectable area
*/
private inverseSelection() {
const firstBlockInStack = this.Editor.BlockManager.getBlockByIndex(this.stackOfSelected[0]);
const isSelecteMode = firstBlockInStack.selected;
if (this.rectCrossesBlocks && !isSelecteMode) {
for (const it of this.stackOfSelected) {
this.Editor.BlockSelection.selectBlockByIndex(it);
}
}
if (!this.rectCrossesBlocks && isSelecteMode) {
for (const it of this.stackOfSelected) {
this.Editor.BlockSelection.unSelectBlockByIndex(it);
}
}
}
/**
* Updates size of rectangle
*/
private updateRectangleSize() {
// Depending on the position of the mouse relative to the starting point,
// change this.e distance from the desired edge of the screen*/
if (this.mouseY >= this.startY) {
this.overlayRectangle.style.top = `${this.startY - window.pageYOffset}px`;
this.overlayRectangle.style.bottom = `calc(100% - ${this.mouseY - window.pageYOffset}px`;
} else {
this.overlayRectangle.style.bottom = `calc(100% - ${this.startY - window.pageYOffset}px`;
this.overlayRectangle.style.top = `${this.mouseY - window.pageYOffset}px`;
}
if (this.mouseX >= this.startX) {
this.overlayRectangle.style.left = `${this.startX - window.pageXOffset}px`;
this.overlayRectangle.style.right = `calc(100% - ${this.mouseX - window.pageXOffset}px`;
} else {
this.overlayRectangle.style.right = `calc(100% - ${this.startX - window.pageXOffset}px`;
this.overlayRectangle.style.left = `${this.mouseX - window.pageXOffset}px`;
}
}
/**
* Collects information needed to determine the behavior of the rectangle
* @return {number} index - index next Block, leftPos - start of left border of Block, rightPos - right border
*/
private genInfoForMouseSelection() {
const widthOfRedactor = document.body.offsetWidth;
const centerOfRedactor = widthOfRedactor / 2;
const Y = this.mouseY - window.pageYOffset;
const elementUnderMouse = document.elementFromPoint(centerOfRedactor, Y);
const blockInCurrentPos = this.Editor.BlockManager.getBlockByChildNode(elementUnderMouse);
let index;
if (blockInCurrentPos !== undefined) {
index = this.Editor.BlockManager.blocks.findIndex((block) => block.holder === blockInCurrentPos.holder);
}
const contentElement = this.Editor.BlockManager.lastBlock.holder.querySelector('.' + Block.CSS.content);
const centerOfBlock = Number.parseInt(window.getComputedStyle(contentElement).width, 10) / 2;
const leftPos = centerOfRedactor - centerOfBlock;
const rightPos = centerOfRedactor + centerOfBlock;
return {
index,
leftPos,
rightPos,
};
}
/**
* Select block with index index
* @param index - index of block in redactor
*/
private addBlockInSelection(index) {
if (this.rectCrossesBlocks) {
this.Editor.BlockSelection.selectBlockByIndex(index);
}
this.stackOfSelected.push(index);
}
/**
* Adds a block to the selection and determines which blocks should be selected
* @param {object} index - index of new block in the reactor
*/
private trySelectNextBlock(index) {
const sameBlock = this.stackOfSelected[this.stackOfSelected.length - 1] === index;
const sizeStack = this.stackOfSelected.length;
const down = 1, up = -1, undef = 0;
if (sameBlock) {
return;
}
const blockNumbersIncrease = this.stackOfSelected[sizeStack - 1] - this.stackOfSelected[sizeStack - 2] > 0;
const direction = sizeStack <= 1 ? undef : blockNumbersIncrease ? down : up;
const selectionInDownDurection = index > this.stackOfSelected[sizeStack - 1] && direction === down;
const selectionInUpDirection = index < this.stackOfSelected[sizeStack - 1] && direction === up;
const generalSelection = selectionInDownDurection || selectionInUpDirection || direction === undef;
const reduction = !generalSelection;
// When the selection is too fast, some blocks do not have time to be noticed. Fix it.
if (!reduction && (index > this.stackOfSelected[sizeStack - 1] ||
this.stackOfSelected[sizeStack - 1] === undefined)) {
let ind = this.stackOfSelected[sizeStack - 1] + 1 || index;
for (ind; ind <= index; ind++) {
this.addBlockInSelection(ind);
}
return;
}
// for both directions
if (!reduction && (index < this.stackOfSelected[sizeStack - 1])) {
for (let ind = this.stackOfSelected[sizeStack - 1] - 1; ind >= index; ind--) {
this.addBlockInSelection(ind);
}
return;
}
if (!reduction) {
return;
}
let i = sizeStack - 1;
let cmp;
// cmp for different directions
if (index > this.stackOfSelected[sizeStack - 1]) {
cmp = () => index > this.stackOfSelected[i];
} else {
cmp = () => index < this.stackOfSelected[i];
}
// Remove blocks missed due to speed.
// cmp checks if we have removed all the necessary blocks
while (cmp()) {
if (this.rectCrossesBlocks) {
this.Editor.BlockSelection.unSelectBlockByIndex(this.stackOfSelected[i]);
}
this.stackOfSelected.pop();
i--;
}
return;
}
}