(core) add grist.onRecord and grist.onRecords event handlers

Summary:
This simplifies writing custom widgets that access selected
data.  To access the record at which the cursor is set, and
get any future changes to it as the cursor moves or data
changes, it suffices now to do:

```
grist.ready();
grist.onRecord(record => /* render */);
```

Similarly to access the set of selected records, and get any
changes, it suffices now to do:

```
grist.ready();
grist.onRecords(records => /* render */);
```

The `records` argument will be a list of objects, each of which
is a single record.  This is distinct from the column-based
representation favored in Grist up ontil now.  That remains
how methods like `fetchTable` or `fetchSelectedTable` represent
their results.  In the future, methods named like `fetchRecords`
or `fetchSelectedRecords` could be added that return lists.

Test Plan: extended tests

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2583
This commit is contained in:
Paul Fitzpatrick
2020-08-13 14:13:16 -04:00
parent 4e11b6a922
commit ac5452c89f
6 changed files with 60 additions and 6 deletions

View File

@@ -2,6 +2,10 @@
* This mirrors action definitions from sandbox/grist/actions.py
*/
// Some definitions have moved to be part of plugin API.
import { CellValue } from 'app/plugin/GristData';
export { CellValue, RowRecord } from 'app/plugin/GristData';
import map = require('lodash/map');
export type AddRecord = ['AddRecord', string, number, ColValues];
@@ -83,7 +87,6 @@ export function getTableId(action: DocAction): string {
// Helper types used in the definitions above.
export type CellValue = number|string|boolean|null|[string, any?];
export interface ColValues { [colId: string]: CellValue; }
export interface BulkColValues { [colId: string]: CellValue[]; }
export interface ColInfoMap { [colId: string]: ColInfo; }
@@ -98,11 +101,6 @@ export interface ColInfoWithId extends ColInfo {
id: string;
}
export interface RowRecord {
id: number;
[colId: string]: CellValue;
}
// Multiple records in column-oriented format, i.e. same as BulkColValues but with a mandatory
// 'id' column. This is preferred over TableDataAction in external APIs.
export interface TableColValues {