@@ -13,8 +13,8 @@ import {
13
13
} from "../modules/selection" ;
14
14
import { cancelPaintModel , handleBold } from "../modules/toolbar" ;
15
15
import { hasPartMC } from "../modules/validation" ;
16
- import { GlobalCache } from "../types" ;
17
- import { getNowDateTime , isAllowEdit } from "../utils" ;
16
+ import { CellMatrix , GlobalCache } from "../types" ;
17
+ import { getNowDateTime , getSheetIndex , isAllowEdit } from "../utils" ;
18
18
import { handleCopy } from "./copy" ;
19
19
import { jfrefreshgrid } from "../modules/refresh" ;
20
20
@@ -95,29 +95,174 @@ export function handleGlobalEnter(
95
95
}
96
96
}
97
97
98
- function handleBatchSelectionWithArrowKey ( ctx : Context , e : KeyboardEvent ) {
99
- if (
100
- ctx . luckysheetCellUpdate . length > 0
101
- // || $("#luckysheet-singleRange-dialog").is(":visible") ||
102
- // $("#luckysheet-multiRange-dialog").is(":visible")
103
- ) {
104
- return ;
98
+ function moveToEdge (
99
+ sheetData : CellMatrix ,
100
+ key : string ,
101
+ curr : number ,
102
+ rowDelta : 0 | 1 | - 1 ,
103
+ colDelta : 0 | 1 | - 1 ,
104
+ startR : number ,
105
+ endR : number ,
106
+ startC : number ,
107
+ endC : number ,
108
+ maxRow : number ,
109
+ maxCol : number
110
+ ) {
111
+ let selectedLimit = - 1 ;
112
+ if ( key === "ArrowUp" ) selectedLimit = startR - 1 ;
113
+ else if ( key === "ArrowDown" ) selectedLimit = endR + 1 ;
114
+ else if ( key === "ArrowLeft" ) selectedLimit = startC - 1 ;
115
+ else if ( key === "ArrowRight" ) selectedLimit = endC + 1 ;
116
+
117
+ const maxRowCol = colDelta === 0 ? maxRow : maxCol ;
118
+ let r = colDelta === 0 ? selectedLimit : curr ;
119
+ let c = colDelta === 0 ? curr : selectedLimit ;
120
+
121
+ while ( r > 0 && c > 0 && ( colDelta === 0 ? r : c ) < maxRowCol - 1 ) {
122
+ if (
123
+ ! _ . isNil ( sheetData ?. [ r ] ?. [ c ] ?. v ) &&
124
+ ( _ . isNil ( sheetData ?. [ r - rowDelta ] ?. [ c - colDelta ] ?. v ) ||
125
+ _ . isNil ( sheetData ?. [ r + rowDelta ] ?. [ c + colDelta ] ?. v ) )
126
+ ) {
127
+ break ;
128
+ } else {
129
+ r += 1 * rowDelta ;
130
+ c += 1 * colDelta ;
131
+ }
105
132
}
133
+ return colDelta === 0 ? r : c ;
134
+ }
135
+
136
+ function handleControlPlusArrowKey (
137
+ ctx : Context ,
138
+ e : KeyboardEvent ,
139
+ shiftPressed : boolean
140
+ ) {
141
+ if ( ctx . luckysheetCellUpdate . length > 0 ) return ;
142
+
143
+ const idx = getSheetIndex ( ctx , ctx . currentSheetId ) ;
144
+ if ( _ . isNil ( idx ) ) return ;
145
+
146
+ const file = ctx . luckysheetfile [ idx ] ;
147
+ if ( ! file || ! file . row || ! file . column ) return ;
148
+ const maxRow = file . row ;
149
+ const maxCol = file . column ;
150
+ let last ;
151
+ if ( ctx . luckysheet_select_save && ctx . luckysheet_select_save . length > 0 )
152
+ last = ctx . luckysheet_select_save [ ctx . luckysheet_select_save . length - 1 ] ;
153
+ if ( ! last ) return ;
154
+
155
+ const currR = last . row_focus ;
156
+ const currC = last . column_focus ;
157
+ if ( _ . isNil ( currR ) || _ . isNil ( currC ) ) return ;
158
+
159
+ const startR = last . row [ 0 ] ;
160
+ const endR = last . row [ 1 ] ;
161
+ const startC = last . column [ 0 ] ;
162
+ const endC = last . column [ 1 ] ;
163
+
164
+ const horizontalOffset = currC - endC !== 0 ? currC - endC : currC - startC ;
165
+ const verticalOffset = currR - endR !== 0 ? currR - endR : currR - startR ;
166
+
167
+ const sheetData = file . data ;
168
+ if ( ! sheetData ) return ;
169
+ let selectedLimit ;
170
+
106
171
switch ( e . key ) {
107
- /*
108
172
case "ArrowUp" :
109
- luckysheetMoveHighlightRange2("up", "rangeOfSelect");
173
+ selectedLimit = moveToEdge (
174
+ sheetData ,
175
+ e . key ,
176
+ currC ,
177
+ - 1 ,
178
+ 0 ,
179
+ startR ,
180
+ endR ,
181
+ startC ,
182
+ endC ,
183
+ maxRow ,
184
+ maxCol
185
+ ) ;
186
+ if ( shiftPressed ) {
187
+ moveHighlightRange ( ctx , "down" , verticalOffset , "rangeOfSelect" ) ;
188
+ moveHighlightRange ( ctx , "down" , selectedLimit - currR , "rangeOfSelect" ) ;
189
+ } else {
190
+ moveHighlightCell ( ctx , "down" , selectedLimit - currR , "rangeOfSelect" ) ;
191
+ }
110
192
break ;
111
193
case "ArrowDown" :
112
- luckysheetMoveHighlightRange2("down", "rangeOfSelect");
194
+ selectedLimit = moveToEdge (
195
+ sheetData ,
196
+ e . key ,
197
+ currC ,
198
+ 1 ,
199
+ 0 ,
200
+ startR ,
201
+ endR ,
202
+ startC ,
203
+ endC ,
204
+ maxRow ,
205
+ maxCol
206
+ ) ;
207
+ if ( shiftPressed ) {
208
+ moveHighlightRange ( ctx , "down" , verticalOffset , "rangeOfSelect" ) ;
209
+ moveHighlightRange ( ctx , "down" , selectedLimit - currR , "rangeOfSelect" ) ;
210
+ } else {
211
+ moveHighlightCell ( ctx , "down" , selectedLimit - currR , "rangeOfSelect" ) ;
212
+ }
113
213
break ;
114
214
case "ArrowLeft" :
115
- luckysheetMoveHighlightRange2("left", "rangeOfSelect");
215
+ selectedLimit = moveToEdge (
216
+ sheetData ,
217
+ e . key ,
218
+ currR ,
219
+ 0 ,
220
+ - 1 ,
221
+ startR ,
222
+ endR ,
223
+ startC ,
224
+ endC ,
225
+ maxRow ,
226
+ maxCol
227
+ ) ;
228
+ if ( shiftPressed ) {
229
+ moveHighlightRange ( ctx , "right" , horizontalOffset , "rangeOfSelect" ) ;
230
+ moveHighlightRange (
231
+ ctx ,
232
+ "right" ,
233
+ selectedLimit - currC ,
234
+ "rangeOfSelect"
235
+ ) ;
236
+ } else {
237
+ moveHighlightCell ( ctx , "right" , selectedLimit - currC , "rangeOfSelect" ) ;
238
+ }
116
239
break ;
117
240
case "ArrowRight" :
118
- luckysheetMoveHighlightRange2("right", "rangeOfSelect");
241
+ selectedLimit = moveToEdge (
242
+ sheetData ,
243
+ e . key ,
244
+ currR ,
245
+ 0 ,
246
+ 1 ,
247
+ startR ,
248
+ endR ,
249
+ startC ,
250
+ endC ,
251
+ maxRow ,
252
+ maxCol
253
+ ) ;
254
+ if ( shiftPressed ) {
255
+ moveHighlightRange ( ctx , "right" , horizontalOffset , "rangeOfSelect" ) ;
256
+ moveHighlightRange (
257
+ ctx ,
258
+ "right" ,
259
+ selectedLimit - currC ,
260
+ "rangeOfSelect"
261
+ ) ;
262
+ } else {
263
+ moveHighlightCell ( ctx , "right" , selectedLimit - currC , "rangeOfSelect" ) ;
264
+ }
119
265
break ;
120
- */
121
266
default :
122
267
break ;
123
268
}
@@ -143,7 +288,7 @@ export function handleWithCtrlOrMetaKey(
143
288
144
289
if ( [ "ArrowUp" , "ArrowDown" , "ArrowLeft" , "ArrowRight" ] . includes ( e . key ) ) {
145
290
// Ctrl + Shift + 方向键 调整选区
146
- handleBatchSelectionWithArrowKey ( ctx , e ) ;
291
+ handleControlPlusArrowKey ( ctx , e , true ) ;
147
292
} else if ( _ . includes ( [ ";" , '"' , ":" , "'" ] , e . key ) ) {
148
293
const last =
149
294
ctx . luckysheet_select_save ?. [ ctx . luckysheet_select_save . length - 1 ] ;
@@ -166,6 +311,10 @@ export function handleWithCtrlOrMetaKey(
166
311
e . stopPropagation ( ) ;
167
312
return ;
168
313
}
314
+ } else if (
315
+ [ "ArrowUp" , "ArrowDown" , "ArrowLeft" , "ArrowRight" ] . includes ( e . key )
316
+ ) {
317
+ handleControlPlusArrowKey ( ctx , e , false ) ;
169
318
} else if ( e . code === "KeyB" ) {
170
319
// Ctrl + B 加粗
171
320
handleBold ( ctx , cellInput ) ;
0 commit comments