2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* This file defines the interface for the grist api exposed to SafeBrowser plugins. Grist supports
|
|
|
|
* various ways to require it to cover various scenarios. If writing the main safeBrowser module
|
|
|
|
* (the one referenced by the components.safeBrowser key of the manifest) use
|
|
|
|
* `self.importScript('grist');`, if writing a view include the script in the html `<script src="grist"></script>`
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Example usage (let's assume that Grist let's plugin contributes to a Foo API defined as follow ):
|
|
|
|
*
|
|
|
|
* interface Foo {
|
|
|
|
* foo(name: string): Promise<string>;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* > main.ts:
|
|
|
|
* class MyFoo {
|
|
|
|
* public foo(name: string): Promise<string> {
|
|
|
|
* return new Promise<string>( async resolve => {
|
|
|
|
* grist.rpc.onMessage( e => {
|
|
|
|
* resolve(e.data + name);
|
|
|
|
* });
|
|
|
|
* grist.ready();
|
|
|
|
* await grist.api.render('view1.html', 'fullscreen');
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* grist.rpc.registerImpl<Foo>('grist', new MyFoo()); // can add 3rd arg with type information
|
|
|
|
*
|
|
|
|
* > view1.html includes:
|
|
|
|
* grist.api.render('static/view2.html', 'fullscreen').then( view => {
|
|
|
|
* grist.rpc.onMessage(e => grist.rpc.postMessageForward("main.ts", e.data));
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* > view2.html includes:
|
|
|
|
* grist.rpc.postMessage('view1.html', 'foo ');
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {RenderOptions, RenderTarget} from './RenderOptions';
|
|
|
|
|
2023-08-28 09:16:17 +00:00
|
|
|
// This is the row ID used in the client, but it's helpful to have available in some common code
|
|
|
|
// as well, which is why it's declared here. Note that for data actions and stored data,
|
|
|
|
// 'new' is not used.
|
|
|
|
/**
|
|
|
|
* Represents the id of a row in a table. The value of the `id` column. Might be a number or 'new' value for a new row.
|
|
|
|
*/
|
|
|
|
export type UIRowId = number | 'new';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the position of an active cursor on a page.
|
|
|
|
*/
|
|
|
|
export interface CursorPos {
|
|
|
|
/**
|
|
|
|
* The rowId (value of the `id` column) of the current cursor position, or 'new' if the cursor is on a new row.
|
|
|
|
*/
|
|
|
|
rowId?: UIRowId;
|
|
|
|
/**
|
|
|
|
* The index of the current row in the current view.
|
|
|
|
*/
|
|
|
|
rowIndex?: number;
|
|
|
|
/**
|
|
|
|
* The index of the selected field in the current view.
|
|
|
|
*/
|
|
|
|
fieldIndex?: number;
|
|
|
|
/**
|
|
|
|
* The id of a section that this cursor is in. Ignored when setting a cursor position for a particular view.
|
|
|
|
*/
|
|
|
|
sectionId?: number;
|
2023-12-29 05:17:50 +00:00
|
|
|
/**
|
|
|
|
* When in a linked section, CursorPos may include which rows in the controlling sections are
|
|
|
|
* selected: the rowId in the linking-source section, in _that_ section's linking source, etc.
|
|
|
|
*/
|
|
|
|
linkingRowIds?: UIRowId[];
|
2023-08-28 09:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
export type ComponentKind = "safeBrowser" | "safePython" | "unsafeNode";
|
|
|
|
|
|
|
|
export const RPC_GRISTAPI_INTERFACE = '_grist_api';
|
|
|
|
|
|
|
|
export interface GristAPI {
|
|
|
|
/**
|
|
|
|
* Render the file at `path` into the `target` location in Grist. `path` must be relative to the
|
|
|
|
* root of the plugin's directory and point to an html that is contained within the plugin's
|
2022-02-19 09:46:49 +00:00
|
|
|
* directory. `target` is a predefined location of the Grist UI, it could be `fullscreen` or
|
2020-07-21 13:20:51 +00:00
|
|
|
* identifier for an inline target. Grist provides inline target identifiers in certain call
|
|
|
|
* plugins. E.g. ImportSourceAPI.getImportSource is given a target identifier to allow rende UI
|
|
|
|
* inline in the import dialog. Returns the procId which can be used to dispose the view.
|
|
|
|
*/
|
|
|
|
render(path: string, target: RenderTarget, options?: RenderOptions): Promise<number>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose the process with id procId. If the process was embedded into the UI, removes the
|
|
|
|
* corresponding element from the view.
|
|
|
|
*/
|
|
|
|
dispose(procId: number): Promise<void>;
|
|
|
|
|
|
|
|
// Subscribes to actions for `tableId`. Actions of all subscribed tables are send as rpc's
|
|
|
|
// message.
|
|
|
|
// TODO: document format of messages that can be listened on `rpc.onMessage(...);`
|
|
|
|
subscribe(tableId: string): Promise<void>;
|
|
|
|
|
|
|
|
// Unsubscribe from actions for `tableId`.
|
|
|
|
unsubscribe(tableId: string): Promise<void>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-24 20:22:41 +00:00
|
|
|
* Allows getting information from and interacting with the Grist document to which a plugin or widget is attached.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
|
|
|
export interface GristDocAPI {
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
|
|
|
* Returns an identifier for the document.
|
|
|
|
*/
|
2020-07-21 13:20:51 +00:00
|
|
|
getDocName(): Promise<string>;
|
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
|
|
|
* Returns a sorted list of table IDs.
|
|
|
|
*/
|
2020-07-21 13:20:51 +00:00
|
|
|
listTables(): Promise<string[]>;
|
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
2024-01-11 23:14:24 +00:00
|
|
|
* Returns a complete table of data as {@link GristData.RowRecords | GristData.RowRecords}, including the
|
2022-05-24 20:22:41 +00:00
|
|
|
* 'id' column. Do not modify the returned arrays in-place, especially if used
|
|
|
|
* directly (not over RPC).
|
2022-03-23 13:41:34 +00:00
|
|
|
*/
|
2020-07-21 13:20:51 +00:00
|
|
|
fetchTable(tableId: string): Promise<any>;
|
2022-05-24 20:22:41 +00:00
|
|
|
// TODO: return type is Promise{[colId: string]: CellValue[]}> but cannot be specified
|
|
|
|
// because ts-interface-builder does not properly support index-signature.
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
|
|
|
* Applies an array of user actions.
|
|
|
|
*/
|
2022-03-15 14:35:15 +00:00
|
|
|
applyUserActions(actions: any[][], options?: any): Promise<any>;
|
2022-05-24 20:22:41 +00:00
|
|
|
// TODO: return type should be Promise<ApplyUAResult>, but this requires importing
|
|
|
|
// modules from `app/common` which is not currently supported by the build.
|
2022-07-19 15:39:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a token for out-of-band access to the document.
|
|
|
|
*/
|
|
|
|
getAccessToken(options: AccessTokenOptions): Promise<AccessTokenResult>;
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 21:44:47 +00:00
|
|
|
/**
|
|
|
|
* Options for functions which fetch data from the selected table or record:
|
|
|
|
*
|
2024-01-11 23:14:24 +00:00
|
|
|
* - {@link onRecords}
|
|
|
|
* - {@link onRecord}
|
|
|
|
* - {@link fetchSelectedRecord}
|
|
|
|
* - {@link fetchSelectedTable}
|
|
|
|
* - {@link GristView.fetchSelectedRecord | GristView.fetchSelectedRecord}
|
|
|
|
* - {@link GristView.fetchSelectedTable | GristView.fetchSelectedTable}
|
2023-10-26 21:44:47 +00:00
|
|
|
*
|
|
|
|
* The different methods have different default values for `keepEncoded` and `format`.
|
|
|
|
**/
|
|
|
|
export interface FetchSelectedOptions {
|
|
|
|
/**
|
2024-01-11 23:14:24 +00:00
|
|
|
* - `true`: the returned data will contain raw {@link GristData.CellValue}'s.
|
2023-10-26 21:44:47 +00:00
|
|
|
* - `false`: the values will be decoded, replacing e.g. `['D', timestamp]` with a moment date.
|
|
|
|
*/
|
|
|
|
keepEncoded?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* - `rows`, the returned data will be an array of objects, one per row, with column names as keys.
|
|
|
|
* - `columns`, the returned data will be an object with column names as keys, and arrays of values.
|
|
|
|
*/
|
|
|
|
format?: 'rows' | 'columns';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* - `shown` (default): return only columns that are explicitly shown
|
|
|
|
* in the right panel configuration of the widget. This is the only value that doesn't require full access.
|
|
|
|
* - `normal`: return all 'normal' columns, regardless of whether the user has shown them.
|
|
|
|
* - `all`: also return special invisible columns like `manualSort` and display helper columns.
|
|
|
|
*/
|
|
|
|
includeColumns?: 'shown' | 'normal' | 'all';
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
|
|
|
* Interface for the data backing a single widget.
|
|
|
|
*/
|
2020-07-21 13:20:51 +00:00
|
|
|
export interface GristView {
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
2024-01-11 23:14:24 +00:00
|
|
|
* Like {@link GristDocAPI.fetchTable | GristDocAPI.fetchTable},
|
|
|
|
* but gets data for the custom section specifically, if there is any.
|
2023-10-26 21:44:47 +00:00
|
|
|
* By default, `options.keepEncoded` is `true` and `format` is `columns`.
|
2022-03-23 13:41:34 +00:00
|
|
|
*/
|
2023-10-26 21:44:47 +00:00
|
|
|
fetchSelectedTable(options?: FetchSelectedOptions): Promise<any>;
|
2020-08-13 18:13:16 +00:00
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
2023-10-26 21:44:47 +00:00
|
|
|
* Fetches selected record by its `rowId`. By default, `options.keepEncoded` is `true`.
|
2022-03-23 13:41:34 +00:00
|
|
|
*/
|
2023-10-26 21:44:47 +00:00
|
|
|
fetchSelectedRecord(rowId: number, options?: FetchSelectedOptions): Promise<any>;
|
2022-05-24 20:22:41 +00:00
|
|
|
// TODO: return type is Promise{[colId: string]: CellValue}> but cannot be specified
|
|
|
|
// because ts-interface-builder does not properly support index-signature.
|
2022-02-01 19:51:40 +00:00
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
2023-08-28 09:16:17 +00:00
|
|
|
* Deprecated now. It was used for filtering selected table by `setSelectedRows` method.
|
|
|
|
* Now the preferred way it to use ready message.
|
2022-03-23 13:41:34 +00:00
|
|
|
*/
|
2022-02-01 19:51:40 +00:00
|
|
|
allowSelectBy(): Promise<void>;
|
|
|
|
|
2022-03-23 13:41:34 +00:00
|
|
|
/**
|
2023-08-28 09:16:17 +00:00
|
|
|
* Set the list of selected rows to be used against any linked widget.
|
|
|
|
*/
|
|
|
|
setSelectedRows(rowIds: number[]|null): Promise<void>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the cursor position to a specific row and field. `sectionId` is ignored. Used for widget linking.
|
2022-03-23 13:41:34 +00:00
|
|
|
*/
|
2023-08-28 09:16:17 +00:00
|
|
|
setCursorPos(pos: CursorPos): Promise<void>
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
2022-07-19 15:39:49 +00:00
|
|
|
|
2022-07-27 19:29:20 +00:00
|
|
|
/**
|
|
|
|
* Options when creating access tokens.
|
|
|
|
*/
|
2022-07-19 15:39:49 +00:00
|
|
|
export interface AccessTokenOptions {
|
2022-07-27 19:29:20 +00:00
|
|
|
/** Restrict use of token to reading only */
|
|
|
|
readOnly?: boolean;
|
2022-07-19 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 19:29:20 +00:00
|
|
|
/**
|
|
|
|
* Access token information, including the token string itself, a base URL for
|
|
|
|
* API calls for which the access token can be used, and the time-to-live the
|
|
|
|
* token was created with.
|
|
|
|
*/
|
2022-07-19 15:39:49 +00:00
|
|
|
export interface AccessTokenResult {
|
2022-07-27 19:29:20 +00:00
|
|
|
/**
|
|
|
|
* The token string, which can currently be provided in an api call as a
|
|
|
|
* query parameter called "auth"
|
|
|
|
*/
|
|
|
|
token: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The base url of the API for which the token can be used. Currently tokens
|
|
|
|
* are associated with a single document, so the base url will be something
|
|
|
|
* like `https://..../api/docs/DOCID`
|
|
|
|
*
|
|
|
|
* Access tokens currently only grant access to endpoints dealing with the
|
|
|
|
* internal content of a document (such as tables and cells) and not its
|
|
|
|
* metadata (such as the document name or who it is shared with).
|
|
|
|
*/
|
|
|
|
baseUrl: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of milliseconds the access token will remain valid for
|
|
|
|
* after creation. This will be several minutes.
|
|
|
|
*/
|
|
|
|
ttlMsecs: number;
|
2022-07-19 15:39:49 +00:00
|
|
|
}
|