Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/keydownhandlers/ve.ce.LinearTabKeyDownHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable linear escape key down handler
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* istanbul ignore next */
8 /**
9 * Tab key down handler for linear selections.
10 *
11 * @class
12 * @extends ve.ce.KeyDownHandler
13 *
14 * @constructor
15 */
16 ve.ce.LinearTabKeyDownHandler = function VeCeLinearTabKeyDownHandler() {
17 // Parent constructor - never called because class is fully static
18 // ve.ui.LinearTabKeyDownHandler.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ce.LinearTabKeyDownHandler, ve.ce.KeyDownHandler );
24
25 /* Static properties */
26
27 ve.ce.LinearTabKeyDownHandler.static.name = 'linearTab';
28
29 ve.ce.LinearTabKeyDownHandler.static.keys = [ OO.ui.Keys.TAB ];
30
31 ve.ce.LinearTabKeyDownHandler.static.supportedSelections = [ 'linear' ];
32
33 /* Static methods */
34
35 /**
36 * Handle tab key down events with a linear selection while table editing.
37 *
38 * @inheritdoc
39 */
40 ve.ce.LinearTabKeyDownHandler.static.execute = function ( surface, e ) {
41 const activeTableNode = surface.getActiveNode() && surface.getActiveNode().findParent( ve.ce.TableNode ),
42 activeTableCaptionNode = surface.getActiveNode() && surface.getActiveNode().findParent( ve.ce.TableCaptionNode ),
43 documentModel = surface.getModel().getDocument();
44 // Check we have an active table node
45 if ( activeTableNode ) {
46 if ( e.ctrlKey || e.altKey || e.metaKey ) {
47 // Support: Firefox
48 // In Firefox, ctrl-tab to switch browser-tabs still triggers the
49 // keydown event.
50 return;
51 }
52
53 if ( activeTableNode.editingFragment ) {
54 // we are inside a cell (editingFragment), and not just a caption
55 e.preventDefault();
56 e.stopPropagation();
57 activeTableNode.setEditing( false );
58 // If this was a merged cell, we're going to have unexpected behavior when the selection moves,
59 // so preemptively collapse to the top-left point of the merged cell.
60 surface.getModel().setSelection( surface.getModel().getSelection().collapseToStart() );
61 ve.ce.TableArrowKeyDownHandler.static.moveTableSelection(
62 surface,
63 0, // Rows
64 e.shiftKey ? -1 : 1, // Columns
65 false, // Logical direction, not visual
66 false, // Don't expand the current selection,
67 true // Wrap to next/previous row
68 );
69 if ( surface.getModel().getSelection() instanceof ve.dm.TableSelection ) {
70 // Might have moved off the table and into the caption
71 activeTableNode.setEditing( true );
72 }
73 return true;
74 } else if ( activeTableCaptionNode ) {
75 // We're in a table caption
76 e.preventDefault();
77 e.stopPropagation();
78 if ( e.shiftKey ) {
79 // back out of the table
80 surface.getModel().setSelection( new ve.dm.LinearSelection( documentModel.getRelativeRange( activeTableNode.getRange(), -1 ) ) );
81 } else {
82 // move to the first cell
83 surface.getModel().setSelection( new ve.dm.TableSelection( activeTableNode.getOuterRange(), 0, 0 ) );
84 }
85 return true;
86 }
87 }
88 return false;
89 };
90
91 /* Registration */
92
93 ve.ce.keyDownHandlerFactory.register( ve.ce.LinearTabKeyDownHandler );