-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
tablemouse.js
223 lines (182 loc) · 7.44 KB
/
tablemouse.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tablemouse
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import TableSelection from './tableselection';
import MouseEventsObserver from './tablemouse/mouseeventsobserver';
import { findAncestor } from './utils/common';
import { getTableCellsContainingSelection } from './utils/selection';
/**
* This plugin enables a table cells' selection with the mouse.
* It is loaded automatically by the {@link module:table/table~Table} plugin.
*
* @extends module:core/plugin~Plugin
*/
export default class TableMouse extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'TableMouse';
}
/**
* @inheritDoc
*/
static get requires() {
return [ TableSelection ];
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
// Currently the MouseObserver only handles `mouseup` events.
// TODO move to the engine?
editor.editing.view.addObserver( MouseEventsObserver );
this._enableShiftClickSelection();
this._enableMouseDragSelection();
}
/**
* Enables making cells selection by <kbd>Shift</kbd>+click. Creates a selection from the cell which previously held
* the selection to the cell which was clicked. It can be the same cell, in which case it selects a single cell.
*
* @private
*/
_enableShiftClickSelection() {
const editor = this.editor;
let blockSelectionChange = false;
const tableSelection = editor.plugins.get( TableSelection );
this.listenTo( editor.editing.view.document, 'mousedown', ( evt, domEventData ) => {
if ( !this.isEnabled || !tableSelection.isEnabled ) {
return;
}
if ( !domEventData.domEvent.shiftKey ) {
return;
}
const anchorCell = tableSelection.getAnchorCell() || getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
if ( !anchorCell ) {
return;
}
const targetCell = this._getModelTableCellFromDomEvent( domEventData );
if ( targetCell && haveSameTableParent( anchorCell, targetCell ) ) {
blockSelectionChange = true;
tableSelection.setCellSelection( anchorCell, targetCell );
domEventData.preventDefault();
}
} );
this.listenTo( editor.editing.view.document, 'mouseup', () => {
blockSelectionChange = false;
} );
// We need to ignore a `selectionChange` event that is fired after we render our new table cells selection.
// When downcasting table cells selection to the view, we put the view selection in the last selected cell
// in a place that may not be natively a "correct" location. This is – we put it directly in the `<td>` element.
// All browsers fire the native `selectionchange` event.
// However, all browsers except Safari return the selection in the exact place where we put it
// (even though it's visually normalized). Safari returns `<td><p>^foo` that makes our selection observer
// fire our `selectionChange` event (because the view selection that we set in the first step differs from the DOM selection).
// Since `selectionChange` is fired, we automatically update the model selection that moves it that paragraph.
// This breaks our dear cells selection.
//
// Theoretically this issue concerns only Safari that is the only browser that do normalize the selection.
// However, to avoid code branching and to have a good coverage for this event blocker, I enabled it for all browsers.
//
// Note: I'm keeping the `blockSelectionChange` state separately for shift+click and mouse drag (exact same logic)
// so I don't have to try to analyze whether they don't overlap in some weird cases. Probably they don't.
// But I have other things to do, like writing this comment.
this.listenTo( editor.editing.view.document, 'selectionChange', evt => {
if ( blockSelectionChange ) {
// @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
evt.stop();
}
}, { priority: 'highest' } );
}
/**
* Enables making cells selection by dragging.
*
* The selection is made only on mousemove. Mouse tracking is started on mousedown.
* However, the cells selection is enabled only after the mouse cursor left the anchor cell.
* Thanks to that normal text selection within one cell works just fine. However, you can still select
* just one cell by leaving the anchor cell and moving back to it.
*
* @private
*/
_enableMouseDragSelection() {
const editor = this.editor;
let anchorCell, targetCell;
let beganCellSelection = false;
let blockSelectionChange = false;
const tableSelection = editor.plugins.get( TableSelection );
this.listenTo( editor.editing.view.document, 'mousedown', ( evt, domEventData ) => {
if ( !this.isEnabled || !tableSelection.isEnabled ) {
return;
}
// Make sure to not conflict with the shift+click listener and any other possible handler.
if ( domEventData.domEvent.shiftKey || domEventData.domEvent.ctrlKey || domEventData.domEvent.altKey ) {
return;
}
anchorCell = this._getModelTableCellFromDomEvent( domEventData );
} );
this.listenTo( editor.editing.view.document, 'mousemove', ( evt, domEventData ) => {
if ( !domEventData.domEvent.buttons ) {
return;
}
if ( !anchorCell ) {
return;
}
const newTargetCell = this._getModelTableCellFromDomEvent( domEventData );
if ( newTargetCell && haveSameTableParent( anchorCell, newTargetCell ) ) {
targetCell = newTargetCell;
// Switch to the cell selection mode after the mouse cursor left the anchor cell.
// Switch off only on mouseup (makes selecting a single cell possible).
if ( !beganCellSelection && targetCell != anchorCell ) {
beganCellSelection = true;
}
}
// Yep, not making a cell selection yet. See method docs.
if ( !beganCellSelection ) {
return;
}
blockSelectionChange = true;
tableSelection.setCellSelection( anchorCell, targetCell );
domEventData.preventDefault();
} );
this.listenTo( editor.editing.view.document, 'mouseup', () => {
beganCellSelection = false;
blockSelectionChange = false;
anchorCell = null;
targetCell = null;
} );
// See the explanation in `_enableShiftClickSelection()`.
this.listenTo( editor.editing.view.document, 'selectionChange', evt => {
if ( blockSelectionChange ) {
// @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
evt.stop();
}
}, { priority: 'highest' } );
}
/**
* Returns the model table cell element based on the target element of the passed DOM event.
*
* @private
* @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
* @returns {module:engine/model/element~Element|undefined} Returns the table cell or `undefined`.
*/
_getModelTableCellFromDomEvent( domEventData ) {
// Note: Work with positions (not element mapping) because the target element can be an attribute or other non-mapped element.
const viewTargetElement = domEventData.target;
const viewPosition = this.editor.editing.view.createPositionAt( viewTargetElement, 0 );
const modelPosition = this.editor.editing.mapper.toModelPosition( viewPosition );
const modelElement = modelPosition.parent;
if ( modelElement.is( 'tableCell' ) ) {
return modelElement;
}
return findAncestor( 'tableCell', modelElement );
}
}
function haveSameTableParent( cellA, cellB ) {
return cellA.parent.parent == cellB.parent.parent;
}