@cmssy/react 4.7.2 → 5.0.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/client.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use client";
2
2
  import { createContext, useState, useRef, useEffect, useMemo, useContext, useCallback, createElement } from 'react';
3
+ import { fields, resolveInitialTarget, buildBlockContext, postToEditor, PROTOCOL_VERSION, parseEditorMessage, getBlockContentForLanguage, asBucket, toMinorUnits, formatPrice } from '@cmssy/core';
4
+ export { formatPrice, fractionDigits, fromMinorUnits, toMinorUnits } from '@cmssy/core';
3
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
6
 
5
7
  // src/registry.ts
@@ -36,100 +38,6 @@ function blocksToMeta(blocks, defaults = {}) {
36
38
  }
37
39
  return out;
38
40
  }
39
-
40
- // src/bridge/protocol.ts
41
- var PROTOCOL_VERSION = 2;
42
-
43
- // src/bridge/messages.ts
44
- function normalizeOrigin(origin) {
45
- const trimmed = origin.trim();
46
- if (trimmed === "*") return "*";
47
- try {
48
- return new URL(trimmed).origin;
49
- } catch {
50
- return trimmed;
51
- }
52
- }
53
- function postToEditor(target, editorOrigin, message) {
54
- target.postMessage(message, normalizeOrigin(editorOrigin));
55
- }
56
- function isOriginAllowed(origin, allowed) {
57
- const list = Array.isArray(allowed) ? allowed : [allowed];
58
- const actual = normalizeOrigin(origin);
59
- return list.some((candidate) => {
60
- const expected = normalizeOrigin(candidate);
61
- return expected === "*" || expected === actual;
62
- });
63
- }
64
- function resolveInitialTarget(editorOrigin) {
65
- const list = (Array.isArray(editorOrigin) ? editorOrigin : [editorOrigin]).map((origin) => normalizeOrigin(origin));
66
- if (list.includes("*")) return "*";
67
- if (list.length === 1) return list[0];
68
- const referrerOrigin = typeof document !== "undefined" && document.referrer ? normalizeOrigin(document.referrer) : "";
69
- return list.find((origin) => origin === referrerOrigin) ?? list[0];
70
- }
71
- function isObject(value) {
72
- return typeof value === "object" && value !== null && !Array.isArray(value);
73
- }
74
- function parseEditorMessage(data, origin, expectedOrigin) {
75
- if (!isOriginAllowed(origin, expectedOrigin)) return null;
76
- if (!isObject(data)) return null;
77
- switch (data.type) {
78
- case "cmssy:select":
79
- return typeof data.blockId === "string" && data.protocolVersion === PROTOCOL_VERSION ? {
80
- type: "cmssy:select",
81
- protocolVersion: PROTOCOL_VERSION,
82
- blockId: data.blockId
83
- } : null;
84
- case "cmssy:patch":
85
- return typeof data.blockId === "string" && isObject(data.content) && data.protocolVersion === PROTOCOL_VERSION ? {
86
- type: "cmssy:patch",
87
- blockId: data.blockId,
88
- content: data.content,
89
- protocolVersion: PROTOCOL_VERSION,
90
- ...isObject(data.style) ? { style: data.style } : {},
91
- ...isObject(data.advanced) ? { advanced: data.advanced } : {},
92
- ...typeof data.layoutPosition === "string" ? { layoutPosition: data.layoutPosition } : {}
93
- } : null;
94
- case "cmssy:parent-ready":
95
- return data.protocolVersion === PROTOCOL_VERSION ? { type: "cmssy:parent-ready", protocolVersion: PROTOCOL_VERSION } : null;
96
- case "cmssy:insert":
97
- return typeof data.blockId === "string" && typeof data.blockType === "string" && isObject(data.content) && typeof data.index === "number" && data.protocolVersion === PROTOCOL_VERSION ? {
98
- type: "cmssy:insert",
99
- protocolVersion: PROTOCOL_VERSION,
100
- blockId: data.blockId,
101
- blockType: data.blockType,
102
- content: data.content,
103
- ...isObject(data.style) ? { style: data.style } : {},
104
- ...isObject(data.advanced) ? { advanced: data.advanced } : {},
105
- index: data.index
106
- } : null;
107
- case "cmssy:reorder":
108
- return Array.isArray(data.blockIds) && data.blockIds.every((id) => typeof id === "string") && data.protocolVersion === PROTOCOL_VERSION ? {
109
- type: "cmssy:reorder",
110
- protocolVersion: PROTOCOL_VERSION,
111
- blockIds: data.blockIds
112
- } : null;
113
- case "cmssy:remove":
114
- return typeof data.blockId === "string" && data.protocolVersion === PROTOCOL_VERSION ? {
115
- type: "cmssy:remove",
116
- protocolVersion: PROTOCOL_VERSION,
117
- blockId: data.blockId
118
- } : null;
119
- case "cmssy:drag-over":
120
- return typeof data.y === "number" && data.protocolVersion === PROTOCOL_VERSION ? {
121
- type: "cmssy:drag-over",
122
- protocolVersion: PROTOCOL_VERSION,
123
- y: data.y
124
- } : null;
125
- case "cmssy:drag-end":
126
- return data.protocolVersion === PROTOCOL_VERSION ? { type: "cmssy:drag-end", protocolVersion: PROTOCOL_VERSION } : null;
127
- default:
128
- return null;
129
- }
130
- }
131
-
132
- // src/bridge/use-edit-bridge.tsx
133
41
  var ZERO_RECT = { x: 0, y: 0, width: 0, height: 0 };
134
42
  function collectRects() {
135
43
  const rects = /* @__PURE__ */ new Map();
@@ -512,48 +420,6 @@ function useDragAgent(config) {
512
420
  }, [config.editorOrigin]);
513
421
  return { dropY };
514
422
  }
515
-
516
- // src/components/block-context.ts
517
- function buildBlockContext(locale, defaultLocale, enabledLocales, isPreview, forms, extra) {
518
- return {
519
- locale: {
520
- current: locale,
521
- default: defaultLocale,
522
- enabled: enabledLocales && enabledLocales.length > 0 ? enabledLocales : Array.from(/* @__PURE__ */ new Set([defaultLocale, locale]))
523
- },
524
- isPreview: isPreview ?? false,
525
- forms,
526
- ...{},
527
- ...{}
528
- };
529
- }
530
-
531
- // src/content/get-block-content.ts
532
- function isPlainObject(value) {
533
- return typeof value === "object" && value !== null && !Array.isArray(value);
534
- }
535
- function looksLikeLocaleKey(key) {
536
- return /^[a-z]{2}(-[A-Za-z]{2})?$/.test(key);
537
- }
538
- function getBlockContentForLanguage(content, locale, defaultLocale = "en", availableLocales) {
539
- if (!isPlainObject(content)) return {};
540
- const isLocale = looksLikeLocaleKey;
541
- const localeEntries = Object.entries(content).filter(
542
- ([key, value]) => isLocale(key) && isPlainObject(value)
543
- );
544
- if (localeEntries.length === 0) return { ...content };
545
- const localeMap = Object.fromEntries(localeEntries);
546
- const nonTranslatable = {};
547
- for (const [key, value] of Object.entries(content)) {
548
- if (!(isLocale(key) && isPlainObject(value))) nonTranslatable[key] = value;
549
- }
550
- const fallbackKey = Object.keys(localeMap)[0];
551
- const chosen = localeMap[locale] ?? localeMap[defaultLocale] ?? localeMap[fallbackKey];
552
- return { ...nonTranslatable, ...chosen };
553
- }
554
- function asBucket(value) {
555
- return isPlainObject(value) ? value : {};
556
- }
557
423
  var WARN_CAP = 256;
558
424
  var warned = /* @__PURE__ */ new Set();
559
425
  function UnknownBlock({ type }) {
@@ -901,11 +767,11 @@ function CmssyAuthProvider({
901
767
  [postAction, commitUser]
902
768
  );
903
769
  const register = useCallback(
904
- async (identity, password, fields2) => {
770
+ async (identity, password, fields3) => {
905
771
  const data = await postAction("register", {
906
772
  identity,
907
773
  password,
908
- fields: fields2 ?? {}
774
+ fields: fields3 ?? {}
909
775
  });
910
776
  return { ok: Boolean(data.ok), message: data.message };
911
777
  },
@@ -1098,92 +964,6 @@ function useCart() {
1098
964
  }
1099
965
  return ctx;
1100
966
  }
1101
-
1102
- // src/fields.ts
1103
- function control(type) {
1104
- return (opts = {}) => ({
1105
- type,
1106
- label: opts.label ?? "",
1107
- ...opts
1108
- });
1109
- }
1110
- var fields = {
1111
- text: control("text"),
1112
- textarea: control("textarea"),
1113
- richText: control("richText"),
1114
- markdown: control("markdown"),
1115
- number: control("number"),
1116
- date: control("date"),
1117
- datetime: control("datetime"),
1118
- boolean: control("boolean"),
1119
- color: control("color"),
1120
- media: control("media"),
1121
- link: control("link"),
1122
- url: control("url"),
1123
- email: control("email"),
1124
- select: control("select"),
1125
- multiselect: control("multiselect"),
1126
- radio: control("radio"),
1127
- repeater: control("repeater"),
1128
- table: control("table"),
1129
- json: control("json"),
1130
- form: control("form"),
1131
- pageSelector: control("pageSelector")
1132
- };
1133
-
1134
- // src/commerce/money.ts
1135
- var ZERO_DECIMAL = /* @__PURE__ */ new Set([
1136
- "BIF",
1137
- "CLP",
1138
- "DJF",
1139
- "GNF",
1140
- "JPY",
1141
- "KMF",
1142
- "KRW",
1143
- "MGA",
1144
- "PYG",
1145
- "RWF",
1146
- "UGX",
1147
- "VND",
1148
- "VUV",
1149
- "XAF",
1150
- "XOF",
1151
- "XPF"
1152
- ]);
1153
- var THREE_DECIMAL = /* @__PURE__ */ new Set([
1154
- "BHD",
1155
- "IQD",
1156
- "JOD",
1157
- "KWD",
1158
- "LYD",
1159
- "OMR",
1160
- "TND"
1161
- ]);
1162
- function fractionDigits(currency) {
1163
- const code = currency.toUpperCase();
1164
- if (ZERO_DECIMAL.has(code)) return 0;
1165
- if (THREE_DECIMAL.has(code)) return 3;
1166
- return 2;
1167
- }
1168
- function fromMinorUnits(minor, currency) {
1169
- return minor / 10 ** fractionDigits(currency);
1170
- }
1171
- function toMinorUnits(amount, currency) {
1172
- return Math.round(amount * 10 ** fractionDigits(currency));
1173
- }
1174
- function formatPrice(minor, currency) {
1175
- if (!Number.isFinite(minor)) return "";
1176
- const code = currency ?? "USD";
1177
- const amount = fromMinorUnits(minor, code);
1178
- try {
1179
- return new Intl.NumberFormat(void 0, {
1180
- style: "currency",
1181
- currency: code
1182
- }).format(amount);
1183
- } catch {
1184
- return `${amount.toFixed(fractionDigits(code))} ${code}`;
1185
- }
1186
- }
1187
967
  function deriveAxes(variants) {
1188
968
  const axes = /* @__PURE__ */ new Map();
1189
969
  for (const variant of variants) {
@@ -1606,4 +1386,4 @@ function useCmssyOrder(id, options = {}) {
1606
1386
  return { order, loading, error, refresh: load };
1607
1387
  }
1608
1388
 
1609
- export { CmssyAuthProvider, CmssyCommerceProvider, CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, CmssyLocaleProvider, cartBlock, checkoutBlock, formatPrice, fractionDigits, fromMinorUnits, productBlock, toMinorUnits, useCart, useCmssyLocale, useCmssyOrder, useCmssyOrders, useCmssyUser, useEditBridge };
1389
+ export { CmssyAuthProvider, CmssyCommerceProvider, CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, CmssyLocaleProvider, cartBlock, checkoutBlock, productBlock, useCart, useCmssyLocale, useCmssyOrder, useCmssyOrders, useCmssyUser, useEditBridge };