(core) Convert CopySelection and tableUtil to typescript

Summary:
- This should make these easier to work with and make changes to.
- Removes one unused method.

Test Plan: No changes of behavior, existing tests should pass.

Reviewers: alexmojaki

Reviewed By: alexmojaki

Differential Revision: https://phab.getgrist.com/D3091
This commit is contained in:
Dmitry S
2021-10-25 17:59:13 -04:00
parent f7c9919120
commit 65e743931b
6 changed files with 122 additions and 142 deletions

View File

@@ -1,45 +0,0 @@
var ValueFormatter = require('app/common/ValueFormatter');
/**
* The CopySelection class is an abstraction for a subset of currently selected cells.
* @param {Array} rowIds - row ids of the rows selected
* @param {Array} fields - MetaRowModels of the selected view fields
* @param {Object} options.rowStyle - an object that maps rowId to an object containing
* style options. i.e. { 1: { height: 20px } }
* @param {Object} options.colStyle - an object that maps colId to an object containing
* style options.
*/
function CopySelection(tableData, rowIds, fields, options) {
this.fields = fields;
this.rowIds = rowIds || [];
this.colIds = fields.map(f => f.colId());
this.displayColIds = fields.map(f => f.displayColModel().colId());
this.rowStyle = options.rowStyle;
this.colStyle = options.colStyle;
this.columns = fields.map((f, i) => {
let formatter = ValueFormatter.createFormatter(
f.displayColModel().type(),
f.widgetOptionsJson(),
f.documentSettings()
);
let _fmtGetter = tableData.getRowPropFunc(this.displayColIds[i]);
let _rawGetter = tableData.getRowPropFunc(this.colIds[i]);
return {
colId: this.colIds[i],
fmtGetter: rowId => formatter.formatAny(_fmtGetter(rowId)),
rawGetter: rowId => _rawGetter(rowId)
};
});
}
CopySelection.prototype.isCellSelected = function(rowId, colId) {
return this.rowIds.includes(rowId) && this.colIds.includes(colId);
};
CopySelection.prototype.onlyAddRowSelected = function() {
return this.rowIds.length === 1 && this.rowIds[0] === "new";
};
module.exports = CopySelection;

View File

@@ -0,0 +1,60 @@
import type {CellValue} from 'app/common/DocActions';
import type {TableData} from 'app/common/TableData';
import type {UIRowId} from 'app/common/UIRowId';
import {createFormatter} from 'app/common/ValueFormatter';
import type {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
/**
* The CopySelection class is an abstraction for a subset of currently selected cells.
* @param {Array} rowIds - row ids of the rows selected
* @param {Array} fields - MetaRowModels of the selected view fields
* @param {Object} options.rowStyle - an object that maps rowId to an object containing
* style options. i.e. { 1: { height: 20px } }
* @param {Object} options.colStyle - an object that maps colId to an object containing
* style options.
*/
export class CopySelection {
public readonly colIds = this.fields.map(f => f.colId());
public readonly displayColIds = this.fields.map(f => f.displayColModel().colId());
public readonly rowStyle: {[r: number]: object}|undefined;
public readonly colStyle: {[c: string]: object}|undefined;
public readonly columns: Array<{
colId: string,
fmtGetter: (rowId: UIRowId) => string,
rawGetter: (rowId: UIRowId) => CellValue|undefined,
}>;
constructor(tableData: TableData, public readonly rowIds: UIRowId[], public readonly fields: ViewFieldRec[],
options: {
rowStyle?: {[r: number]: object},
colStyle?: {[c: string]: object},
}
) {
this.rowStyle = options.rowStyle;
this.colStyle = options.colStyle;
this.columns = fields.map((f, i) => {
const formatter = createFormatter(
f.displayColModel().type(),
f.widgetOptionsJson(),
f.documentSettings()
);
const _fmtGetter = tableData.getRowPropFunc(this.displayColIds[i])!;
const _rawGetter = tableData.getRowPropFunc(this.colIds[i])!;
return {
colId: this.colIds[i],
fmtGetter: rowId => formatter.formatAny(_fmtGetter(rowId)),
rawGetter: rowId => _rawGetter(rowId)
};
});
}
public isCellSelected(rowId: UIRowId, colId: string): boolean {
return this.rowIds.includes(rowId) && this.colIds.includes(colId);
}
public onlyAddRowSelected(): boolean {
return this.rowIds.length === 1 && this.rowIds[0] === "new";
}
}

View File

@@ -10,7 +10,7 @@ require('app/client/lib/koUtil'); // Needed for subscribeInit.
var Base = require('./Base');
var BaseView = require('./BaseView');
var CopySelection = require('./CopySelection');
var {CopySelection} = require('./CopySelection');
var RecordLayout = require('./RecordLayout');
var commands = require('./commands');
const {RowContextMenu} = require('../ui/RowContextMenu');

View File

@@ -19,7 +19,7 @@ var viewCommon = require('./viewCommon');
var Base = require('./Base');
var BaseView = require('./BaseView');
var selector = require('./Selector');
var CopySelection = require('./CopySelection');
var {CopySelection} = require('./CopySelection');
const {renderAllRows} = require('app/client/components/Printing');
const {reportError} = require('app/client/models/AppModel');
@@ -502,11 +502,6 @@ GridView.prototype.clearSelection = function() {
* @param {CopySelection} selection
*/
GridView.prototype.clearValues = function(selection) {
console.debug('GridView.clearValues', selection);
selection.rowIds = _.without(selection.rowIds, 'new');
// If only the addRow was selected, don't send an action.
if (selection.rowIds.length === 0) { return; }
const options = this._getColumnMenuOptions(selection);
if (options.isFormula === true) {
this.activateEditorAtCursor({ init: ''});