@supernova-studio/client 0.3.0 → 0.4.1

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,39 @@ 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
+ width: z33.number().optional(),
1053
+ value: z33.array(PageBlockItemTableRow)
1054
+ });
1024
1055
  var DocumentationItemConfiguration = z34.object({
1025
1056
  showSidebar: z34.boolean(),
1026
1057
  header: z34.any()
@@ -3782,6 +3813,11 @@ var BlockParsingUtils = {
3782
3813
  const richText = PageBlockItemMultiRichTextValue.parse(objectValue);
3783
3814
  return richText;
3784
3815
  },
3816
+ tablePropertyValue(item, propertyKey) {
3817
+ const objectValue = this.objectPropertyValue(item, propertyKey);
3818
+ const table = PageBlockItemTableValue.parse(objectValue);
3819
+ return table;
3820
+ },
3785
3821
  embedPropertyValue(item, propertyKey) {
3786
3822
  const objectValue = this.objectPropertyValue(item, propertyKey);
3787
3823
  const embed = PageBlockItemEmbedValue.parse(objectValue);
@@ -3815,7 +3851,7 @@ var BlockDefinitionUtils = {
3815
3851
  return void 0;
3816
3852
  },
3817
3853
  firstTableProperty(definition) {
3818
- const property = definition.item.properties.find((p) => p.type === "RichText");
3854
+ const property = definition.item.properties.find((p) => p.type === "Table");
3819
3855
  if (property)
3820
3856
  return property;
3821
3857
  return void 0;
@@ -3881,6 +3917,14 @@ function blockToProsemirrorNode(block, definition) {
3881
3917
  property: multiRichTextProperty
3882
3918
  });
3883
3919
  }
3920
+ const tableProperty = BlockDefinitionUtils.firstTableProperty(definition);
3921
+ if (tableProperty) {
3922
+ return serializeAsTable({
3923
+ block,
3924
+ definition,
3925
+ property: tableProperty
3926
+ });
3927
+ }
3884
3928
  const embedProperty = BlockDefinitionUtils.firstEmbedProperty(definition);
3885
3929
  const embedType = serializeEmbedType(block.data.packageId);
3886
3930
  if (embedProperty && embedType) {
@@ -3926,7 +3970,7 @@ function serializeAsParagraph(input) {
3926
3970
  return {
3927
3971
  type: "paragraph",
3928
3972
  attrs: serializeBlockNodeAttributes(input),
3929
- content: serializeRichText(input.richTextPropertyValue.value)
3973
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3930
3974
  };
3931
3975
  }
3932
3976
  function serializeAsHeading(input) {
@@ -3936,14 +3980,14 @@ function serializeAsHeading(input) {
3936
3980
  ...serializeBlockNodeAttributes(input),
3937
3981
  level: richTextHeadingLevel(input.property)
3938
3982
  },
3939
- content: serializeRichText(input.richTextPropertyValue.value)
3983
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3940
3984
  };
3941
3985
  }
3942
3986
  function serializeAsBlockquote(input) {
3943
3987
  return {
3944
3988
  type: "blockquote",
3945
3989
  attrs: serializeBlockNodeAttributes(input),
3946
- content: serializeRichText(input.richTextPropertyValue.value)
3990
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3947
3991
  };
3948
3992
  }
3949
3993
  function serializeAsCallout(input) {
@@ -3954,7 +3998,7 @@ function serializeAsCallout(input) {
3954
3998
  ...serializeBlockNodeAttributes(input),
3955
3999
  type: serializeCalloutType(calloutType)
3956
4000
  },
3957
- content: serializeRichText(input.richTextPropertyValue.value)
4001
+ ...serializeRichTextNodePart(input.richTextPropertyValue.value)
3958
4002
  };
3959
4003
  }
3960
4004
  function serializeAsMultiRichTextBlock(input) {
@@ -4011,6 +4055,75 @@ function serializeAsListItem(text) {
4011
4055
  ]
4012
4056
  };
4013
4057
  }
4058
+ function serializeAsTable(input) {
4059
+ const { block, definition, property: tableProperty } = input;
4060
+ const blockItem = BlockParsingUtils.singleBlockItem(block);
4061
+ const table = BlockParsingUtils.tablePropertyValue(blockItem, tableProperty.id);
4062
+ return {
4063
+ type: "tableContainer",
4064
+ attrs: {
4065
+ ...serializeBlockNodeAttributes(input),
4066
+ hasBorder: table.showBorder ?? true
4067
+ },
4068
+ content: [
4069
+ {
4070
+ type: "table",
4071
+ content: table.value.map((row, rowIndex) => {
4072
+ const isHeaderRow = rowIndex === 0 && table.highlightHeaderRow === true;
4073
+ return {
4074
+ type: "tableRow",
4075
+ content: row.cells.map((cell, cellIndex) => {
4076
+ const isHeaderColumn = cellIndex === 0 && table.highlightHeaderColumn === true;
4077
+ return {
4078
+ type: isHeaderRow || isHeaderColumn ? "tableHeader" : "tableCell",
4079
+ attrs: {
4080
+ id: cell.id,
4081
+ textAlign: serializeTableCellAlignment(cell.alignment),
4082
+ colspan: 1,
4083
+ rowspan: 1,
4084
+ ...cell.columnWidth && { colwidth: [cell.columnWidth] }
4085
+ },
4086
+ content: cell.nodes.map(serializeTableNode)
4087
+ };
4088
+ })
4089
+ };
4090
+ })
4091
+ }
4092
+ ]
4093
+ };
4094
+ }
4095
+ function serializeTableCellAlignment(alignment) {
4096
+ switch (alignment) {
4097
+ case "Left":
4098
+ return "left";
4099
+ case "Center":
4100
+ return "center";
4101
+ case "Right":
4102
+ return "right";
4103
+ default:
4104
+ return "left";
4105
+ }
4106
+ }
4107
+ function serializeTableNode(node) {
4108
+ switch (node.type) {
4109
+ case "RichText":
4110
+ return {
4111
+ type: "paragraph",
4112
+ ...serializeRichTextNodePart(node.value)
4113
+ };
4114
+ case "Image":
4115
+ return {
4116
+ type: "image",
4117
+ attrs: {
4118
+ src: node.value?.url
4119
+ }
4120
+ };
4121
+ case "MultiRichText":
4122
+ return {
4123
+ type: "paragraph"
4124
+ };
4125
+ }
4126
+ }
4014
4127
  function serializeAsEmbed(input, embedType) {
4015
4128
  const { block, definition, property: embedProperty } = input;
4016
4129
  const blockItem = BlockParsingUtils.singleBlockItem(block);
@@ -4084,6 +4197,12 @@ function serializeCalloutType(calloutType) {
4084
4197
  return "warning";
4085
4198
  }
4086
4199
  }
4200
+ function serializeRichTextNodePart(richText) {
4201
+ const spans = serializeRichText(richText);
4202
+ if (spans.length)
4203
+ return { content: spans };
4204
+ return {};
4205
+ }
4087
4206
  function serializeRichText(richText) {
4088
4207
  return richText.spans.map(serializeTextSpan).flat();
4089
4208
  }
@@ -4094,7 +4213,7 @@ function serializeTextSpan(span) {
4094
4213
  {
4095
4214
  type: "text",
4096
4215
  text: textParts[0],
4097
- marks
4216
+ ...marks.length && { marks }
4098
4217
  }
4099
4218
  ];
4100
4219
  for (let i = 1; i < textParts.length; i++) {
@@ -4105,7 +4224,7 @@ function serializeTextSpan(span) {
4105
4224
  {
4106
4225
  type: "text",
4107
4226
  text: textParts[i],
4108
- marks
4227
+ ...marks.length && { marks }
4109
4228
  }
4110
4229
  );
4111
4230
  }
@@ -5555,6 +5674,10 @@ function prosemirrorNodeToBlock(prosemirrorNode, definition) {
5555
5674
  if (prosemirrorNode.type === "embed" && embedProperty) {
5556
5675
  return parseAsEmbed(prosemirrorNode, definition, embedProperty);
5557
5676
  }
5677
+ const tableProperty = BlockDefinitionUtils.firstTableProperty(definition);
5678
+ if (tableProperty) {
5679
+ return parseAsTable(prosemirrorNode, definition, tableProperty);
5680
+ }
5558
5681
  if (definition.id === "io.supernova.block.divider") {
5559
5682
  return parseAsDivider(prosemirrorNode, definition);
5560
5683
  }
@@ -5670,6 +5793,146 @@ function parseRichTextAttribute(mark) {
5670
5793
  }
5671
5794
  return null;
5672
5795
  }
5796
+ function parseAsTable(prosemirrorNode, definition, property) {
5797
+ const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5798
+ const variantId = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
5799
+ const hasBorder = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "hasBorder") !== false;
5800
+ const tableChild = prosemirrorNode.content?.find((c) => c.type === "table");
5801
+ if (!tableChild) {
5802
+ return emptyTable(id, variantId, 0);
5803
+ }
5804
+ const rows = tableChild.content?.filter((c) => c.type === "tableRow" && !!c.content?.length) ?? [];
5805
+ if (!rows.length) {
5806
+ return emptyTable(id, variantId, 0);
5807
+ }
5808
+ const rowHeaderCells = rows[0].content?.filter((c) => c.type === "tableHeader").length ?? 0;
5809
+ const columnHeaderCells = rows.filter((r) => r.content?.[0]?.type === "tableHeader").length;
5810
+ const hasHeaderRow = rows[0].content?.length === rowHeaderCells;
5811
+ const hasHeaderColumn = rows.length === columnHeaderCells;
5812
+ const tableValue = {
5813
+ showBorder: hasBorder,
5814
+ highlightHeaderRow: hasHeaderRow,
5815
+ highlightHeaderColumn: hasHeaderColumn,
5816
+ value: []
5817
+ };
5818
+ tableValue.value = rows.map((row) => {
5819
+ return {
5820
+ cells: (row.content ?? []).map(parseAsTableCell)
5821
+ };
5822
+ });
5823
+ return {
5824
+ id,
5825
+ data: {
5826
+ packageId: "io.supernova.block.table",
5827
+ indentLevel: 0,
5828
+ ...variantId && { variantId },
5829
+ items: [
5830
+ {
5831
+ id,
5832
+ props: {
5833
+ table: tableValue
5834
+ }
5835
+ }
5836
+ ]
5837
+ }
5838
+ };
5839
+ }
5840
+ function parseAsTableCell(prosemirrorNode) {
5841
+ const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5842
+ const textAlign = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "textAlign");
5843
+ let columnWidth;
5844
+ const columnWidthArray = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "colwidth");
5845
+ if (Array.isArray(columnWidthArray) && typeof columnWidthArray[0] === "number") {
5846
+ columnWidth = columnWidthArray[0];
5847
+ }
5848
+ const tableNodes = (prosemirrorNode.content ?? []).map(parseAsTableNode).filter(nonNullFilter2);
5849
+ return {
5850
+ id,
5851
+ alignment: parseTableCellAlignment(textAlign),
5852
+ ...columnWidth && { columnWidth },
5853
+ nodes: tableNodes.length ? tableNodes : emptyTableCellContent()
5854
+ };
5855
+ }
5856
+ function parseTableCellAlignment(alignment) {
5857
+ if (!alignment)
5858
+ return "Left";
5859
+ switch (alignment) {
5860
+ case "left":
5861
+ return "Left";
5862
+ case "center":
5863
+ return "Center";
5864
+ case "right":
5865
+ return "Right";
5866
+ default:
5867
+ return "Left";
5868
+ }
5869
+ }
5870
+ function parseAsTableNode(prosemirrorNode) {
5871
+ switch (prosemirrorNode.type) {
5872
+ case "paragraph":
5873
+ return {
5874
+ type: "RichText",
5875
+ value: parseRichText(prosemirrorNode.content ?? [])
5876
+ };
5877
+ case "image":
5878
+ const url = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "src");
5879
+ if (!url || typeof url !== "string")
5880
+ return null;
5881
+ return {
5882
+ type: "Image",
5883
+ value: {
5884
+ type: "Upload",
5885
+ url
5886
+ }
5887
+ };
5888
+ default:
5889
+ return null;
5890
+ }
5891
+ }
5892
+ function emptyTable(id, variantId, indentLevel) {
5893
+ const tableValue = {
5894
+ showBorder: true,
5895
+ highlightHeaderColumn: false,
5896
+ highlightHeaderRow: false,
5897
+ value: []
5898
+ };
5899
+ for (let i = 0; i < 2; i++) {
5900
+ tableValue.value.push({
5901
+ cells: []
5902
+ });
5903
+ for (let j = 0; j < 3; j++) {
5904
+ tableValue.value[i].cells.push({
5905
+ id: "",
5906
+ alignment: "Left",
5907
+ nodes: emptyTableCellContent()
5908
+ });
5909
+ }
5910
+ }
5911
+ return {
5912
+ id,
5913
+ data: {
5914
+ packageId: "io.supernova.block.table",
5915
+ indentLevel: indentLevel ?? 0,
5916
+ variantId,
5917
+ items: [
5918
+ {
5919
+ id,
5920
+ props: {
5921
+ table: tableValue
5922
+ }
5923
+ }
5924
+ ]
5925
+ }
5926
+ };
5927
+ }
5928
+ function emptyTableCellContent() {
5929
+ return [
5930
+ {
5931
+ type: "RichText",
5932
+ value: { spans: [] }
5933
+ }
5934
+ ];
5935
+ }
5673
5936
  function parseAsEmbed(prosemirrorNode, definition, property) {
5674
5937
  const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
5675
5938
  const variantId = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
@@ -5745,6 +6008,8 @@ function parseItem(rawItem, definition) {
5745
6008
  const validationResult = valueSchema.safeParse(value);
5746
6009
  if (validationResult.success) {
5747
6010
  sanitizedProps[property.id] = validationResult.data;
6011
+ } else {
6012
+ console.log(validationResult.error.flatten());
5748
6013
  }
5749
6014
  }
5750
6015
  return {
@@ -5808,7 +6073,7 @@ function valueSchemaForPropertyType(type) {
5808
6073
  function parseProsemirrorBlockAttribute(prosemirrorNode, attributeName) {
5809
6074
  const attributeValue = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, attributeName);
5810
6075
  if (!attributeValue) {
5811
- throw new Error(`Attribute ${attributeName} was not defined`);
6076
+ throw new Error(`Attribute ${attributeName} was not defined on node ${prosemirrorNode.type}`);
5812
6077
  }
5813
6078
  return attributeValue;
5814
6079
  }
@@ -6109,6 +6374,12 @@ export {
6109
6374
  PageBlockItemSandboxValue,
6110
6375
  PageBlockItemSingleSelectValue,
6111
6376
  PageBlockItemStorybookValue,
6377
+ PageBlockItemTableCell,
6378
+ PageBlockItemTableImageNode,
6379
+ PageBlockItemTableMultiRichTextNode,
6380
+ PageBlockItemTableNode,
6381
+ PageBlockItemTableRichTextNode,
6382
+ PageBlockItemTableRow,
6112
6383
  PageBlockItemTableValue,
6113
6384
  PageBlockItemTextValue,
6114
6385
  PageBlockItemTokenPropertyValue,
@@ -6122,6 +6393,7 @@ export {
6122
6393
  PageBlockLinkV2,
6123
6394
  PageBlockRenderCodeProperties,
6124
6395
  PageBlockShortcut,
6396
+ PageBlockTableCellAlignment,
6125
6397
  PageBlockTableColumn,
6126
6398
  PageBlockTableProperties,
6127
6399
  PageBlockText,