(core) Add ValueParser, use when pasting

Summary:
Add ValueParser file, base class, and subclasses for column types. Only NumericParser is used for now.

Add valueParser field to ViewFieldRec.

Use valueParser when parsing pasted text data in Grid and Detail views.

Test Plan: Add test to nbrowser CopyPaste suite, copying into a numeric column with different currency and locale settings.

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3082
This commit is contained in:
Alex Hall
2021-10-21 20:50:49 +02:00
parent 5b16dd2e86
commit 99878c08ed
6 changed files with 116 additions and 24 deletions

View File

@@ -111,7 +111,7 @@ class IntFormatter extends NumericFormatter {
}
}
interface DateFormatOptions {
export interface DateFormatOptions {
dateFormat?: string;
}
@@ -132,7 +132,7 @@ class DateFormatter extends BaseFormatter {
}
}
interface DateTimeFormatOptions extends DateFormatOptions {
export interface DateTimeFormatOptions extends DateFormatOptions {
timeFormat?: string;
}

74
app/common/ValueParser.ts Normal file
View File

@@ -0,0 +1,74 @@
import { DocumentSettings } from 'app/common/DocumentSettings';
import * as gristTypes from 'app/common/gristTypes';
import * as gutil from 'app/common/gutil';
import { getCurrency, NumberFormatOptions } from 'app/common/NumberFormat';
import NumberParse from 'app/common/NumberParse';
import { parseDate } from 'app/common/parseDate';
import { DateTimeFormatOptions, FormatOptions } from 'app/common/ValueFormatter';
export class ValueParser {
constructor(public type: string, public widgetOpts: object, public docSettings: DocumentSettings) {
}
public cleanParse(value: string): any {
if (!value) {
return value;
}
return this.parse(value) ?? value;
}
public parse(value: string): any {
return value;
}
}
export class NumericParser extends ValueParser {
private _parse: NumberParse;
constructor(type: string, options: NumberFormatOptions, docSettings: DocumentSettings) {
super(type, options, docSettings);
this._parse = new NumberParse(docSettings.locale, getCurrency(options, docSettings));
}
public parse(value: string): number | null {
return this._parse.parse(value);
}
}
class DateParser extends ValueParser {
public parse(value: string): any {
return parseDate(value, this.widgetOpts);
}
}
class DateTimeParser extends DateParser {
constructor(type: string, widgetOpts: DateTimeFormatOptions, docSettings: DocumentSettings) {
super(type, widgetOpts, docSettings);
const timezone = gutil.removePrefix(type, "DateTime:") || '';
this.widgetOpts = {...widgetOpts, timezone};
}
}
const parsers: { [type: string]: typeof ValueParser } = {
Numeric: NumericParser,
Int: NumericParser,
Date: DateParser,
DateTime: DateTimeParser,
};
// TODO these are not ready yet
delete parsers.Date;
delete parsers.DateTime;
export function createParser(
type: string, widgetOpts: FormatOptions, docSettings: DocumentSettings
): ((value: string) => any) | undefined {
const cls = parsers[gristTypes.extractTypeFromColType(type)];
if (cls) {
const parser = new cls(type, widgetOpts, docSettings);
return parser.cleanParse.bind(parser);
}
}