Skip to content

Commit

Permalink
Improve typings for table embed
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 17, 2022
1 parent 1d54cae commit 1d95615
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
9 changes: 7 additions & 2 deletions modules/tableEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import Delta, { OpIterator } from 'quill-delta';
import Op from 'quill-delta/dist/Op';
import Module from '../core/module';

export type CellData = {
content?: Delta['ops'];
attributes?: Record<string, unknown>;
};

export type TableRowColumnOp = Omit<Op, 'insert'> & {
insert?: { id: string };
};

export interface TableData {
rows?: Delta['ops'];
columns?: Delta['ops'];
rows?: TableRowColumnOp[];
columns?: TableRowColumnOp[];
cells?: Record<string, CellData>;
}

Expand Down
10 changes: 6 additions & 4 deletions test/random.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions test/random.ts
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -18,7 +22,9 @@ const getRandomRowColumnId = () => {
.join('');
};

const attachAttributes = obj => {
const attachAttributes = <T extends object>(
obj: T,
): T & { attributes: Record<string, unknown> } => {
const getRandomAttributes = () => {
const attributeCount = random([1, 4, 8]);
const allowedAttributes = ['align', 'background', 'color', 'font'];
Expand All @@ -30,8 +36,10 @@ const attachAttributes = obj => {
return attributes;
};
if (random([true, false])) {
// @ts-expect-error
obj.attributes = getRandomAttributes();
}
// @ts-expect-error
return obj;
};

Expand Down Expand Up @@ -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<TableRowColumnOp>(() =>
attachAttributes({ insert: { id: getRandomRowColumnId() } }),
);
};

const getRandomBase = () => {
Expand All @@ -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();
}
Expand Down

0 comments on commit 1d95615

Please sign in to comment.