(core) Parsing multiple values in reflists, parsing refs without table data in client

Summary:
Added a new object type code `l` (for lookup) which can be used in user actions as a temporary cell value in ref[list] columns and is immediately converted to a row ID in the data engine. The value contains the original raw string (to be used as alt text), the column ID to lookup (typically the visible column) and one or more values to lookup.

For reflists, valueParser now tries parsing the string first as JSON, then as a CSV row, and applies the visible column parsed to each item.

Both ref and reflists columns no longer format the parsed value when there's no matching reference, the original unparsed string is used as alttext instead.

Test Plan: Added another table "Multi-References" to CopyPaste test. Made that table and the References table test with and without table data loaded in the browser.

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3118
This commit is contained in:
Alex Hall
2021-11-09 14:11:37 +02:00
parent b6dd066b7f
commit ecb30eebb8
7 changed files with 238 additions and 54 deletions

View File

@@ -1,12 +1,13 @@
import { ReferenceUtils } from 'app/client/lib/ReferenceUtils';
import { ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec } from 'app/client/models/DocModel';
import {ReferenceUtils} from 'app/client/lib/ReferenceUtils';
import {ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec} from 'app/client/models/DocModel';
import * as modelUtil from 'app/client/models/modelUtil';
import * as UserType from 'app/client/widgets/UserType';
import { DocumentSettings } from 'app/common/DocumentSettings';
import { isFullReferencingType, isRefListType } from 'app/common/gristTypes';
import { BaseFormatter, createFormatter } from 'app/common/ValueFormatter';
import { createParser } from 'app/common/ValueParser';
import { Computed, fromKo } from 'grainjs';
import {csvDecodeRow} from 'app/common/csvFormat';
import {DocumentSettings} from 'app/common/DocumentSettings';
import {isFullReferencingType} from 'app/common/gristTypes';
import {BaseFormatter, createFormatter} from 'app/common/ValueFormatter';
import {createParser} from 'app/common/ValueParser';
import {Computed, fromKo} from 'grainjs';
import * as ko from 'knockout';
// Represents a page entry in the tree of pages.
@@ -192,24 +193,24 @@ export function createViewFieldRec(this: ViewFieldRec, docModel: DocModel): void
const vcol = this.visibleColModel();
const vcolParser = createParser(vcol.type(), vcol.widgetOptionsJson(), docSettings);
const refUtils = new ReferenceUtils(this, docModel.docData); // uses several more observables immediately
return (s: string) => {
const result = refUtils.parseValue(vcolParser(s));
// 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;
if (!refUtils.isRefList) {
return (s: string) => refUtils.parseReference(s, vcolParser(s));
} else {
return (s: string) => {
let values: any[] | null;
try {
values = JSON.parse(s);
} catch {
values = null;
}
}
return result;
};
if (!Array.isArray(values)) {
// csvDecodeRow should never raise an exception
values = csvDecodeRow(s);
}
values = values.map(v => typeof v === "string" ? vcolParser(v) : v);
return refUtils.parseReferenceList(s, values);
};
}
}
});