diff --git a/modules/tableEmbed.ts b/modules/tableEmbed.ts index 1ee90c7a56..1eb962ba30 100644 --- a/modules/tableEmbed.ts +++ b/modules/tableEmbed.ts @@ -1,4 +1,5 @@ import Delta, { OpIterator } from 'quill-delta'; +import Op from 'quill-delta/dist/Op'; import Module from '../core/module'; export type CellData = { @@ -6,9 +7,13 @@ export type CellData = { attributes?: Record; }; +export type TableRowColumnOp = Omit & { + insert?: { id: string }; +}; + export interface TableData { - rows?: Delta['ops']; - columns?: Delta['ops']; + rows?: TableRowColumnOp[]; + columns?: TableRowColumnOp[]; cells?: Record; } diff --git a/test/random.js b/test/random.js index 349068f19b..7a5b58c7c4 100644 --- a/test/random.js +++ b/test/random.js @@ -14,7 +14,7 @@ const getRandomRowColumnId = () => { .map(() => characters.charAt(Math.floor(Math.random() * characters.length))) .join(''); }; -const attachAttributes = obj => { +const attachAttributes = (obj) => { const getRandomAttributes = () => { const attributeCount = random([1, 4, 8]); const allowedAttributes = ['align', 'background', 'color', 'font']; @@ -26,8 +26,10 @@ const attachAttributes = obj => { return attributes; }; if (random([true, false])) { + // @ts-expect-error obj.attributes = getRandomAttributes(); } + // @ts-expect-error return obj; }; const getRandomCellContent = () => { @@ -90,10 +92,10 @@ const getRandomChange = base => { }); return new Delta([attachAttributes({ retain: { 'table-embed': table } })]); }; -const getRandomRowColumnInsert = count => { - return new Delta(new Array(count) +const getRandomRowColumnInsert = (count) => { + return new Array(count) .fill(0) - .map(() => attachAttributes({ insert: { id: getRandomRowColumnId() } }))).ops; + .map(() => attachAttributes({ insert: { id: getRandomRowColumnId() } })); }; const getRandomBase = () => { const rowCount = random([0, 1, 2, 3]); diff --git a/test/random.ts b/test/random.ts index 346fbf7135..46cfda8ec0 100644 --- a/test/random.ts +++ b/test/random.ts @@ -1,5 +1,9 @@ import Delta from 'quill-delta'; -import TableEmbed, { TableData } from '../modules/tableEmbed'; +import TableEmbed, { + CellData, + TableData, + TableRowColumnOp, +} from '../modules/tableEmbed'; // Random testing in order to find unknown issues. @@ -18,7 +22,9 @@ const getRandomRowColumnId = () => { .join(''); }; -const attachAttributes = obj => { +const attachAttributes = ( + obj: T, +): T & { attributes: Record } => { const getRandomAttributes = () => { const attributeCount = random([1, 4, 8]); const allowedAttributes = ['align', 'background', 'color', 'font']; @@ -30,8 +36,10 @@ const attachAttributes = obj => { return attributes; }; if (random([true, false])) { + // @ts-expect-error obj.attributes = getRandomAttributes(); } + // @ts-expect-error return obj; }; @@ -104,12 +112,12 @@ const getRandomChange = base => { return new Delta([attachAttributes({ retain: { 'table-embed': table } })]); }; -const getRandomRowColumnInsert = count => { - return new Delta( - new Array(count) - .fill(0) - .map(() => attachAttributes({ insert: { id: getRandomRowColumnId() } })), - ).ops; +const getRandomRowColumnInsert = (count: number): TableRowColumnOp[] => { + return new Array(count) + .fill(0) + .map(() => + attachAttributes({ insert: { id: getRandomRowColumnId() } }), + ); }; const getRandomBase = () => { @@ -126,7 +134,7 @@ const getRandomBase = () => { const row = random(rowCount); const column = random(columnCount); const identity = `${row + 1}:${column + 1}`; - const cell = attachAttributes({}); + const cell: CellData = attachAttributes({}); if (random([true, false])) { cell.content = getRandomCellContent(); }