From cf096889db7839ec0d35711613158556d16afd0c Mon Sep 17 00:00:00 2001 From: CamilleLegeron Date: Fri, 13 Jan 2023 05:13:26 +0100 Subject: [PATCH] feat(TypeConversion): allow converting list of string with quotes to ChoiceList (#397) --- app/client/components/TypeConversion.ts | 3 ++- test/nbrowser/ChoiceList.ts | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/client/components/TypeConversion.ts b/app/client/components/TypeConversion.ts index 0fbfd399..cb97fa32 100644 --- a/app/client/components/TypeConversion.ts +++ b/app/client/components/TypeConversion.ts @@ -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. diff --git a/test/nbrowser/ChoiceList.ts b/test/nbrowser/ChoiceList.ts index d528ecb9..901bcec5 100644 --- a/test/nbrowser/ChoiceList.ts +++ b/test/nbrowser/ChoiceList.ts @@ -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. ]); }