| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ce.TableArrowKeyDownHandler = function VeCeTableArrowKeyDownHandler() { |
| 17 |
|
| 18 |
|
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
OO.inheritClass( ve.ce.TableArrowKeyDownHandler, ve.ce.KeyDownHandler ); |
| 24 |
|
| 25 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 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; |
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 132 |
|
| 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 |
|
| 140 |
|
| 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 |
|
| 146 |
newSelection = new ve.dm.LinearSelection( documentModel.getRelativeRange( new ve.Range( captionNode.getRange().start ), 1 ) ); |
| 147 |
} else { |
| 148 |
|
| 149 |
newSelection = new ve.dm.LinearSelection( documentModel.getRelativeRange( selection.tableRange, rowOffset || colOffset ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
surface.getModel().setSelection( newSelection ); |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
ve.ce.keyDownHandlerFactory.register( ve.ce.TableArrowKeyDownHandler ); |