Skip to content

Commit

Permalink
Improve types for tableEmbed
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 13, 2023
1 parent 691377c commit f5e3b54
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
56 changes: 32 additions & 24 deletions modules/tableEmbed.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -12,17 +12,11 @@ export type TableRowColumnOp = Omit<Op, 'insert'> & {
};

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

export interface TableInsert {
insert: {
'table-embed': TableData;
};
}

const parseCellIdentity = (identity: string) => {
const parts = identity.split(':');
return [Number(parts[0]) - 1, Number(parts[1]) - 1];
Expand Down Expand Up @@ -58,39 +52,53 @@ 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;
}
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<string, CellData>;
}) => {
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;
}

return data;
};

const reindexCellIdentities = (
cells: NonNullable<TableData['cells']>,
{ rows, columns }: TableData,
cells: Record<string, CellData>,
{ rows, columns }: { rows: Delta; columns: Delta },
) => {
const reindexedCells: NonNullable<TableData['cells']> = {};
const reindexedCells: Record<string, CellData> = {};
Object.keys(cells).forEach(identity => {
let [row, column] = parseCellIdentity(identity);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 10 additions & 12 deletions test/fuzz/tableEmbed.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,7 +22,7 @@ const attachAttributes = <T extends object>(
const attributeCount = choose([1, 4, 8]);
const allowedAttributes = ['align', 'background', 'color', 'font'];
const allowedValues = ['center', 'red', 'left', 'uppercase'];
const attributes: Record<string, unknown> = {};
const attributes: AttributeMap = {};
new Array(attributeCount).fill(0).forEach(() => {
attributes[choose(allowedAttributes)] = choose(allowedValues);
});
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -89,7 +89,7 @@ const getRandomChange = (base: Delta) => {
break;
}
if (delta.length() > 0) {
table[field] = delta;
table[field] = delta.ops;
}
});

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

return new Delta(ops);
};

const getRandomBase = () => {
Expand All @@ -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<string, CellData> = {};
new Array(cellCount).fill(0).forEach(() => {
const row = randomInt(rowCount);
const column = randomInt(columnCount);
Expand Down
57 changes: 27 additions & 30 deletions test/unit/modules/tableEmbed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand Down Expand Up @@ -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' }],
Expand Down Expand Up @@ -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' }],
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('tableHandler', () => {
{
retain: {
'table-embed': {
columns: new Delta([{ retain: 1 }, { delete: 1 }]),
columns: [{ retain: 1 }, { delete: 1 }],
},
},
},
Expand All @@ -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' } },
]),
],
},
},
},
Expand Down Expand Up @@ -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 } },
]),
],
},
},
},
Expand Down Expand Up @@ -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!' }],
Expand Down Expand Up @@ -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 } },
]),
],
},
},
},
Expand Down Expand Up @@ -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 }],
Expand Down Expand Up @@ -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' }],
Expand Down

0 comments on commit f5e3b54

Please sign in to comment.