admin-lib-cms 1.2.8 → 1.2.9
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/chunk-IXZXII3M.mjs +795 -0
- package/dist/client.d.mts +132 -0
- package/dist/client.d.ts +132 -0
- package/dist/client.js +725 -0
- package/dist/client.mjs +26 -0
- package/dist/index.d.mts +8 -113
- package/dist/index.d.ts +8 -113
- package/dist/index.mjs +703 -1468
- package/package.json +7 -1
package/dist/client.js
ADDED
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/client.ts
|
|
22
|
+
var client_exports = {};
|
|
23
|
+
__export(client_exports, {
|
|
24
|
+
CmsProvider: () => CmsProvider,
|
|
25
|
+
EditableFile: () => EditableFile,
|
|
26
|
+
EditableImage: () => EditableImage,
|
|
27
|
+
EditableLink: () => EditableLink,
|
|
28
|
+
EditableList: () => EditableList,
|
|
29
|
+
EditableReference: () => EditableReference,
|
|
30
|
+
EditableText: () => EditableText,
|
|
31
|
+
useCmsContext: () => useCmsContext,
|
|
32
|
+
useCmsOptional: () => useCmsOptional,
|
|
33
|
+
useSubmit: () => useSubmit
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(client_exports);
|
|
36
|
+
|
|
37
|
+
// src/provider.tsx
|
|
38
|
+
var import_react3 = require("react");
|
|
39
|
+
|
|
40
|
+
// src/subscription/SubscriptionProvider.tsx
|
|
41
|
+
var import_react = require("react");
|
|
42
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
43
|
+
var SubscriptionContext = (0, import_react.createContext)(void 0);
|
|
44
|
+
function useSubscriptionContext() {
|
|
45
|
+
const ctx = (0, import_react.useContext)(SubscriptionContext);
|
|
46
|
+
if (!ctx) {
|
|
47
|
+
throw new Error("useSubscriptionContext must be used within a SubscriptionProvider");
|
|
48
|
+
}
|
|
49
|
+
return ctx;
|
|
50
|
+
}
|
|
51
|
+
function useSubscription() {
|
|
52
|
+
return useSubscriptionContext();
|
|
53
|
+
}
|
|
54
|
+
function SubscriptionProvider({
|
|
55
|
+
children,
|
|
56
|
+
apiUrl,
|
|
57
|
+
projectId,
|
|
58
|
+
email,
|
|
59
|
+
validateSubscriptionUrl
|
|
60
|
+
}) {
|
|
61
|
+
const [subscription, setSubscription] = (0, import_react.useState)(null);
|
|
62
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
63
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
64
|
+
const fetchSubscription = (0, import_react.useCallback)(
|
|
65
|
+
async (silent = false) => {
|
|
66
|
+
if (!apiUrl || !projectId) {
|
|
67
|
+
setLoading(false);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!silent) setLoading(true);
|
|
71
|
+
setError(null);
|
|
72
|
+
try {
|
|
73
|
+
const endpoint = validateSubscriptionUrl || `${apiUrl}/api/validate-subscription`;
|
|
74
|
+
const res = await fetch(endpoint, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: { "Content-Type": "application/json" },
|
|
77
|
+
body: JSON.stringify({ projectId })
|
|
78
|
+
});
|
|
79
|
+
if (!res.ok) throw new Error(`Server error: ${res.status}`);
|
|
80
|
+
const data = await res.json();
|
|
81
|
+
setSubscription({
|
|
82
|
+
subscriptionStatus: data.valid ? data.status : "inactive",
|
|
83
|
+
currentPeriodEnd: data.expiryDate,
|
|
84
|
+
customerEmail: email,
|
|
85
|
+
subscriptionId: data._id,
|
|
86
|
+
stripeCustomerId: data.stripeCustomerId,
|
|
87
|
+
stripePaymentLink: data.stripePaymentLink,
|
|
88
|
+
valid: data.valid
|
|
89
|
+
});
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error("SubscriptionProvider Error:", err);
|
|
92
|
+
setError(err.message || "Failed to fetch subscription");
|
|
93
|
+
setSubscription({ subscriptionStatus: "active", valid: true });
|
|
94
|
+
} finally {
|
|
95
|
+
setLoading(false);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
[apiUrl, projectId, email]
|
|
99
|
+
);
|
|
100
|
+
(0, import_react.useEffect)(() => {
|
|
101
|
+
fetchSubscription();
|
|
102
|
+
const handleFocus = () => {
|
|
103
|
+
fetchSubscription(true);
|
|
104
|
+
};
|
|
105
|
+
window.addEventListener("focus", handleFocus);
|
|
106
|
+
let intervalId;
|
|
107
|
+
if (subscription && !subscription.valid) {
|
|
108
|
+
intervalId = setInterval(() => {
|
|
109
|
+
fetchSubscription(true);
|
|
110
|
+
}, 1e4);
|
|
111
|
+
}
|
|
112
|
+
return () => {
|
|
113
|
+
window.removeEventListener("focus", handleFocus);
|
|
114
|
+
if (intervalId) clearInterval(intervalId);
|
|
115
|
+
};
|
|
116
|
+
}, [fetchSubscription, subscription?.valid]);
|
|
117
|
+
let daysOverdue = 0;
|
|
118
|
+
if (subscription && !subscription.valid && subscription.currentPeriodEnd) {
|
|
119
|
+
const diffTime = Date.now() - new Date(subscription.currentPeriodEnd).getTime();
|
|
120
|
+
if (diffTime > 0) {
|
|
121
|
+
daysOverdue = Math.floor(diffTime / (1e3 * 60 * 60 * 24));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const isSubscriptionActive = subscription ? subscription.valid || daysOverdue < 30 : true;
|
|
125
|
+
const value = {
|
|
126
|
+
subscription,
|
|
127
|
+
loading,
|
|
128
|
+
error,
|
|
129
|
+
refetch: fetchSubscription,
|
|
130
|
+
isSubscriptionActive,
|
|
131
|
+
daysOverdue,
|
|
132
|
+
config: { apiUrl, projectId }
|
|
133
|
+
};
|
|
134
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SubscriptionContext.Provider, { value, children });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/subscription/SubscriptionPage.tsx
|
|
138
|
+
var import_react2 = require("react");
|
|
139
|
+
var import_lucide_react = require("lucide-react");
|
|
140
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
141
|
+
|
|
142
|
+
// src/provider.tsx
|
|
143
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
144
|
+
var CmsContext = (0, import_react3.createContext)(null);
|
|
145
|
+
function useCmsContext() {
|
|
146
|
+
const ctx = (0, import_react3.useContext)(CmsContext);
|
|
147
|
+
if (!ctx) throw new Error("useCms must be used inside <CmsProvider>");
|
|
148
|
+
return ctx;
|
|
149
|
+
}
|
|
150
|
+
function useCmsOptional() {
|
|
151
|
+
return (0, import_react3.useContext)(CmsContext);
|
|
152
|
+
}
|
|
153
|
+
function CmsProvider(props) {
|
|
154
|
+
if (props.projectId) {
|
|
155
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
156
|
+
SubscriptionProvider,
|
|
157
|
+
{
|
|
158
|
+
apiUrl: props.apiUrl,
|
|
159
|
+
projectId: props.projectId,
|
|
160
|
+
email: props.email,
|
|
161
|
+
validateSubscriptionUrl: props.validateSubscriptionUrl,
|
|
162
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CmsProviderInner, { ...props, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SiteGuard, { children: props.children }) })
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CmsProviderInner, { ...props, children: props.children });
|
|
167
|
+
}
|
|
168
|
+
function SiteGuard({ children }) {
|
|
169
|
+
const { daysOverdue, isSubscriptionActive } = useSubscription();
|
|
170
|
+
if (!isSubscriptionActive && daysOverdue >= 60) {
|
|
171
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { display: "flex", height: "100vh", width: "100%", alignItems: "center", justifyContent: "center", backgroundColor: "#f8fafc", color: "#0f172a", fontFamily: "system-ui, -apple-system, sans-serif" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { textAlign: "center", padding: "2rem", maxWidth: "32rem" }, children: [
|
|
172
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h1", { style: { fontSize: "1.5rem", fontWeight: 600, marginBottom: "0.5rem" }, children: "This website is temporarily unavailable" }),
|
|
173
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { color: "#64748b" }, children: "Please contact the site administrator if you believe this is an error." })
|
|
174
|
+
] }) });
|
|
175
|
+
}
|
|
176
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
177
|
+
}
|
|
178
|
+
function CmsProviderInner({ config, adapter, children, iconMap, initialOverrides }) {
|
|
179
|
+
const [overrides, setOverrides] = (0, import_react3.useState)(initialOverrides || {});
|
|
180
|
+
const [savedOverrides, setSavedOverrides] = (0, import_react3.useState)(initialOverrides || {});
|
|
181
|
+
const [selectedId, setSelectedIdRaw] = (0, import_react3.useState)(null);
|
|
182
|
+
const [editMode, setEditMode] = (0, import_react3.useState)(false);
|
|
183
|
+
const [collectionChangedAt, setCollectionChangedAt] = (0, import_react3.useState)(0);
|
|
184
|
+
const registryRef = (0, import_react3.useRef)(/* @__PURE__ */ new Map());
|
|
185
|
+
const refCountRef = (0, import_react3.useRef)(/* @__PURE__ */ new Map());
|
|
186
|
+
(0, import_react3.useEffect)(() => {
|
|
187
|
+
if (typeof window === "undefined" || typeof BroadcastChannel === "undefined") return;
|
|
188
|
+
const bc = new BroadcastChannel("admin-cms:collection-changed");
|
|
189
|
+
bc.onmessage = () => setCollectionChangedAt(Date.now());
|
|
190
|
+
return () => bc.close();
|
|
191
|
+
}, []);
|
|
192
|
+
const notifyCollectionChanged = (0, import_react3.useCallback)(() => {
|
|
193
|
+
setCollectionChangedAt(Date.now());
|
|
194
|
+
if (typeof BroadcastChannel !== "undefined") {
|
|
195
|
+
const bc = new BroadcastChannel("admin-cms:collection-changed");
|
|
196
|
+
bc.postMessage({ at: Date.now() });
|
|
197
|
+
bc.close();
|
|
198
|
+
}
|
|
199
|
+
}, []);
|
|
200
|
+
(0, import_react3.useEffect)(() => {
|
|
201
|
+
if (typeof window === "undefined") return;
|
|
202
|
+
const params = new URLSearchParams(window.location.search);
|
|
203
|
+
if (params.get("__cms") === "edit") setEditMode(true);
|
|
204
|
+
}, []);
|
|
205
|
+
(0, import_react3.useEffect)(() => {
|
|
206
|
+
let cancelled = false;
|
|
207
|
+
adapter.getOverrides().then((o) => {
|
|
208
|
+
if (cancelled) return;
|
|
209
|
+
setOverrides(o);
|
|
210
|
+
setSavedOverrides(o);
|
|
211
|
+
});
|
|
212
|
+
return () => {
|
|
213
|
+
cancelled = true;
|
|
214
|
+
};
|
|
215
|
+
}, [adapter]);
|
|
216
|
+
(0, import_react3.useEffect)(() => {
|
|
217
|
+
if (!editMode || typeof window === "undefined") return;
|
|
218
|
+
const parent = window.parent;
|
|
219
|
+
if (!parent || parent === window) return;
|
|
220
|
+
const send = (msg) => parent.postMessage({ __cms: true, ...msg }, "*");
|
|
221
|
+
send({ type: "cms:ready" });
|
|
222
|
+
const onMsg = (e) => {
|
|
223
|
+
const data = e.data;
|
|
224
|
+
if (!data || !data.__cms) return;
|
|
225
|
+
if (data.type === "cms:set-override") {
|
|
226
|
+
setOverrides((prev) => ({ ...prev, [data.id]: data.value }));
|
|
227
|
+
} else if (data.type === "cms:set-all-overrides") {
|
|
228
|
+
setOverrides(data.overrides);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
window.addEventListener("message", onMsg);
|
|
232
|
+
return () => window.removeEventListener("message", onMsg);
|
|
233
|
+
}, [editMode]);
|
|
234
|
+
const setSelectedId = (0, import_react3.useCallback)(
|
|
235
|
+
(id) => {
|
|
236
|
+
setSelectedIdRaw(id);
|
|
237
|
+
if (editMode && typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
238
|
+
window.parent.postMessage(
|
|
239
|
+
{ __cms: true, type: "cms:select", id },
|
|
240
|
+
"*"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
[editMode]
|
|
245
|
+
);
|
|
246
|
+
const getSafeItems = (0, import_react3.useCallback)(() => {
|
|
247
|
+
try {
|
|
248
|
+
return JSON.parse(
|
|
249
|
+
JSON.stringify(Array.from(registryRef.current.values()), (_key, value2) => {
|
|
250
|
+
if (typeof value2 === "function") {
|
|
251
|
+
return value2.name || void 0;
|
|
252
|
+
}
|
|
253
|
+
return value2;
|
|
254
|
+
})
|
|
255
|
+
);
|
|
256
|
+
} catch {
|
|
257
|
+
return Array.from(registryRef.current.values());
|
|
258
|
+
}
|
|
259
|
+
}, []);
|
|
260
|
+
const registerEditable = (0, import_react3.useCallback)((meta) => {
|
|
261
|
+
refCountRef.current.set(meta.id, (refCountRef.current.get(meta.id) || 0) + 1);
|
|
262
|
+
registryRef.current.set(meta.id, meta);
|
|
263
|
+
if (typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
264
|
+
window.parent.postMessage(
|
|
265
|
+
{
|
|
266
|
+
__cms: true,
|
|
267
|
+
type: "cms:register",
|
|
268
|
+
items: getSafeItems()
|
|
269
|
+
},
|
|
270
|
+
"*"
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}, [getSafeItems]);
|
|
274
|
+
const unregisterEditable = (0, import_react3.useCallback)((id) => {
|
|
275
|
+
const count = (refCountRef.current.get(id) || 1) - 1;
|
|
276
|
+
if (count <= 0) {
|
|
277
|
+
refCountRef.current.delete(id);
|
|
278
|
+
registryRef.current.delete(id);
|
|
279
|
+
} else {
|
|
280
|
+
refCountRef.current.set(id, count);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
284
|
+
window.parent.postMessage(
|
|
285
|
+
{
|
|
286
|
+
__cms: true,
|
|
287
|
+
type: "cms:register",
|
|
288
|
+
items: getSafeItems()
|
|
289
|
+
},
|
|
290
|
+
"*"
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}, [getSafeItems]);
|
|
294
|
+
const setOverride = (0, import_react3.useCallback)((id, value2) => {
|
|
295
|
+
setOverrides((prev) => ({ ...prev, [id]: value2 }));
|
|
296
|
+
}, []);
|
|
297
|
+
const save = (0, import_react3.useCallback)(async () => {
|
|
298
|
+
await adapter.setOverrides(overrides);
|
|
299
|
+
setSavedOverrides(overrides);
|
|
300
|
+
}, [adapter, overrides]);
|
|
301
|
+
const reset = (0, import_react3.useCallback)(
|
|
302
|
+
async (id) => {
|
|
303
|
+
if (id) {
|
|
304
|
+
setOverrides((prev) => {
|
|
305
|
+
const next = { ...prev };
|
|
306
|
+
delete next[id];
|
|
307
|
+
return next;
|
|
308
|
+
});
|
|
309
|
+
} else {
|
|
310
|
+
setOverrides({});
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
[]
|
|
314
|
+
);
|
|
315
|
+
const isDirty = (0, import_react3.useMemo)(
|
|
316
|
+
() => JSON.stringify(overrides) !== JSON.stringify(savedOverrides),
|
|
317
|
+
[overrides, savedOverrides]
|
|
318
|
+
);
|
|
319
|
+
const getRegistered = (0, import_react3.useCallback)(
|
|
320
|
+
() => Array.from(registryRef.current.values()),
|
|
321
|
+
[]
|
|
322
|
+
);
|
|
323
|
+
const value = (0, import_react3.useMemo)(() => ({
|
|
324
|
+
config,
|
|
325
|
+
adapter,
|
|
326
|
+
overrides,
|
|
327
|
+
editMode,
|
|
328
|
+
selectedId,
|
|
329
|
+
setSelectedId,
|
|
330
|
+
setOverride,
|
|
331
|
+
registerEditable,
|
|
332
|
+
unregisterEditable,
|
|
333
|
+
getRegistered,
|
|
334
|
+
save,
|
|
335
|
+
reset,
|
|
336
|
+
isDirty,
|
|
337
|
+
iconMap,
|
|
338
|
+
uploadImage: adapter.uploadImage?.bind(adapter),
|
|
339
|
+
collectionChangedAt,
|
|
340
|
+
notifyCollectionChanged
|
|
341
|
+
}), [config, adapter, overrides, editMode, selectedId, setSelectedId, setOverride, registerEditable, unregisterEditable, getRegistered, save, reset, isDirty, iconMap, collectionChangedAt, notifyCollectionChanged]);
|
|
342
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CmsContext.Provider, { value, children });
|
|
343
|
+
}
|
|
344
|
+
function useSubmit(formId) {
|
|
345
|
+
const ctx = (0, import_react3.useContext)(CmsContext);
|
|
346
|
+
return async (data) => {
|
|
347
|
+
const submission = {
|
|
348
|
+
id: crypto.randomUUID(),
|
|
349
|
+
formId,
|
|
350
|
+
submittedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
351
|
+
data,
|
|
352
|
+
read: false
|
|
353
|
+
};
|
|
354
|
+
if (ctx?.adapter) {
|
|
355
|
+
await ctx.adapter.saveSubmission(submission);
|
|
356
|
+
} else {
|
|
357
|
+
const KEY = "admin-cms:submissions";
|
|
358
|
+
const list = (() => {
|
|
359
|
+
try {
|
|
360
|
+
return JSON.parse(localStorage.getItem(KEY) ?? "[]");
|
|
361
|
+
} catch {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
})();
|
|
365
|
+
list.unshift(submission);
|
|
366
|
+
localStorage.setItem(KEY, JSON.stringify(list));
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// src/editable.tsx
|
|
372
|
+
var import_react4 = require("react");
|
|
373
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
374
|
+
function useRegister(id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable) {
|
|
375
|
+
const ctx = useCmsOptional();
|
|
376
|
+
(0, import_react4.useEffect)(() => {
|
|
377
|
+
if (!ctx) return;
|
|
378
|
+
ctx.registerEditable({ id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable });
|
|
379
|
+
return () => ctx.unregisterEditable(id);
|
|
380
|
+
}, [ctx?.registerEditable, ctx?.unregisterEditable, id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable]);
|
|
381
|
+
return ctx;
|
|
382
|
+
}
|
|
383
|
+
function editableClasses(active, selected) {
|
|
384
|
+
if (!active) return "";
|
|
385
|
+
return [
|
|
386
|
+
"outline-dashed outline-2 outline-offset-2 cursor-pointer transition-colors",
|
|
387
|
+
selected ? "outline-blue-500 bg-blue-500/5" : "outline-blue-400/60 hover:outline-blue-500"
|
|
388
|
+
].join(" ");
|
|
389
|
+
}
|
|
390
|
+
function EditableText({
|
|
391
|
+
id,
|
|
392
|
+
label,
|
|
393
|
+
section,
|
|
394
|
+
pageId,
|
|
395
|
+
children,
|
|
396
|
+
as = "span",
|
|
397
|
+
className,
|
|
398
|
+
type = "text",
|
|
399
|
+
lineClassNames,
|
|
400
|
+
renderLine
|
|
401
|
+
}) {
|
|
402
|
+
const ctx = useRegister(id, type, label, section, pageId, children);
|
|
403
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? "") : children;
|
|
404
|
+
const active = !!ctx?.editMode;
|
|
405
|
+
const selected = ctx?.selectedId === id;
|
|
406
|
+
const isEmpty = !value || String(value).trim() === "";
|
|
407
|
+
const renderValue = active && isEmpty ? `[Empty: ${label || id}]` : value;
|
|
408
|
+
const onClick = (e) => {
|
|
409
|
+
if (!active || !ctx) return;
|
|
410
|
+
e.preventDefault();
|
|
411
|
+
e.stopPropagation();
|
|
412
|
+
ctx.setSelectedId(id);
|
|
413
|
+
};
|
|
414
|
+
let finalChildren = renderValue;
|
|
415
|
+
if (typeof renderValue === "string" && renderValue.includes("\n")) {
|
|
416
|
+
finalChildren = renderValue.split("\n").map((line, i, arr) => {
|
|
417
|
+
if (renderLine) {
|
|
418
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_react4.Fragment, { children: [
|
|
419
|
+
renderLine(line, i),
|
|
420
|
+
i < arr.length - 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("br", {})
|
|
421
|
+
] }, i);
|
|
422
|
+
}
|
|
423
|
+
const lineClass = lineClassNames?.[i];
|
|
424
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_react4.Fragment, { children: [
|
|
425
|
+
lineClass ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: lineClass, children: line }) : line,
|
|
426
|
+
i < arr.length - 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("br", {})
|
|
427
|
+
] }, i);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
return (0, import_react4.createElement)(
|
|
431
|
+
as,
|
|
432
|
+
{
|
|
433
|
+
className: [
|
|
434
|
+
className,
|
|
435
|
+
editableClasses(active, selected),
|
|
436
|
+
active && isEmpty ? "opacity-50" : ""
|
|
437
|
+
].filter(Boolean).join(" "),
|
|
438
|
+
"data-cms-id": id,
|
|
439
|
+
onClick: active ? onClick : void 0
|
|
440
|
+
},
|
|
441
|
+
finalChildren
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
function EditableImage({
|
|
445
|
+
id,
|
|
446
|
+
label,
|
|
447
|
+
section,
|
|
448
|
+
pageId,
|
|
449
|
+
src,
|
|
450
|
+
alt,
|
|
451
|
+
className,
|
|
452
|
+
width,
|
|
453
|
+
height
|
|
454
|
+
}) {
|
|
455
|
+
const ctx = useRegister(id, "image", label, section, pageId, src);
|
|
456
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? src) : src;
|
|
457
|
+
const active = !!ctx?.editMode;
|
|
458
|
+
const selected = ctx?.selectedId === id;
|
|
459
|
+
const onClick = (e) => {
|
|
460
|
+
if (!active || !ctx) return;
|
|
461
|
+
e.preventDefault();
|
|
462
|
+
e.stopPropagation();
|
|
463
|
+
ctx.setSelectedId(id);
|
|
464
|
+
};
|
|
465
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
466
|
+
"img",
|
|
467
|
+
{
|
|
468
|
+
src: value || void 0,
|
|
469
|
+
alt,
|
|
470
|
+
width,
|
|
471
|
+
height,
|
|
472
|
+
"data-cms-id": id,
|
|
473
|
+
onClick: active ? onClick : void 0,
|
|
474
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" ")
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
function EditableFile({
|
|
479
|
+
id,
|
|
480
|
+
label,
|
|
481
|
+
section,
|
|
482
|
+
pageId,
|
|
483
|
+
src,
|
|
484
|
+
defaultUrl = "",
|
|
485
|
+
className,
|
|
486
|
+
children
|
|
487
|
+
}) {
|
|
488
|
+
const initialUrl = src || defaultUrl;
|
|
489
|
+
const ctx = useRegister(id, "file", label, section, pageId, initialUrl);
|
|
490
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? initialUrl) : initialUrl;
|
|
491
|
+
const active = !!ctx?.editMode;
|
|
492
|
+
const selected = ctx?.selectedId === id;
|
|
493
|
+
const onClick = (e) => {
|
|
494
|
+
if (!active || !ctx) return;
|
|
495
|
+
e.preventDefault();
|
|
496
|
+
e.stopPropagation();
|
|
497
|
+
ctx.setSelectedId(id);
|
|
498
|
+
};
|
|
499
|
+
if (children) {
|
|
500
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
501
|
+
"div",
|
|
502
|
+
{
|
|
503
|
+
"data-cms-id": id,
|
|
504
|
+
onClick: active ? onClick : void 0,
|
|
505
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
506
|
+
children: children(value)
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
511
|
+
"div",
|
|
512
|
+
{
|
|
513
|
+
"data-cms-id": id,
|
|
514
|
+
onClick: active ? onClick : void 0,
|
|
515
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
516
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
517
|
+
"iframe",
|
|
518
|
+
{
|
|
519
|
+
src: value,
|
|
520
|
+
className: "w-full h-[500px] rounded-xl border-0",
|
|
521
|
+
title: label || id
|
|
522
|
+
}
|
|
523
|
+
)
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
function EditableLink({
|
|
528
|
+
id,
|
|
529
|
+
label,
|
|
530
|
+
section,
|
|
531
|
+
pageId,
|
|
532
|
+
href,
|
|
533
|
+
children,
|
|
534
|
+
text,
|
|
535
|
+
className,
|
|
536
|
+
target
|
|
537
|
+
}) {
|
|
538
|
+
let defaultText = text ?? "";
|
|
539
|
+
if (!defaultText) {
|
|
540
|
+
if (typeof children === "string") {
|
|
541
|
+
defaultText = children;
|
|
542
|
+
} else if (Array.isArray(children)) {
|
|
543
|
+
const strChild = children.find((c) => typeof c === "string");
|
|
544
|
+
if (typeof strChild === "string") {
|
|
545
|
+
defaultText = strChild.trim();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const ctx = useRegister(id, "link", label, section, pageId, { href, text: defaultText });
|
|
550
|
+
const override = ctx?.overrides[id];
|
|
551
|
+
const hrefValue = override?.href ?? href;
|
|
552
|
+
const textValue = override?.text ?? defaultText;
|
|
553
|
+
const active = !!ctx?.editMode;
|
|
554
|
+
const selected = ctx?.selectedId === id;
|
|
555
|
+
const onClick = (e) => {
|
|
556
|
+
if (!active || !ctx) return;
|
|
557
|
+
e.preventDefault();
|
|
558
|
+
e.stopPropagation();
|
|
559
|
+
ctx.setSelectedId(id);
|
|
560
|
+
};
|
|
561
|
+
const isExternal = hrefValue.startsWith("http");
|
|
562
|
+
const computedTarget = target || (isExternal ? "_blank" : void 0);
|
|
563
|
+
const computedRel = computedTarget === "_blank" ? "noopener noreferrer" : void 0;
|
|
564
|
+
let content;
|
|
565
|
+
if (typeof children === "function") {
|
|
566
|
+
content = children(textValue);
|
|
567
|
+
} else if (override && typeof override.text === "string" && override.text !== "") {
|
|
568
|
+
if (Array.isArray(children)) {
|
|
569
|
+
let replaced = false;
|
|
570
|
+
content = children.map((c) => {
|
|
571
|
+
if (typeof c === "string" && !replaced) {
|
|
572
|
+
replaced = true;
|
|
573
|
+
return override.text;
|
|
574
|
+
}
|
|
575
|
+
return c;
|
|
576
|
+
});
|
|
577
|
+
} else if ((0, import_react4.isValidElement)(children)) {
|
|
578
|
+
content = /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
579
|
+
override.text,
|
|
580
|
+
" ",
|
|
581
|
+
children
|
|
582
|
+
] });
|
|
583
|
+
} else {
|
|
584
|
+
content = override.text;
|
|
585
|
+
}
|
|
586
|
+
} else if (typeof children !== "undefined") {
|
|
587
|
+
content = children;
|
|
588
|
+
} else {
|
|
589
|
+
content = textValue;
|
|
590
|
+
}
|
|
591
|
+
const isEmpty = !content || typeof content === "string" && content.trim() === "";
|
|
592
|
+
const renderContent = active && isEmpty ? `[Empty Link: ${label || id}]` : content;
|
|
593
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
594
|
+
"a",
|
|
595
|
+
{
|
|
596
|
+
href: hrefValue,
|
|
597
|
+
target: computedTarget,
|
|
598
|
+
rel: computedRel,
|
|
599
|
+
"data-cms-id": id,
|
|
600
|
+
onClick: active ? onClick : void 0,
|
|
601
|
+
className: [
|
|
602
|
+
className,
|
|
603
|
+
editableClasses(active, selected),
|
|
604
|
+
active && isEmpty ? "opacity-50" : ""
|
|
605
|
+
].filter(Boolean).join(" "),
|
|
606
|
+
children: renderContent
|
|
607
|
+
}
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
function EditableList({
|
|
611
|
+
id,
|
|
612
|
+
label,
|
|
613
|
+
section,
|
|
614
|
+
pageId,
|
|
615
|
+
initialItems,
|
|
616
|
+
renderItem,
|
|
617
|
+
renderList,
|
|
618
|
+
itemSchema,
|
|
619
|
+
fixedLength,
|
|
620
|
+
className,
|
|
621
|
+
listClassName,
|
|
622
|
+
search,
|
|
623
|
+
reorderable,
|
|
624
|
+
sortable
|
|
625
|
+
}) {
|
|
626
|
+
const ctx = useRegister(id, "list", label, section, pageId, initialItems, itemSchema, fixedLength, search, void 0, void 0, void 0, reorderable, sortable);
|
|
627
|
+
const rawValue = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? ctx.overrides[id] : initialItems;
|
|
628
|
+
const rawList = Array.isArray(rawValue) ? rawValue : initialItems;
|
|
629
|
+
const value = Array.isArray(rawList) ? rawList.map((item, idx) => {
|
|
630
|
+
const def = Array.isArray(initialItems) ? initialItems[idx] : void 0;
|
|
631
|
+
if (def && typeof def === "object" && item && typeof item === "object") {
|
|
632
|
+
return { ...def, ...item };
|
|
633
|
+
}
|
|
634
|
+
return item;
|
|
635
|
+
}) : initialItems;
|
|
636
|
+
const active = !!ctx?.editMode;
|
|
637
|
+
const selected = ctx?.selectedId === id;
|
|
638
|
+
const onClick = (e) => {
|
|
639
|
+
if (!active || !ctx) return;
|
|
640
|
+
e.preventDefault();
|
|
641
|
+
e.stopPropagation();
|
|
642
|
+
ctx.setSelectedId(id);
|
|
643
|
+
};
|
|
644
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
645
|
+
"div",
|
|
646
|
+
{
|
|
647
|
+
"data-cms-id": id,
|
|
648
|
+
onClick: active ? onClick : void 0,
|
|
649
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
650
|
+
children: [
|
|
651
|
+
renderList ? renderList(value) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: listClassName, children: renderItem && value.map(renderItem) }),
|
|
652
|
+
active && value.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { padding: "2rem", textAlign: "center", fontSize: "0.875rem", color: "#737373", backgroundColor: "rgba(245, 245, 245, 0.5)", border: "2px dashed #d4d4d4", borderRadius: "0.75rem", width: "100%", cursor: "pointer" }, children: [
|
|
653
|
+
"Empty List ",
|
|
654
|
+
label ? `(${label})` : "",
|
|
655
|
+
" \u2014 Click to add items"
|
|
656
|
+
] })
|
|
657
|
+
]
|
|
658
|
+
}
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
function EditableReference({
|
|
662
|
+
id,
|
|
663
|
+
label,
|
|
664
|
+
section,
|
|
665
|
+
pageId,
|
|
666
|
+
collection,
|
|
667
|
+
max,
|
|
668
|
+
displayField,
|
|
669
|
+
className,
|
|
670
|
+
children
|
|
671
|
+
}) {
|
|
672
|
+
const ctx = useRegister(id, "reference", label, section, pageId, max === 1 ? "" : [], void 0, void 0, void 0, collection, max, displayField);
|
|
673
|
+
const active = !!ctx?.editMode;
|
|
674
|
+
const selected = ctx?.selectedId === id;
|
|
675
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? ctx.overrides[id] : void 0;
|
|
676
|
+
const [resolvedItems, setResolvedItems] = (0, import_react4.useState)([]);
|
|
677
|
+
(0, import_react4.useEffect)(() => {
|
|
678
|
+
if (!ctx) return;
|
|
679
|
+
const ids = Array.isArray(value) ? value : value ? [value] : [];
|
|
680
|
+
if (ids.length === 0) {
|
|
681
|
+
setResolvedItems([]);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
ctx.adapter.listEntries(collection).then((res) => {
|
|
685
|
+
setResolvedItems(res.filter((r) => ids.includes(r.id)));
|
|
686
|
+
}).catch(() => {
|
|
687
|
+
});
|
|
688
|
+
}, [value, collection, ctx]);
|
|
689
|
+
const onClick = (e) => {
|
|
690
|
+
if (!active || !ctx) return;
|
|
691
|
+
e.preventDefault();
|
|
692
|
+
e.stopPropagation();
|
|
693
|
+
ctx.setSelectedId(id);
|
|
694
|
+
};
|
|
695
|
+
const childContent = typeof children === "function" ? children(resolvedItems) : children;
|
|
696
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
697
|
+
"div",
|
|
698
|
+
{
|
|
699
|
+
"data-cms-id": id,
|
|
700
|
+
onClick: active ? onClick : void 0,
|
|
701
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
702
|
+
children: [
|
|
703
|
+
childContent,
|
|
704
|
+
active && (!resolvedItems || resolvedItems.length === 0) && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { padding: "2rem", textAlign: "center", fontSize: "0.875rem", color: "#737373", backgroundColor: "rgba(245, 245, 245, 0.5)", border: "2px dashed #d4d4d4", borderRadius: "0.75rem", width: "100%", cursor: "pointer" }, children: [
|
|
705
|
+
"Empty Reference ",
|
|
706
|
+
label ? `(${label})` : "",
|
|
707
|
+
" \u2014 Click to select"
|
|
708
|
+
] })
|
|
709
|
+
]
|
|
710
|
+
}
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
714
|
+
0 && (module.exports = {
|
|
715
|
+
CmsProvider,
|
|
716
|
+
EditableFile,
|
|
717
|
+
EditableImage,
|
|
718
|
+
EditableLink,
|
|
719
|
+
EditableList,
|
|
720
|
+
EditableReference,
|
|
721
|
+
EditableText,
|
|
722
|
+
useCmsContext,
|
|
723
|
+
useCmsOptional,
|
|
724
|
+
useSubmit
|
|
725
|
+
});
|