diff --git a/src/storage/GridFile.test.ts b/src/storage/GridFile.test.ts index 9da2abe9ef..503277b918 100644 --- a/src/storage/GridFile.test.ts +++ b/src/storage/GridFile.test.ts @@ -37,6 +37,28 @@ describe('validateFile()', () => { cell_dependency: undefined, }) ).not.toBe(null); + + const result = validateFile({ + ...v1File, + borders: [ + { x: 0, y: 0, horizontal: { type: 0 }, vertical: { type: 0 } }, + { x: 1, y: 1, horizontal: { type: 1 } }, + { x: 2, y: 2, horizontal: { type: 2 } }, + { x: 3, y: 3, horizontal: { type: 3 } }, + { x: 4, y: 4, horizontal: { type: 4 } }, + { x: 5, y: 5, horizontal: { type: 5 } }, + { x: 6, y: 6, horizontal: {} }, + ], + }); + + expect(result).toHaveProperty('borders[0].horizontal.type', 'line1'); + expect(result).toHaveProperty('borders[0].vertical.type', 'line1'); + expect(result).toHaveProperty('borders[1].horizontal.type', 'line2'); + expect(result).toHaveProperty('borders[2].horizontal.type', 'line3'); + expect(result).toHaveProperty('borders[3].horizontal.type', 'dotted'); + expect(result).toHaveProperty('borders[4].horizontal.type', 'dashed'); + expect(result).toHaveProperty('borders[5].horizontal.type', 'double'); + expect(result).toHaveProperty('borders[6].horizontal', {}); }); test('Upgrades a valid file from v1 to v1.1', () => { diff --git a/src/storage/GridFile.ts b/src/storage/GridFile.ts index a20c2c1b9e..775d3aa477 100644 --- a/src/storage/GridFile.ts +++ b/src/storage/GridFile.ts @@ -99,6 +99,28 @@ export function validateFile(jsonFile: {}): GridFile | null { jsonFile.cell_dependency = ''; } + // The previous enums for borders were integers but now we use strings + // So we have to change them all, e.g. from "3" to "dotted" + // https://github.com/quadratichq/quadratic/pull/308/files#diff-fb2ecd77a7c43aa1f68a862e8866d079391f51b6ae9665059d523221fdf5256fL44-R41 + const enumMapping = { + 0: 'line1', + 1: 'line2', + 2: 'line3', + 3: 'dotted', + 4: 'dashed', + 5: 'double', + }; + if (jsonFile && jsonFile.borders && jsonFile.borders.length > 0) { + ['horizontal', 'vertical'].forEach((key) => { + for (let i = 0; i < jsonFile.borders.length; i++) { + if (jsonFile.borders[i][key] && typeof jsonFile.borders[i][key].type === 'number') { + const value: 0 | 1 | 2 | 3 | 4 | 5 = jsonFile.borders[i][key].type; + jsonFile.borders[i][key].type = enumMapping[value]; + } + } + }); + } + return jsonFile; }; diff --git a/src/storage/useLocalFiles.ts b/src/storage/useLocalFiles.ts index b56c1ddb5e..aba21d1257 100644 --- a/src/storage/useLocalFiles.ts +++ b/src/storage/useLocalFiles.ts @@ -101,7 +101,7 @@ export const useLocalFiles = (sheetController: SheetController): LocalFiles => { // Check if the JSON is a valid quadratic file const quadraticJson = validateFile(json); if (!quadraticJson) { - console.error('Failed to parse JSON as a valid Quadratic file', json); + console.error('Failed to parse JSON as a valid Quadratic file', json, contents); return false; }