Skip to content

Commit

Permalink
fix: allow table-row type to have both cells (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Zak authored Sep 15, 2021
1 parent 3800ad5 commit ec40598
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 188 deletions.
20 changes: 5 additions & 15 deletions packages/rich-text-types/src/nodeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,38 +157,28 @@ export interface EntryHyperlink extends Inline {
}

export interface TableCell extends Block {
nodeType: BLOCKS.TABLE_CELL;
nodeType: BLOCKS.TABLE_HEADER_CELL | BLOCKS.TABLE_CELL;
data: {
colspan?: number;
rowspan?: number;
};
content: Paragraph[];
}

export interface TableHeaderCell extends Block {
export interface TableHeaderCell extends TableCell {
nodeType: BLOCKS.TABLE_HEADER_CELL;
data: {
colspan?: number;
rowspan?: number;
};
content: Paragraph[];
}

// An abstract table row can have both table cell types

export interface TableRow extends Block {
nodeType: BLOCKS.TABLE_ROW;
data: EmptyNodeData;
content: TableCell[];
}

// A helper type to only allow all cells in a row to be header cells
interface TableHeaderRow extends Block {
nodeType: BLOCKS.TABLE_ROW;
data: EmptyNodeData;
content: TableHeaderCell[];
}

export interface Table extends Block {
nodeType: BLOCKS.TABLE;
data: EmptyNodeData;
content: TableRow[] | [TableHeaderRow, ...Array<TableRow>];
content: TableRow[];
}
9 changes: 8 additions & 1 deletion packages/rich-text-types/src/schemaConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export const LIST_ITEM_BLOCKS: TopLevelBlockEnum[] = [
BLOCKS.EMBEDDED_ASSET,
];

export const TABLE_BLOCKS = [
BLOCKS.TABLE,
BLOCKS.TABLE_ROW,
BLOCKS.TABLE_CELL,
BLOCKS.TABLE_HEADER_CELL,
];

/**
* Array of all void block types
*/
Expand All @@ -85,7 +92,7 @@ export const CONTAINERS = {
[BLOCKS.LIST_ITEM]: LIST_ITEM_BLOCKS,
[BLOCKS.QUOTE]: [BLOCKS.PARAGRAPH],
[BLOCKS.TABLE]: [BLOCKS.TABLE_ROW],
[BLOCKS.TABLE_ROW]: [BLOCKS.TABLE_CELL],
[BLOCKS.TABLE_ROW]: [BLOCKS.TABLE_CELL, BLOCKS.TABLE_HEADER_CELL],
[BLOCKS.TABLE_CELL]: [BLOCKS.PARAGRAPH],
[BLOCKS.TABLE_HEADER_CELL]: [BLOCKS.PARAGRAPH],
};
Original file line number Diff line number Diff line change
Expand Up @@ -2820,26 +2820,10 @@ Object {
"additionalProperties": false,
"properties": Object {
"content": Object {
"anyOf": Array [
Object {
"items": Object {
"$ref": "#/definitions/TableRow",
},
"type": "array",
},
Object {
"additionalItems": Object {
"$ref": "#/definitions/TableRow",
},
"items": Array [
Object {
"$ref": "#/definitions/TableHeaderRow",
},
],
"minItems": 1,
"type": "array",
},
],
"items": Object {
"$ref": "#/definitions/TableRow",
},
"type": "array",
},
"data": Object {
"properties": Object {},
Expand Down Expand Up @@ -2883,40 +2867,6 @@ Object {
"nodeType": Object {
"enum": Array [
"table-cell",
],
"type": "string",
},
},
"required": Array [
"content",
"data",
"nodeType",
],
"type": "object",
},
"TableHeaderCell": Object {
"additionalProperties": false,
"properties": Object {
"content": Object {
"items": Object {
"$ref": "#/definitions/Paragraph",
},
"type": "array",
},
"data": Object {
"additionalProperties": false,
"properties": Object {
"colspan": Object {
"type": "number",
},
"rowspan": Object {
"type": "number",
},
},
"type": "object",
},
"nodeType": Object {
"enum": Array [
"table-header-cell",
],
"type": "string",
Expand All @@ -2929,33 +2879,6 @@ Object {
],
"type": "object",
},
"TableHeaderRow": Object {
"additionalProperties": false,
"properties": Object {
"content": Object {
"items": Object {
"$ref": "#/definitions/TableHeaderCell",
},
"type": "array",
},
"data": Object {
"properties": Object {},
"type": "object",
},
"nodeType": Object {
"enum": Array [
"table-row",
],
"type": "string",
},
},
"required": Array [
"content",
"data",
"nodeType",
],
"type": "object",
},
"TableRow": Object {
"additionalProperties": false,
"properties": Object {
Expand Down Expand Up @@ -3136,6 +3059,7 @@ Object {
"nodeType": Object {
"enum": Array [
"table-cell",
"table-header-cell",
],
"type": "string",
},
Expand Down Expand Up @@ -3464,6 +3388,7 @@ Object {
"nodeType": Object {
"enum": Array [
"table-cell",
"table-header-cell",
],
"type": "string",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"type": "object",
"properties": {
"nodeType": {
"type": "string",
"enum": [
"table-cell"
]
"table-cell",
"table-header-cell"
],
"type": "string"
},
"data": {
"type": "object",
Expand Down
7 changes: 4 additions & 3 deletions packages/rich-text-types/src/schemas/generated/table-row.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
"type": "object",
"properties": {
"nodeType": {
"type": "string",
"enum": [
"table-cell"
]
"table-cell",
"table-header-cell"
],
"type": "string"
},
"data": {
"type": "object",
Expand Down
93 changes: 8 additions & 85 deletions packages/rich-text-types/src/schemas/generated/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@
"properties": {}
},
"content": {
"anyOf": [
{
"type": "array",
"items": {
"$ref": "#/definitions/TableRow"
}
},
{
"type": "array",
"items": [
{
"$ref": "#/definitions/TableHeaderRow"
}
],
"minItems": 1,
"additionalItems": {
"$ref": "#/definitions/TableRow"
}
}
]
"type": "array",
"items": {
"$ref": "#/definitions/TableRow"
}
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -75,10 +59,11 @@
"type": "object",
"properties": {
"nodeType": {
"type": "string",
"enum": [
"table-cell"
]
"table-cell",
"table-header-cell"
],
"type": "string"
},
"data": {
"type": "object",
Expand Down Expand Up @@ -225,68 +210,6 @@
"NodeData": {
"additionalProperties": true,
"type": "object"
},
"TableHeaderRow": {
"type": "object",
"properties": {
"nodeType": {
"type": "string",
"enum": [
"table-row"
]
},
"data": {
"type": "object",
"properties": {}
},
"content": {
"type": "array",
"items": {
"$ref": "#/definitions/TableHeaderCell"
}
}
},
"additionalProperties": false,
"required": [
"content",
"data",
"nodeType"
]
},
"TableHeaderCell": {
"type": "object",
"properties": {
"nodeType": {
"type": "string",
"enum": [
"table-header-cell"
]
},
"data": {
"type": "object",
"properties": {
"colspan": {
"type": "number"
},
"rowspan": {
"type": "number"
}
},
"additionalProperties": false
},
"content": {
"type": "array",
"items": {
"$ref": "#/definitions/Paragraph"
}
}
},
"additionalProperties": false,
"required": [
"content",
"data",
"nodeType"
]
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
Expand Down

0 comments on commit ec40598

Please sign in to comment.