diff --git a/modules/tableEmbed.ts b/modules/tableEmbed.ts index d2d99a50a7..b44ca354cc 100644 --- a/modules/tableEmbed.ts +++ b/modules/tableEmbed.ts @@ -1,4 +1,4 @@ -import Delta, { OpIterator } from 'quill-delta'; +import Delta, { AttributeMap, OpIterator } from 'quill-delta'; import Op from 'quill-delta/dist/Op'; import Module from '../core/module'; @@ -12,17 +12,11 @@ export type TableRowColumnOp = Omit & { }; export interface TableData { - rows?: Delta; - columns?: Delta; + rows?: Delta['ops']; + columns?: Delta['ops']; cells?: Record; } -export interface TableInsert { - insert: { - 'table-embed': TableData; - }; -} - const parseCellIdentity = (identity: string) => { const parts = identity.split(':'); return [Number(parts[0]) - 1, Number(parts[1]) - 1]; @@ -58,10 +52,16 @@ export const composePosition = (delta: Delta, index: number) => { return newIndex; }; -const compactCellData = ({ content, attributes }: CellData) => { +const compactCellData = ({ + content, + attributes, +}: { + content: Delta; + attributes: AttributeMap | undefined; +}) => { const data: CellData = {}; - if (content && content.length > 0) { - data.content = content; + if (content.length() > 0) { + data.content = content.ops; } if (attributes && Object.keys(attributes).length > 0) { data.attributes = attributes; @@ -69,17 +69,25 @@ const compactCellData = ({ content, attributes }: CellData) => { return Object.keys(data).length > 0 ? data : null; }; -const compactTableData = ({ rows, columns, cells }: TableData) => { +const compactTableData = ({ + rows, + columns, + cells, +}: { + rows: Delta; + columns: Delta; + cells: Record; +}) => { const data: TableData = {}; - if (rows && rows.length() > 0) { - data.rows = rows; + if (rows.length() > 0) { + data.rows = rows.ops; } - if (columns && columns.length() > 0) { - data.columns = columns; + if (columns.length() > 0) { + data.columns = columns.ops; } - if (cells && Object.keys(cells).length) { + if (Object.keys(cells).length) { data.cells = cells; } @@ -87,10 +95,10 @@ const compactTableData = ({ rows, columns, cells }: TableData) => { }; const reindexCellIdentities = ( - cells: NonNullable, - { rows, columns }: TableData, + cells: Record, + { rows, columns }: { rows: Delta; columns: Delta }, ) => { - const reindexedCells: NonNullable = {}; + const reindexedCells: Record = {}; Object.keys(cells).forEach(identity => { let [row, column] = parseCellIdentity(identity); @@ -126,7 +134,7 @@ export const tableHandler = { const content = new Delta(aCell.content || []).compose( new Delta(bCell.content || []), - ).ops; + ); const attributes = Delta.AttributeMap.compose( aCell.attributes, @@ -180,7 +188,7 @@ export const tableHandler = { const content = new Delta(aCell.content || []).transform( new Delta(bCell.content || []), priority, - ).ops; + ); const attributes = Delta.AttributeMap.transform( aCell.attributes, @@ -216,7 +224,7 @@ export const tableHandler = { const baseCell = (base.cells || {})[identity] || {}; const content = new Delta(changeCell.content || []).invert( new Delta(baseCell.content || []), - ).ops; + ); const attributes = Delta.AttributeMap.invert( changeCell.attributes, baseCell.attributes, diff --git a/test/fuzz/tableEmbed.spec.ts b/test/fuzz/tableEmbed.spec.ts index 1316222d98..7e5e155887 100644 --- a/test/fuzz/tableEmbed.spec.ts +++ b/test/fuzz/tableEmbed.spec.ts @@ -1,8 +1,7 @@ -import Delta from 'quill-delta'; +import Delta, { AttributeMap } from 'quill-delta'; import TableEmbed, { CellData, TableData, - TableInsert, TableRowColumnOp, } from '../../modules/tableEmbed'; import { choose, randomInt } from './__helpers__/utils'; @@ -23,7 +22,7 @@ const attachAttributes = ( const attributeCount = choose([1, 4, 8]); const allowedAttributes = ['align', 'background', 'color', 'font']; const allowedValues = ['center', 'red', 'left', 'uppercase']; - const attributes: Record = {}; + const attributes: AttributeMap = {}; new Array(attributeCount).fill(0).forEach(() => { attributes[choose(allowedAttributes)] = choose(allowedValues); }); @@ -54,12 +53,13 @@ const getRandomCellContent = () => { }; const getRandomChange = (base: Delta) => { - const tableInsert = base.ops[0] as TableInsert; const table: TableData = {}; const dimension = { - rows: new Delta(tableInsert.insert['table-embed'].rows || []).length(), + rows: new Delta( + (base.ops[0].insert as any)['table-embed'].rows || [], + ).length(), columns: new Delta( - tableInsert.insert['table-embed'].columns || [], + (base.ops[0].insert as any)['table-embed'].columns || [], ).length(), }; (['rows', 'columns'] as const).forEach(field => { @@ -89,7 +89,7 @@ const getRandomChange = (base: Delta) => { break; } if (delta.length() > 0) { - table[field] = delta; + table[field] = delta.ops; } }); @@ -107,14 +107,12 @@ const getRandomChange = (base: Delta) => { return new Delta([attachAttributes({ retain: { 'table-embed': table } })]); }; -const getRandomRowColumnInsert = (count: number): Delta => { - const ops = new Array(count) +const getRandomRowColumnInsert = (count: number): TableRowColumnOp[] => { + return new Array(count) .fill(0) .map(() => attachAttributes({ insert: { id: getRandomRowColumnId() } }), ); - - return new Delta(ops); }; const getRandomBase = () => { @@ -126,7 +124,7 @@ const getRandomBase = () => { if (rowCount) table.rows = getRandomRowColumnInsert(rowCount); if (columnCount) table.columns = getRandomRowColumnInsert(columnCount); if (cellCount) { - const cells: TableData['cells'] = {}; + const cells: Record = {}; new Array(cellCount).fill(0).forEach(() => { const row = randomInt(rowCount); const column = randomInt(columnCount); diff --git a/test/unit/modules/tableEmbed.spec.ts b/test/unit/modules/tableEmbed.spec.ts index 0494b3d7b7..a779085675 100644 --- a/test/unit/modules/tableEmbed.spec.ts +++ b/test/unit/modules/tableEmbed.spec.ts @@ -47,15 +47,15 @@ describe('tableHandler', () => { { insert: { 'table-embed': { - rows: new Delta([ + rows: [ { insert: { id: '55555555' } }, { insert: { id: '11111111' }, attributes: { height: 20 } }, - ]), - columns: new Delta([ + ], + columns: [ { insert: { id: '22222222' } }, { insert: { id: '33333333' }, attributes: { width: 30 } }, { insert: { id: '44444444' } }, - ]), + ], cells: { '2:2': { content: [{ insert: 'Hello' }], @@ -111,16 +111,16 @@ describe('tableHandler', () => { { insert: { 'table-embed': { - rows: new Delta([ + rows: [ { insert: { id: '55555555' } }, { insert: { id: '66666666' } }, { insert: { id: '11111111' }, attributes: { height: 20 } }, - ]), - columns: new Delta([ + ], + columns: [ { insert: { id: '22222222' } }, { insert: { id: '33333333' }, attributes: { width: 30 } }, { insert: { id: '44444444' } }, - ]), + ], cells: { '3:2': { content: [{ insert: 'Hello' }], @@ -176,16 +176,16 @@ describe('tableHandler', () => { { insert: { 'table-embed': { - rows: new Delta([ + rows: [ { insert: { id: '66666666' } }, { insert: { id: '11111111' } }, { insert: { id: '22222222' }, attributes: { height: 20 } }, - ]), - columns: new Delta([ + ], + columns: [ { insert: { id: '33333333' } }, { insert: { id: '44444444' }, attributes: { width: 30 } }, { insert: { id: '55555555' } }, - ]), + ], cells: { '3:2': { content: [{ insert: 'Hello' }], @@ -228,7 +228,7 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - columns: new Delta([{ retain: 1 }, { delete: 1 }]), + columns: [{ retain: 1 }, { delete: 1 }], }, }, }, @@ -239,13 +239,13 @@ describe('tableHandler', () => { { insert: { 'table-embed': { - rows: new Delta([ + rows: [ { insert: { id: '11111111' }, attributes: { height: 20 } }, - ]), - columns: new Delta([ + ], + columns: [ { insert: { id: '22222222' } }, { insert: { id: '44444444' } }, - ]), + ], }, }, }, @@ -336,16 +336,16 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - rows: new Delta([ + rows: [ { retain: 3 }, { delete: 1 }, { retain: 1, attributes: { height: 50 } }, - ]), - columns: new Delta([ + ], + columns: [ { retain: 3 }, { delete: 1 }, { retain: 2, attributes: { width: 40 } }, - ]), + ], }, }, }, @@ -395,7 +395,7 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - rows: new Delta([{ retain: 1 }, { delete: 1 }]), + rows: [{ retain: 1 }, { delete: 1 }], cells: { '7:1': { content: [{ insert: 'Hello 6:1!' }], @@ -485,11 +485,11 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - rows: new Delta([{ insert: { id: '11111111' } }]), - columns: new Delta([ + rows: [{ insert: { id: '11111111' } }], + columns: [ { retain: 1 }, { insert: { id: '44444444' }, attributes: { width: 100 } }, - ]), + ], }, }, }, @@ -539,7 +539,7 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - rows: new Delta([{ delete: 1 }]), + rows: [{ delete: 1 }], cells: { '1:2': { content: [{ retain: 6 }, { insert: '1' }, { delete: 1 }], @@ -589,10 +589,7 @@ describe('tableHandler', () => { { retain: { 'table-embed': { - columns: new Delta([ - { retain: 1 }, - { insert: { id: '44444444' } }, - ]), + columns: [{ retain: 1 }, { insert: { id: '44444444' } }], cells: { '1:2': { content: [{ insert: 'content' }],