(core) Parsing pasting in reflist columns

Summary: Handle reflist columns in ViewFieldRec.parseValue

Test Plan: Reused section of test of reference columns

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3105
This commit is contained in:
Alex Hall 2021-11-03 14:35:41 +02:00
parent ab7af2b2ef
commit 0f946616b6

View File

@ -1,9 +1,9 @@
import { ReferenceUtils } from 'app/client/lib/ReferenceUtils';
import { ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec } from 'app/client/models/DocModel'; import { ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec } from 'app/client/models/DocModel';
import * as modelUtil from 'app/client/models/modelUtil'; import * as modelUtil from 'app/client/models/modelUtil';
import { ReferenceUtils } from 'app/client/lib/ReferenceUtils';
import * as UserType from 'app/client/widgets/UserType'; import * as UserType from 'app/client/widgets/UserType';
import { DocumentSettings } from 'app/common/DocumentSettings'; import { DocumentSettings } from 'app/common/DocumentSettings';
import { extractTypeFromColType } from 'app/common/gristTypes'; import { isFullReferencingType, isRefListType } from 'app/common/gristTypes';
import { BaseFormatter, createFormatter } from 'app/common/ValueFormatter'; import { BaseFormatter, createFormatter } from 'app/common/ValueFormatter';
import { createParser } from 'app/common/ValueParser'; import { createParser } from 'app/common/ValueParser';
import { Computed, fromKo } from 'grainjs'; import { Computed, fromKo } from 'grainjs';
@ -186,13 +186,30 @@ export function createViewFieldRec(this: ViewFieldRec, docModel: DocModel): void
const docSettings = this.documentSettings(); const docSettings = this.documentSettings();
const type = this.column().type(); const type = this.column().type();
if (extractTypeFromColType(type) === "Ref") { // TODO reflists if (!isFullReferencingType(type)) {
return createParser(type, this.widgetOptionsJson(), docSettings);
} else {
const vcol = this.visibleColModel(); const vcol = this.visibleColModel();
const vcolParser = createParser(vcol.type(), vcol.widgetOptionsJson(), docSettings); const vcolParser = createParser(vcol.type(), vcol.widgetOptionsJson(), docSettings);
const refUtils = new ReferenceUtils(this, docModel.docData); // uses several more observables immediately const refUtils = new ReferenceUtils(this, docModel.docData); // uses several more observables immediately
return (s: string) => refUtils.parseValue(vcolParser(s)); return (s: string) => {
} else { const result = refUtils.parseValue(vcolParser(s));
return createParser(type, this.widgetOptionsJson(), docSettings); // If `result` is a number that means it successfully parsed a reference (row ID).
// For a reflist we need to wrap that row ID in a list.
// Otherwise `result` is a string meaning it couldn't be parsed
// and it will be saved as AltText (i.e. invalid for the column type).
// We don't try to parse multiple references from a single string.
if (isRefListType(type) && typeof result === "number") {
if (result > 0) {
return ['L', result];
} else {
// parseValue returns 0 sometimes because that's the default for reference columns.
// The default for a reflist column is null.
return null;
}
}
return result;
};
} }
}); });