mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
c470c4041b
Summary: Some things (like rendering cells) use the `visibleCol` for `createFormatter`, while other things (like `CopySelection`) used the `displayCol`. For references, the display column has type Any and doesn't know about the original formatting. This resulted in formatting being lost when copying from reference columns even though formatting was preserved when copying from the original (visible) column which looked identical. This diff fixes this and ensures that `createFormatter` is always used with the `visibleCol`. This was agreed on in https://grist.slack.com/archives/C0234CPPXPA/p1639571321043000 Additionally: - Replaces the functions `createVisibleColFormatter` computed properties `visibleColFormatter` as suggested by a `TODO`. - Extracts common code from `createVisibleColFormatter` in `ColumnRec` and `ViewFieldRec` Test Plan: Fixed a test in CopyPaste which displayed the previous inconsistent behaviour. Reviewers: jarek Reviewed By: jarek Differential Revision: https://phab.getgrist.com/D3189
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import {DocData} from 'app/client/models/DocData';
|
|
import {ColumnRec} from 'app/client/models/entities/ColumnRec';
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
import {TableData} from 'app/client/models/TableData';
|
|
import {getReferencedTableId, isRefListType} from 'app/common/gristTypes';
|
|
import {BaseFormatter} from 'app/common/ValueFormatter';
|
|
|
|
/**
|
|
* Utilities for common operations involving Ref[List] fields.
|
|
*/
|
|
export class ReferenceUtils {
|
|
public readonly refTableId: string;
|
|
public readonly tableData: TableData;
|
|
public readonly formatter: BaseFormatter;
|
|
public readonly visibleColModel: ColumnRec;
|
|
public readonly visibleColId: string;
|
|
public readonly isRefList: boolean;
|
|
|
|
constructor(public readonly field: ViewFieldRec, docData: DocData) {
|
|
const colType = field.column().type();
|
|
const refTableId = getReferencedTableId(colType);
|
|
if (!refTableId) {
|
|
throw new Error("Non-Reference column of type " + colType);
|
|
}
|
|
this.refTableId = refTableId;
|
|
|
|
const tableData = docData.getTable(refTableId);
|
|
if (!tableData) {
|
|
throw new Error("Invalid referenced table " + refTableId);
|
|
}
|
|
this.tableData = tableData;
|
|
|
|
this.formatter = field.visibleColFormatter();
|
|
this.visibleColModel = field.visibleColModel();
|
|
this.visibleColId = this.visibleColModel.colId() || 'id';
|
|
this.isRefList = isRefListType(colType);
|
|
}
|
|
|
|
public idToText(value: unknown) {
|
|
if (typeof value === 'number') {
|
|
return this.formatter.formatAny(this.tableData.getValue(value, this.visibleColId));
|
|
}
|
|
return String(value || '');
|
|
}
|
|
|
|
public autocompleteSearch(text: string) {
|
|
const acIndex = this.tableData.columnACIndexes.getColACIndex(this.visibleColId, this.formatter);
|
|
return acIndex.search(text);
|
|
}
|
|
}
|
|
|
|
export function nocaseEqual(a: string, b: string) {
|
|
return a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
}
|