@remkoj/optimizely-cms-react 1.0.3
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/LICENSE +13 -0
- package/README.md +2 -0
- package/dist/browser/context.d.ts +6 -0
- package/dist/browser/context.js +15 -0
- package/dist/browser/context.js.map +1 -0
- package/dist/browser/index.d.ts +1 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/factory.d.ts +30 -0
- package/dist/factory.js +65 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/server/components/cms-content-area.d.ts +14 -0
- package/dist/server/components/cms-content-area.js +101 -0
- package/dist/server/components/cms-content-area.js.map +1 -0
- package/dist/server/components/cms-content.d.ts +12 -0
- package/dist/server/components/cms-content.js +106 -0
- package/dist/server/components/cms-content.js.map +1 -0
- package/dist/server/components/get-content-type.d.ts +12 -0
- package/dist/server/components/get-content-type.js +55 -0
- package/dist/server/components/get-content-type.js.map +1 -0
- package/dist/server/components/index.d.ts +2 -0
- package/dist/server/components/index.js +3 -0
- package/dist/server/components/index.js.map +1 -0
- package/dist/server/components/queries.d.ts +19 -0
- package/dist/server/components/queries.js +142 -0
- package/dist/server/components/queries.js.map +1 -0
- package/dist/server/components/types.d.ts +176 -0
- package/dist/server/components/types.js +2 -0
- package/dist/server/components/types.js.map +1 -0
- package/dist/server/components/utils.d.ts +10 -0
- package/dist/server/components/utils.js +28 -0
- package/dist/server/context.d.ts +35 -0
- package/dist/server/context.js +82 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/factory.d.ts +10 -0
- package/dist/server/factory.js +18 -0
- package/dist/server/factory.js.map +1 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +6 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/is-debug.d.ts +3 -0
- package/dist/server/is-debug.js +19 -0
- package/dist/server/is-debug.js.map +1 -0
- package/dist/server/type-utils.d.ts +31 -0
- package/dist/server/type-utils.js +2 -0
- package/dist/server/type-utils.js.map +1 -0
- package/dist/types.d.ts +100 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +29 -0
- package/dist/utilities.js +98 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2024 Remko Jantzen
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Services, type OptimizelyGraphConfig, type IOptiGraphClient } from '@remkoj/optimizely-graph-client';
|
|
2
|
+
import { type FunctionComponent, type PropsWithChildren } from 'react';
|
|
3
|
+
export declare const OptimizelyRouterProvider: FunctionComponent<PropsWithChildren<{
|
|
4
|
+
clientOrConfig: IOptiGraphClient | OptimizelyGraphConfig;
|
|
5
|
+
}>>;
|
|
6
|
+
export declare const useOptimizelyRouter: () => Services.RouteResolver;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Services } from '@remkoj/optimizely-graph-client';
|
|
3
|
+
import { createContext, useContext } from 'react';
|
|
4
|
+
const RouteResolverContext = createContext(undefined);
|
|
5
|
+
export const OptimizelyRouterProvider = ({ clientOrConfig, children }) => {
|
|
6
|
+
const router = new Services.RouteResolver(clientOrConfig);
|
|
7
|
+
return _jsx(RouteResolverContext.Provider, { value: router, children: children });
|
|
8
|
+
};
|
|
9
|
+
export const useOptimizelyRouter = () => {
|
|
10
|
+
const router = useContext(RouteResolverContext);
|
|
11
|
+
if (router == undefined)
|
|
12
|
+
throw new Error("useOptimizelyRouter can only be used within components that are contained within an OptimizelyRouterProvider");
|
|
13
|
+
return router;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/browser/context.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,QAAQ,EAAqD,MAAM,iCAAiC,CAAA;AAC7G,OAAO,EAAE,aAAa,EAAE,UAAU,EAAkD,MAAM,OAAO,CAAA;AAEjG,MAAM,oBAAoB,GAAG,aAAa,CAAqC,SAAS,CAAC,CAAA;AAEzF,MAAM,CAAC,MAAM,wBAAwB,GAAwG,CAAC,EAAE,cAAc,EAAE,QAAQ,EAAE,EAAE,EAAE;IAE1K,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,aAAa,CAAC,cAAc,CAAC,CAAA;IACzD,OAAO,KAAC,oBAAoB,CAAC,QAAQ,IAAC,KAAK,EAAG,MAAM,YAAK,QAAQ,GAAkC,CAAA;AACvG,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAkC,GAAG,EAAE;IACnE,MAAM,MAAM,GAAG,UAAU,CAAC,oBAAoB,CAAC,CAAA;IAC/C,IAAI,MAAM,IAAI,SAAS;QACnB,MAAM,IAAI,KAAK,CAAC,8GAA8G,CAAC,CAAA;IACnI,OAAO,MAAM,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './context';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/browser/index.tsx"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA"}
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class InvalidContentLinkError extends Error {
|
|
2
|
+
_contentLink;
|
|
3
|
+
get contentLink() {
|
|
4
|
+
return this._contentLink;
|
|
5
|
+
}
|
|
6
|
+
constructor(invalidContentLink, message) {
|
|
7
|
+
super(message ?? "Invalid content link");
|
|
8
|
+
this._contentLink = invalidContentLink;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAEpC,YAAY,CAAM;IAE5B,IAAW,WAAW;QAElB,OAAO,IAAI,CAAC,YAAY,CAAA;IAC5B,CAAC;IAED,YAAmB,kBAAwB,EAAE,OAAgB;QAEzD,KAAK,CAAC,OAAO,IAAI,sBAAsB,CAAC,CAAA;QACxC,IAAI,CAAC,YAAY,GAAG,kBAAkB,CAAA;IAC1C,CAAC;CACJ"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ComponentFactory, ComponentType, ComponentTypeHandle, ComponentTypeDictionary } from './types';
|
|
2
|
+
export declare const EmptyComponentHandle = "$$fragment$$";
|
|
3
|
+
/**
|
|
4
|
+
* The default implementation of the ComponentFactory iterface
|
|
5
|
+
*/
|
|
6
|
+
export declare class DefaultComponentFactory implements ComponentFactory {
|
|
7
|
+
private registry;
|
|
8
|
+
register(type: ComponentTypeHandle, component: ComponentType): void;
|
|
9
|
+
registerAll(components: ComponentTypeDictionary): void;
|
|
10
|
+
has(type: ComponentTypeHandle): boolean;
|
|
11
|
+
resolve(type: ComponentTypeHandle): undefined | ComponentType;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Retrieve the currently staticly cached ComponentFactory instance, if there's no
|
|
15
|
+
* currently staticly cached ComponentFactory, the default ComponentFactory will be
|
|
16
|
+
* returned
|
|
17
|
+
*
|
|
18
|
+
* @returns The ComponentFactory
|
|
19
|
+
*/
|
|
20
|
+
export declare const getFactory: () => ComponentFactory;
|
|
21
|
+
/**
|
|
22
|
+
* Update the staticly cached Component Factory, which will be returned by all future
|
|
23
|
+
* "getFactory" calls
|
|
24
|
+
*
|
|
25
|
+
* @param newFactory The ComponentFactory to set as staticly cached instance
|
|
26
|
+
* @returns void
|
|
27
|
+
*/
|
|
28
|
+
export declare const setFactory: (newFactory: ComponentFactory) => void;
|
|
29
|
+
declare const _default: ComponentFactory;
|
|
30
|
+
export default _default;
|
package/dist/factory.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const MERGE_SYMBOL = '/';
|
|
2
|
+
const DBG = process.env.DXP_DEBUG == '1';
|
|
3
|
+
export const EmptyComponentHandle = '$$fragment$$';
|
|
4
|
+
/**
|
|
5
|
+
* The default implementation of the ComponentFactory iterface
|
|
6
|
+
*/
|
|
7
|
+
export class DefaultComponentFactory {
|
|
8
|
+
registry = {};
|
|
9
|
+
register(type, component) {
|
|
10
|
+
type = processComponentTypeHandle(type);
|
|
11
|
+
this.registry[type] = component;
|
|
12
|
+
}
|
|
13
|
+
registerAll(components) {
|
|
14
|
+
components.forEach(c => this.register(c.type, c.component));
|
|
15
|
+
}
|
|
16
|
+
has(type) {
|
|
17
|
+
type = processComponentTypeHandle(type);
|
|
18
|
+
return Object.getOwnPropertyNames(this.registry).includes(type);
|
|
19
|
+
}
|
|
20
|
+
resolve(type) {
|
|
21
|
+
type = processComponentTypeHandle(type);
|
|
22
|
+
if (Object.getOwnPropertyNames(this.registry).includes(type))
|
|
23
|
+
return this.registry[type];
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function processComponentTypeHandle(handle) {
|
|
28
|
+
if (typeof (handle) == 'string')
|
|
29
|
+
return handle == "" ? EmptyComponentHandle : handle;
|
|
30
|
+
if (Array.isArray(handle) && handle.every(s => typeof (s) == 'string'))
|
|
31
|
+
return handle.filter(s => s.toLowerCase() != 'content').map(s => s == "" ? EmptyComponentHandle : s).join(MERGE_SYMBOL);
|
|
32
|
+
throw new Error(`Invalid component type handle: ${typeof (handle)}`);
|
|
33
|
+
}
|
|
34
|
+
const _static = {};
|
|
35
|
+
/**
|
|
36
|
+
* Retrieve the currently staticly cached ComponentFactory instance, if there's no
|
|
37
|
+
* currently staticly cached ComponentFactory, the default ComponentFactory will be
|
|
38
|
+
* returned
|
|
39
|
+
*
|
|
40
|
+
* @returns The ComponentFactory
|
|
41
|
+
*/
|
|
42
|
+
export const getFactory = () => {
|
|
43
|
+
if (!_static.factory) {
|
|
44
|
+
if (DBG)
|
|
45
|
+
console.log("⚪ [ComponentFactory] Creating new Component Factory");
|
|
46
|
+
_static.factory = new DefaultComponentFactory();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
if (DBG)
|
|
50
|
+
console.log("⚪ [ComponentFactory] Reusing existing Component Factory");
|
|
51
|
+
}
|
|
52
|
+
return _static.factory;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Update the staticly cached Component Factory, which will be returned by all future
|
|
56
|
+
* "getFactory" calls
|
|
57
|
+
*
|
|
58
|
+
* @param newFactory The ComponentFactory to set as staticly cached instance
|
|
59
|
+
* @returns void
|
|
60
|
+
*/
|
|
61
|
+
export const setFactory = (newFactory) => {
|
|
62
|
+
_static.factory = newFactory;
|
|
63
|
+
};
|
|
64
|
+
export default getFactory();
|
|
65
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AACA,MAAM,YAAY,GAAG,GAAG,CAAA;AACxB,MAAM,GAAG,GAAG,OAAO,CAAC,GAA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AM,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC;QACjE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,IAAI,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IAC3H,MAAM,IAAI,KAAK,CAAC,kCAAmC,OAAM,CAAC,MAAM,CAAE,EAAE,CAAC,CAAA;AACzE,CAAC;AAED,MAAM,OAAO,GAAqC,EAAE,CAAA;AAEpD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAA4B,GAAG,EAAE;IACpD,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACnB,IAAI,GAAG;YAAE,OAAO,CAAC,GAAG,CAAC,qDAAqD,CAAC,CAAA;QAC3E,OAAO,CAAC,OAAO,GAAG,IAAI,uBAAuB,EAAE,CAAA;IACnD,CAAC;SAAM,CAAC;QACJ,IAAI,GAAG;YAAE,OAAO,CAAC,GAAG,CAAC,yDAAyD,CAAC,CAAA;IACnF,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,CAAA;AAC1B,CAAC,CAAA;AACD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAA4C,CAAC,UAA4B,EAAE,EAAE;IAChG,OAAO,CAAC,OAAO,GAAG,UAAU,CAAA;AAChC,CAAC,CAAA;AACD,eAAe,UAAU,EAAE,CAAA"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The version of the current Optimizely DXP React SDK
|
|
3
|
+
*/
|
|
4
|
+
export const Version = '0.0.1';
|
|
5
|
+
// Export library
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * as Errors from './errors';
|
|
8
|
+
export * as Utils from './utilities';
|
|
9
|
+
export * from './factory';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAA;AAE9B,iBAAiB;AACjB,cAAc,SAAS,CAAA;AACvB,OAAO,KAAK,MAAM,MAAM,UAAU,CAAA;AAClC,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AACpC,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import 'server-only';
|
|
3
|
+
import type { ElementType } from '../type-utils';
|
|
4
|
+
import type { CmsContentAreaProps, ContentAreaItemDefinition } from './types';
|
|
5
|
+
export type { CmsContentAreaClassMapper, CmsContentAreaProps, ContentAreaItemDefinition } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* React server component to render a content area
|
|
8
|
+
*
|
|
9
|
+
* @param param0 The content area information for rendering
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare const CmsContentArea: <T extends ElementType = "div", I extends ElementType = "div">({ items, classMapper, className, fieldName, as: elementType, itemsProperty, itemWrapper, locale: _locale, client: _client, factory: _factory, inEditMode: _inEditMode, ...additionalProps }: CmsContentAreaProps<T, I>) => Promise<JSX.Element>;
|
|
13
|
+
export declare function processContentAreaItems(items?: (ContentAreaItemDefinition | null)[] | null, locale?: string): Promise<JSX.Element[]>;
|
|
14
|
+
export default CmsContentArea;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import 'server-only';
|
|
3
|
+
import CmsContent from './cms-content';
|
|
4
|
+
import * as Utils from '../../utilities';
|
|
5
|
+
import * as Errors from '../../errors';
|
|
6
|
+
import createClient from '@remkoj/optimizely-graph-client';
|
|
7
|
+
import getServerContext from '../context';
|
|
8
|
+
//#endregion
|
|
9
|
+
/**
|
|
10
|
+
* React server component to render a content area
|
|
11
|
+
*
|
|
12
|
+
* @param param0 The content area information for rendering
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export const CmsContentArea = async ({ items, classMapper, className, fieldName, as: elementType, itemsProperty, itemWrapper, locale: _locale, client: _client, factory: _factory, inEditMode: _inEditMode, ...additionalProps }) => {
|
|
16
|
+
const context = getServerContext();
|
|
17
|
+
if (context.isDebugOrDevelopment && !context.client)
|
|
18
|
+
console.warn(`🟠 [CmsContentArea] No Content Graph client provided for ${fieldName ?? 'CmsContentArea'}, this will cause problems with edit mode!`);
|
|
19
|
+
// Parse & prepare props
|
|
20
|
+
const { inEditMode, factory, locale: ctxLocale } = context;
|
|
21
|
+
const gqlClient = context.client ?? createClient();
|
|
22
|
+
// Determine locale
|
|
23
|
+
const locale = _locale ?? ctxLocale;
|
|
24
|
+
if (context.isDebug && locale == _locale && locale != ctxLocale) {
|
|
25
|
+
console.warn(`🟠 [CmsContentArea] The Context Locale for ContentArea ${fieldName ?? 'CmsContentArea'}, was overridden. Context Locale ${ctxLocale}, current locale ${locale}`);
|
|
26
|
+
}
|
|
27
|
+
if (!locale) {
|
|
28
|
+
console.error(`🔴 [CmsContentArea] No locale set on the ServerContext, which is required to make the ContentArea${fieldName ? ' ' + fieldName : ''} render its content`);
|
|
29
|
+
return _jsx(_Fragment, {});
|
|
30
|
+
}
|
|
31
|
+
// Convert the items to a list of enriched content types and filter out items cannot be loaded
|
|
32
|
+
const actualItems = (items || []).filter(forValidContentAreaItems);
|
|
33
|
+
const componentData = await Promise.all(actualItems.map(async (item, idx) => {
|
|
34
|
+
// Prepare data from received content area format
|
|
35
|
+
const contentLink = Utils.normalizeContentLinkWithLocale({ ...item.item, locale: locale });
|
|
36
|
+
if (!contentLink)
|
|
37
|
+
throw new Errors.InvalidContentLinkError({ ...item.item, locale: locale });
|
|
38
|
+
const contentType = Utils.normalizeContentType(item.item?.data?.contentType);
|
|
39
|
+
const fragmentData = item.item?.data || undefined;
|
|
40
|
+
// Read element wrapper configuration
|
|
41
|
+
const { as: contentItemElement, className: contentItemBaseClassName, itemsProperty: contentItemTarget, ...contentItemElementProps } = itemWrapper ?? {};
|
|
42
|
+
// Generate element wrapper properties
|
|
43
|
+
const contentAreaItemContainerKey = `ContentAreaItem-${idx}-${contentLink.guidValue}-${contentLink.id}-${contentLink.workId}`;
|
|
44
|
+
const contentAreaItemContainerProps = {
|
|
45
|
+
className: `opti-content-area-item opti-content-area-item-${idx}${contentItemBaseClassName ? ' ' + contentItemBaseClassName : ''} ${classMapper ? classMapper(item.displayOption ?? 'default', contentType ?? null, idx) : ""}`,
|
|
46
|
+
"data-epi-block-id": inEditMode && fieldName ? contentLink?.id || undefined : undefined,
|
|
47
|
+
"data-displayoption": item.displayOption || undefined,
|
|
48
|
+
"data-tag": item.tag || undefined,
|
|
49
|
+
...contentItemElementProps
|
|
50
|
+
};
|
|
51
|
+
const contentAraeItemContent = await CmsContent({ contentLink, contentType, fragmentData, client: gqlClient, factory, outputEditorWarning: inEditMode, contentTypePrefix: "Block" });
|
|
52
|
+
// Inject the element into the wrapper
|
|
53
|
+
const childrenTarget = contentItemTarget || "children";
|
|
54
|
+
let contentAreaItemContainerChildren = undefined;
|
|
55
|
+
if (childrenTarget == "children")
|
|
56
|
+
contentAreaItemContainerChildren = contentAraeItemContent;
|
|
57
|
+
else
|
|
58
|
+
contentAreaItemContainerProps[childrenTarget] = contentAraeItemContent;
|
|
59
|
+
const ContentAreaItemContainer = contentItemElement || "div";
|
|
60
|
+
return _jsx(ContentAreaItemContainer, { ...contentAreaItemContainerProps, children: contentAreaItemContainerChildren }, contentAreaItemContainerKey);
|
|
61
|
+
}));
|
|
62
|
+
// Build container element
|
|
63
|
+
const contentAreaContainerProps = {
|
|
64
|
+
className: `opti-content-area ${Array.isArray(className) ? className.join(' ') : (className ?? '')}`.trim(),
|
|
65
|
+
"data-epi-edit": inEditMode && fieldName ? fieldName : undefined,
|
|
66
|
+
...additionalProps
|
|
67
|
+
};
|
|
68
|
+
const contentAreaContainerChildrenTarget = itemsProperty ?? "children";
|
|
69
|
+
let contentAreaContainerChildren = undefined;
|
|
70
|
+
if (contentAreaContainerChildrenTarget == "children")
|
|
71
|
+
contentAreaContainerChildren = componentData;
|
|
72
|
+
else
|
|
73
|
+
contentAreaContainerProps[contentAreaContainerChildrenTarget] = componentData;
|
|
74
|
+
const ContentAreaContainer = elementType ?? "div";
|
|
75
|
+
return _jsx(ContentAreaContainer, { ...contentAreaContainerProps, children: contentAreaContainerChildren });
|
|
76
|
+
};
|
|
77
|
+
function forValidContentAreaItems(itm) {
|
|
78
|
+
if (itm == undefined || itm == null)
|
|
79
|
+
return false;
|
|
80
|
+
if (itm.item == undefined || itm.item == null)
|
|
81
|
+
return false;
|
|
82
|
+
if (itm.item.data == undefined || itm.item.data == null)
|
|
83
|
+
return typeof (itm.item.guidValue) == 'string' && itm.item.guidValue.length > 0;
|
|
84
|
+
return itm.item.guidValue == itm.item.data.id?.guidValue;
|
|
85
|
+
}
|
|
86
|
+
export async function processContentAreaItems(items, locale) {
|
|
87
|
+
const actualItems = (items ?? []).filter(Utils.isNotNullOrUndefined);
|
|
88
|
+
return Promise.all(actualItems.map(async (item, idx) => {
|
|
89
|
+
// Prepare data from received content area format
|
|
90
|
+
const contentLink = Utils.normalizeContentLinkWithLocale({ ...item.item, locale: locale });
|
|
91
|
+
if (!contentLink)
|
|
92
|
+
throw new Errors.InvalidContentLinkError({ ...item.item, locale: locale });
|
|
93
|
+
const contentType = Utils.normalizeContentType(item.item?.data?.contentType);
|
|
94
|
+
const fragmentData = item.item?.data || undefined;
|
|
95
|
+
const guidValue = (item.item?.guidValue ?? "00000000-0000-0000-0000-000000000000") + "::" + idx;
|
|
96
|
+
// Build output
|
|
97
|
+
return await CmsContent({ contentLink, contentType, fragmentData, key: guidValue });
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
export default CmsContentArea;
|
|
101
|
+
//# sourceMappingURL=cms-content-area.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cms-content-area.js","sourceRoot":"","sources":["../../../src/server/components/cms-content-area.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAA;AAEpB,OAAO,UAAU,MAAM,eAAe,CAAA;AAEtC,OAAO,KAAK,KAAK,MAAM,iBAAiB,CAAA;AACxC,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,YAAY,MAAM,iCAAiC,CAAA;AAC1D,OAAO,gBAAgB,MAAM,YAAY,CAAA;AAKzC,YAAY;AAEZ;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EAAgE,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,WAAW,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAC,WAAW,EAAE,GAAG,eAAe,EAA4B,EAAyB,EAAE;IAE9U,MAAM,OAAO,GAAG,gBAAgB,EAAE,CAAA;IAClC,IAAI,OAAO,CAAC,oBAAoB,IAAI,CAAC,OAAO,CAAC,MAAM;QAC/C,OAAO,CAAC,IAAI,CAAC,4DAA6D,SAAS,IAAI,gBAAiB,4CAA4C,CAAC,CAAA;IAEzJ,wBAAwB;IACxB,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;IAC1D,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,IAAI,YAAY,EAAE,CAAA;IAElD,mBAAmB;IACnB,MAAM,MAAM,GAAG,OAAO,IAAI,SAAS,CAAA;IACnC,IAAI,OAAO,CAAC,OAAO,IAAI,MAAM,IAAI,OAAO,IAAI,MAAM,IAAI,SAAS,EAAE,CAAC;Q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M,CAAC,KAAK,UAAU,uBAAuB,CAAE,KAAmD,EAAE,MAAe;IAE/G,MAAM,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;IACpE,OAAO,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAA+B,EAAE;QAChF,iDAAiD;QACjD,MAAM,WAAW,GAAG,KAAK,CAAC,8BAA8B,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;QAC1F,IAAI,CAAC,WAAW;YACZ,MAAM,IAAI,MAAM,CAAC,uBAAuB,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;QAC9E,MAAM,WAAW,GAAG,KAAK,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,CAAA;QAC5E,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,EAAE,IAAI,IAAI,SAAS,CAAA;QACjD,MAAM,SAAS,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,IAAI,sCAAsC,CAAC,GAAC,IAAI,GAAC,GAAG,CAAA;QAE3F,eAAe;QACf,OAAO,MAAM,UAAU,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAA;IACvF,CAAC,CAAC,CAAC,CAAA;AACP,CAAC;AAED,eAAe,cAAc,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import 'server-only';
|
|
3
|
+
import type { CmsContentProps } from './types';
|
|
4
|
+
export type { CmsContentProps } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* React Server Side component for the CmsContent
|
|
7
|
+
*
|
|
8
|
+
* @param param0
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare const CmsContent: ({ contentType, contentTypePrefix, contentLink, children, fragmentData }: CmsContentProps) => Promise<JSX.Element>;
|
|
12
|
+
export default CmsContent;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { jsxs as _jsxs, Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import 'server-only';
|
|
3
|
+
import getContentType from './get-content-type';
|
|
4
|
+
import getServerContext from '../context';
|
|
5
|
+
import createClient, { AuthMode } from '@remkoj/optimizely-graph-client';
|
|
6
|
+
import { print } from 'graphql';
|
|
7
|
+
import * as Utils from "../../utilities";
|
|
8
|
+
import * as Queries from './queries';
|
|
9
|
+
/**
|
|
10
|
+
* React Server Side component for the CmsContent
|
|
11
|
+
*
|
|
12
|
+
* @param param0
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export const CmsContent = async ({ contentType, contentTypePrefix, contentLink, children, fragmentData }) => {
|
|
16
|
+
const context = getServerContext();
|
|
17
|
+
if (context.isDebugOrDevelopment && !context.client)
|
|
18
|
+
console.warn(`🟠 [CmsContent] No Content Graph client provided with ${JSON.stringify(contentLink)}, this will cause problems with edit mode!`);
|
|
19
|
+
// Parse & prepare props
|
|
20
|
+
const inEditMode = context.inEditMode && context.isEditableContent(contentLink);
|
|
21
|
+
const outputEditorWarning = context.forceEditorWarnings;
|
|
22
|
+
const factory = context.factory;
|
|
23
|
+
const client = context.client ?? createClient();
|
|
24
|
+
const isInlineBlock = Utils.isInlineContentLink(contentLink);
|
|
25
|
+
if (context.isDebug && inEditMode)
|
|
26
|
+
console.log(`👔 [CmsContent] Edit mode active for content with id: ${JSON.stringify(contentLink)}`);
|
|
27
|
+
if (context.isDebug && inEditMode && client.currentAuthMode == AuthMode.Public)
|
|
28
|
+
console.warn(`🟠 [CmsContent] Edit mode active without an authenticated client, this will cause problems`);
|
|
29
|
+
// DEBUG Tracing
|
|
30
|
+
if (context.isDebug)
|
|
31
|
+
console.log("⚪ [CmsContent] Rendering CMS Content for:", JSON.stringify(contentType), isInlineBlock ? "Inline content" : JSON.stringify({ id: contentLink.id, workId: contentLink.workId, guidValue: contentLink.guidValue, locale: contentLink.locale }), inEditMode ? "edit-mode" : "published");
|
|
32
|
+
// Ensure we have a content type to work with
|
|
33
|
+
if (!isInlineBlock && !contentType) {
|
|
34
|
+
if (context.isDebugOrDevelopment)
|
|
35
|
+
console.warn(`🟠 [CmsContent] No content type provided for content ${JSON.stringify({ id: contentLink.id, workId: contentLink.workId, guidValue: contentLink.guidValue, locale: contentLink.locale })}, this causes an additional GraphQL query to resolve the ContentType`);
|
|
36
|
+
contentType = await getContentType(contentLink, client);
|
|
37
|
+
}
|
|
38
|
+
// Apply the content-type prefix if needed
|
|
39
|
+
if (Array.isArray(contentType) && Utils.isNonEmptyString(contentTypePrefix) && contentType.length > 0 && contentType[0] != contentTypePrefix) {
|
|
40
|
+
if (context.isDebug)
|
|
41
|
+
console.info(`⚪ [CmsContent] Component type [${contentType.join('/')}] doesn't have the configured prefix, adding ${contentTypePrefix} as prefix`);
|
|
42
|
+
contentType.unshift(contentTypePrefix);
|
|
43
|
+
}
|
|
44
|
+
// Resolve component
|
|
45
|
+
const Component = factory.resolve(contentType ?? "");
|
|
46
|
+
if (!Component) {
|
|
47
|
+
if (context.isDebugOrDevelopment) {
|
|
48
|
+
console.warn(`🟠 [CmsContent] Component of type "${contentType?.join('/') ?? ""}" not resolved by factory`);
|
|
49
|
+
}
|
|
50
|
+
if (context.isDebug || inEditMode || outputEditorWarning) {
|
|
51
|
+
const errorMsg = _jsxs("div", { className: 'opti-error', children: ["Component of type \"", contentType?.join('/') ?? "", "\" not resolved by factory"] });
|
|
52
|
+
return children ? _jsxs(_Fragment, { children: [errorMsg, children] }) : errorMsg;
|
|
53
|
+
}
|
|
54
|
+
return _jsx(_Fragment, { children: children ? children : undefined });
|
|
55
|
+
}
|
|
56
|
+
if (context.isDebug)
|
|
57
|
+
console.log("⚪ [CmsContent] Rendering item using component:", Component?.displayName ?? Component);
|
|
58
|
+
// Render with previously loaded data
|
|
59
|
+
const fragmentProps = fragmentData ? Object.getOwnPropertyNames(fragmentData).filter(x => !Queries.CmsContentFragments.IContentDataProps.includes(x)) : [];
|
|
60
|
+
if (fragmentProps.length > 0) {
|
|
61
|
+
if (context.isDebug)
|
|
62
|
+
console.log("⚪ [CmsContent] Rendering CMS Component using fragment information", fragmentProps);
|
|
63
|
+
if (Utils.validatesFragment(Component) && !Component.validateFragment(fragmentData)) {
|
|
64
|
+
console.error("🔴 [CmsContent] Invalid fragment data received for ", Component.displayName ?? contentType?.join("/") ?? "[Undetermined component]");
|
|
65
|
+
return _jsx(_Fragment, {});
|
|
66
|
+
}
|
|
67
|
+
return _jsx(Component, { inEditMode: inEditMode, contentLink: contentLink, data: fragmentData || {}, client: client });
|
|
68
|
+
}
|
|
69
|
+
// If we don't have previously loaded data we cannot load content for inline blocks
|
|
70
|
+
if (isInlineBlock)
|
|
71
|
+
return (context.isDebug || inEditMode || outputEditorWarning) ? _jsx("div", { className: 'opti-error', children: "Inline blocks cannot be loaded individually" }) : _jsx(_Fragment, {});
|
|
72
|
+
// Render using included query
|
|
73
|
+
if (Utils.isCmsComponentWithDataQuery(Component)) {
|
|
74
|
+
const gqlQuery = Component.getDataQuery();
|
|
75
|
+
const gqlVariables = Utils.contentLinkToRequestVariables(contentLink);
|
|
76
|
+
if (context.isDebug)
|
|
77
|
+
console.log("⚪ [CmsContent] Component data fetching variables:", gqlVariables);
|
|
78
|
+
const gqlResponse = await client.request(gqlQuery, gqlVariables);
|
|
79
|
+
if (context.isDebug)
|
|
80
|
+
console.log("⚪ [CmsContent] Component request the following data:", gqlResponse);
|
|
81
|
+
return _jsx(Component, { inEditMode: inEditMode, contentLink: contentLink, data: gqlResponse, client: client });
|
|
82
|
+
}
|
|
83
|
+
// Render using included fragment
|
|
84
|
+
if (Utils.isCmsComponentWithFragment(Component)) {
|
|
85
|
+
const [name, fragment] = Component.getDataFragment();
|
|
86
|
+
const fragmentQuery = `query getContentFragmentById($id: Int!, $workId: Int, $guidValue: String, $locale: [Locales]!, $isCommonDraft: Boolean ) { contentById: Content( where: { ContentLink: { Id: { eq: $id }, WorkId: { eq: $workId }, GuidValue: { eq: $guidValue } },IsCommonDraft: {eq: $isCommonDraft} }, orderBy: { Status: ASC }, locale: $locale, limit: 1 ) { total items { contentType: ContentType id: ContentLink { id: Id, workId: WorkId, guidValue: GuidValue } locale: Language { name: Name } ...${name} } } } ${print(fragment)}`;
|
|
87
|
+
const fragmentVariables = Utils.contentLinkToRequestVariables(contentLink);
|
|
88
|
+
if (!fragmentVariables?.workId && inEditMode)
|
|
89
|
+
fragmentVariables.isCommonDraft = true;
|
|
90
|
+
if (context.isDebug)
|
|
91
|
+
console.log(`⚪ [CmsContent] Component data fetching using fragment ${name}, with variables: ${JSON.stringify(fragmentVariables)}`);
|
|
92
|
+
const fragmentResponse = await client.request(fragmentQuery, fragmentVariables);
|
|
93
|
+
const totalItems = fragmentResponse.contentById.total || 0;
|
|
94
|
+
if (totalItems < 1)
|
|
95
|
+
throw new Error(`CmsContent expected to load exactly one content item of type ${name}, received ${totalItems} from Optimizely Graph. Content Item: ${JSON.stringify(fragmentVariables)}`);
|
|
96
|
+
if (totalItems > 1 && context.isDebug)
|
|
97
|
+
console.warn(`🟠 [CmsContent] Resolved ${totalItems} content items, expected only 1. Picked the first one`);
|
|
98
|
+
return _jsx(Component, { inEditMode: inEditMode, contentLink: contentLink, data: fragmentResponse.contentById.items[0], client: client });
|
|
99
|
+
}
|
|
100
|
+
// Assume there's no server side prepared data needed for the component
|
|
101
|
+
if (context.isDebug)
|
|
102
|
+
console.log(`⚪ [CmsContent] Component of type "${contentType?.join('/') ?? Component.displayName ?? '?'}" did not request pre-loading of data`);
|
|
103
|
+
return _jsx(Component, { inEditMode: inEditMode, contentLink: contentLink, data: fragmentData || {}, client: client });
|
|
104
|
+
};
|
|
105
|
+
export default CmsContent;
|
|
106
|
+
//# sourceMappingURL=cms-content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cms-content.js","sourceRoot":"","sources":["../../../src/server/components/cms-content.tsx"],"names":[],"mappings":";AAAA,OAAO,aAAa,CAAA;AAIpB,OAAO,cAAc,MAAM,oBAAoB,CAAA;AAC/C,OAAO,gBAAgB,MAAM,YAAY,CAAA;AACzC,OAAO,YAAY,EAAE,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AACxE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,KAAK,KAAK,MAAM,iBAAiB,CAAA;AACxC,OAAO,KAAK,OAAO,MAAM,WAAW,CAAA;AAUpC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,EAAC,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAmB,EAAyB,EAAE;IAE/I,MAAM,OAAO,GAAG,gBAAgB,EAAE,CAAA;IAClC,IAAI,OAAO,CAAC,oBAAoB,IAAI,CAAC,OAAO,CAAC,MAAM;QAC/C,OAAO,CAAC,IAAI,CAAC,yDAA0D,IAAI,CAAC,SAAS,CAAC,WAAW,CAAE,4CAA4C,CAAC,CAAA;IAEpJ,wBAAwB;IACxB,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAA;IAC/E,MAAM,mBAAmB,GAAG,OAAO,CAAC,mBAAmB,CAAA;IACvD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;IAC/B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,YAAY,EAAE,CAAA;IAC/C,MAAM,aAAa,GAAG,KAAK,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAA;IAC5D,IAAI,OAAO,CAAC,OAAO,IAAI,UAAU;QAC7B,OAAO,CAAC,GAAG,CAAC,yDAA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UAAU,EAAG,UAAU,EAAG,WAAW,EAAG,WAAW,EAAG,IAAI,EAAG,gBAAgB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,EAAG,MAAM,EAAG,MAAM,GAAK,CAAA;IAC/I,CAAC;IAED,uEAAuE;IACvE,IAAI,OAAO,CAAC,OAAO;QACf,OAAO,CAAC,GAAG,CAAC,qCAAsC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,SAAS,CAAC,WAAW,IAAI,GAAG,uCAAuC,CAAC,CAAA;IACpJ,OAAO,KAAC,SAAS,IAAC,UAAU,EAAG,UAAU,EAAG,WAAW,EAAG,WAAW,EAAG,IAAI,EAAG,YAAY,IAAI,EAAE,EAAG,MAAM,EAAG,MAAM,GAAK,CAAA;AAC5H,CAAC,CAAA;AAED,eAAe,UAAU,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { IOptiGraphClient } from "@remkoj/optimizely-graph-client";
|
|
2
|
+
import type { ContentType, ContentLinkWithLocale } from "../../types";
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the ContentType of an Optimizely CMS Component, identified by its content link
|
|
5
|
+
*
|
|
6
|
+
* @param link The ContentLink of the content item
|
|
7
|
+
* @param gqlClient The GraphQL client to use
|
|
8
|
+
* @returns The ContentType, or undefined if it cannot be resolved
|
|
9
|
+
*/
|
|
10
|
+
export declare function getContentType(link: ContentLinkWithLocale, gqlClient: IOptiGraphClient): Promise<ContentType | undefined>;
|
|
11
|
+
export default getContentType;
|
|
12
|
+
export declare const getContentTypeQuery: string;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { gql } from 'graphql-request';
|
|
2
|
+
import * as Utils from "../../utilities";
|
|
3
|
+
const DEBUG = false;
|
|
4
|
+
/**
|
|
5
|
+
* Resolve the ContentType of an Optimizely CMS Component, identified by its content link
|
|
6
|
+
*
|
|
7
|
+
* @param link The ContentLink of the content item
|
|
8
|
+
* @param gqlClient The GraphQL client to use
|
|
9
|
+
* @returns The ContentType, or undefined if it cannot be resolved
|
|
10
|
+
*/
|
|
11
|
+
export async function getContentType(link, gqlClient) {
|
|
12
|
+
if ((!link.id || link.id < 1) && (!link.guidValue || link.guidValue == ""))
|
|
13
|
+
throw new Error("Cannot dynamically determine the content type of an inline block");
|
|
14
|
+
const gqlQueryVars = Utils.contentLinkToRequestVariables(link);
|
|
15
|
+
const gqlResponse = await gqlClient.request(getContentTypeQuery, gqlQueryVars);
|
|
16
|
+
if (gqlResponse.Content?.total != 1) {
|
|
17
|
+
if (DEBUG)
|
|
18
|
+
console.error(`getContentType: Expected exactly one type, received ${gqlResponse.Content?.total ?? 0} types for`, gqlQueryVars);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const items = gqlResponse.Content?.items;
|
|
22
|
+
if (!items || items.length == 0)
|
|
23
|
+
throw new Error("The content item could not be found!");
|
|
24
|
+
const contentType = Utils.normalizeContentType(items[0]?.ContentType);
|
|
25
|
+
if (!contentType)
|
|
26
|
+
throw new Error("The item did not contain type information");
|
|
27
|
+
return contentType;
|
|
28
|
+
}
|
|
29
|
+
export default getContentType;
|
|
30
|
+
export const getContentTypeQuery = gql `query getContentType($id: Int, $workId: Int, $guidValue: String, $locale: [Locales])
|
|
31
|
+
{
|
|
32
|
+
Content(
|
|
33
|
+
where: {
|
|
34
|
+
ContentLink: {
|
|
35
|
+
GuidValue: {
|
|
36
|
+
eq: $guidValue
|
|
37
|
+
}
|
|
38
|
+
Id: {
|
|
39
|
+
eq: $id
|
|
40
|
+
},
|
|
41
|
+
WorkId: {
|
|
42
|
+
eq: $workId
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
locale: $locale
|
|
47
|
+
limit: 1
|
|
48
|
+
) {
|
|
49
|
+
items {
|
|
50
|
+
ContentType
|
|
51
|
+
},
|
|
52
|
+
total
|
|
53
|
+
}
|
|
54
|
+
}`;
|
|
55
|
+
//# sourceMappingURL=get-content-type.js.map
|
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,KAAK,CAAC,2CAA2C,CAAC,CAAA;IAEhE,OAAO,WAAW,CAAA;AACtB,CAAC;AAED,eAAe,cAAc,CAAA;AAW7B,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;EAwBpC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,oBAAoB,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const CmsContentFragments: {
|
|
2
|
+
IContentDataProps: string[];
|
|
3
|
+
ContentLink: string;
|
|
4
|
+
ContentLinkSearch: string;
|
|
5
|
+
IContentData: string;
|
|
6
|
+
ContentAreaItemData: string;
|
|
7
|
+
BlockContentAreaItemSearchData: string;
|
|
8
|
+
BlockContentAreaItemData: string;
|
|
9
|
+
LinkItemData: string;
|
|
10
|
+
ImageData: string;
|
|
11
|
+
ImageDataSearch: string;
|
|
12
|
+
BlockData: string;
|
|
13
|
+
PageData: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const ContentAreaFragments: {
|
|
16
|
+
ContentAreaItemBase: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const getContentById: string;
|
|
19
|
+
export declare const getContentByPath: string;
|