| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2019, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { Platform } from '@lumino/domutils'; |
| 11 |
|
| 12 |
import { getKeyboardLayout } from '@lumino/keyboard'; |
| 13 |
|
| 14 |
import { DataGrid } from './datagrid'; |
| 15 |
|
| 16 |
import { SelectionModel } from './selectionmodel'; |
| 17 |
|
| 18 |
import { CellEditor } from './celleditor'; |
| 19 |
|
| 20 |
import { MutableDataModel } from './datamodel'; |
| 21 |
|
| 22 |
/** |
| 23 |
* A basic implementation of a data grid key handler. |
| 24 |
* |
| 25 |
* #### Notes |
| 26 |
* This class may be subclassed and customized as needed. |
| 27 |
*/ |
| 28 |
export class BasicKeyHandler implements DataGrid.IKeyHandler { |
| 29 |
/** |
| 30 |
* Whether the key handler is disposed. |
| 31 |
*/ |
| 32 |
get isDisposed(): boolean { |
| 33 |
return this._disposed; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Dispose of the resources held by the key handler. |
| 38 |
*/ |
| 39 |
dispose(): void { |
| 40 |
this._disposed = true; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle the key down event for the data grid. |
| 45 |
* |
| 46 |
* @param grid - The data grid of interest. |
| 47 |
* |
| 48 |
* @param event - The keydown event of interest. |
| 49 |
* |
| 50 |
* #### Notes |
| 51 |
* This will not be called if the mouse button is pressed. |
| 52 |
*/ |
| 53 |
onKeyDown(grid: DataGrid, event: KeyboardEvent): void { |
| 54 |
// if grid is editable and cell selection available, start cell editing |
| 55 |
// on key press (letters, numbers and space only) |
| 56 |
if ( |
| 57 |
grid.editable && |
| 58 |
grid.selectionModel!.cursorRow !== -1 && |
| 59 |
grid.selectionModel!.cursorColumn !== -1 |
| 60 |
) { |
| 61 |
const input = String.fromCharCode(event.keyCode); |
| 62 |
if (/[a-zA-Z0-9-_ ]/.test(input)) { |
| 63 |
const row = grid.selectionModel!.cursorRow; |
| 64 |
const column = grid.selectionModel!.cursorColumn; |
| 65 |
const cell: CellEditor.CellConfig = { |
| 66 |
grid: grid, |
| 67 |
row: row, |
| 68 |
column: column |
| 69 |
}; |
| 70 |
grid.editorController!.edit(cell); |
| 71 |
if (getKeyboardLayout().keyForKeydownEvent(event) === 'Space') { |
| 72 |
event.stopPropagation(); |
| 73 |
event.preventDefault(); |
| 74 |
} |
| 75 |
return; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
switch (getKeyboardLayout().keyForKeydownEvent(event)) { |
| 80 |
case 'ArrowLeft': |
| 81 |
this.onArrowLeft(grid, event); |
| 82 |
break; |
| 83 |
case 'ArrowRight': |
| 84 |
this.onArrowRight(grid, event); |
| 85 |
break; |
| 86 |
case 'ArrowUp': |
| 87 |
this.onArrowUp(grid, event); |
| 88 |
break; |
| 89 |
case 'ArrowDown': |
| 90 |
this.onArrowDown(grid, event); |
| 91 |
break; |
| 92 |
case 'PageUp': |
| 93 |
this.onPageUp(grid, event); |
| 94 |
break; |
| 95 |
case 'PageDown': |
| 96 |
this.onPageDown(grid, event); |
| 97 |
break; |
| 98 |
case 'Escape': |
| 99 |
this.onEscape(grid, event); |
| 100 |
break; |
| 101 |
case 'Delete': |
| 102 |
this.onDelete(grid, event); |
| 103 |
break; |
| 104 |
case 'C': |
| 105 |
this.onKeyC(grid, event); |
| 106 |
break; |
| 107 |
case 'Enter': |
| 108 |
if (grid.selectionModel) { |
| 109 |
grid.moveCursor(event.shiftKey ? 'up' : 'down'); |
| 110 |
grid.scrollToCursor(); |
| 111 |
} |
| 112 |
break; |
| 113 |
case 'Tab': |
| 114 |
if (grid.selectionModel) { |
| 115 |
grid.moveCursor(event.shiftKey ? 'left' : 'right'); |
| 116 |
grid.scrollToCursor(); |
| 117 |
event.stopPropagation(); |
| 118 |
event.preventDefault(); |
| 119 |
} |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Handle the `'ArrowLeft'` key press for the data grid. |
| 126 |
* |
| 127 |
* @param grid - The data grid of interest. |
| 128 |
* |
| 129 |
* @param event - The keyboard event of interest. |
| 130 |
*/ |
| 131 |
protected onArrowLeft(grid: DataGrid, event: KeyboardEvent): void { |
| 132 |
// Stop the event propagation. |
| 133 |
event.preventDefault(); |
| 134 |
event.stopPropagation(); |
| 135 |
|
| 136 |
// Fetch the selection model. |
| 137 |
let model = grid.selectionModel; |
| 138 |
|
| 139 |
// Fetch the modifier flags. |
| 140 |
let shift = event.shiftKey; |
| 141 |
let accel = Platform.accelKey(event); |
| 142 |
|
| 143 |
// Handle no model with the accel modifier. |
| 144 |
if (!model && accel) { |
| 145 |
grid.scrollTo(0, grid.scrollY); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// Handle no model and no modifier. (ignore shift) |
| 150 |
if (!model) { |
| 151 |
grid.scrollByStep('left'); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
// Fetch the selection mode. |
| 156 |
let mode = model.selectionMode; |
| 157 |
|
| 158 |
// Handle the row selection mode with accel key. |
| 159 |
if (mode === 'row' && accel) { |
| 160 |
grid.scrollTo(0, grid.scrollY); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Handle the row selection mode with no modifier. (ignore shift) |
| 165 |
if (mode === 'row') { |
| 166 |
grid.scrollByStep('left'); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
// Fetch the cursor and selection. |
| 171 |
let r = model.cursorRow; |
| 172 |
let c = model.cursorColumn; |
| 173 |
let cs = model.currentSelection(); |
| 174 |
|
| 175 |
// Set up the selection variables. |
| 176 |
let r1: number; |
| 177 |
let r2: number; |
| 178 |
let c1: number; |
| 179 |
let c2: number; |
| 180 |
let cr: number; |
| 181 |
let cc: number; |
| 182 |
let clear: SelectionModel.ClearMode; |
| 183 |
|
| 184 |
// Dispatch based on the modifier keys. |
| 185 |
if (accel && shift) { |
| 186 |
r1 = cs ? cs.r1 : 0; |
| 187 |
r2 = cs ? cs.r2 : 0; |
| 188 |
c1 = cs ? cs.c1 : 0; |
| 189 |
c2 = 0; |
| 190 |
cr = r; |
| 191 |
cc = c; |
| 192 |
clear = 'current'; |
| 193 |
} else if (shift) { |
| 194 |
r1 = cs ? cs.r1 : 0; |
| 195 |
r2 = cs ? cs.r2 : 0; |
| 196 |
c1 = cs ? cs.c1 : 0; |
| 197 |
c2 = cs ? cs.c2 - 1 : 0; |
| 198 |
cr = r; |
| 199 |
cc = c; |
| 200 |
clear = 'current'; |
| 201 |
} else if (accel) { |
| 202 |
r1 = r; |
| 203 |
r2 = r; |
| 204 |
c1 = 0; |
| 205 |
c2 = 0; |
| 206 |
cr = r1; |
| 207 |
cc = c1; |
| 208 |
clear = 'all'; |
| 209 |
} else { |
| 210 |
r1 = r; |
| 211 |
r2 = r; |
| 212 |
c1 = c - 1; |
| 213 |
c2 = c - 1; |
| 214 |
cr = r1; |
| 215 |
cc = c1; |
| 216 |
clear = 'all'; |
| 217 |
} |
| 218 |
|
| 219 |
// Create the new selection. |
| 220 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 221 |
|
| 222 |
// Re-fetch the current selection. |
| 223 |
cs = model.currentSelection(); |
| 224 |
|
| 225 |
// Bail if there is no selection. |
| 226 |
if (!cs) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
// Scroll the grid appropriately. |
| 231 |
if (shift || mode === 'column') { |
| 232 |
grid.scrollToColumn(cs.c2); |
| 233 |
} else { |
| 234 |
grid.scrollToCursor(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Handle the `'ArrowRight'` key press for the data grid. |
| 240 |
* |
| 241 |
* @param grid - The data grid of interest. |
| 242 |
* |
| 243 |
* @param event - The keyboard event of interest. |
| 244 |
*/ |
| 245 |
protected onArrowRight(grid: DataGrid, event: KeyboardEvent): void { |
| 246 |
// Stop the event propagation. |
| 247 |
event.preventDefault(); |
| 248 |
event.stopPropagation(); |
| 249 |
|
| 250 |
// Fetch the selection model. |
| 251 |
let model = grid.selectionModel; |
| 252 |
|
| 253 |
// Fetch the modifier flags. |
| 254 |
let shift = event.shiftKey; |
| 255 |
let accel = Platform.accelKey(event); |
| 256 |
|
| 257 |
// Handle no model with the accel modifier. |
| 258 |
if (!model && accel) { |
| 259 |
grid.scrollTo(grid.maxScrollX, grid.scrollY); |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
// Handle no model and no modifier. (ignore shift) |
| 264 |
if (!model) { |
| 265 |
grid.scrollByStep('right'); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
// Fetch the selection mode. |
| 270 |
let mode = model.selectionMode; |
| 271 |
|
| 272 |
// Handle the row selection model with accel key. |
| 273 |
if (mode === 'row' && accel) { |
| 274 |
grid.scrollTo(grid.maxScrollX, grid.scrollY); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
// Handle the row selection mode with no modifier. (ignore shift) |
| 279 |
if (mode === 'row') { |
| 280 |
grid.scrollByStep('right'); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
// Fetch the cursor and selection. |
| 285 |
let r = model.cursorRow; |
| 286 |
let c = model.cursorColumn; |
| 287 |
let cs = model.currentSelection(); |
| 288 |
|
| 289 |
// Set up the selection variables. |
| 290 |
let r1: number; |
| 291 |
let r2: number; |
| 292 |
let c1: number; |
| 293 |
let c2: number; |
| 294 |
let cr: number; |
| 295 |
let cc: number; |
| 296 |
let clear: SelectionModel.ClearMode; |
| 297 |
|
| 298 |
// Dispatch based on the modifier keys. |
| 299 |
if (accel && shift) { |
| 300 |
r1 = cs ? cs.r1 : 0; |
| 301 |
r2 = cs ? cs.r2 : 0; |
| 302 |
c1 = cs ? cs.c1 : 0; |
| 303 |
c2 = Infinity; |
| 304 |
cr = r; |
| 305 |
cc = c; |
| 306 |
clear = 'current'; |
| 307 |
} else if (shift) { |
| 308 |
r1 = cs ? cs.r1 : 0; |
| 309 |
r2 = cs ? cs.r2 : 0; |
| 310 |
c1 = cs ? cs.c1 : 0; |
| 311 |
c2 = cs ? cs.c2 + 1 : 0; |
| 312 |
cr = r; |
| 313 |
cc = c; |
| 314 |
clear = 'current'; |
| 315 |
} else if (accel) { |
| 316 |
r1 = r; |
| 317 |
r2 = r; |
| 318 |
c1 = Infinity; |
| 319 |
c2 = Infinity; |
| 320 |
cr = r1; |
| 321 |
cc = c1; |
| 322 |
clear = 'all'; |
| 323 |
} else { |
| 324 |
r1 = r; |
| 325 |
r2 = r; |
| 326 |
c1 = c + 1; |
| 327 |
c2 = c + 1; |
| 328 |
cr = r1; |
| 329 |
cc = c1; |
| 330 |
clear = 'all'; |
| 331 |
} |
| 332 |
|
| 333 |
// Create the new selection. |
| 334 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 335 |
|
| 336 |
// Re-fetch the current selection. |
| 337 |
cs = model.currentSelection(); |
| 338 |
|
| 339 |
// Bail if there is no selection. |
| 340 |
if (!cs) { |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
// Scroll the grid appropriately. |
| 345 |
if (shift || mode === 'column') { |
| 346 |
grid.scrollToColumn(cs.c2); |
| 347 |
} else { |
| 348 |
grid.scrollToCursor(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Handle the `'ArrowUp'` key press for the data grid. |
| 354 |
* |
| 355 |
* @param grid - The data grid of interest. |
| 356 |
* |
| 357 |
* @param event - The keyboard event of interest. |
| 358 |
*/ |
| 359 |
protected onArrowUp(grid: DataGrid, event: KeyboardEvent): void { |
| 360 |
// Stop the event propagation. |
| 361 |
event.preventDefault(); |
| 362 |
event.stopPropagation(); |
| 363 |
|
| 364 |
// Fetch the selection model. |
| 365 |
let model = grid.selectionModel; |
| 366 |
|
| 367 |
// Fetch the modifier flags. |
| 368 |
let shift = event.shiftKey; |
| 369 |
let accel = Platform.accelKey(event); |
| 370 |
|
| 371 |
// Handle no model with the accel modifier. |
| 372 |
if (!model && accel) { |
| 373 |
grid.scrollTo(grid.scrollX, 0); |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
// Handle no model and no modifier. (ignore shift) |
| 378 |
if (!model) { |
| 379 |
grid.scrollByStep('up'); |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
// Fetch the selection mode. |
| 384 |
let mode = model.selectionMode; |
| 385 |
|
| 386 |
// Handle the column selection mode with accel key. |
| 387 |
if (mode === 'column' && accel) { |
| 388 |
grid.scrollTo(grid.scrollX, 0); |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
// Handle the column selection mode with no modifier. (ignore shift) |
| 393 |
if (mode === 'column') { |
| 394 |
grid.scrollByStep('up'); |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
// Fetch the cursor and selection. |
| 399 |
let r = model.cursorRow; |
| 400 |
let c = model.cursorColumn; |
| 401 |
let cs = model.currentSelection(); |
| 402 |
|
| 403 |
// Set up the selection variables. |
| 404 |
let r1: number; |
| 405 |
let r2: number; |
| 406 |
let c1: number; |
| 407 |
let c2: number; |
| 408 |
let cr: number; |
| 409 |
let cc: number; |
| 410 |
let clear: SelectionModel.ClearMode; |
| 411 |
|
| 412 |
// Dispatch based on the modifier keys. |
| 413 |
if (accel && shift) { |
| 414 |
r1 = cs ? cs.r1 : 0; |
| 415 |
r2 = 0; |
| 416 |
c1 = cs ? cs.c1 : 0; |
| 417 |
c2 = cs ? cs.c2 : 0; |
| 418 |
cr = r; |
| 419 |
cc = c; |
| 420 |
clear = 'current'; |
| 421 |
} else if (shift) { |
| 422 |
r1 = cs ? cs.r1 : 0; |
| 423 |
r2 = cs ? cs.r2 - 1 : 0; |
| 424 |
c1 = cs ? cs.c1 : 0; |
| 425 |
c2 = cs ? cs.c2 : 0; |
| 426 |
cr = r; |
| 427 |
cc = c; |
| 428 |
clear = 'current'; |
| 429 |
} else if (accel) { |
| 430 |
r1 = 0; |
| 431 |
r2 = 0; |
| 432 |
c1 = c; |
| 433 |
c2 = c; |
| 434 |
cr = r1; |
| 435 |
cc = c1; |
| 436 |
clear = 'all'; |
| 437 |
} else { |
| 438 |
r1 = r - 1; |
| 439 |
r2 = r - 1; |
| 440 |
c1 = c; |
| 441 |
c2 = c; |
| 442 |
cr = r1; |
| 443 |
cc = c1; |
| 444 |
clear = 'all'; |
| 445 |
} |
| 446 |
|
| 447 |
// Create the new selection. |
| 448 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 449 |
|
| 450 |
// Re-fetch the current selection. |
| 451 |
cs = model.currentSelection(); |
| 452 |
|
| 453 |
// Bail if there is no selection. |
| 454 |
if (!cs) { |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
// Scroll the grid appropriately. |
| 459 |
if (shift || mode === 'row') { |
| 460 |
grid.scrollToRow(cs.r2); |
| 461 |
} else { |
| 462 |
grid.scrollToCursor(); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Handle the `'ArrowDown'` key press for the data grid. |
| 468 |
* |
| 469 |
* @param grid - The data grid of interest. |
| 470 |
* |
| 471 |
* @param event - The keyboard event of interest. |
| 472 |
*/ |
| 473 |
protected onArrowDown(grid: DataGrid, event: KeyboardEvent): void { |
| 474 |
// Stop the event propagation. |
| 475 |
event.preventDefault(); |
| 476 |
event.stopPropagation(); |
| 477 |
|
| 478 |
// Fetch the selection model. |
| 479 |
let model = grid.selectionModel; |
| 480 |
|
| 481 |
// Fetch the modifier flags. |
| 482 |
let shift = event.shiftKey; |
| 483 |
let accel = Platform.accelKey(event); |
| 484 |
|
| 485 |
// Handle no model with the accel modifier. |
| 486 |
if (!model && accel) { |
| 487 |
grid.scrollTo(grid.scrollX, grid.maxScrollY); |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
// Handle no model and no modifier. (ignore shift) |
| 492 |
if (!model) { |
| 493 |
grid.scrollByStep('down'); |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
// Fetch the selection mode. |
| 498 |
let mode = model.selectionMode; |
| 499 |
|
| 500 |
// Handle the column selection mode with accel key. |
| 501 |
if (mode === 'column' && accel) { |
| 502 |
grid.scrollTo(grid.scrollX, grid.maxScrollY); |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
// Handle the column selection mode with no modifier. (ignore shift) |
| 507 |
if (mode === 'column') { |
| 508 |
grid.scrollByStep('down'); |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
// Fetch the cursor and selection. |
| 513 |
let r = model.cursorRow; |
| 514 |
let c = model.cursorColumn; |
| 515 |
let cs = model.currentSelection(); |
| 516 |
|
| 517 |
// Set up the selection variables. |
| 518 |
let r1: number; |
| 519 |
let r2: number; |
| 520 |
let c1: number; |
| 521 |
let c2: number; |
| 522 |
let cr: number; |
| 523 |
let cc: number; |
| 524 |
let clear: SelectionModel.ClearMode; |
| 525 |
|
| 526 |
// Dispatch based on the modifier keys. |
| 527 |
if (accel && shift) { |
| 528 |
r1 = cs ? cs.r1 : 0; |
| 529 |
r2 = Infinity; |
| 530 |
c1 = cs ? cs.c1 : 0; |
| 531 |
c2 = cs ? cs.c2 : 0; |
| 532 |
cr = r; |
| 533 |
cc = c; |
| 534 |
clear = 'current'; |
| 535 |
} else if (shift) { |
| 536 |
r1 = cs ? cs.r1 : 0; |
| 537 |
r2 = cs ? cs.r2 + 1 : 0; |
| 538 |
c1 = cs ? cs.c1 : 0; |
| 539 |
c2 = cs ? cs.c2 : 0; |
| 540 |
cr = r; |
| 541 |
cc = c; |
| 542 |
clear = 'current'; |
| 543 |
} else if (accel) { |
| 544 |
r1 = Infinity; |
| 545 |
r2 = Infinity; |
| 546 |
c1 = c; |
| 547 |
c2 = c; |
| 548 |
cr = r1; |
| 549 |
cc = c1; |
| 550 |
clear = 'all'; |
| 551 |
} else { |
| 552 |
r1 = r + 1; |
| 553 |
r2 = r + 1; |
| 554 |
c1 = c; |
| 555 |
c2 = c; |
| 556 |
cr = r1; |
| 557 |
cc = c1; |
| 558 |
clear = 'all'; |
| 559 |
} |
| 560 |
|
| 561 |
// Create the new selection. |
| 562 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 563 |
|
| 564 |
// Re-fetch the current selection. |
| 565 |
cs = model.currentSelection(); |
| 566 |
|
| 567 |
// Bail if there is no selection. |
| 568 |
if (!cs) { |
| 569 |
return; |
| 570 |
} |
| 571 |
|
| 572 |
// Scroll the grid appropriately. |
| 573 |
if (shift || mode === 'row') { |
| 574 |
grid.scrollToRow(cs.r2); |
| 575 |
} else { |
| 576 |
grid.scrollToCursor(); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Handle the `'PageUp'` key press for the data grid. |
| 582 |
* |
| 583 |
* @param grid - The data grid of interest. |
| 584 |
* |
| 585 |
* @param event - The keyboard event of interest. |
| 586 |
*/ |
| 587 |
protected onPageUp(grid: DataGrid, event: KeyboardEvent): void { |
| 588 |
// Ignore the event if the accel key is pressed. |
| 589 |
if (Platform.accelKey(event)) { |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
// Stop the event propagation. |
| 594 |
event.preventDefault(); |
| 595 |
event.stopPropagation(); |
| 596 |
|
| 597 |
// Fetch the selection model. |
| 598 |
let model = grid.selectionModel; |
| 599 |
|
| 600 |
// Scroll by page if there is no selection model. |
| 601 |
if (!model || model.selectionMode === 'column') { |
| 602 |
grid.scrollByPage('up'); |
| 603 |
return; |
| 604 |
} |
| 605 |
|
| 606 |
// Get the normal number of cells in the page height. |
| 607 |
let n = Math.floor(grid.pageHeight / grid.defaultSizes.rowHeight); |
| 608 |
|
| 609 |
// Fetch the cursor and selection. |
| 610 |
let r = model.cursorRow; |
| 611 |
let c = model.cursorColumn; |
| 612 |
let cs = model.currentSelection(); |
| 613 |
|
| 614 |
// Set up the selection variables. |
| 615 |
let r1: number; |
| 616 |
let r2: number; |
| 617 |
let c1: number; |
| 618 |
let c2: number; |
| 619 |
let cr: number; |
| 620 |
let cc: number; |
| 621 |
let clear: SelectionModel.ClearMode; |
| 622 |
|
| 623 |
// Select or resize as needed. |
| 624 |
if (event.shiftKey) { |
| 625 |
r1 = cs ? cs.r1 : 0; |
| 626 |
r2 = cs ? cs.r2 - n : 0; |
| 627 |
c1 = cs ? cs.c1 : 0; |
| 628 |
c2 = cs ? cs.c2 : 0; |
| 629 |
cr = r; |
| 630 |
cc = c; |
| 631 |
clear = 'current'; |
| 632 |
} else { |
| 633 |
r1 = cs ? cs.r1 - n : 0; |
| 634 |
r2 = r1; |
| 635 |
c1 = c; |
| 636 |
c2 = c; |
| 637 |
cr = r1; |
| 638 |
cc = c; |
| 639 |
clear = 'all'; |
| 640 |
} |
| 641 |
|
| 642 |
// Create the new selection. |
| 643 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 644 |
|
| 645 |
// Re-fetch the current selection. |
| 646 |
cs = model.currentSelection(); |
| 647 |
|
| 648 |
// Bail if there is no selection. |
| 649 |
if (!cs) { |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
// Scroll the grid appropriately. |
| 654 |
grid.scrollToRow(cs.r2); |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Handle the `'PageDown'` key press for the data grid. |
| 659 |
* |
| 660 |
* @param grid - The data grid of interest. |
| 661 |
* |
| 662 |
* @param event - The keyboard event of interest. |
| 663 |
*/ |
| 664 |
protected onPageDown(grid: DataGrid, event: KeyboardEvent): void { |
| 665 |
// Ignore the event if the accel key is pressed. |
| 666 |
if (Platform.accelKey(event)) { |
| 667 |
return; |
| 668 |
} |
| 669 |
|
| 670 |
// Stop the event propagation. |
| 671 |
event.preventDefault(); |
| 672 |
event.stopPropagation(); |
| 673 |
|
| 674 |
// Fetch the selection model. |
| 675 |
let model = grid.selectionModel; |
| 676 |
|
| 677 |
// Scroll by page if there is no selection model. |
| 678 |
if (!model || model.selectionMode === 'column') { |
| 679 |
grid.scrollByPage('down'); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
// Get the normal number of cells in the page height. |
| 684 |
let n = Math.floor(grid.pageHeight / grid.defaultSizes.rowHeight); |
| 685 |
|
| 686 |
// Fetch the cursor and selection. |
| 687 |
let r = model.cursorRow; |
| 688 |
let c = model.cursorColumn; |
| 689 |
let cs = model.currentSelection(); |
| 690 |
|
| 691 |
// Set up the selection variables. |
| 692 |
let r1: number; |
| 693 |
let r2: number; |
| 694 |
let c1: number; |
| 695 |
let c2: number; |
| 696 |
let cr: number; |
| 697 |
let cc: number; |
| 698 |
let clear: SelectionModel.ClearMode; |
| 699 |
|
| 700 |
// Select or resize as needed. |
| 701 |
if (event.shiftKey) { |
| 702 |
r1 = cs ? cs.r1 : 0; |
| 703 |
r2 = cs ? cs.r2 + n : 0; |
| 704 |
c1 = cs ? cs.c1 : 0; |
| 705 |
c2 = cs ? cs.c2 : 0; |
| 706 |
cr = r; |
| 707 |
cc = c; |
| 708 |
clear = 'current'; |
| 709 |
} else { |
| 710 |
r1 = cs ? cs.r1 + n : 0; |
| 711 |
r2 = r1; |
| 712 |
c1 = c; |
| 713 |
c2 = c; |
| 714 |
cr = r1; |
| 715 |
cc = c; |
| 716 |
clear = 'all'; |
| 717 |
} |
| 718 |
|
| 719 |
// Create the new selection. |
| 720 |
model.select({ r1, c1, r2, c2, cursorRow: cr, cursorColumn: cc, clear }); |
| 721 |
|
| 722 |
// Re-fetch the current selection. |
| 723 |
cs = model.currentSelection(); |
| 724 |
|
| 725 |
// Bail if there is no selection. |
| 726 |
if (!cs) { |
| 727 |
return; |
| 728 |
} |
| 729 |
|
| 730 |
// Scroll the grid appropriately. |
| 731 |
grid.scrollToRow(cs.r2); |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Handle the `'Escape'` key press for the data grid. |
| 736 |
* |
| 737 |
* @param grid - The data grid of interest. |
| 738 |
* |
| 739 |
* @param event - The keyboard event of interest. |
| 740 |
*/ |
| 741 |
protected onEscape(grid: DataGrid, event: KeyboardEvent): void { |
| 742 |
if (grid.selectionModel) { |
| 743 |
grid.selectionModel.clear(); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
/** |
| 748 |
* Handle the `'Delete'` key press for the data grid. |
| 749 |
* |
| 750 |
* @param grid - The data grid of interest. |
| 751 |
* |
| 752 |
* @param event - The keyboard event of interest. |
| 753 |
*/ |
| 754 |
protected onDelete(grid: DataGrid, event: KeyboardEvent): void { |
| 755 |
if (grid.editable && !grid.selectionModel!.isEmpty) { |
| 756 |
const dataModel = grid.dataModel as MutableDataModel; |
| 757 |
// Fetch the max row and column. |
| 758 |
let maxRow = dataModel.rowCount('body') - 1; |
| 759 |
let maxColumn = dataModel.columnCount('body') - 1; |
| 760 |
|
| 761 |
for (let s of grid.selectionModel!.selections()) { |
| 762 |
// Clamp the cell to the model bounds. |
| 763 |
let sr1 = Math.max(0, Math.min(s.r1, maxRow)); |
| 764 |
let sc1 = Math.max(0, Math.min(s.c1, maxColumn)); |
| 765 |
let sr2 = Math.max(0, Math.min(s.r2, maxRow)); |
| 766 |
let sc2 = Math.max(0, Math.min(s.c2, maxColumn)); |
| 767 |
|
| 768 |
for (let r = sr1; r <= sr2; ++r) { |
| 769 |
for (let c = sc1; c <= sc2; ++c) { |
| 770 |
dataModel.setData('body', r, c, null); |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Handle the `'C'` key press for the data grid. |
| 779 |
* |
| 780 |
* @param grid - The data grid of interest. |
| 781 |
* |
| 782 |
* @param event - The keyboard event of interest. |
| 783 |
*/ |
| 784 |
protected onKeyC(grid: DataGrid, event: KeyboardEvent): void { |
| 785 |
// Bail early if the modifiers aren't correct for copy. |
| 786 |
if (event.shiftKey || !Platform.accelKey(event)) { |
| 787 |
return; |
| 788 |
} |
| 789 |
|
| 790 |
// Stop the event propagation. |
| 791 |
event.preventDefault(); |
| 792 |
event.stopPropagation(); |
| 793 |
|
| 794 |
// Copy the current selection to the clipboard. |
| 795 |
grid.copyToClipboard(); |
| 796 |
} |
| 797 |
|
| 798 |
private _disposed = false; |
| 799 |
} |