@madebywild/sanity-richtext-field 0.1.9 → 0.1.10

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.cjs CHANGED
@@ -16,152 +16,7 @@ function _interopNamespaceCompat(e) {
16
16
  }
17
17
  }), n.default = e, Object.freeze(n);
18
18
  }
19
- var o__namespace = /* @__PURE__ */ _interopNamespaceCompat(o);
20
- const presets = {
21
- basic: {
22
- size: "sm",
23
- initiallyActive: !0,
24
- whitelist: ["em", "strong", "link"]
25
- },
26
- full: {
27
- size: "lg",
28
- initiallyActive: !1,
29
- // Empty blacklist means "enable all".
30
- blacklist: []
31
- }
32
- };
33
- function handleListKind(entry, findNeedle) {
34
- const list = entry.type?.options?.list;
35
- if (!list) return entry;
36
- const filteredList = list.filter((s) => findNeedle(s.value));
37
- return {
38
- ...entry,
39
- type: {
40
- ...entry.type,
41
- options: {
42
- ...entry.type.options,
43
- list: filteredList
44
- }
45
- }
46
- };
47
- }
48
- function handleOfKind(entry, findNeedle) {
49
- const list = entry.type?.of;
50
- if (!list) return entry;
51
- const filteredOf = list.filter((a) => findNeedle(a.name));
52
- return {
53
- ...entry,
54
- type: {
55
- ...entry.type,
56
- of: filteredOf
57
- }
58
- };
59
- }
60
- const sizeToHeightMap = {
61
- sm: "10rem",
62
- lg: "50vh"
63
- };
64
- function RichtextInput({
65
- richtextConfig,
66
- ...props
67
- }) {
68
- const schemaOptions = props.schemaType.options, presetOptions = schemaOptions?.preset ? presets[schemaOptions.preset] : {}, options = o__namespace.useMemo(() => {
69
- const {
70
- preset: _2,
71
- ...mainOptions
72
- } = schemaOptions ?? {};
73
- return tsDeepmerge.merge(presetOptions, mainOptions);
74
- }, [schemaOptions, presetOptions]), initiallyActive = options?.initiallyActive ?? !1, height = options?.size ? sizeToHeightMap[options.size] : sizeToHeightMap.sm, whitelist = options?.whitelist, blacklist = options?.blacklist;
75
- whitelist && blacklist && console.warn("Wild Sanity Rich Text Field: Both whitelist and blacklist are set. Whitelist will take precedence.");
76
- const filterPartsBy = o__namespace.useCallback((parts2, findNeedle) => parts2?.filter((b) => {
77
- const needle = findNeedle(b);
78
- return needle ? whitelist ? whitelist.includes(needle) : blacklist ? !blacklist.includes(needle) : !1 : !0;
79
- }), [whitelist, blacklist]), enabledParts = o__namespace.useMemo(() => {
80
- const blocks2 = filterPartsBy(richtextConfig.blocks, (s) => s.name), spans2 = filterPartsBy(richtextConfig.spans, (s_0) => s_0.name), lists2 = filterPartsBy(richtextConfig.lists, (s_1) => s_1.value), annotations2 = filterPartsBy(richtextConfig.annotations, (s_2) => s_2.name), decorators2 = filterPartsBy(richtextConfig.decorators, (s_3) => s_3.value), styles2 = filterPartsBy(richtextConfig.styles, (s_4) => s_4.value), normalizedStyles = styles2.length === 0 ? [...styles2, {
81
- title: "Normal",
82
- value: "normal"
83
- }] : styles2;
84
- return {
85
- blocks: blocks2,
86
- spans: spans2,
87
- lists: lists2,
88
- annotations: annotations2,
89
- decorators: decorators2,
90
- styles: normalizedStyles
91
- };
92
- }, [filterPartsBy]), reshapeSchemaTypes = o__namespace.useCallback((schema) => schema.name !== "block" ? schema : {
93
- ...schema,
94
- fields: schema.fields?.map((f2) => {
95
- const field = f2;
96
- switch (field.name) {
97
- // Headings, Normal, Quote, etc.
98
- case "style":
99
- return handleListKind(field, (__3) => enabledParts.styles.some((b_5) => b_5.value === __3));
100
- // Ul, Ol, etc.
101
- case "listItem":
102
- return handleListKind(field, (__2) => enabledParts.lists.some((b_4) => b_4.value === __2));
103
- // Links, Colors, etc.
104
- case "markDefs":
105
- return handleOfKind(field, (__1) => enabledParts.annotations.some((b_3) => b_3.name === __1));
106
- // Text content.
107
- case "children": {
108
- const spans_0 = [...enabledParts.spans, {
109
- name: "span"
110
- }], filtered_0 = handleOfKind(field, (__0) => spans_0.some((b_0) => b_0.name === __0));
111
- return {
112
- ...filtered_0,
113
- type: {
114
- ...filtered_0.type,
115
- // Inline markers within the text content.
116
- of: filtered_0.type.of?.map((t) => {
117
- if (t.name !== "span") return t;
118
- const span = t, annotations_0 = span.annotations?.filter((a) => enabledParts.annotations.some((b_1) => b_1.name === a.name)), decorators_0 = span.decorators?.filter((d) => enabledParts.decorators.some((b_2) => b_2.value === d.value));
119
- return {
120
- ...t,
121
- annotations: annotations_0,
122
- decorators: decorators_0
123
- };
124
- })
125
- }
126
- };
127
- }
128
- default:
129
- return field;
130
- }
131
- })
132
- }, [enabledParts]), patchedSchemaType = o__namespace.useMemo(() => {
133
- const of = props.schemaType?.of;
134
- if (!Array.isArray(of)) return props.schemaType;
135
- try {
136
- const filtered_1 = of.filter((s_5) => s_5.name === "block" || enabledParts.blocks.some((b_6) => b_6.name === s_5.name));
137
- return {
138
- ...props.schemaType,
139
- of: filtered_1.map((s_6) => reshapeSchemaTypes(s_6))
140
- };
141
- } catch (e) {
142
- return console.error("Wild Sanity Rich Text Field: Error while reshaping schema types.", e), props.schemaType;
143
- }
144
- }, [props.schemaType, enabledParts.blocks, reshapeSchemaTypes]), enabledPartsCount = o__namespace.useMemo(() => Object.values(enabledParts).reduce((acc, curr) => acc + curr.length, 0), [enabledParts]);
145
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
146
- /* @__PURE__ */ jsxRuntime.jsx("style", { children: `
147
- [data-wild-richtext-field] [data-testid="pt-editor"][data-fullscreen="false"] {
148
- height: var(--pt-editor-height, initial);
149
- }
150
- ` }),
151
- /* @__PURE__ */ jsxRuntime.jsx("div", { "data-wild-richtext-field": !0, style: {
152
- "--pt-editor-height": height
153
- }, children: props.renderDefault({
154
- ...props,
155
- initialActive: initiallyActive,
156
- displayInlineChanges: !1,
157
- // If the only enabled part is a style part, its the default "Normal" style. No need to show the toolbar.
158
- hideToolbar: enabledPartsCount === 0 || enabledPartsCount === 1 && enabledParts.styles.length === 1,
159
- // @ts-expect-error: schemaType.of is too narrow. The runtime type is valid.
160
- schemaType: patchedSchemaType
161
- }) })
162
- ] });
163
- }
164
- var __assign = function() {
19
+ var o__namespace = /* @__PURE__ */ _interopNamespaceCompat(o), __assign = function() {
165
20
  return __assign = Object.assign || function(t) {
166
21
  for (var s, i = 1, n = arguments.length; i < n; i++) {
167
22
  s = arguments[i];
@@ -1498,20 +1353,34 @@ const typeName = "wild.richtext", ColorText = dt.span`
1498
1353
  media: media$1
1499
1354
  }, strong = {
1500
1355
  title: "Strong",
1501
- value: "strong"
1356
+ value: `${typeName}.decorator.strong`,
1357
+ icon: icons.BoldIcon,
1358
+ component: ({
1359
+ children
1360
+ }) => /* @__PURE__ */ jsxRuntime.jsx("strong", { children })
1502
1361
  }, em = {
1503
1362
  title: "Emphasis",
1504
- value: "em"
1363
+ value: `${typeName}.decorator.em`,
1364
+ icon: icons.ItalicIcon,
1365
+ component: ({
1366
+ children
1367
+ }) => /* @__PURE__ */ jsxRuntime.jsx("em", { children })
1505
1368
  }, code = {
1506
1369
  title: "Code",
1507
- value: "code"
1370
+ value: `${typeName}.decorator.code`,
1371
+ icon: icons.CodeIcon,
1372
+ component: ({
1373
+ children
1374
+ }) => /* @__PURE__ */ jsxRuntime.jsx("code", { children })
1508
1375
  }, sup = {
1509
1376
  title: "Sup",
1510
- value: "sup",
1377
+ value: `${typeName}.decorator.sup`,
1511
1378
  icon: () => /* @__PURE__ */ jsxRuntime.jsx("sup", { children: "[1]" }),
1512
- component: (props) => /* @__PURE__ */ jsxRuntime.jsx("sup", { style: {
1379
+ component: ({
1380
+ children
1381
+ }) => /* @__PURE__ */ jsxRuntime.jsx("sup", { style: {
1513
1382
  verticalAlign: "super"
1514
- }, children: props.children })
1383
+ }, children })
1515
1384
  }, decorators = {
1516
1385
  strong,
1517
1386
  em,
@@ -1519,10 +1388,12 @@ const typeName = "wild.richtext", ColorText = dt.span`
1519
1388
  sup
1520
1389
  }, bullet = {
1521
1390
  title: "Bullet",
1522
- value: "bullet"
1391
+ value: `${typeName}.list.bullet`,
1392
+ icon: icons.UlistIcon
1523
1393
  }, number = {
1524
1394
  title: "Number",
1525
- value: "number"
1395
+ value: `${typeName}.list.number`,
1396
+ icon: icons.OlistIcon
1526
1397
  }, lists = {
1527
1398
  bullet,
1528
1399
  number
@@ -1582,16 +1453,37 @@ const typeName = "wild.richtext", ColorText = dt.span`
1582
1453
  media
1583
1454
  }, normal = {
1584
1455
  title: "Normal",
1585
- value: "normal"
1456
+ value: `${typeName}.style.normal`
1586
1457
  }, h2 = {
1587
1458
  title: "Heading 2",
1588
- value: "h2"
1459
+ value: `${typeName}.style.h2`,
1460
+ component: ({
1461
+ children
1462
+ }) => /* @__PURE__ */ jsxRuntime.jsx("h2", { style: {
1463
+ fontSize: "2.0625rem",
1464
+ fontWeight: "700",
1465
+ lineHeight: "calc(41 / 33)"
1466
+ }, children })
1589
1467
  }, h3 = {
1590
1468
  title: "Heading 3",
1591
- value: "h3"
1469
+ value: `${typeName}.style.h3`,
1470
+ component: ({
1471
+ children
1472
+ }) => /* @__PURE__ */ jsxRuntime.jsx("h3", { style: {
1473
+ fontSize: "1.6875rem",
1474
+ fontWeight: "700",
1475
+ lineHeight: "calc(35 / 27)"
1476
+ }, children })
1592
1477
  }, h4 = {
1593
1478
  title: "Heading 4",
1594
- value: "h4"
1479
+ value: `${typeName}.style.h4`,
1480
+ component: ({
1481
+ children
1482
+ }) => /* @__PURE__ */ jsxRuntime.jsx("h4", { style: {
1483
+ fontSize: "1.3125rem",
1484
+ fontWeight: "700",
1485
+ lineHeight: "calc(29 / 21)"
1486
+ }, children })
1595
1487
  }, styles = {
1596
1488
  normal,
1597
1489
  h2,
@@ -1604,7 +1496,164 @@ const typeName = "wild.richtext", ColorText = dt.span`
1604
1496
  lists,
1605
1497
  spans,
1606
1498
  styles
1607
- }, wildSanityRichtextFieldPlugin = sanity.definePlugin((config) => {
1499
+ }, presets = {
1500
+ basic: {
1501
+ size: "sm",
1502
+ initiallyActive: !0,
1503
+ whitelist: ["em", "strong", "link"]
1504
+ },
1505
+ full: {
1506
+ size: "lg",
1507
+ initiallyActive: !1,
1508
+ // Empty blacklist means "enable all".
1509
+ blacklist: []
1510
+ }
1511
+ };
1512
+ function handleListKind(entry, findNeedle) {
1513
+ const list = entry.type?.options?.list;
1514
+ if (!list) return entry;
1515
+ const filteredList = list.filter((s) => findNeedle(s.value));
1516
+ return {
1517
+ ...entry,
1518
+ type: {
1519
+ ...entry.type,
1520
+ options: {
1521
+ ...entry.type.options,
1522
+ list: filteredList
1523
+ }
1524
+ }
1525
+ };
1526
+ }
1527
+ function handleOfKind(entry, findNeedle) {
1528
+ const list = entry.type?.of;
1529
+ if (!list) return entry;
1530
+ const filteredOf = list.filter((a) => findNeedle(a.name));
1531
+ return {
1532
+ ...entry,
1533
+ type: {
1534
+ ...entry.type,
1535
+ of: filteredOf
1536
+ }
1537
+ };
1538
+ }
1539
+ const sizeToHeightMap = {
1540
+ sm: "10rem",
1541
+ lg: "50vh"
1542
+ };
1543
+ function RichtextInput({
1544
+ richtextConfig,
1545
+ ...props
1546
+ }) {
1547
+ const schemaOptions = props.schemaType.options, presetOptions = schemaOptions?.preset ? presets[schemaOptions.preset] : {}, options = o__namespace.useMemo(() => {
1548
+ const {
1549
+ preset: _2,
1550
+ ...mainOptions
1551
+ } = schemaOptions ?? {};
1552
+ return tsDeepmerge.merge(presetOptions, mainOptions);
1553
+ }, [schemaOptions, presetOptions]), initiallyActive = options?.initiallyActive ?? !1, height = options?.size ? sizeToHeightMap[options.size] : sizeToHeightMap.sm, whitelist = options?.whitelist, blacklist = options?.blacklist;
1554
+ whitelist && blacklist && console.warn("Wild Sanity Rich Text Field: Both whitelist and blacklist are set. Whitelist will take precedence.");
1555
+ const filterPartsBy = o__namespace.useCallback((parts2, findNeedle) => parts2?.filter((b) => {
1556
+ const needle = findNeedle(b);
1557
+ return needle ? whitelist ? whitelist.includes(needle) : blacklist ? !blacklist.includes(needle) : !1 : !0;
1558
+ }), [whitelist, blacklist]), enabledParts = o__namespace.useMemo(() => {
1559
+ const blocks2 = filterPartsBy(richtextConfig.blocks, (s) => s.name), spans2 = filterPartsBy(richtextConfig.spans, (s_0) => s_0.name), lists2 = filterPartsBy(richtextConfig.lists, (s_1) => s_1.value), annotations2 = filterPartsBy(richtextConfig.annotations, (s_2) => s_2.name), decorators2 = filterPartsBy(richtextConfig.decorators, (s_3) => s_3.value), styles2 = filterPartsBy(richtextConfig.styles, (s_4) => s_4.value), normalizedStyles = styles2.length === 0 ? [...styles2, parts.styles.normal] : styles2;
1560
+ return {
1561
+ blocks: blocks2,
1562
+ spans: spans2,
1563
+ lists: lists2,
1564
+ annotations: annotations2,
1565
+ decorators: decorators2,
1566
+ styles: normalizedStyles
1567
+ };
1568
+ }, [filterPartsBy]), reshapeSchemaTypes = o__namespace.useCallback((schema) => schema.name !== "block" ? schema : {
1569
+ ...schema,
1570
+ fields: schema.fields?.map((f2) => {
1571
+ const field = f2;
1572
+ switch (field.name) {
1573
+ // Headings, Normal, Quote, etc.
1574
+ case "style":
1575
+ return handleListKind(field, (__3) => enabledParts.styles.some((b_5) => b_5.value === __3));
1576
+ // Ul, Ol, etc.
1577
+ case "listItem":
1578
+ return handleListKind(field, (__2) => enabledParts.lists.some((b_4) => b_4.value === __2));
1579
+ // Links, Colors, etc.
1580
+ case "markDefs":
1581
+ return handleOfKind(field, (__1) => enabledParts.annotations.some((b_3) => b_3.name === __1));
1582
+ // Text content.
1583
+ case "children": {
1584
+ const spans_0 = [...enabledParts.spans, {
1585
+ name: "span"
1586
+ }], filtered_0 = handleOfKind(field, (__0) => spans_0.some((b_0) => b_0.name === __0));
1587
+ return {
1588
+ ...filtered_0,
1589
+ type: {
1590
+ ...filtered_0.type,
1591
+ // Inline markers within the text content.
1592
+ of: filtered_0.type.of?.map((t) => {
1593
+ if (t.name !== "span") return t;
1594
+ const span = t, annotations_0 = span.annotations?.filter((a) => enabledParts.annotations.some((b_1) => b_1.name === a.name)), decorators_0 = span.decorators?.filter((d) => enabledParts.decorators.some((b_2) => b_2.value === d.value));
1595
+ return {
1596
+ ...t,
1597
+ annotations: annotations_0,
1598
+ decorators: decorators_0
1599
+ };
1600
+ })
1601
+ }
1602
+ };
1603
+ }
1604
+ default:
1605
+ return field;
1606
+ }
1607
+ })
1608
+ }, [enabledParts]), patchedSchemaType = o__namespace.useMemo(() => {
1609
+ const of = props.schemaType?.of;
1610
+ if (!Array.isArray(of)) return props.schemaType;
1611
+ try {
1612
+ const filtered_1 = of.filter((s_5) => s_5.name === "block" || enabledParts.blocks.some((b_6) => b_6.name === s_5.name));
1613
+ return {
1614
+ ...props.schemaType,
1615
+ of: filtered_1.map((s_6) => reshapeSchemaTypes(s_6))
1616
+ };
1617
+ } catch (e) {
1618
+ return console.error("Wild Sanity Rich Text Field: Error while reshaping schema types.", e), props.schemaType;
1619
+ }
1620
+ }, [props.schemaType, enabledParts.blocks, reshapeSchemaTypes]), enabledPartsCount = o__namespace.useMemo(() => Object.values(enabledParts).reduce((acc, curr) => acc + curr.length, 0), [enabledParts]);
1621
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1622
+ /* @__PURE__ */ jsxRuntime.jsx("style", { children: `
1623
+ [data-wild-richtext-field] [data-testid="pt-editor"][data-fullscreen="false"] {
1624
+ height: var(--pt-editor-height, initial);
1625
+ }
1626
+
1627
+ [data-wild-richtext-field] .pt-list-item [data-list-item="wild.richtext.list.bullet"] [data-list-prefix] > span::before,
1628
+ [data-wild-richtext-field] .pt-list-item [data-list-item="wild.richtext.list.number"] [data-list-prefix] > span::before {
1629
+ font-size: 1.0em;
1630
+ display: inline-block;
1631
+ padding-top: 2px;
1632
+ color: inherit;
1633
+ }
1634
+
1635
+ [data-wild-richtext-field] .pt-list-item [data-list-item="wild.richtext.list.bullet"] [data-list-prefix] > span::before {
1636
+ content: "\u2022";
1637
+ }
1638
+
1639
+ [data-wild-richtext-field] .pt-list-item [data-list-item="wild.richtext.list.number"] [data-list-prefix] > span::before {
1640
+ content: counter(list-level-1) ". ";
1641
+ }
1642
+ ` }),
1643
+ /* @__PURE__ */ jsxRuntime.jsx("div", { "data-wild-richtext-field": !0, style: {
1644
+ "--pt-editor-height": height
1645
+ }, children: props.renderDefault({
1646
+ ...props,
1647
+ initialActive: initiallyActive,
1648
+ displayInlineChanges: !1,
1649
+ // If the only enabled part is a style part, its the default "Normal" style. No need to show the toolbar.
1650
+ hideToolbar: enabledPartsCount === 0 || enabledPartsCount === 1 && enabledParts.styles.length === 1,
1651
+ // @ts-expect-error: schemaType.of is too narrow. The runtime type is valid.
1652
+ schemaType: patchedSchemaType
1653
+ }) })
1654
+ ] });
1655
+ }
1656
+ const wildSanityRichtextFieldPlugin = sanity.definePlugin((config) => {
1608
1657
  const userSpans = config?.spans ?? [], userLists = config?.lists ?? [], userBlocks = config?.blocks ?? [], userStyles = config?.styles ?? [], userDecorators = config?.decorators ?? [], userAnnotations = config?.annotations ?? [], richtextConfig = {
1609
1658
  blocks: [...userBlocks, ...Object.values(parts.blocks)],
1610
1659
  spans: [...userSpans, ...Object.values(parts.spans)],