@supernova-studio/client 0.6.0 → 0.7.0
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.d.mts +48 -32
- package/dist/index.d.ts +48 -32
- package/dist/index.js +82 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +74 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/docs-editor/blocks-to-prosemirror.ts +1 -0
- package/src/docs-editor/prosemirror-to-blocks.ts +87 -28
package/package.json
CHANGED
|
@@ -560,6 +560,7 @@ export function serializeAsCustomBlock(block: PageBlockEditorModel, definition:
|
|
|
560
560
|
definitionId: block.data.packageId,
|
|
561
561
|
variantId: block.data.variantId,
|
|
562
562
|
items: JSON.stringify(items),
|
|
563
|
+
appearance: JSON.stringify(block.data.appearance),
|
|
563
564
|
},
|
|
564
565
|
};
|
|
565
566
|
}
|
|
@@ -38,6 +38,9 @@ import {
|
|
|
38
38
|
PageBlockItemTableCell,
|
|
39
39
|
PageBlockTableCellAlignment,
|
|
40
40
|
DocumentationItemConfiguration,
|
|
41
|
+
PageBlockAppearanceV2,
|
|
42
|
+
PageBlockEditorModelV2,
|
|
43
|
+
ColorValue,
|
|
41
44
|
} from "@supernova-studio/model";
|
|
42
45
|
import { PageBlockEditorModel } from "./model/block";
|
|
43
46
|
import { DocumentationPageEditorModel } from "./model/page";
|
|
@@ -65,7 +68,7 @@ export function prosemirrorDocToPage(
|
|
|
65
68
|
return {
|
|
66
69
|
blocks: (prosemirrorDoc.content ?? [])
|
|
67
70
|
.map(prosemirrorNode => {
|
|
68
|
-
const definitionId = prosemirrorNode
|
|
71
|
+
const definitionId = parseProsemirrorBlockAttribute(prosemirrorNode, "definitionId");
|
|
69
72
|
if (!definitionId) throw new Error(`Node is missing defintion id`);
|
|
70
73
|
if (typeof definitionId !== "string")
|
|
71
74
|
throw new Error(`Definition id is ${typeof definitionId}, has to be string`);
|
|
@@ -118,9 +121,13 @@ function parseAsRichText(
|
|
|
118
121
|
prosemirrorNode: ProsemirrorNode,
|
|
119
122
|
definition: PageBlockDefinition,
|
|
120
123
|
property: PageBlockDefinitionProperty
|
|
121
|
-
): PageBlockEditorModel {
|
|
124
|
+
): PageBlockEditorModel | null {
|
|
122
125
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
123
|
-
|
|
126
|
+
if (typeof id !== "string") return null;
|
|
127
|
+
|
|
128
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
129
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
130
|
+
|
|
124
131
|
const calloutType = parseCalloutType(parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "type"));
|
|
125
132
|
|
|
126
133
|
return {
|
|
@@ -150,7 +157,7 @@ function parseAsRichText(
|
|
|
150
157
|
};
|
|
151
158
|
}
|
|
152
159
|
|
|
153
|
-
function parseCalloutType(prosemirrorCalloutType:
|
|
160
|
+
function parseCalloutType(prosemirrorCalloutType: unknown): PageBlockCalloutType | undefined {
|
|
154
161
|
if (!prosemirrorCalloutType) return undefined;
|
|
155
162
|
|
|
156
163
|
switch (prosemirrorCalloutType) {
|
|
@@ -173,9 +180,12 @@ function parseAsMultiRichText(
|
|
|
173
180
|
prosemirrorNode: ProsemirrorNode,
|
|
174
181
|
definition: PageBlockDefinition,
|
|
175
182
|
property: PageBlockDefinitionProperty
|
|
176
|
-
) {
|
|
183
|
+
): PageBlockEditorModel | null {
|
|
177
184
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
178
|
-
|
|
185
|
+
if (typeof id !== "string") return null;
|
|
186
|
+
|
|
187
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
188
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
179
189
|
|
|
180
190
|
return {
|
|
181
191
|
// TODO Artem: indent
|
|
@@ -243,8 +253,11 @@ function parseRichTextAttribute(mark: ProsemirrorMark): PageBlockTextSpanAttribu
|
|
|
243
253
|
case "code":
|
|
244
254
|
return { type: "Code" };
|
|
245
255
|
case "link":
|
|
246
|
-
const
|
|
247
|
-
const
|
|
256
|
+
const itemIdRaw = parseProsemirrorBlockAttribute(mark, "itemId");
|
|
257
|
+
const itemId = typeof itemIdRaw === "string" ? itemIdRaw : undefined;
|
|
258
|
+
|
|
259
|
+
const hrefRaw = parseProsemirrorBlockAttribute(mark, "href");
|
|
260
|
+
const href = typeof hrefRaw === "string" ? hrefRaw : undefined;
|
|
248
261
|
|
|
249
262
|
return {
|
|
250
263
|
type: "Link",
|
|
@@ -265,9 +278,12 @@ function parseAsTable(
|
|
|
265
278
|
prosemirrorNode: ProsemirrorNode,
|
|
266
279
|
definition: PageBlockDefinition,
|
|
267
280
|
property: PageBlockDefinitionProperty
|
|
268
|
-
): PageBlockEditorModel {
|
|
281
|
+
): PageBlockEditorModel | null {
|
|
269
282
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
270
|
-
|
|
283
|
+
if (typeof id !== "string") return null;
|
|
284
|
+
|
|
285
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
286
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
271
287
|
|
|
272
288
|
const hasBorder = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "hasBorder") !== false;
|
|
273
289
|
|
|
@@ -297,7 +313,7 @@ function parseAsTable(
|
|
|
297
313
|
|
|
298
314
|
tableValue.value = rows.map(row => {
|
|
299
315
|
return {
|
|
300
|
-
cells: (row.content ?? []).map(parseAsTableCell),
|
|
316
|
+
cells: (row.content ?? []).map(parseAsTableCell).filter(nonNullFilter),
|
|
301
317
|
};
|
|
302
318
|
});
|
|
303
319
|
|
|
@@ -321,8 +337,10 @@ function parseAsTable(
|
|
|
321
337
|
};
|
|
322
338
|
}
|
|
323
339
|
|
|
324
|
-
function parseAsTableCell(prosemirrorNode: ProsemirrorNode): PageBlockItemTableCell {
|
|
340
|
+
function parseAsTableCell(prosemirrorNode: ProsemirrorNode): PageBlockItemTableCell | null {
|
|
325
341
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
342
|
+
if (typeof id !== "string") return null;
|
|
343
|
+
|
|
326
344
|
const textAlign = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "textAlign");
|
|
327
345
|
|
|
328
346
|
let columnWidth: number | undefined;
|
|
@@ -341,7 +359,7 @@ function parseAsTableCell(prosemirrorNode: ProsemirrorNode): PageBlockItemTableC
|
|
|
341
359
|
};
|
|
342
360
|
}
|
|
343
361
|
|
|
344
|
-
function parseTableCellAlignment(alignment
|
|
362
|
+
function parseTableCellAlignment(alignment: unknown): PageBlockTableCellAlignment {
|
|
345
363
|
if (!alignment) return "Left";
|
|
346
364
|
|
|
347
365
|
switch (alignment) {
|
|
@@ -439,9 +457,12 @@ function parseAsEmbed(
|
|
|
439
457
|
prosemirrorNode: ProsemirrorNode,
|
|
440
458
|
definition: PageBlockDefinition,
|
|
441
459
|
property: PageBlockDefinitionProperty
|
|
442
|
-
): PageBlockEditorModel {
|
|
460
|
+
): PageBlockEditorModel | null {
|
|
443
461
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
444
|
-
|
|
462
|
+
if (typeof id !== "string") return null;
|
|
463
|
+
|
|
464
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
465
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
445
466
|
|
|
446
467
|
const url = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "url");
|
|
447
468
|
const caption = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "caption");
|
|
@@ -475,9 +496,15 @@ function parseAsEmbed(
|
|
|
475
496
|
// Divider
|
|
476
497
|
//
|
|
477
498
|
|
|
478
|
-
function parseAsDivider(
|
|
499
|
+
function parseAsDivider(
|
|
500
|
+
prosemirrorNode: ProsemirrorNode,
|
|
501
|
+
definition: PageBlockDefinition
|
|
502
|
+
): PageBlockEditorModel | null {
|
|
479
503
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
480
|
-
|
|
504
|
+
if (typeof id !== "string") return null;
|
|
505
|
+
|
|
506
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
507
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
481
508
|
|
|
482
509
|
return {
|
|
483
510
|
id: id,
|
|
@@ -501,16 +528,14 @@ function parseAsCustomBlock(
|
|
|
501
528
|
definition: PageBlockDefinition
|
|
502
529
|
): PageBlockEditorModel | null {
|
|
503
530
|
const id = parseProsemirrorBlockAttribute(prosemirrorNode, "id");
|
|
504
|
-
|
|
531
|
+
if (typeof id !== "string") return null;
|
|
505
532
|
|
|
506
|
-
const
|
|
507
|
-
const
|
|
508
|
-
if (!Array.isArray(itemsJson)) {
|
|
509
|
-
console.error("Block `items` property must be a json array");
|
|
510
|
-
return null;
|
|
511
|
-
}
|
|
533
|
+
const variantIdRaw = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "variantId");
|
|
534
|
+
const variantId = typeof variantIdRaw === "string" ? variantIdRaw : undefined;
|
|
512
535
|
|
|
513
|
-
const
|
|
536
|
+
const appearance = parseAppearance(prosemirrorNode);
|
|
537
|
+
const parsedItems = parseBlockItems(prosemirrorNode, definition);
|
|
538
|
+
if (!parsedItems) return null;
|
|
514
539
|
|
|
515
540
|
return {
|
|
516
541
|
id: id,
|
|
@@ -520,11 +545,45 @@ function parseAsCustomBlock(
|
|
|
520
545
|
|
|
521
546
|
...(variantId && { variantId: variantId }),
|
|
522
547
|
|
|
548
|
+
appearance: appearance,
|
|
549
|
+
|
|
523
550
|
items: parsedItems,
|
|
524
551
|
},
|
|
525
552
|
};
|
|
526
553
|
}
|
|
527
554
|
|
|
555
|
+
function parseBlockItems(prosemirrorNode: ProsemirrorNode, definition: PageBlockDefinition): PageBlockItemV2[] | null {
|
|
556
|
+
const itemsString = parseProsemirrorBlockAttribute(prosemirrorNode, "items");
|
|
557
|
+
if (typeof itemsString !== "string") return null;
|
|
558
|
+
|
|
559
|
+
const itemsJson: unknown = JSON.parse(itemsString);
|
|
560
|
+
if (!Array.isArray(itemsJson)) {
|
|
561
|
+
console.error("Block `items` property must be a json array");
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return itemsJson.map(i => parseItem(i, definition)).filter(nonNullFilter);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function parseAppearance(prosemirrorNode: ProsemirrorNode): PageBlockAppearanceV2 {
|
|
569
|
+
const appearance: PageBlockAppearanceV2 = {};
|
|
570
|
+
|
|
571
|
+
const columns: unknown = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "columns");
|
|
572
|
+
if (typeof columns === "number") {
|
|
573
|
+
appearance.numberOfColumns = columns;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const backgroundColor: unknown = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, "backgroundColor");
|
|
577
|
+
if (typeof backgroundColor === "string") {
|
|
578
|
+
const parsedColor = ColorValue.safeParse(JSON.parse(backgroundColor));
|
|
579
|
+
if (parsedColor.success) {
|
|
580
|
+
appearance.itemBackgroundColor = parsedColor.data;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
return appearance;
|
|
585
|
+
}
|
|
586
|
+
|
|
528
587
|
function parseItem(rawItem: unknown, definition: PageBlockDefinition): PageBlockItemV2 | null {
|
|
529
588
|
const parsedItem = PageBlockItemV2.safeParse(rawItem);
|
|
530
589
|
if (!parsedItem.success) {
|
|
@@ -613,8 +672,8 @@ function valueSchemaForPropertyType(type: PageBlockDefinitionPropertyType) {
|
|
|
613
672
|
// Attributes
|
|
614
673
|
//
|
|
615
674
|
|
|
616
|
-
function parseProsemirrorBlockAttribute(prosemirrorNode: ProsemirrorNode, attributeName: string) {
|
|
617
|
-
const attributeValue = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, attributeName);
|
|
675
|
+
function parseProsemirrorBlockAttribute(prosemirrorNode: ProsemirrorNode, attributeName: string): unknown {
|
|
676
|
+
const attributeValue = parseProsemirrorOptionalBlockAttribute(prosemirrorNode, attributeName);
|
|
618
677
|
if (!attributeValue) {
|
|
619
678
|
throw new Error(`Attribute ${attributeName} was not defined on node ${prosemirrorNode.type}`);
|
|
620
679
|
}
|
|
@@ -622,7 +681,7 @@ function parseProsemirrorBlockAttribute(prosemirrorNode: ProsemirrorNode, attrib
|
|
|
622
681
|
return attributeValue;
|
|
623
682
|
}
|
|
624
683
|
|
|
625
|
-
function parseProsemirrorOptionalBlockAttribute(prosemirrorNode: ProsemirrorNode, attributeName: string) {
|
|
684
|
+
function parseProsemirrorOptionalBlockAttribute(prosemirrorNode: ProsemirrorNode, attributeName: string): unknown {
|
|
626
685
|
return prosemirrorNode.attrs?.[attributeName] ?? undefined;
|
|
627
686
|
}
|
|
628
687
|
|