@@ -16,6 +16,7 @@ const tableUtil = require('../lib/tableUtil');
16
16
const { addToSort, sortBy} = require ( '../lib/sortUtil' ) ;
17
17
18
18
const commands = require ( './commands' ) ;
19
+ const { viewCommands} = require ( './RegionFocusSwitcher' ) ;
19
20
const viewCommon = require ( './viewCommon' ) ;
20
21
const Base = require ( './Base' ) ;
21
22
const BaseView = require ( './BaseView' ) ;
@@ -266,8 +267,10 @@ function GridView(gristDoc, viewSectionModel, isPreview = false) {
266
267
this . onEvent ( this . scrollPane , 'scroll' , this . onScroll ) ;
267
268
268
269
//--------------------------------------------------
269
- // Command group implementing all grid level commands (except cancel)
270
- this . autoDispose ( commands . createGroup ( GridView . gridCommands , this , this . viewSection . enableCommands ) ) ;
270
+ // Command groups implementing all grid level commands (except cancel)
271
+ this . autoDispose ( commands . createGroup ( viewCommands ( GridView . gridCommands , this ) , this , this . viewSection . hasFocus ) ) ;
272
+ this . autoDispose ( commands . createGroup ( GridView . gridFocusedCommands , this , this . viewSection . hasRegionFocus ) ) ;
273
+
271
274
// Cancel command is registered conditionally, only when there is an active
272
275
// cell selection. This command is also used by Raw Data Views, to close the Grid popup.
273
276
const hasSelection = this . autoDispose ( ko . pureComputed ( ( ) =>
@@ -295,6 +298,56 @@ GridView.selectionCommands = {
295
298
// TODO: move commands with modifications to gridEditCommands and use a single guard for
296
299
// readonly state.
297
300
GridView . gridCommands = {
301
+ fillSelectionDown : function ( ) { tableUtil . fillSelectionDown ( this . getSelection ( ) , this . tableModel ) ; } ,
302
+ selectAll : function ( ) { this . selectAll ( ) ; } ,
303
+ insertFieldBefore : function ( event ) { this . _insertField ( event , this . cursor . fieldIndex ( ) ) ; } ,
304
+ insertFieldAfter : function ( event ) { this . _insertField ( event , this . cursor . fieldIndex ( ) + 1 ) ; } ,
305
+ makeHeadersFromRow : function ( ) { this . makeHeadersFromRow ( this . getSelection ( ) ) ; } ,
306
+ renameField : function ( ) { this . renameColumn ( this . cursor . fieldIndex ( ) ) ; } ,
307
+ hideFields : function ( ) { this . hideFields ( this . getSelection ( ) ) ; } ,
308
+ deleteFields : function ( ) { this . _deleteFields ( ) ; } ,
309
+ clearColumns : function ( ) { this . _clearColumns ( this . getSelection ( ) ) ; } ,
310
+ convertFormulasToData : function ( ) { this . _convertFormulasToData ( this . getSelection ( ) ) ; } ,
311
+ sortAsc : function ( ) {
312
+ sortBy ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . ASC ) ;
313
+ } ,
314
+ sortDesc : function ( ) {
315
+ sortBy ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . DESC ) ;
316
+ } ,
317
+ addSortAsc : function ( ) {
318
+ addToSort ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . ASC ) ;
319
+ } ,
320
+ addSortDesc : function ( ) {
321
+ addToSort ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . DESC ) ;
322
+ } ,
323
+ toggleFreeze : function ( ) {
324
+ // get column selection
325
+ const selection = this . getSelection ( ) ;
326
+ // convert it menu option
327
+ const options = this . _getColumnMenuOptions ( selection ) ;
328
+ // generate action that is available for freeze toggle
329
+ const action = freezeAction ( options ) ;
330
+ // if no action, do nothing
331
+ if ( ! action ) { return ; }
332
+ // if grist document is in readonly - simply change the value
333
+ // without saving
334
+ if ( this . isReadonly ) {
335
+ this . viewSection . rawNumFrozen ( action . numFrozen ) ;
336
+ return ;
337
+ }
338
+ this . viewSection . rawNumFrozen . setAndSave ( action . numFrozen ) ;
339
+ } ,
340
+ copy : function ( ) { console . log ( this . getSelection ( ) ) ; return this . copy ( this . getSelection ( ) ) ; } ,
341
+ cut : function ( ) { return this . cut ( this . getSelection ( ) ) ; } ,
342
+ paste : async function ( pasteObj , cutCallback ) {
343
+ if ( this . gristDoc . isReadonly . get ( ) ) { return ; }
344
+ await this . gristDoc . docData . bundleActions ( null , ( ) => this . paste ( pasteObj , cutCallback ) ) ;
345
+ await this . scrollToCursor ( false ) ;
346
+ } ,
347
+ } ;
348
+
349
+ // These commands are enabled only when the grid is the user-focused region.
350
+ GridView . gridFocusedCommands = {
298
351
cursorDown : function ( ) {
299
352
if ( this . cursor . rowIndex ( ) === this . viewData . peekLength - 1 ) {
300
353
// When the cursor is in the bottom row, the view may not be scrolled all the way to
@@ -335,58 +388,10 @@ GridView.gridCommands = {
335
388
ctrlShiftUp : function ( ) { this . _shiftSelectUntilFirstOrLastNonEmptyCell ( { direction : 'up' } ) ; } ,
336
389
ctrlShiftRight : function ( ) { this . _shiftSelectUntilFirstOrLastNonEmptyCell ( { direction : 'right' } ) ; } ,
337
390
ctrlShiftLeft : function ( ) { this . _shiftSelectUntilFirstOrLastNonEmptyCell ( { direction : 'left' } ) ; } ,
338
- fillSelectionDown : function ( ) { tableUtil . fillSelectionDown ( this . getSelection ( ) , this . tableModel ) ; } ,
339
-
340
- selectAll : function ( ) { this . selectAll ( ) ; } ,
341
-
342
391
fieldEditSave : function ( ) { this . cursor . rowIndex ( this . cursor . rowIndex ( ) + 1 ) ; } ,
343
392
// Re-define editField after fieldEditSave to make it take precedence for the Enter key.
344
393
editField : function ( event ) { closeRegisteredMenu ( ) ; this . scrollToCursor ( true ) ; this . activateEditorAtCursor ( { event} ) ; } ,
345
- insertFieldBefore : function ( event ) { this . _insertField ( event , this . cursor . fieldIndex ( ) ) ; } ,
346
- insertFieldAfter : function ( event ) { this . _insertField ( event , this . cursor . fieldIndex ( ) + 1 ) ; } ,
347
- makeHeadersFromRow : function ( ) { this . makeHeadersFromRow ( this . getSelection ( ) ) ; } ,
348
- renameField : function ( ) { this . renameColumn ( this . cursor . fieldIndex ( ) ) ; } ,
349
- hideFields : function ( ) { this . hideFields ( this . getSelection ( ) ) ; } ,
350
- deleteFields : function ( ) { this . _deleteFields ( ) ; } ,
351
394
clearValues : function ( ) { this . clearValues ( this . getSelection ( ) ) ; } ,
352
- clearColumns : function ( ) { this . _clearColumns ( this . getSelection ( ) ) ; } ,
353
- convertFormulasToData : function ( ) { this . _convertFormulasToData ( this . getSelection ( ) ) ; } ,
354
- copy : function ( ) { return this . copy ( this . getSelection ( ) ) ; } ,
355
- cut : function ( ) { return this . cut ( this . getSelection ( ) ) ; } ,
356
- paste : async function ( pasteObj , cutCallback ) {
357
- if ( this . gristDoc . isReadonly . get ( ) ) { return ; }
358
- await this . gristDoc . docData . bundleActions ( null , ( ) => this . paste ( pasteObj , cutCallback ) ) ;
359
- await this . scrollToCursor ( false ) ;
360
- } ,
361
- sortAsc : function ( ) {
362
- sortBy ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . ASC ) ;
363
- } ,
364
- sortDesc : function ( ) {
365
- sortBy ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . DESC ) ;
366
- } ,
367
- addSortAsc : function ( ) {
368
- addToSort ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . ASC ) ;
369
- } ,
370
- addSortDesc : function ( ) {
371
- addToSort ( this . viewSection . activeSortSpec , this . currentColumn ( ) . getRowId ( ) , Sort . DESC ) ;
372
- } ,
373
- toggleFreeze : function ( ) {
374
- // get column selection
375
- const selection = this . getSelection ( ) ;
376
- // convert it menu option
377
- const options = this . _getColumnMenuOptions ( selection ) ;
378
- // generate action that is available for freeze toggle
379
- const action = freezeAction ( options ) ;
380
- // if no action, do nothing
381
- if ( ! action ) { return ; }
382
- // if grist document is in readonly - simply change the value
383
- // without saving
384
- if ( this . isReadonly ) {
385
- this . viewSection . rawNumFrozen ( action . numFrozen ) ;
386
- return ;
387
- }
388
- this . viewSection . rawNumFrozen . setAndSave ( action . numFrozen ) ;
389
- } ,
390
395
viewAsCard ( ) {
391
396
const selectedRows = this . selectedRows ( ) ;
392
397
if ( selectedRows . length !== 1 ) { return ; }
0 commit comments