(core) Support 'new' row in anchor links.

Summary:
- Anchor links with row of 'new' could be created but weren't parsed or used
  correctly. This fixes it.
- Also adds UIRowId type for row IDs which includes the special 'new' row. It's
  already been used in places as `number|'new'`, this diff gives it a name usable in app/common
  (it doesn't touch another name, RowId, that's been available in app/client).

Test Plan: Added a test assert for anchor links to new row

Reviewers: alexmojaki

Reviewed By: alexmojaki

Differential Revision: https://phab.getgrist.com/D3039
This commit is contained in:
Dmitry S
2021-09-23 18:47:36 -04:00
parent 3c4d71aeca
commit fb583f303a
6 changed files with 42 additions and 25 deletions

View File

@@ -1,16 +1,20 @@
/**
* TableData maintains a single table's data.
*/
import {getDefaultForType} from 'app/common/gristTypes';
import fromPairs = require('lodash/fromPairs');
import {ActionDispatcher} from './ActionDispatcher';
import {ActionDispatcher} from 'app/common/ActionDispatcher';
import {BulkColValues, CellValue, ColInfo, ColInfoWithId, ColValues, DocAction,
isSchemaAction, ReplaceTableData, RowRecord, TableDataAction} from './DocActions';
import {arrayRemove, arraySplice} from './gutil';
import {SchemaTypes} from "./schema";
isSchemaAction, ReplaceTableData, RowRecord, TableDataAction} from 'app/common/DocActions';
import {getDefaultForType} from 'app/common/gristTypes';
import {arrayRemove, arraySplice} from 'app/common/gutil';
import {SchemaTypes} from "app/common/schema";
import {UIRowId} from 'app/common/UIRowId';
import fromPairs = require('lodash/fromPairs');
export interface ColTypeMap { [colId: string]: string; }
type RowFunc<T> = (rowId: number) => T;
type UIRowFunc<T> = (rowId: UIRowId) => T;
interface ColData {
colId: string;
type: string;
@@ -23,7 +27,7 @@ interface ColData {
*/
export interface SkippableRows {
// If there may be skippable rows, return a function to test rowIds for keeping.
getKeepFunc(): undefined | ((rowId: number|"new") => boolean);
getKeepFunc(): undefined | UIRowFunc<boolean>;
// Get a special row id which represents a skipped sequence of rows.
getSkipRowId(): number;
}
@@ -149,9 +153,9 @@ export class TableData extends ActionDispatcher implements SkippableRows {
/**
* Returns the specified value from this table.
*/
public getValue(rowId: number, colId: string): CellValue|undefined {
public getValue(rowId: UIRowId, colId: string): CellValue|undefined {
const colData = this._columns.get(colId);
const index = this._rowMap.get(rowId);
const index = this._rowMap.get(rowId as number); // rowId of 'new' will not be found.
return colData && index !== undefined ? colData.values[index] : undefined;
}
@@ -159,16 +163,16 @@ export class TableData extends ActionDispatcher implements SkippableRows {
* Given a column name, returns a function that takes a rowId and returns the value for that
* column of that row. The returned function is faster than getValue() calls.
*/
public getRowPropFunc(colId: string): undefined | ((rowId: number|"new") => CellValue|undefined) {
public getRowPropFunc(colId: string): undefined | UIRowFunc<CellValue|undefined> {
const colData = this._columns.get(colId);
if (!colData) { return undefined; }
const values = colData.values;
const rowMap = this._rowMap;
return function(rowId: number|"new") { return rowId === "new" ? "new" : values[rowMap.get(rowId)!]; };
return function(rowId: UIRowId) { return values[rowMap.get(rowId as number)!]; };
}
// By default, no rows are skippable, all are kept.
public getKeepFunc(): undefined | ((rowId: number|"new") => boolean) {
public getKeepFunc(): undefined | UIRowFunc<boolean> {
return undefined;
}
@@ -494,7 +498,7 @@ export class MetaTableData<TableId extends keyof SchemaTypes> extends TableData
*/
public getMetaRowPropFunc<ColId extends keyof SchemaTypes[TableId]>(
colId: ColId
): ((rowId: number | "new") => SchemaTypes[TableId][ColId]) {
): RowFunc<SchemaTypes[TableId][ColId]|undefined> {
return super.getRowPropFunc(colId as any) as any;
}
}