(core) Use MetaTableData more

Summary:
Add more method overrides to MetaTableData for extra type safety.

Use MetaTableData, MetaRowRecord, and getMetaTable in more places.

Test Plan: Mostly it just has to compile. Tested manually that types are being checked more strictly now, e.g. by adding a typo to property names. Some type casting has also been removed.

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D3168
This commit is contained in:
Alex Hall
2021-12-07 13:21:16 +02:00
parent 116fb15eda
commit faec8177ab
18 changed files with 157 additions and 126 deletions

View File

@@ -7,7 +7,7 @@ import {dom, LiveIndex, makeLiveIndex, styled} from 'grainjs';
import {DocComm} from 'app/client/components/DocComm';
import {selectFiles, uploadFiles} from 'app/client/lib/uploads';
import {DocData} from 'app/client/models/DocData';
import {TableData} from 'app/client/models/TableData';
import {MetaTableData} from 'app/client/models/TableData';
import {basicButton, basicButtonLink, cssButtonGroup} from 'app/client/ui2018/buttons';
import {colors, testId, vars} from 'app/client/ui2018/cssVars';
import {editableLabel} from 'app/client/ui2018/editableLabel';
@@ -47,7 +47,7 @@ interface Attachment {
* download, add or remove attachments in the edited cell.
*/
export class AttachmentsEditor extends NewBaseEditor {
private _attachmentsTable: TableData;
private _attachmentsTable: MetaTableData<'_grist_Attachments'>;
private _docComm: DocComm;
private _rowIds: MutableObsArray<number>;
@@ -64,15 +64,15 @@ export class AttachmentsEditor extends NewBaseEditor {
// editValue is abused slightly to indicate a 1-based index of the attachment.
const initRowIndex: number|undefined = (options.editValue && parseInt(options.editValue, 0) - 1) || 0;
this._attachmentsTable = docData.getTable('_grist_Attachments')!;
this._attachmentsTable = docData.getMetaTable('_grist_Attachments');
this._docComm = docData.docComm;
this._rowIds = obsArray(Array.isArray(cellValue) ? cellValue.slice(1) as number[] : []);
this._attachments = computedArray(this._rowIds, (val: number): Attachment => {
const fileIdent: string = this._attachmentsTable.getValue(val, 'fileIdent') as string;
const fileIdent: string = this._attachmentsTable.getValue(val, 'fileIdent')!;
const fileType = mimeTypes.lookup(fileIdent) || 'application/octet-stream';
const filename: Observable<string> =
observable(this._attachmentsTable.getValue(val, 'fileName') as string);
observable(this._attachmentsTable.getValue(val, 'fileName')!);
return {
rowId: val,
fileIdent,
@@ -187,7 +187,7 @@ export class AttachmentsEditor extends NewBaseEditor {
private async _renameAttachment(att: Attachment, fileName: string): Promise<void> {
await this._attachmentsTable.sendTableAction(['UpdateRecord', att.rowId, {fileName}]);
// Update the observable, since it's not on its own observing changes.
att.filename.set(this._attachmentsTable.getValue(att.rowId, 'fileName') as string);
att.filename.set(this._attachmentsTable.getValue(att.rowId, 'fileName')!);
}
private _getUrl(fileIdent: string, filename: string, inline?: boolean): string {

View File

@@ -7,7 +7,7 @@ import {cssRow} from 'app/client/ui/RightPanel';
import {colors, vars} from 'app/client/ui2018/cssVars';
import {NewAbstractWidget} from 'app/client/widgets/NewAbstractWidget';
import {encodeQueryParams} from 'app/common/gutil';
import {TableData} from 'app/common/TableData';
import {MetaTableData} from 'app/client/models/TableData';
import {UploadResult} from 'app/common/uploads';
import {extname} from 'path';
@@ -66,7 +66,7 @@ export interface SavingObservable<T> extends ko.Observable<T> {
*/
export class AttachmentsWidget extends NewAbstractWidget {
private _attachmentsTable: TableData;
private _attachmentsTable: MetaTableData<'_grist_Attachments'>;
private _height: SavingObservable<string>;
constructor(field: any) {
@@ -74,7 +74,7 @@ export class AttachmentsWidget extends NewAbstractWidget {
// TODO: the Attachments table currently treated as metadata, and loaded on open,
// but should probably be loaded on demand as it contains user data, which may be large.
this._attachmentsTable = this._getDocData().getTable('_grist_Attachments')!;
this._attachmentsTable = this._getDocData().getMetaTable('_grist_Attachments');
this._height = this.options.prop('height') as SavingObservable<string>;
@@ -122,12 +122,12 @@ export class AttachmentsWidget extends NewAbstractWidget {
}
protected _buildAttachment(value: number, allValues: Computed<number[]>): Element {
const filename: string = this._attachmentsTable.getValue(value, 'fileName') as string;
const fileIdent: string = this._attachmentsTable.getValue(value, 'fileIdent') as string;
const height: number = this._attachmentsTable.getValue(value, 'imageHeight') as number;
const width: number = this._attachmentsTable.getValue(value, 'imageWidth') as number;
const hasPreview: boolean = Boolean(height);
const ratio: number = hasPreview ? (width / height) : 1;
const filename = this._attachmentsTable.getValue(value, 'fileName')!;
const fileIdent = this._attachmentsTable.getValue(value, 'fileIdent')!;
const height = this._attachmentsTable.getValue(value, 'imageHeight')!;
const width = this._attachmentsTable.getValue(value, 'imageWidth')!;
const hasPreview = Boolean(height);
const ratio = hasPreview ? (width / height) : 1;
return attachmentPreview({title: filename}, // Add a filename tooltip to the previews.
dom.style('height', (use) => `${use(this._height)}px`),
@@ -146,7 +146,7 @@ export class AttachmentsWidget extends NewAbstractWidget {
// Returns the attachment download url.
private _getUrl(rowId: number): string {
const ident = this._attachmentsTable.getValue(rowId, 'fileIdent') as string;
const ident = this._attachmentsTable.getValue(rowId, 'fileIdent');
if (!ident) {
return '';
} else {
@@ -154,7 +154,7 @@ export class AttachmentsWidget extends NewAbstractWidget {
return docComm.docUrl('attachment') + '?' + encodeQueryParams({
...docComm.getUrlParams(),
ident,
name: this._attachmentsTable.getValue(rowId, 'fileName') as string
name: this._attachmentsTable.getValue(rowId, 'fileName')
});
}
}