Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable table arrow key down handler
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* istanbul ignore next */
8 /**
9 * Arrow key down handler for table selections.
10 *
11 * @class
12 * @extends ve.ce.KeyDownHandler
13 *
14 * @constructor
15 */
16 ve.ce.TableArrowKeyDownHandler = function VeCeTableArrowKeyDownHandler() {
17 // Parent constructor - never called because class is fully static
18 // ve.ui.TableArrowKeyDownHandler.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ce.TableArrowKeyDownHandler, ve.ce.KeyDownHandler );
24
25 /* Static properties */
26
27 ve.ce.TableArrowKeyDownHandler.static.name = 'tableArrow';
28
29 ve.ce.TableArrowKeyDownHandler.static.keys = [
30 OO.ui.Keys.UP, OO.ui.Keys.DOWN, OO.ui.Keys.LEFT, OO.ui.Keys.RIGHT,
31 OO.ui.Keys.HOME, OO.ui.Keys.END, OO.ui.Keys.PAGEUP, OO.ui.Keys.PAGEDOWN,
32 OO.ui.Keys.TAB
33 ];
34
35 ve.ce.TableArrowKeyDownHandler.static.supportedSelections = [ 'table' ];
36
37 /* Static methods */
38
39 /**
40 * @inheritdoc
41 */
42 ve.ce.TableArrowKeyDownHandler.static.execute = function ( surface, e ) {
43 let wrap = false,
44 checkDir = false,
45 colOffset = 0,
46 rowOffset = 0,
47 expand = e.shiftKey;
48
49 if ( e.ctrlKey || e.altKey || e.metaKey ) {
50 // Support: Firefox
51 // In Firefox, ctrl-tab to switch browser-tabs still triggers the
52 // keydown event.
53 return;
54 }
55
56 switch ( e.keyCode ) {
57 case OO.ui.Keys.LEFT:
58 colOffset = -1;
59 checkDir = true;
60 break;
61 case OO.ui.Keys.RIGHT:
62 colOffset = 1;
63 checkDir = true;
64 break;
65 case OO.ui.Keys.UP:
66 rowOffset = -1;
67 break;
68 case OO.ui.Keys.DOWN:
69 rowOffset = 1;
70 break;
71 case OO.ui.Keys.HOME:
72 colOffset = -Infinity;
73 break;
74 case OO.ui.Keys.END:
75 colOffset = Infinity;
76 break;
77 case OO.ui.Keys.PAGEUP:
78 rowOffset = -Infinity;
79 break;
80 case OO.ui.Keys.PAGEDOWN:
81 rowOffset = Infinity;
82 break;
83 case OO.ui.Keys.TAB:
84 colOffset = e.shiftKey ? -1 : 1;
85 expand = false; // Shift-tab is a movement, not an expansion
86 wrap = true;
87 break;
88 }
89
90 e.preventDefault();
91
92 this.moveTableSelection( surface, rowOffset, colOffset, checkDir, expand, wrap );
93 return true;
94 };
95
96 /**
97 * @param {ve.ce.Surface} surface
98 * @param {number} rowOffset how many rows to move
99 * @param {number} colOffset how many columns to move
100 * @param {boolean} checkDir whether to translate offsets according to ltr settings
101 * @param {boolean} expand whether to expand the selection or replace it
102 * @param {boolean} wrap Wrap to the next/previous row at edges, insert new row at end
103 */
104 ve.ce.TableArrowKeyDownHandler.static.moveTableSelection = function ( surface, rowOffset, colOffset, checkDir, expand, wrap ) {
105 let selection = surface.getModel().getSelection();
106 if ( colOffset && checkDir ) {
107 const tableNode = surface.documentView.getBranchNodeFromOffset( selection.tableRange.start + 1 );
108 if ( tableNode.$element.css( 'direction' ) !== 'ltr' ) {
109 colOffset *= -1;
110 }
111 }
112 if ( !expand ) {
113 selection = selection.collapseToFrom();
114 }
115
116 let newSelection;
117
118 function adjust() {
119 newSelection = selection.newFromAdjustment(
120 surface.getModel().getDocument(),
121 expand ? 0 : colOffset,
122 expand ? 0 : rowOffset,
123 colOffset,
124 rowOffset,
125 wrap
126 );
127 }
128
129 adjust();
130
131 // If wrapping forwards didn't move, we must be at the end of the table,
132 // so insert a new row and try again
133 if ( wrap && colOffset > 0 && selection.equals( newSelection ) ) {
134 surface.getSurface().execute( 'table', 'insert', 'row', 'after' );
135 selection = surface.getModel().getSelection();
136 adjust();
137 }
138
139 // If moving up/down didn't move, we must be at the start/end of the table,
140 // so move outside
141 if ( ( rowOffset !== 0 || ( rowOffset === 0 && colOffset === -1 && wrap ) ) && selection.equals( newSelection ) ) {
142 const documentModel = surface.getModel().getDocument();
143 let captionNode;
144 if ( ( rowOffset === -1 || ( colOffset === -1 && wrap ) ) && ( captionNode = selection.getTableNode( documentModel ).getCaptionNode() ) ) {
145 // If we're moving up/backwards, and there's a caption node, put the selection in it
146 newSelection = new ve.dm.LinearSelection( documentModel.getRelativeRange( new ve.Range( captionNode.getRange().start ), 1 ) );
147 } else {
148 // Otherwise, go outside the table
149 newSelection = new ve.dm.LinearSelection( documentModel.getRelativeRange( selection.tableRange, rowOffset || colOffset ) );
150 }
151 }
152
153 surface.getModel().setSelection( newSelection );
154 };
155
156 /* Registration */
157
158 ve.ce.keyDownHandlerFactory.register( ve.ce.TableArrowKeyDownHandler );