mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Use visibleCol instead of displayCol with createFormatter
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
This commit is contained in:
@@ -29,7 +29,7 @@ export class ColumnACIndexes {
|
||||
|
||||
/**
|
||||
* Returns the column index for the given column, using a cached one if available.
|
||||
* The formatter should be created using field.createVisibleColFormatter(). It's assumed that
|
||||
* The formatter should be created using field.visibleColFormatter(). It's assumed that
|
||||
* getColACIndex() is called for the same column with the the same formatter.
|
||||
*/
|
||||
public getColACIndex(colId: string, formatter: BaseFormatter): ACIndex<ICellItem> {
|
||||
|
||||
@@ -8,7 +8,7 @@ import {reportError} from 'app/client/models/errors';
|
||||
import {delay} from 'app/common/delay';
|
||||
import {waitObs} from 'app/common/gutil';
|
||||
import {TableData} from 'app/common/TableData';
|
||||
import {BaseFormatter, createFormatter} from 'app/common/ValueFormatter';
|
||||
import {BaseFormatter} from 'app/common/ValueFormatter';
|
||||
import {Disposable, Observable} from 'grainjs';
|
||||
import debounce = require('lodash/debounce');
|
||||
|
||||
@@ -218,8 +218,7 @@ class FinderImpl implements IFinder {
|
||||
this._sectionTableData = tableModel.tableData;
|
||||
|
||||
this._fieldStepper.array = section.viewFields().peek();
|
||||
this._fieldFormatters = this._fieldStepper.array.map(
|
||||
f => createFormatter(f.displayColModel().type(), f.widgetOptionsJson(), f.documentSettings()));
|
||||
this._fieldFormatters = this._fieldStepper.array.map(f => f.visibleColFormatter.peek());
|
||||
return tableModel;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,12 +52,12 @@ export interface ColumnRec extends IRowModel<"_grist_Tables_column"> {
|
||||
// Returns the rowModel for the referenced table, or null, if is not a reference column.
|
||||
refTable: ko.Computed<TableRec|null>;
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according
|
||||
// to the visibleCol associated with column.
|
||||
visibleColFormatter: ko.Computed<BaseFormatter>;
|
||||
|
||||
// Helper which adds/removes/updates column's displayCol to match the formula.
|
||||
saveDisplayFormula(formula: string): Promise<void>|undefined;
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according
|
||||
// to the visibleCol associated with column. Subscribes to observables if used within a computed.
|
||||
createVisibleColFormatter(): BaseFormatter;
|
||||
}
|
||||
|
||||
export function createColumnRec(this: ColumnRec, docModel: DocModel): void {
|
||||
@@ -117,13 +117,15 @@ export function createColumnRec(this: ColumnRec, docModel: DocModel): void {
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according to the visibleCol
|
||||
// associated with this column. If no visible column available, return formatting for the column itself.
|
||||
// Subscribes to observables if used within a computed.
|
||||
// TODO: It would be better to replace this with a pureComputed whose value is a formatter.
|
||||
this.createVisibleColFormatter = function() {
|
||||
const vcol = this.visibleColModel();
|
||||
const documentSettings = docModel.docInfoRow.documentSettingsJson();
|
||||
return (vcol.getRowId() !== 0) ?
|
||||
createFormatter(vcol.type(), vcol.widgetOptionsJson(), documentSettings) :
|
||||
createFormatter(this.type(), this.widgetOptionsJson(), documentSettings);
|
||||
};
|
||||
this.visibleColFormatter = ko.pureComputed(() => visibleColFormatterForRec(this, this, docModel));
|
||||
}
|
||||
|
||||
export function visibleColFormatterForRec(
|
||||
rec: ColumnRec | ViewFieldRec, colRec: ColumnRec, docModel: DocModel
|
||||
): BaseFormatter {
|
||||
const vcol = rec.visibleColModel();
|
||||
const documentSettings = docModel.docInfoRow.documentSettingsJson();
|
||||
return (vcol.getRowId() !== 0) ?
|
||||
createFormatter(vcol.type(), vcol.widgetOptionsJson(), documentSettings) :
|
||||
createFormatter(colRec.type(), rec.widgetOptionsJson(), documentSettings);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import {ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec} from 'app/client/models/DocModel';
|
||||
import {visibleColFormatterForRec} from 'app/client/models/entities/ColumnRec';
|
||||
import * as modelUtil from 'app/client/models/modelUtil';
|
||||
import * as UserType from 'app/client/widgets/UserType';
|
||||
import {DocumentSettings} from 'app/common/DocumentSettings';
|
||||
import {BaseFormatter, createFormatter} from 'app/common/ValueFormatter';
|
||||
import {BaseFormatter} from 'app/common/ValueFormatter';
|
||||
import {createParser} from 'app/common/ValueParser';
|
||||
import * as ko from 'knockout';
|
||||
|
||||
@@ -69,15 +70,15 @@ export interface ViewFieldRec extends IRowModel<"_grist_Views_section_field"> {
|
||||
|
||||
documentSettings: ko.PureComputed<DocumentSettings>;
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according
|
||||
// to the visibleCol associated with field.
|
||||
visibleColFormatter: ko.Computed<BaseFormatter>;
|
||||
|
||||
createValueParser(): (value: string) => any;
|
||||
|
||||
// Helper which adds/removes/updates field's displayCol to match the formula.
|
||||
saveDisplayFormula(formula: string): Promise<void>|undefined;
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according
|
||||
// to the visibleCol associated with field. Subscribes to observables if used within a computed.
|
||||
createVisibleColFormatter(): BaseFormatter;
|
||||
|
||||
// Helper for Choice/ChoiceList columns, that saves widget options and renames values in a document
|
||||
// in one bundle
|
||||
updateChoices(renameMap: Record<string, string>, options: any): Promise<void>;
|
||||
@@ -164,14 +165,7 @@ export function createViewFieldRec(this: ViewFieldRec, docModel: DocModel): void
|
||||
|
||||
// Helper for Reference/ReferenceList columns, which returns a formatter according to the visibleCol
|
||||
// associated with this field. If no visible column available, return formatting for the field itself.
|
||||
// Subscribes to observables if used within a computed.
|
||||
// TODO: It would be better to replace this with a pureComputed whose value is a formatter.
|
||||
this.createVisibleColFormatter = function() {
|
||||
const vcol = this.visibleColModel();
|
||||
return (vcol.getRowId() !== 0) ?
|
||||
createFormatter(vcol.type(), vcol.widgetOptionsJson(), this.documentSettings()) :
|
||||
createFormatter(this.column().type(), this.widgetOptionsJson(), this.documentSettings());
|
||||
};
|
||||
this.visibleColFormatter = ko.pureComputed(() => visibleColFormatterForRec(this, this.column(), docModel));
|
||||
|
||||
this.createValueParser = function() {
|
||||
const fieldRef = this.useColOptions.peek() ? undefined : this.id.peek();
|
||||
|
||||
Reference in New Issue
Block a user