From 4401ec4d796bc41b64177dfc0d543682f6d02ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Sadzi=C5=84ski?= Date: Mon, 11 Apr 2022 21:33:26 +0200 Subject: [PATCH] (core) Adding onNewRecord event for custom widgets Summary: New onNewRecord event in custom widgets that is triggered when the user selects a blank row. Together with this diff there is PR for grist-help https://github.com/gristlabs/grist-help/pull/150 Test Plan: added test case Reviewers: cyprien Reviewed By: cyprien Differential Revision: https://phab.getgrist.com/D3375 --- app/client/components/CustomView.ts | 2 +- app/client/components/WidgetFrame.ts | 2 +- app/client/declarations.d.ts | 2 +- app/plugin/grist-plugin-api.ts | 14 +++++++++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/client/components/CustomView.ts b/app/client/components/CustomView.ts index 36a08157..2085a6a7 100644 --- a/app/client/components/CustomView.ts +++ b/app/client/components/CustomView.ts @@ -73,7 +73,7 @@ export class CustomView extends Disposable { private _emptyWidgetPage: string; public create(gristDoc: GristDoc, viewSectionModel: ViewSectionRec) { - BaseView.call(this as any, gristDoc, viewSectionModel); + BaseView.call(this as any, gristDoc, viewSectionModel, { 'addNewRow': true }); this._customDef = this.viewSection.customDef; diff --git a/app/client/components/WidgetFrame.ts b/app/client/components/WidgetFrame.ts index a2af7d0a..b214e45f 100644 --- a/app/client/components/WidgetFrame.ts +++ b/app/client/components/WidgetFrame.ts @@ -328,7 +328,7 @@ export class GristViewImpl implements GristView { // Hidden/Visible columns will eventually reflect what is available, but this operation // is not instant - and widget can receive rows with fields that are not in the mapping. const columns: ColumnRec[] = this._visibleColumns(); - const rowIds: number[] = this._baseView.sortedRows.getKoArray().peek() as number[]; + const rowIds = this._baseView.sortedRows.getKoArray().peek().filter(id => id != 'new'); const data: BulkColValues = {}; for (const column of columns) { // Use the colId of the displayCol, which may be different in case of Reference columns. diff --git a/app/client/declarations.d.ts b/app/client/declarations.d.ts index 5178d856..ac5b9b50 100644 --- a/app/client/declarations.d.ts +++ b/app/client/declarations.d.ts @@ -63,7 +63,7 @@ declare module "app/client/components/BaseView" { public isTruncated: ko.Observable; public tableModel: DataTableModel; - constructor(gristDoc: GristDoc, viewSectionModel: any); + constructor(gristDoc: GristDoc, viewSectionModel: any, options?: {addNewRow?: boolean, isPreview?: boolean}); public setCursorPos(cursorPos: CursorPos): void; public createFilterMenu(ctl: IOpenController, filterInfo: FilterInfo, onClose?: () => void): HTMLElement; public buildTitleControls(): DomArg; diff --git a/app/plugin/grist-plugin-api.ts b/app/plugin/grist-plugin-api.ts index c888a62a..dd17c180 100644 --- a/app/plugin/grist-plugin-api.ts +++ b/app/plugin/grist-plugin-api.ts @@ -310,12 +310,24 @@ export function mapColumnNamesBack(data: any, options: { */ export function onRecord(callback: (data: RowRecord | null, mappings: WidgetColumnMap | null) => unknown) { on('message', async function(msg) { - if (!msg.tableId || !msg.rowId) { return; } + if (!msg.tableId || !msg.rowId || msg.rowId === 'new') { return; } const rec = await docApi.fetchSelectedRecord(msg.rowId); callback(rec, await getMappingsIfChanged(msg)); }); } +/** + * For custom widgets, add a handler that will be called whenever the + * new (blank) row is selected. + */ +export function onNewRecord(callback: () => unknown) { + on('message', async function(msg) { + if (msg.tableId && msg.rowId === 'new') { + callback(); + } + }); +} + /** * For custom widgets, add a handler that will be called whenever the * selected records change. Handler will be called with a list of records.