Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/nodes/ve.ce.TableNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable TableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable table node.
9 *
10 * @class
11 * @extends ve.ce.BranchNode
12 * @constructor
13 * @param {ve.dm.TableNode} model Model to observe
14 * @param {Object} [config] Configuration options
15 */
16 ve.ce.TableNode = function VeCeTableNode() {
17 // Parent constructor
18 ve.ce.TableNode.super.apply( this, arguments );
19
20 // Properties
21 this.surface = null;
22 this.active = false;
23 this.startCell = null;
24 this.endCell = null;
25 // Stores the original table selection as
26 // a fragment when entering cell edit mode
27 this.editingFragment = null;
28
29 // DOM changes
30 this.$element
31 .addClass( 've-ce-tableNode' )
32 .prop( 'contentEditable', 'false' );
33 };
34
35 /* Inheritance */
36
37 OO.inheritClass( ve.ce.TableNode, ve.ce.BranchNode );
38
39 /* Static properties */
40
41 ve.ce.TableNode.static.autoFocus = false;
42
43 /* Methods */
44
45 /**
46 * @inheritdoc
47 */
48 ve.ce.TableNode.prototype.onSetup = function () {
49 // Parent method
50 ve.ce.TableNode.super.prototype.onSetup.call( this );
51
52 // Exit if already setup or not attached
53 if ( this.surface || !this.root ) {
54 return;
55 }
56 this.surface = this.getRoot().getSurface();
57
58 // Overlay
59 this.$selectionBox = $( '<div>' ).addClass( 've-ce-tableNodeOverlay-selection-box' );
60 this.$selectionBoxAnchor = $( '<div>' ).addClass( 've-ce-tableNodeOverlay-selection-box-anchor' );
61 if ( OO.ui.isMobile() ) {
62 this.nodeContext = new ve.ui.TableLineContext( this, 'table' );
63 } else {
64 this.nodeContext = null;
65 }
66 this.colContext = new ve.ui.TableLineContext( this, 'col' );
67 this.rowContext = new ve.ui.TableLineContext( this, 'row' );
68
69 this.$overlay = $( '<div>' )
70 .addClass( 've-ce-tableNodeOverlay oo-ui-element-hidden' )
71 .append(
72 this.$selectionBox,
73 this.$selectionBoxAnchor,
74 this.nodeContext ? this.nodeContext.$element : undefined,
75 this.colContext.$element,
76 this.rowContext.$element,
77 this.$rowBracket,
78 this.$colBracket
79 );
80 this.surface.surface.$blockers.append( this.$overlay );
81
82 // Events
83 this.$element.on( {
84 'mousedown.ve-ce-tableNode': this.onTableMouseDown.bind( this ),
85 'dblclick.ve-ce-tableNode': this.onTableDblClick.bind( this )
86 } );
87 this.$overlay.on( {
88 'mousedown.ve-ce-tableNode': this.onTableMouseDown.bind( this ),
89 'dblclick.ve-ce-tableNode': this.onTableDblClick.bind( this )
90 } );
91 this.onTableMouseUpHandler = this.onTableMouseUp.bind( this );
92 this.onTableMouseMoveHandler = this.onTableMouseMove.bind( this );
93 // Select and position events both fire updateOverlay, so debounce. Also makes
94 // sure that this.selectedRectangle is up to date before redrawing.
95 this.updateOverlayDebounced = ve.debounce( this.updateOverlay.bind( this ) );
96 this.surface.getModel().connect( this, { select: 'onSurfaceModelSelect' } );
97 this.surface.connect( this, {
98 position: this.updateOverlayDebounced,
99 activation: 'onSurfaceActivation'
100 } );
101 };
102
103 /**
104 * @inheritdoc
105 */
106 ve.ce.TableNode.prototype.onTeardown = function () {
107 // Parent method
108 ve.ce.TableNode.super.prototype.onTeardown.call( this );
109
110 // Not yet setup
111 if ( !this.surface ) {
112 return;
113 }
114
115 // Events
116 this.$element.off( '.ve-ce-tableNode' );
117 this.$overlay.off( '.ve-ce-tableNode' );
118 this.surface.getModel().disconnect( this );
119 this.surface.disconnect( this );
120 this.$overlay.remove();
121
122 this.surface = null;
123 };
124
125 /**
126 * Handle table double click events
127 *
128 * @param {jQuery.Event} e Double click event
129 */
130 ve.ce.TableNode.prototype.onTableDblClick = function ( e ) {
131 if ( !this.getCellNodeFromEvent( e ) ) {
132 return;
133 }
134 if ( this.surface.getModel().getSelection() instanceof ve.dm.TableSelection ) {
135 // Don't change selection in setEditing to avoid scrolling to bottom of cell
136 this.setEditing( true, true );
137 // getOffsetFromEventCoords doesn't work in ce=false in Firefox, so ensure
138 // this is called after setEditing( true ).
139 const offset = this.surface.getOffsetFromEventCoords( e.originalEvent );
140 if ( offset !== -1 ) {
141 // Set selection to where the double click happened
142 this.surface.getModel().setLinearSelection( new ve.Range( offset ) );
143 } else {
144 this.setEditing( true );
145 }
146 }
147 };
148
149 /**
150 * Handle mouse down or touch start events
151 *
152 * @param {jQuery.Event} e Mouse down or touch start event
153 */
154 ve.ce.TableNode.prototype.onTableMouseDown = function ( e ) {
155 const cellNode = this.getCellNodeFromEvent( e );
156 if ( !cellNode ) {
157 return;
158 }
159
160 const endCell = this.getModel().getMatrix().lookupCell( cellNode.getModel() );
161 if ( !endCell ) {
162 e.preventDefault();
163 return;
164 }
165 const selection = this.surface.getModel().getSelection();
166
167 let startCell;
168 let newSelection;
169 if ( e.shiftKey && this.active ) {
170 // Extend selection from the anchor cell
171 if ( selection instanceof ve.dm.TableSelection ) {
172 startCell = { col: selection.fromCol, row: selection.fromRow };
173 } else {
174 startCell = this.getModel().getMatrix().lookupCell( this.getActiveCellNode().getModel() );
175 }
176 } else if (
177 ( e.which === OO.ui.MouseButtons.RIGHT || this.surface.isDeactivated() ) &&
178 selection instanceof ve.dm.TableSelection &&
179 selection.containsCell( endCell )
180 ) {
181 // Right click within the current selection, or any click in deactviated selection:
182 // leave selection as is
183 newSelection = selection;
184 // Make sure there's a startCell
185 startCell = this.startCell || endCell;
186 } else {
187 // Select single cell
188 startCell = endCell;
189 }
190
191 if ( !newSelection ) {
192 newSelection = new ve.dm.TableSelection(
193 this.getModel().getOuterRange(),
194 startCell.col,
195 startCell.row,
196 endCell.col,
197 endCell.row
198 );
199 newSelection = newSelection.expand( this.getModel().getDocument() );
200 }
201
202 if ( this.editingFragment ) {
203 if ( newSelection.equals( this.editingFragment.getSelection() ) ) {
204 // Clicking on the editing cell, don't prevent default
205 return;
206 } else {
207 this.setEditing( false, true );
208 }
209 }
210 this.surface.getModel().setSelection( newSelection );
211 // Ensure surface is active as native 'focus' event won't be fired
212 this.surface.activate();
213
214 // Right-click on a cell which isn't being edited
215 if ( e.which === OO.ui.MouseButtons.RIGHT && !this.getActiveCellNode() ) {
216 // The same technique is used in ve.ce.FocusableNode
217 // Make ce=true so we get cut/paste options in the context menu
218 cellNode.$element.prop( 'contentEditable', true );
219 // Select the clicked element so we get a copy option in the context menu
220 ve.selectElement( cellNode.$element[ 0 ] );
221 setTimeout( () => {
222 // Undo ce=true as soon as the context menu is shown
223 cellNode.$element.prop( 'contentEditable', 'false' );
224 // Trigger onModelSelect to restore the selection
225 this.surface.onModelSelect();
226 } );
227 return;
228 }
229
230 this.startCell = startCell;
231 this.endCell = endCell;
232 if ( !( selection instanceof ve.dm.TableSelection ) && OO.ui.isMobile() ) {
233 // On mobile, fall through to the double-click behavior on a single tap --
234 // this will place the cursor within the cell, rather than remaining in
235 // table-selection mode.
236 // As we just have only just set the table selection, the surface is in
237 // process of deactivating, so wait for the event loop to clear before
238 // continuing.
239 setTimeout( () => {
240 this.onTableDblClick( e );
241 } );
242 } else {
243 this.surface.$document.on( {
244 'mouseup touchend': this.onTableMouseUpHandler,
245 'mousemove touchmove': this.onTableMouseMoveHandler
246 } );
247 }
248 e.preventDefault();
249 };
250
251 /**
252 * Get a table cell node from a mouse event
253 *
254 * Works around various issues with touch events and browser support.
255 *
256 * @param {jQuery.Event} e Mouse event
257 * @return {ve.ce.TableCellNode|null} Table cell node
258 */
259 ve.ce.TableNode.prototype.getCellNodeFromEvent = function ( e ) {
260 // 'touchmove' doesn't give a correct e.target, so calculate it from coordinates
261 if ( e.type === 'touchstart' && e.originalEvent.touches.length > 1 ) {
262 // Ignore multi-touch
263 return null;
264 } else if ( e.type === 'touchmove' ) {
265 if ( e.originalEvent.touches.length > 1 ) {
266 // Ignore multi-touch
267 return null;
268 }
269 const touch = e.originalEvent.touches[ 0 ];
270 return this.getCellNodeFromPoint( touch.clientX, touch.clientY );
271 } else {
272 return this.getNearestCellNode( e.target );
273 }
274 };
275
276 /**
277 * Get the cell node from a point
278 *
279 * @param {number} x X offset
280 * @param {number} y Y offset
281 * @return {ve.ce.TableCellNode|null} Table cell node, or null if none found
282 */
283 ve.ce.TableNode.prototype.getCellNodeFromPoint = function ( x, y ) {
284 return this.getNearestCellNode(
285 this.surface.getElementDocument().elementFromPoint( x, y )
286 );
287 };
288
289 /**
290 * Get the nearest cell node in this table to an element
291 *
292 * If the nearest cell node is in another table, return null.
293 *
294 * @param {HTMLElement} element Element target to find nearest cell node to
295 * @return {ve.ce.TableCellNode|null} Table cell node, or null if none found
296 */
297 ve.ce.TableNode.prototype.getNearestCellNode = function ( element ) {
298 const $element = $( element ),
299 $table = $element.closest( 'table' );
300
301 // Nested table, ignore
302 if ( !this.$element.is( $table ) ) {
303 return null;
304 }
305
306 return $element.closest( 'td, th' ).data( 'view' );
307 };
308
309 /**
310 * Handle mouse/touch move events
311 *
312 * @param {jQuery.Event} e Mouse/touch move event
313 */
314 ve.ce.TableNode.prototype.onTableMouseMove = function ( e ) {
315 const endCellNode = this.getCellNodeFromEvent( e );
316 if ( !endCellNode ) {
317 return;
318 }
319
320 const endCell = this.getModel().matrix.lookupCell( endCellNode.getModel() );
321 if ( !endCell || endCell === this.endCell ) {
322 return;
323 }
324
325 this.endCell = endCell;
326
327 let selection = new ve.dm.TableSelection(
328 this.getModel().getOuterRange(),
329 this.startCell.col, this.startCell.row, endCell.col, endCell.row
330 );
331 selection = selection.expand( this.getModel().getDocument() );
332 this.surface.getModel().setSelection( selection );
333 };
334
335 /**
336 * Handle mouse up or touch end events
337 *
338 * @param {jQuery.Event} e Mouse up or touch end event
339 */
340 ve.ce.TableNode.prototype.onTableMouseUp = function () {
341 this.startCell = null;
342 this.endCell = null;
343 this.surface.$document.off( {
344 'mouseup touchend': this.onTableMouseUpHandler,
345 'mousemove touchmove': this.onTableMouseMoveHandler
346 } );
347 };
348
349 /**
350 * Set the editing state of the table
351 *
352 * @param {boolean} isEditing The table is being edited
353 * @param {boolean} noSelect Don't change the selection
354 */
355 ve.ce.TableNode.prototype.setEditing = function ( isEditing, noSelect ) {
356 const surfaceModel = this.surface.getModel(),
357 documentModel = surfaceModel.getDocument();
358
359 let selection = surfaceModel.getSelection();
360 if ( isEditing ) {
361 if ( !selection.isSingleCell( documentModel ) ) {
362 selection = selection.collapseToFrom();
363 this.surface.getModel().setSelection( selection );
364 }
365 const cell = this.getCellNodesFromSelection( selection )[ 0 ];
366 if ( !cell.isCellEditable() ) {
367 return;
368 }
369 this.editingFragment = this.surface.getModel().getFragment( selection );
370 cell.setEditing( true );
371 if ( !noSelect ) {
372 const cellRange = cell.getModel().getRange();
373 const offset = surfaceModel.getDocument().data.getNearestContentOffset( cellRange.end, -1 );
374 if ( offset > cellRange.start ) {
375 surfaceModel.setLinearSelection( new ve.Range( offset ) );
376 }
377 }
378 } else {
379 let activeCellNode;
380 if ( ( activeCellNode = this.getActiveCellNode() ) ) {
381 activeCellNode.setEditing( false );
382 if ( !noSelect ) {
383 surfaceModel.setSelection( this.editingFragment.getSelection() );
384 }
385 }
386 this.editingFragment = null;
387 }
388
389 this.$element.toggleClass( 've-ce-tableNode-editing', isEditing );
390 this.$overlay.toggleClass( 've-ce-tableNodeOverlay-editing', isEditing );
391 };
392
393 /**
394 * Handle select events from the surface model.
395 *
396 * @param {ve.dm.Selection} selection
397 */
398 ve.ce.TableNode.prototype.onSurfaceModelSelect = function ( selection ) {
399 // The table is active if there is a linear selection inside a cell being edited
400 // or a table selection matching this table.
401 const active =
402 (
403 this.editingFragment !== null &&
404 selection instanceof ve.dm.LinearSelection &&
405 this.editingFragment.getSelection().getRanges(
406 this.editingFragment.getDocument()
407 )[ 0 ].containsRange( selection.getRange() )
408 ) ||
409 (
410 selection instanceof ve.dm.TableSelection &&
411 selection.tableRange.equalsSelection( this.getModel().getOuterRange() )
412 );
413
414 if ( active ) {
415 if ( !this.active ) {
416 this.$overlay.removeClass( 'oo-ui-element-hidden' );
417 // Only register touchstart event after table has become active to prevent
418 // accidental focusing of the table while scrolling
419 this.$element.on( 'touchstart.ve-ce-tableNode', this.onTableMouseDown.bind( this ) );
420 }
421 // Ignore update the overlay if the table selection changed, i.e. not an in-cell selection change
422 if ( selection instanceof ve.dm.TableSelection ) {
423 if ( this.editingFragment ) {
424 this.setEditing( false, true );
425 }
426 this.updateOverlayDebounced();
427 }
428 } else if ( !active && this.active ) {
429 this.$overlay.addClass( 'oo-ui-element-hidden' );
430 if ( this.editingFragment ) {
431 this.setEditing( false, true );
432 }
433 // When the table of the active node is deactivated, clear the active node
434 if ( this.getActiveCellNode() ) {
435 this.surface.setActiveNode( null );
436 }
437 this.$element.off( 'touchstart.ve-ce-tableNode' );
438 }
439 this.$element.toggleClass( 've-ce-tableNode-active', active );
440 this.active = active;
441 };
442
443 /**
444 * Get the active node in this table, if it has one
445 *
446 * @return {ve.ce.TableNode|null} The active cell node in this table
447 */
448 ve.ce.TableNode.prototype.getActiveCellNode = function () {
449 const activeNode = this.surface.getActiveNode(),
450 tableNodeOfActiveCellNode = activeNode && activeNode instanceof ve.ce.TableCellNode && activeNode.findParent( ve.ce.TableNode );
451
452 return tableNodeOfActiveCellNode === this ? activeNode : null;
453 };
454
455 /**
456 * Handle activation events from the surface
457 */
458 ve.ce.TableNode.prototype.onSurfaceActivation = function () {
459 this.$overlay.toggleClass( 've-ce-tableNodeOverlay-deactivated', !!this.surface.isShownAsDeactivated() );
460 };
461
462 /**
463 * Update the overlay positions
464 */
465 ve.ce.TableNode.prototype.updateOverlay = function () {
466 if (
467 !this.active || !this.root ||
468 !this.surface ||
469 // Overlay isn't attached, e.g. in tests
470 !this.surface.surface.$blockers[ 0 ].parentNode
471 ) {
472 return;
473 }
474
475 const selection = this.editingFragment ?
476 this.editingFragment.getSelection() :
477 this.surface.getModel().getSelection();
478 const documentModel = this.editingFragment ?
479 this.editingFragment.getDocument() :
480 this.surface.getModel().getDocument();
481 // getBoundingClientRect is more accurate but must be used consistently
482 // due to the iOS7 bug where it is relative to the document.
483 const tableOffset = this.getFirstSectionNode().$element[ 0 ].getBoundingClientRect();
484 const surfaceOffset = this.surface.getSurface().$element[ 0 ].getBoundingClientRect();
485
486 if ( !tableOffset ) {
487 return;
488 }
489
490 const selectionRect = this.surface.getSelection( selection ).getSelectionBoundingRect();
491
492 if ( !selectionRect ) {
493 return;
494 }
495
496 // Compute a bounding box for the given cell elements
497 const selectionOffset = ve.translateRect(
498 selectionRect,
499 surfaceOffset.left - tableOffset.left, surfaceOffset.top - tableOffset.top
500 );
501
502 let anchorOffset;
503 if ( selection.isSingleCell( documentModel ) ) {
504 // Optimization, use same rects as whole selection
505 anchorOffset = selectionOffset;
506 } else {
507 anchorOffset = ve.translateRect(
508 this.surface.getSelection( selection.collapseToFrom() ).getSelectionBoundingRect(),
509 surfaceOffset.left - tableOffset.left, surfaceOffset.top - tableOffset.top
510 );
511 }
512
513 // Resize controls
514 this.$selectionBox.css( {
515 top: selectionOffset.top,
516 left: selectionOffset.left,
517 width: selectionOffset.width,
518 height: selectionOffset.height
519 } );
520 this.$selectionBoxAnchor.css( {
521 top: anchorOffset.top,
522 left: anchorOffset.left,
523 width: anchorOffset.width,
524 height: anchorOffset.height
525 } );
526
527 // Position controls
528 this.$overlay.css( {
529 top: tableOffset.top - surfaceOffset.top,
530 left: tableOffset.left - surfaceOffset.left,
531 width: tableOffset.width
532 } );
533 // this.nodeContext doesn't need to adjust to the line
534 this.colContext.icon.$element.css( {
535 left: selectionOffset.left,
536 width: selectionOffset.width
537 } );
538 this.rowContext.icon.$element.css( {
539 top: selectionOffset.top,
540 height: selectionOffset.height
541 } );
542
543 if ( this.nodeContext ) {
544 this.nodeContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() );
545 }
546 this.colContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() );
547 this.rowContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() );
548
549 // Classes
550 this.$selectionBox.toggleClass( 've-ce-tableNodeOverlay-selection-box-notEditable', !selection.isEditable( documentModel ) );
551 };
552
553 /**
554 * Get the first section node of the table, skipping over any caption nodes
555 *
556 * @return {ve.ce.TableSectionNode} First table section node
557 */
558 ve.ce.TableNode.prototype.getFirstSectionNode = function () {
559 let i = 0;
560 while ( !( this.children[ i ] instanceof ve.ce.TableSectionNode ) ) {
561 i++;
562 }
563 return this.children[ i ];
564 };
565
566 /**
567 * Get a cell node from a single cell selection
568 *
569 * @param {ve.dm.TableSelection} selection Single cell table selection
570 * @return {ve.ce.TableCellNode[]} Cell nodes
571 */
572 ve.ce.TableNode.prototype.getCellNodesFromSelection = function ( selection ) {
573 const cells = selection.getMatrixCells( this.getModel().getDocument() ),
574 nodes = [];
575
576 for ( let i = 0, l = cells.length; i < l; i++ ) {
577 const cellModel = cells[ i ].node;
578 const cellView = this.getNodeFromOffset( cellModel.getOffset() - this.model.getOffset() );
579 nodes.push( cellView );
580 }
581 return nodes;
582 };
583
584 /* Static Properties */
585
586 ve.ce.TableNode.static.name = 'table';
587
588 ve.ce.TableNode.static.tagName = 'table';
589
590 /* Registration */
591
592 ve.ce.nodeFactory.register( ve.ce.TableNode );