@axcode/axcloud-sdk 0.1.5 → 0.1.6
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 +41 -1
- package/dist/index.d.ts +41 -1
- package/dist/index.js +94 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +92 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -236,6 +236,88 @@ function getClient(key) {
|
|
|
236
236
|
throw new Error("AxCloud client not found. Make sure AxCloudProvider is mounted and use useAxCloudContext() to get the client.");
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
// src/core/contentSlots.ts
|
|
240
|
+
var SLOT_ATTRIBUTE_KEYS = ["slot", "field", "key", "name"];
|
|
241
|
+
function getContentSlotMap(content) {
|
|
242
|
+
const fields = isRecord(content?.fields) ? { ...content?.fields } : {};
|
|
243
|
+
for (const block of content?.blocks || []) {
|
|
244
|
+
const slot = getBlockSlot(block);
|
|
245
|
+
if (!slot) continue;
|
|
246
|
+
fields[slot] = blockToSlotValue(block);
|
|
247
|
+
}
|
|
248
|
+
return fields;
|
|
249
|
+
}
|
|
250
|
+
function createContentSlotReader(content) {
|
|
251
|
+
const fields = getContentSlotMap(content);
|
|
252
|
+
const value = (key, fallback) => {
|
|
253
|
+
return key in fields ? fields[key] : fallback;
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
fields,
|
|
257
|
+
value,
|
|
258
|
+
text: (key, fallback = "") => stringifySlotValue(value(key, fallback)),
|
|
259
|
+
html: (key, fallback = "") => stringifySlotValue(value(key, fallback)),
|
|
260
|
+
list: (key, fallback = []) => slotValueToList(value(key, fallback)),
|
|
261
|
+
image: (key, fallback) => slotValueToImage(value(key, fallback))
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function getBlockSlot(block) {
|
|
265
|
+
const attributes = block.attributes || {};
|
|
266
|
+
for (const key of SLOT_ATTRIBUTE_KEYS) {
|
|
267
|
+
const value = attributes[key];
|
|
268
|
+
if (typeof value === "string" && value.trim()) return value.trim();
|
|
269
|
+
}
|
|
270
|
+
return void 0;
|
|
271
|
+
}
|
|
272
|
+
function blockToSlotValue(block) {
|
|
273
|
+
if (block.attributes && "value" in block.attributes) {
|
|
274
|
+
return block.attributes.value;
|
|
275
|
+
}
|
|
276
|
+
if (block.type === "list") {
|
|
277
|
+
return Array.isArray(block.items) ? block.items : [];
|
|
278
|
+
}
|
|
279
|
+
if (block.type === "image" && block.url) {
|
|
280
|
+
return {
|
|
281
|
+
url: block.url,
|
|
282
|
+
alt: block.alt,
|
|
283
|
+
caption: block.caption
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return block.content ?? "";
|
|
287
|
+
}
|
|
288
|
+
function stringifySlotValue(value) {
|
|
289
|
+
if (value === void 0 || value === null) return "";
|
|
290
|
+
if (typeof value === "string") return value;
|
|
291
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
292
|
+
if (Array.isArray(value)) return value.map((item) => stringifySlotValue(item)).filter(Boolean).join("\n");
|
|
293
|
+
return "";
|
|
294
|
+
}
|
|
295
|
+
function slotValueToList(value) {
|
|
296
|
+
if (Array.isArray(value)) {
|
|
297
|
+
return value.map((item) => stringifySlotValue(item)).filter(Boolean);
|
|
298
|
+
}
|
|
299
|
+
if (typeof value === "string") {
|
|
300
|
+
return value.split(/\r?\n/).map((item) => item.trim()).filter(Boolean);
|
|
301
|
+
}
|
|
302
|
+
return [];
|
|
303
|
+
}
|
|
304
|
+
function slotValueToImage(value) {
|
|
305
|
+
if (isRecord(value) && typeof value.url === "string") {
|
|
306
|
+
return {
|
|
307
|
+
url: value.url,
|
|
308
|
+
alt: typeof value.alt === "string" ? value.alt : void 0,
|
|
309
|
+
caption: typeof value.caption === "string" ? value.caption : void 0
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (typeof value === "string" && value) {
|
|
313
|
+
return { url: value };
|
|
314
|
+
}
|
|
315
|
+
return void 0;
|
|
316
|
+
}
|
|
317
|
+
function isRecord(value) {
|
|
318
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
319
|
+
}
|
|
320
|
+
|
|
239
321
|
// src/core/types.ts
|
|
240
322
|
function userHasPermission(user, permission) {
|
|
241
323
|
if (user.permissions && user.permissions.length > 0) {
|
|
@@ -789,6 +871,15 @@ function useContentItem(contentType, idOrSlug, options = {}) {
|
|
|
789
871
|
refetch: query.refetch
|
|
790
872
|
};
|
|
791
873
|
}
|
|
874
|
+
function usePageContent(slug, options = {}) {
|
|
875
|
+
const state = useContentItem("pages", slug, options);
|
|
876
|
+
const reader = useMemo(() => createContentSlotReader(state.data), [state.data]);
|
|
877
|
+
return {
|
|
878
|
+
...state,
|
|
879
|
+
page: state.data,
|
|
880
|
+
...reader
|
|
881
|
+
};
|
|
882
|
+
}
|
|
792
883
|
function useMySection(options = {}) {
|
|
793
884
|
const { includeMembers = true } = options;
|
|
794
885
|
const { isAuthenticated } = useAuth0();
|
|
@@ -1308,6 +1399,6 @@ function LinkedAccountsList({
|
|
|
1308
1399
|
] });
|
|
1309
1400
|
}
|
|
1310
1401
|
|
|
1311
|
-
export { AxCloudClient, AxCloudError, AxCloudProvider, LinkedAccountsList, LoginButton, LogoutButton, ModuleGate, ProtectedRoute, createClient, createFormHelper, getClient, isAxCloudError, submitForm, useAuth, useAvatar, useAxCloudContext, useBranding, useContent, useContentItem, useFormStats, useForms, useLinkedAccounts, useLogo, useMedia, useMediaList, useModuleAccess, useMySection, useNews, useOrg, useSections, useUpdateFormSubmission, useUser, userHasAllPermissions, userHasAnyPermission, userHasPermission };
|
|
1402
|
+
export { AxCloudClient, AxCloudError, AxCloudProvider, LinkedAccountsList, LoginButton, LogoutButton, ModuleGate, ProtectedRoute, createClient, createContentSlotReader, createFormHelper, getClient, getContentSlotMap, isAxCloudError, submitForm, useAuth, useAvatar, useAxCloudContext, useBranding, useContent, useContentItem, useFormStats, useForms, useLinkedAccounts, useLogo, useMedia, useMediaList, useModuleAccess, useMySection, useNews, useOrg, usePageContent, useSections, useUpdateFormSubmission, useUser, userHasAllPermissions, userHasAnyPermission, userHasPermission };
|
|
1312
1403
|
//# sourceMappingURL=index.mjs.map
|
|
1313
1404
|
//# sourceMappingURL=index.mjs.map
|