@supernova-studio/client 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supernova-studio/client",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Supernova Data Models",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -10,6 +10,9 @@ import {
10
10
  PageBlockItemMultiRichTextValue,
11
11
  PageBlockTableCellAlignment,
12
12
  PageBlockItemTableNode,
13
+ PageBlockItemTableCell,
14
+ PageBlockItemTableValue,
15
+ PageBlockItemTableRow,
13
16
  } from "@supernova-studio/model";
14
17
  import { PageBlockEditorModel } from "./model/block";
15
18
  import { ProsemirrorNode, ProsemirrorMark } from "./prosemirror/types";
@@ -293,33 +296,43 @@ function serializeAsTable(input: InputWithProperty): ProsemirrorNode {
293
296
  content: [
294
297
  {
295
298
  type: "table",
296
- content: table.value.map<ProsemirrorNode>((row, rowIndex) => {
297
- const isHeaderRow = rowIndex === 0 && table.highlightHeaderRow === true;
298
-
299
- return {
300
- type: "tableRow",
301
- content: row.cells.map<ProsemirrorNode>((cell, cellIndex) => {
302
- const isHeaderColumn = cellIndex === 0 && table.highlightHeaderColumn === true;
303
-
304
- return {
305
- type: isHeaderRow || isHeaderColumn ? "tableHeader" : "tableCell",
306
- attrs: {
307
- id: cell.id,
308
- textAlign: serializeTableCellAlignment(cell.alignment),
309
- colspan: 1,
310
- rowspan: 1,
311
- ...(cell.columnWidth && { colwidth: [cell.columnWidth] }),
312
- },
313
- content: cell.nodes.map<ProsemirrorNode>(serializeTableNode),
314
- };
315
- }),
316
- };
317
- }),
299
+ content: serializeTableRows(table),
318
300
  },
319
301
  ],
320
302
  };
321
303
  }
322
304
 
305
+ function serializeTableRows(table: PageBlockItemTableValue): ProsemirrorNode[] {
306
+ // If no rows are present, we have to generate a least one empty row
307
+ const rows: PageBlockItemTableRow[] = table.value.length ? table.value : [{ cells: [] }];
308
+
309
+ return rows.map<ProsemirrorNode>((row, rowIndex) => {
310
+ const isHeaderRow = rowIndex === 0 && table.highlightHeaderRow === true;
311
+
312
+ // If no cells are present, we have to generate at least one cell
313
+ const cells: PageBlockItemTableCell[] = row.cells.length ? row.cells : [{ id: "", alignment: "Left", nodes: [] }];
314
+
315
+ return {
316
+ type: "tableRow",
317
+ content: cells.map<ProsemirrorNode>((cell, cellIndex) => {
318
+ const isHeaderColumn = cellIndex === 0 && table.highlightHeaderColumn === true;
319
+
320
+ return {
321
+ type: isHeaderRow || isHeaderColumn ? "tableHeader" : "tableCell",
322
+ attrs: {
323
+ ...(cell.id && { id: cell.id }),
324
+ textAlign: serializeTableCellAlignment(cell.alignment),
325
+ colspan: 1,
326
+ rowspan: 1,
327
+ ...(cell.columnWidth && { colwidth: [cell.columnWidth] }),
328
+ },
329
+ content: serializeTableCellNodes(cell),
330
+ };
331
+ }),
332
+ };
333
+ });
334
+ }
335
+
323
336
  function serializeTableCellAlignment(alignment: PageBlockTableCellAlignment) {
324
337
  switch (alignment) {
325
338
  case "Left":
@@ -334,6 +347,16 @@ function serializeTableCellAlignment(alignment: PageBlockTableCellAlignment) {
334
347
  }
335
348
  }
336
349
 
350
+ function serializeTableCellNodes(cell: PageBlockItemTableCell): ProsemirrorNode[] {
351
+ const result = cell.nodes.map(serializeTableNode);
352
+
353
+ if (result.length) {
354
+ return result;
355
+ } else {
356
+ return [{ type: "paragraph" }];
357
+ }
358
+ }
359
+
337
360
  function serializeTableNode(node: PageBlockItemTableNode): ProsemirrorNode {
338
361
  switch (node.type) {
339
362
  case "RichText":