@supernova-studio/client 0.3.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/dist/index.mjs CHANGED
@@ -880,6 +880,7 @@ function traversePageBlocksV1(blocks2, action) {
880
880
  var PageBlockLinkType = z33.enum(["DocumentationItem", "PageHeading", "Url"]);
881
881
  var PageBlockImageType = z33.enum(["Upload", "Asset", "FigmaFrame"]);
882
882
  var PageBlockImageAlignment = z33.enum(["Left", "Center", "Stretch"]);
883
+ var PageBlockTableCellAlignment = z33.enum(["Left", "Center", "Right"]);
883
884
  var PageBlockAppearanceV2 = z33.object({
884
885
  itemBackgroundColor: ColorValue
885
886
  });
@@ -994,9 +995,6 @@ var PageBlockItemStorybookValue = z33.object({
994
995
  showAddons: z33.boolean().optional(),
995
996
  value: z33.string()
996
997
  });
997
- var PageBlockItemTableValue = z33.object({
998
- value: z33.any()
999
- });
1000
998
  var PageBlockItemTextValue = z33.object({
1001
999
  value: z33.string()
1002
1000
  });
@@ -1021,6 +1019,38 @@ var PageBlockItemTokenTypeValue = z33.object({
1021
1019
  var PageBlockItemUrlValue = z33.object({
1022
1020
  value: z33.string()
1023
1021
  });
1022
+ var PageBlockItemTableRichTextNode = z33.object({
1023
+ type: z33.literal("RichText"),
1024
+ value: PageBlockItemRichTextValue.shape.value
1025
+ });
1026
+ var PageBlockItemTableMultiRichTextNode = z33.object({
1027
+ type: z33.literal("MultiRichText"),
1028
+ value: PageBlockItemMultiRichTextValue.shape.value
1029
+ });
1030
+ var PageBlockItemTableImageNode = z33.object({
1031
+ type: z33.literal("Image"),
1032
+ value: PageBlockItemImageValue.shape.value
1033
+ });
1034
+ var PageBlockItemTableNode = z33.discriminatedUnion("type", [
1035
+ PageBlockItemTableRichTextNode,
1036
+ PageBlockItemTableMultiRichTextNode,
1037
+ PageBlockItemTableImageNode
1038
+ ]);
1039
+ var PageBlockItemTableCell = z33.object({
1040
+ id: z33.string(),
1041
+ nodes: z33.array(PageBlockItemTableNode),
1042
+ columnWidth: z33.number().optional(),
1043
+ alignment: PageBlockTableCellAlignment
1044
+ });
1045
+ var PageBlockItemTableRow = z33.object({
1046
+ cells: z33.array(PageBlockItemTableCell)
1047
+ });
1048
+ var PageBlockItemTableValue = z33.object({
1049
+ highlightHeaderColumn: z33.boolean().optional(),
1050
+ highlightHeaderRow: z33.boolean().optional(),
1051
+ showBorder: z33.boolean().optional(),
1052
+ value: z33.array(PageBlockItemTableRow)
1053
+ });
1024
1054
  var DocumentationItemConfiguration = z34.object({
1025
1055
  showSidebar: z34.boolean(),
1026
1056
  header: z34.any()
@@ -3782,6 +3812,11 @@ var BlockParsingUtils = {
3782
3812
  const richText = PageBlockItemMultiRichTextValue.parse(objectValue);
3783
3813
  return richText;
3784
3814
  },
3815
+ tablePropertyValue(item, propertyKey) {
3816
+ const objectValue = this.objectPropertyValue(item, propertyKey);
3817
+ const table = PageBlockItemTableValue.parse(objectValue);
3818
+ return table;
3819
+ },
3785
3820
  embedPropertyValue(item, propertyKey) {
3786
3821
  const objectValue = this.objectPropertyValue(item, propertyKey);
3787
3822
  const embed = PageBlockItemEmbedValue.parse(objectValue);
@@ -3815,7 +3850,7 @@ var BlockDefinitionUtils = {
3815
3850
  return void 0;
3816
3851
  },
3817
3852
  firstTableProperty(definition) {
3818
- const property = definition.item.properties.find((p) => p.type === "RichText");
3853
+ const property = definition.item.properties.find((p) => p.type === "Table");
3819
3854
  if (property)
3820
3855
  return property;
3821
3856
  return void 0;
@@ -3881,6 +3916,14 @@ function blockToProsemirrorNode(block, definition) {
3881
3916
  property: multiRichTextProperty
3882
3917
  });
3883
3918
  }
3919
+ const tableProperty = BlockDefinitionUtils.firstTableProperty(definition);
3920
+ if (tableProperty) {
3921
+ return serializeAsTable({
3922
+ block,
3923
+ definition,
3924
+ property: tableProperty
3925
+ });
3926
+ }
3884
3927
  const embedProperty = BlockDefinitionUtils.firstEmbedProperty(definition);
3885
3928
  const embedType = serializeEmbedType(block.data.packageId);
3886
3929
  if (embedProperty && embedType) {
@@ -3926,7 +3969,7 @@ function serializeAsParagraph(input) {
3926
3969
  return {
3927
3970
  type: "paragraph",
3928
3971
  attrs: serializeBlockNodeAttributes(input),
3929
- content: serializeRichText(input.richTextPropertyValue.value)
3972
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3930
3973
  };
3931
3974
  }
3932
3975
  function serializeAsHeading(input) {
@@ -3936,14 +3979,14 @@ function serializeAsHeading(input) {
3936
3979
  ...serializeBlockNodeAttributes(input),
3937
3980
  level: richTextHeadingLevel(input.property)
3938
3981
  },
3939
- content: serializeRichText(input.richTextPropertyValue.value)
3982
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3940
3983
  };
3941
3984
  }
3942
3985
  function serializeAsBlockquote(input) {
3943
3986
  return {
3944
3987
  type: "blockquote",
3945
3988
  attrs: serializeBlockNodeAttributes(input),
3946
- content: serializeRichText(input.richTextPropertyValue.value)
3989
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3947
3990
  };
3948
3991
  }
3949
3992
  function serializeAsCallout(input) {
@@ -3954,7 +3997,7 @@ function serializeAsCallout(input) {
3954
3997
  ...serializeBlockNodeAttributes(input),
3955
3998
  type: serializeCalloutType(calloutType)
3956
3999
  },
3957
- content: serializeRichText(input.richTextPropertyValue.value)
4000
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3958
4001
  };
3959
4002
  }
3960
4003
  function serializeAsMultiRichTextBlock(input) {
@@ -4011,6 +4054,88 @@ function serializeAsListItem(text) {
4011
4054
  ]
4012
4055
  };
4013
4056
  }
4057
+ function serializeAsTable(input) {
4058
+ const { block, definition, property: tableProperty } = input;
4059
+ const blockItem = BlockParsingUtils.singleBlockItem(block);
4060
+ const table = BlockParsingUtils.tablePropertyValue(blockItem, tableProperty.id);
4061
+ return {
4062
+ type: "tableContainer",
4063
+ attrs: {
4064
+ ...serializeBlockNodeAttributes(input),
4065
+ hasBorder: table.showBorder ?? true
4066
+ },
4067
+ content: [
4068
+ {
4069
+ type: "table",
4070
+ content: serializeTableRows(table)
4071
+ }
4072
+ ]
4073
+ };
4074
+ }
4075
+ function serializeTableRows(table) {
4076
+ const rows = table.value.length ? table.value : [{ cells: [] }];
4077
+ return rows.map((row, rowIndex) => {
4078
+ const isHeaderRow = rowIndex === 0 && table.highlightHeaderRow === true;
4079
+ const cells = row.cells.length ? row.cells : [{ id: "", alignment: "Left", nodes: [] }];
4080
+ return {
4081
+ type: "tableRow",
4082
+ content: cells.map((cell, cellIndex) => {
4083
+ const isHeaderColumn = cellIndex === 0 && table.highlightHeaderColumn === true;
4084
+ return {
4085
+ type: isHeaderRow || isHeaderColumn ? "tableHeader" : "tableCell",
4086
+ attrs: {
4087
+ ...cell.id && { id: cell.id },
4088
+ textAlign: serializeTableCellAlignment(cell.alignment),
4089
+ colspan: 1,
4090
+ rowspan: 1,
4091
+ ...cell.columnWidth && { colwidth: [cell.columnWidth] }
4092
+ },
4093
+ content: serializeTableCellNodes(cell)
4094
+ };
4095
+ })
4096
+ };
4097
+ });
4098
+ }
4099
+ function serializeTableCellAlignment(alignment) {
4100
+ switch (alignment) {
4101
+ case "Left":
4102
+ return "left";
4103
+ case "Center":
4104
+ return "center";
4105
+ case "Right":
4106
+ return "right";
4107
+ default:
4108
+ return "left";
4109
+ }
4110
+ }
4111
+ function serializeTableCellNodes(cell) {
4112
+ const result = cell.nodes.map(serializeTableNode);
4113
+ if (result.length) {
4114
+ return result;
4115
+ } else {
4116
+ return [{ type: "paragraph" }];
4117
+ }
4118
+ }
4119
+ function serializeTableNode(node) {
4120
+ switch (node.type) {
4121
+ case "RichText":
4122
+ return {
4123
+ type: "paragraph",
4124
+ ...serializeRichTextNodePart(node.value)
4125
+ };
4126
+ case "Image":
4127
+ return {
4128
+ type: "image",
4129
+ attrs: {
4130
+ src: node.value?.url
4131
+ }
4132
+ };
4133
+ case "MultiRichText":
4134
+ return {
4135
+ type: "paragraph"
4136
+ };
4137
+ }
4138
+ }
4014
4139
  function serializeAsEmbed(input, embedType) {
4015
4140
  const { block, definition, property: embedProperty } = input;
4016
4141
  const blockItem = BlockParsingUtils.singleBlockItem(block);
@@ -4084,6 +4209,12 @@ function serializeCalloutType(calloutType) {
4084
4209
  return "warning";
4085
4210
  }
4086
4211
  }
4212
+ function serializeRichTextNodePart(richText) {
4213
+ const spans = serializeRichText(richText);
4214
+ if (spans.length)
4215
+ return { content: spans };
4216
+ return {};
4217
+ }
4087
4218
  function serializeRichText(richText) {
4088
4219
  return richText.spans.map(serializeTextSpan).flat();
4089
4220
  }
@@ -4094,7 +4225,7 @@ function serializeTextSpan(span) {
4094
4225
  {
4095
4226
  type: "text",
4096
4227
  text: textParts[0],
4097
- marks
4228
+ ...marks.length && { marks }
4098
4229
  }
4099
4230
  ];
4100
4231
  for (let i = 1; i < textParts.length; i++) {
@@ -4105,7 +4236,7 @@ function serializeTextSpan(span) {
4105
4236
  {
4106
4237
  type: "text",
4107
4238
  text: textParts[i],
4108
- marks
4239
+ ...marks.length && { marks }
4109
4240
  }
4110
4241
  );
4111
4242
  }
@@ -5555,6 +5686,10 @@ function prosemirrorNodeToBlock(prosemirrorNode, definition) {
5555
5686
  if (prosemirrorNode.type === "embed" && embedProperty) {
5556
5687
  return parseAsEmbed(prosemirrorNode, definition, embedProperty);
5557
5688
  }
5689
+ const tableProperty = BlockDefinitionUtils.firstTableProperty(definition);
5690
+ if (tableProperty) {
5691
+ return parseAsTable(prosemirrorNode, definition, tableProperty);
5692
+ }
5558
5693
  if (definition.id === "io.supernova.block.divider") {
5559
5694
  return parseAsDivider(prosemirrorNode, definition);
5560
5695
  }
@@ -5670,6 +5805,146 @@ function parseRichTextAttribute(mark) {
5670
5805
  }
5671
5806
  return null;
5672
5807
  }
5808
+ function parseAsTable(prosemirrorNode, definition, property) {
5809
+ const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5810
+ const variantId = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
5811
+ const hasBorder = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "hasBorder") !== false;
5812
+ const tableChild = prosemirrorNode.content?.find((c) => c.type === "table");
5813
+ if (!tableChild) {
5814
+ return emptyTable(id, variantId, 0);
5815
+ }
5816
+ const rows = tableChild.content?.filter((c) => c.type === "tableRow" && !!c.content?.length) ?? [];
5817
+ if (!rows.length) {
5818
+ return emptyTable(id, variantId, 0);
5819
+ }
5820
+ const rowHeaderCells = rows[0].content?.filter((c) => c.type === "tableHeader").length ?? 0;
5821
+ const columnHeaderCells = rows.filter((r) => r.content?.[0]?.type === "tableHeader").length;
5822
+ const hasHeaderRow = rows[0].content?.length === rowHeaderCells;
5823
+ const hasHeaderColumn = rows.length === columnHeaderCells;
5824
+ const tableValue = {
5825
+ showBorder: hasBorder,
5826
+ highlightHeaderRow: hasHeaderRow,
5827
+ highlightHeaderColumn: hasHeaderColumn,
5828
+ value: []
5829
+ };
5830
+ tableValue.value = rows.map((row) => {
5831
+ return {
5832
+ cells: (row.content ?? []).map(parseAsTableCell)
5833
+ };
5834
+ });
5835
+ return {
5836
+ id,
5837
+ data: {
5838
+ packageId: "io.supernova.block.table",
5839
+ indentLevel: 0,
5840
+ ...variantId && { variantId },
5841
+ items: [
5842
+ {
5843
+ id,
5844
+ props: {
5845
+ table: tableValue
5846
+ }
5847
+ }
5848
+ ]
5849
+ }
5850
+ };
5851
+ }
5852
+ function parseAsTableCell(prosemirrorNode) {
5853
+ const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5854
+ const textAlign = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "textAlign");
5855
+ let columnWidth;
5856
+ const columnWidthArray = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "colwidth");
5857
+ if (Array.isArray(columnWidthArray) && typeof columnWidthArray[0] === "number") {
5858
+ columnWidth = columnWidthArray[0];
5859
+ }
5860
+ const tableNodes = (prosemirrorNode.content ?? []).map(parseAsTableNode).filter(nonNullFilter2);
5861
+ return {
5862
+ id,
5863
+ alignment: parseTableCellAlignment(textAlign),
5864
+ ...columnWidth && { columnWidth },
5865
+ nodes: tableNodes.length ? tableNodes : emptyTableCellContent()
5866
+ };
5867
+ }
5868
+ function parseTableCellAlignment(alignment) {
5869
+ if (!alignment)
5870
+ return "Left";
5871
+ switch (alignment) {
5872
+ case "left":
5873
+ return "Left";
5874
+ case "center":
5875
+ return "Center";
5876
+ case "right":
5877
+ return "Right";
5878
+ default:
5879
+ return "Left";
5880
+ }
5881
+ }
5882
+ function parseAsTableNode(prosemirrorNode) {
5883
+ switch (prosemirrorNode.type) {
5884
+ case "paragraph":
5885
+ return {
5886
+ type: "RichText",
5887
+ value: parseRichText(prosemirrorNode.content ?? [])
5888
+ };
5889
+ case "image":
5890
+ const url = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "src");
5891
+ if (!url || typeof url !== "string")
5892
+ return null;
5893
+ return {
5894
+ type: "Image",
5895
+ value: {
5896
+ type: "Upload",
5897
+ url
5898
+ }
5899
+ };
5900
+ default:
5901
+ return null;
5902
+ }
5903
+ }
5904
+ function emptyTable(id, variantId, indentLevel) {
5905
+ const tableValue = {
5906
+ showBorder: true,
5907
+ highlightHeaderColumn: false,
5908
+ highlightHeaderRow: false,
5909
+ value: []
5910
+ };
5911
+ for (let i = 0; i < 2; i++) {
5912
+ tableValue.value.push({
5913
+ cells: []
5914
+ });
5915
+ for (let j = 0; j < 3; j++) {
5916
+ tableValue.value[i].cells.push({
5917
+ id: "",
5918
+ alignment: "Left",
5919
+ nodes: emptyTableCellContent()
5920
+ });
5921
+ }
5922
+ }
5923
+ return {
5924
+ id,
5925
+ data: {
5926
+ packageId: "io.supernova.block.table",
5927
+ indentLevel: indentLevel ?? 0,
5928
+ variantId,
5929
+ items: [
5930
+ {
5931
+ id,
5932
+ props: {
5933
+ table: tableValue
5934
+ }
5935
+ }
5936
+ ]
5937
+ }
5938
+ };
5939
+ }
5940
+ function emptyTableCellContent() {
5941
+ return [
5942
+ {
5943
+ type: "RichText",
5944
+ value: { spans: [] }
5945
+ }
5946
+ ];
5947
+ }
5673
5948
  function parseAsEmbed(prosemirrorNode, definition, property) {
5674
5949
  const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5675
5950
  const variantId = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
@@ -5810,7 +6085,7 @@ function valueSchemaForPropertyType(type) {
5810
6085
  function parseProsemirrorBlockAttribute(prosemirrorNode, attributeName) {
5811
6086
  const attributeValue = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, attributeName);
5812
6087
  if (!attributeValue) {
5813
- throw new Error(`Attribute ${attributeName} was not defined`);
6088
+ throw new Error(`Attribute ${attributeName} was not defined on node ${prosemirrorNode.type}`);
5814
6089
  }
5815
6090
  return attributeValue;
5816
6091
  }
@@ -6111,6 +6386,12 @@ export {
6111
6386
  PageBlockItemSandboxValue,
6112
6387
  PageBlockItemSingleSelectValue,
6113
6388
  PageBlockItemStorybookValue,
6389
+ PageBlockItemTableCell,
6390
+ PageBlockItemTableImageNode,
6391
+ PageBlockItemTableMultiRichTextNode,
6392
+ PageBlockItemTableNode,
6393
+ PageBlockItemTableRichTextNode,
6394
+ PageBlockItemTableRow,
6114
6395
  PageBlockItemTableValue,
6115
6396
  PageBlockItemTextValue,
6116
6397
  PageBlockItemTokenPropertyValue,
@@ -6124,6 +6405,7 @@ export {
6124
6405
  PageBlockLinkV2,
6125
6406
  PageBlockRenderCodeProperties,
6126
6407
  PageBlockShortcut,
6408
+ PageBlockTableCellAlignment,
6127
6409
  PageBlockTableColumn,
6128
6410
  PageBlockTableProperties,
6129
6411
  PageBlockText,