feat(TypeConversion): allow converting list of string with quotes to ChoiceList (#397)

pull/403/head
CamilleLegeron 1 year ago committed by GitHub
parent 9e009bbab9
commit cf096889db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@
import {isString} from 'app/client/lib/sessionObs';
import {DocModel} from 'app/client/models/DocModel';
import {ColumnRec} from 'app/client/models/entities/ColumnRec';
import {csvDecodeRow} from 'app/common/csvFormat';
import * as gristTypes from 'app/common/gristTypes';
import {isFullReferencingType} from 'app/common/gristTypes';
import * as gutil from 'app/common/gutil';
@ -175,7 +176,7 @@ export async function prepTransformColInfo(docModel: DocModel, origCol: ColumnRe
for (let value of tableData.getColValues(sourceCol.colId()) || []) {
if (value === null) { continue; }
value = String(decodeObject(value)).trim();
const tags: unknown[] = (value.startsWith('[') && gutil.safeJsonParse(value, null)) || value.split(",");
const tags: unknown[] = (value.startsWith('[') && gutil.safeJsonParse(value, null)) || csvDecodeRow(value);
for (const tag of tags) {
choices.add(String(tag).trim());
if (choices.size > 100) { break; } // Don't suggest excessively many choices.

@ -447,7 +447,7 @@ describe('ChoiceList', function() {
it('should allow reasonable conversions between ChoiceList and other types', async function() {
await gu.enterGridRows({rowNum: 1, col: 'A'},
[['Hello'], ['World'], [' Foo,Bar;Baz!,']]);
[['Hello'], ['World'], [' Foo,Bar;Baz!,"Qux, quux corge", "80\'s",']]);
await testTextChoiceListConversions();
});
@ -468,17 +468,17 @@ describe('ChoiceList', function() {
// Check that choices got populated.
await driver.find('.test-right-tab-field').click();
assert.deepEqual(await getChoiceLabels(), ['Hello', 'World', 'Foo', 'Bar;Baz!']);
assert.deepEqual(await getChoiceLabels(), ['Hello', 'World', 'Foo', 'Bar;Baz!', 'Qux, quux corge', '80\'s']);
assert.deepEqual(
await getChoiceColors(),
[UNSET_FILL, UNSET_FILL, UNSET_FILL, UNSET_FILL]
[UNSET_FILL, UNSET_FILL, UNSET_FILL, UNSET_FILL, UNSET_FILL, UNSET_FILL]
);
// Check that the result contains the right tags.
assert.deepEqual(await gu.getVisibleGridCells({rowNums: [1, 2, 3], cols: ['A']}), [
'Hello',
'World',
'Foo\nBar;Baz!'
'Foo\nBar;Baz!\nQux, quux corge\n80\'s'
]);
await gu.checkForErrors();
@ -494,17 +494,21 @@ describe('ChoiceList', function() {
[
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
]
);
// Open a cell to see the actual tags.
await gu.getCell({rowNum: 3, col: 'A'}).click();
await driver.sendKeys(Key.ENTER);
assert.deepEqual(await getEditorTokens(), ['Foo', 'Bar;Baz!']);
assert.deepEqual(await getEditorTokensIsInvalid(), [false, false]);
assert.deepEqual(await getEditorTokens(), ['Foo', 'Bar;Baz!', 'Qux, quux corge', '80\'s']);
assert.deepEqual(await getEditorTokensIsInvalid(), [ false, false, false, false ]);
assert.deepEqual(
await getEditorTokenStyles(),
[
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE},
{fillColor: DEFAULT_FILL, textColor: DEFAULT_TEXT, ...VALID_CHOICE}
]
@ -520,7 +524,7 @@ describe('ChoiceList', function() {
assert.deepEqual(await gu.getVisibleGridCells({rowNums: [1, 2, 3], cols: ['A']}), [
'Hello',
'World',
'Foo, Bar;Baz!, hooray'
'Foo, Bar;Baz!, "Qux, quux corge", 80\'s, hooray'
]);
// Undo the cell change and both conversions (back to ChoiceList, back to Text), and check
@ -529,7 +533,7 @@ describe('ChoiceList', function() {
assert.deepEqual(await gu.getVisibleGridCells({rowNums: [1, 2, 3], cols: ['A']}), [
'Hello',
'World',
' Foo,Bar;Baz!,', // That's the text originally entered into this Text cell.
' Foo,Bar;Baz!,"Qux, quux corge", "80\'s",', // That's the text originally entered into this Text cell.
]);
}

Loading…
Cancel
Save