@dotcms/react 0.0.1-beta.20 → 0.0.1-beta.22
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/README.md +7 -7
- package/next.esm.js +122 -3
- package/package.json +3 -3
- package/src/lib/next/hooks/useEditableDotCMSPage.d.ts +90 -0
- package/src/next.d.ts +1 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
`@dotcms/react` is the official set of React components and hooks designed to work seamlessly with dotCMS, making it easy to render dotCMS pages and use the page builder.
|
|
4
4
|
|
|
5
5
|
> **Note:** This SDK is currently in **beta** (v0.0.1-beta.13 or newest).
|
|
6
|
-
>
|
|
6
|
+
>
|
|
7
7
|
> For comprehensive documentation, visit our [developer portal](https://dev.dotcms.com/docs/javascript-sdk-react-library).
|
|
8
8
|
|
|
9
9
|
> **⚠️ IMPORTANT:** Versions published under the `next` tag (`npm install @dotcms/react@next`) are experimental, in beta, and not code complete. For the current stable and functional version, please use `latest` (`npm install @dotcms/react@latest`). Once we release the stable version, we will provide a migration guide from the alpha to stable version. The current alpha version (under `latest`) will continue to work, allowing you to migrate progressively at your own pace.
|
|
@@ -108,7 +108,7 @@ The `DotCMSLayoutBody` component renders the layout body for a DotCMS page.
|
|
|
108
108
|
#### Usage
|
|
109
109
|
|
|
110
110
|
```javascript
|
|
111
|
-
import { DotCMSLayoutBody } from '@dotcms/react';
|
|
111
|
+
import { DotCMSLayoutBody } from '@dotcms/react/next';
|
|
112
112
|
|
|
113
113
|
const MyPage = ({ page }) => {
|
|
114
114
|
return <DotCMSLayoutBody page={page} components={components} />;
|
|
@@ -129,7 +129,7 @@ The `DotCMSShow` component conditionally renders content based on dotCMS conditi
|
|
|
129
129
|
#### Usage
|
|
130
130
|
|
|
131
131
|
```javascript
|
|
132
|
-
import { DotCMSShow } from '@dotcms/react';
|
|
132
|
+
import { DotCMSShow } from '@dotcms/react/next';
|
|
133
133
|
import { UVE_MODE } from '@dotcms/uve';
|
|
134
134
|
|
|
135
135
|
const MyComponent = () => {
|
|
@@ -171,12 +171,12 @@ A custom hook that provides the same functionality as the `DotCMSShow` component
|
|
|
171
171
|
#### Usage
|
|
172
172
|
|
|
173
173
|
```javascript
|
|
174
|
-
import { useDotCMSShowWhen } from '@dotcms/react';
|
|
174
|
+
import { useDotCMSShowWhen } from '@dotcms/react/next';
|
|
175
175
|
import { UVE_MODE } from '@dotcms/uve';
|
|
176
176
|
|
|
177
177
|
const MyComponent = () => {
|
|
178
178
|
const isVisible = useDotCMSShowWhen(UVE_MODE.EDIT);
|
|
179
|
-
|
|
179
|
+
|
|
180
180
|
return isVisible ? <div>Visible content</div> : null;
|
|
181
181
|
};
|
|
182
182
|
```
|
|
@@ -211,7 +211,7 @@ export const usePageAsset = (currentPageAsset) => {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
// Note: If using plain JavaScript instead of TypeScript, you can use the string literals directly
|
|
214
|
-
sendMessageToEditor({ action: DotCMSUVEAction.CLIENT_READY || "client-ready" });
|
|
214
|
+
sendMessageToEditor({ action: DotCMSUVEAction.CLIENT_READY || "client-ready" });
|
|
215
215
|
const subscription = createUVESubscription(UVEEventType.CONTENT_CHANGES || "changes", (pageAsset) => setPageAsset(pageAsset));
|
|
216
216
|
|
|
217
217
|
return () => {
|
|
@@ -231,7 +231,7 @@ import { usePageAsset } from './hooks/usePageAsset';
|
|
|
231
231
|
|
|
232
232
|
const MyPage = ({ initialPageAsset }) => {
|
|
233
233
|
const pageAsset = usePageAsset(initialPageAsset);
|
|
234
|
-
|
|
234
|
+
|
|
235
235
|
return <DotCMSLayoutBody page={pageAsset} components={components} />;
|
|
236
236
|
};
|
|
237
237
|
```
|
package/next.esm.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { createContext, useContext, useState, useEffect, useLayoutEffect, useRef, useMemo } from 'react';
|
|
3
3
|
import { s as styleInject } from './es.json.stringify.esm.js';
|
|
4
|
-
import { getUVEState } from '@dotcms/uve';
|
|
4
|
+
import { getUVEState, initUVE, createUVESubscription } from '@dotcms/uve';
|
|
5
5
|
import { DEVELOPMENT_MODE, EMPTY_CONTAINER_STYLE_REACT, getDotContentletAttributes, CUSTOM_NO_COMPONENT, getContainersData, getContentletsInContainer, getDotContainerAttributes, getColumnPositionClasses, combineClasses } from '@dotcms/uve/internal';
|
|
6
|
-
import { UVE_MODE } from '@dotcms/uve/types';
|
|
6
|
+
import { UVE_MODE, UVEEventType } from '@dotcms/uve/types';
|
|
7
|
+
import { updateNavigation } from '@dotcms/client';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The `PageContext` is a React context that provides access to the DotCMS page context.
|
|
@@ -521,4 +522,122 @@ const DotCMSShow = ({
|
|
|
521
522
|
return children;
|
|
522
523
|
};
|
|
523
524
|
|
|
524
|
-
|
|
525
|
+
/**
|
|
526
|
+
* Custom hook to manage the editable state of a DotCMS page.
|
|
527
|
+
*
|
|
528
|
+
* This hook initializes the Universal Visual Editor (UVE) and subscribes to content changes.
|
|
529
|
+
* It updates the editable page state when content changes are detected in the UVE,
|
|
530
|
+
* ensuring your React components always display the latest content when editing in DotCMS.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```ts
|
|
534
|
+
* // Import the hook and the client
|
|
535
|
+
* import { useEditableDotCMSPage } from '@dotcms/react';
|
|
536
|
+
* import { createDotCMSClient } from '@dotcms/client';
|
|
537
|
+
*
|
|
538
|
+
* // Create the client
|
|
539
|
+
* const client = createDotCMSClient({
|
|
540
|
+
* dotcmsURL: 'https://your-dotcms-instance.com',
|
|
541
|
+
* authToken: 'your-auth-token'
|
|
542
|
+
* });
|
|
543
|
+
*
|
|
544
|
+
* // Get the page
|
|
545
|
+
* const page = await client.page.get('/', {
|
|
546
|
+
* languageId: '1',
|
|
547
|
+
* });
|
|
548
|
+
*
|
|
549
|
+
* // Use the hook to get an editable version of the page
|
|
550
|
+
* const editablePage = useEditableDotCMSPage(page);
|
|
551
|
+
*
|
|
552
|
+
* // Then use the page data in your component
|
|
553
|
+
* return (
|
|
554
|
+
* <div>
|
|
555
|
+
* <h1>{editablePage.page.title}</h1>
|
|
556
|
+
* <div dangerouslySetInnerHTML={{ __html: editablePage.page.body }} />
|
|
557
|
+
* </div>
|
|
558
|
+
* );
|
|
559
|
+
* ```
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```ts
|
|
563
|
+
* // Import the hook and the client
|
|
564
|
+
* import { useEditableDotCMSPage } from '@dotcms/react';
|
|
565
|
+
* import { createDotCMSClient } from '@dotcms/client';
|
|
566
|
+
*
|
|
567
|
+
* // Create the client
|
|
568
|
+
* const client = createDotCMSClient({
|
|
569
|
+
* dotcmsURL: 'https://your-dotcms-instance.com',
|
|
570
|
+
* authToken: 'your-auth-token'
|
|
571
|
+
* });
|
|
572
|
+
*
|
|
573
|
+
* // Get the page with GraphQL content
|
|
574
|
+
* const page = await client.page.get('/', {
|
|
575
|
+
* languageId: '1',
|
|
576
|
+
* graphql: {
|
|
577
|
+
* content: {
|
|
578
|
+
* products: `ProductCollection(query: "+title:snow", limit: 10, offset: 0, sortBy: "score") {
|
|
579
|
+
* title
|
|
580
|
+
* urlMap
|
|
581
|
+
* category {
|
|
582
|
+
* name
|
|
583
|
+
* inode
|
|
584
|
+
* }
|
|
585
|
+
* retailPrice
|
|
586
|
+
* image {
|
|
587
|
+
* versionPath
|
|
588
|
+
* }
|
|
589
|
+
* }`
|
|
590
|
+
* }
|
|
591
|
+
* }
|
|
592
|
+
* });
|
|
593
|
+
*
|
|
594
|
+
* // Use the hook to get an editable version of the page and its content
|
|
595
|
+
* const editablePage = useEditableDotCMSPage(page);
|
|
596
|
+
*
|
|
597
|
+
* // Access both page data and GraphQL content
|
|
598
|
+
* const { page: pageData, content } = editablePage;
|
|
599
|
+
*
|
|
600
|
+
* // Use the products from GraphQL content
|
|
601
|
+
* return (
|
|
602
|
+
* <div>
|
|
603
|
+
* <h1>{pageData.title}</h1>
|
|
604
|
+
* <ProductList products={content.products} />
|
|
605
|
+
* </div>
|
|
606
|
+
* );
|
|
607
|
+
* ```
|
|
608
|
+
* @param {DotCMSEditablePage} editablePage - The initial editable page data from client.page.get().
|
|
609
|
+
*
|
|
610
|
+
* @returns {DotCMSEditablePage} The updated editable page state that reflects any changes made in the UVE.
|
|
611
|
+
* The structure includes page data and any GraphQL content that was requested.
|
|
612
|
+
*/
|
|
613
|
+
const useEditableDotCMSPage = editablePage => {
|
|
614
|
+
const [updatedEditablePage, setUpdatedEditablePage] = useState(editablePage);
|
|
615
|
+
useEffect(() => {
|
|
616
|
+
var _editablePage$page$pa, _editablePage$page;
|
|
617
|
+
if (!getUVEState()) {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const pageURI = (_editablePage$page$pa = editablePage == null || (_editablePage$page = editablePage.page) == null ? void 0 : _editablePage$page.pageURI) != null ? _editablePage$page$pa : '/';
|
|
621
|
+
const {
|
|
622
|
+
destroyUVESubscriptions
|
|
623
|
+
} = initUVE(editablePage);
|
|
624
|
+
// Update the navigation to the pageURI
|
|
625
|
+
updateNavigation(pageURI);
|
|
626
|
+
return () => {
|
|
627
|
+
destroyUVESubscriptions();
|
|
628
|
+
};
|
|
629
|
+
}, [editablePage]);
|
|
630
|
+
useEffect(() => {
|
|
631
|
+
const {
|
|
632
|
+
unsubscribe
|
|
633
|
+
} = createUVESubscription(UVEEventType.CONTENT_CHANGES, payload => {
|
|
634
|
+
setUpdatedEditablePage(payload);
|
|
635
|
+
});
|
|
636
|
+
return () => {
|
|
637
|
+
unsubscribe();
|
|
638
|
+
};
|
|
639
|
+
}, []);
|
|
640
|
+
return updatedEditablePage;
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
export { DotCMSLayoutBody, DotCMSShow, useDotCMSShowWhen, useEditableDotCMSPage };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcms/react",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.22",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"react": ">=18",
|
|
6
6
|
"react-dom": ">=18",
|
|
7
|
-
"@dotcms/client": "0.0.1-beta.
|
|
8
|
-
"@dotcms/uve": "0.0.1-beta.
|
|
7
|
+
"@dotcms/client": "0.0.1-beta.22",
|
|
8
|
+
"@dotcms/uve": "0.0.1-beta.22",
|
|
9
9
|
"@tinymce/tinymce-react": "^5.1.1"
|
|
10
10
|
},
|
|
11
11
|
"description": "Official React Components library to render a dotCMS page.",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { DotCMSEditablePage } from '@dotcms/uve/types';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook to manage the editable state of a DotCMS page.
|
|
4
|
+
*
|
|
5
|
+
* This hook initializes the Universal Visual Editor (UVE) and subscribes to content changes.
|
|
6
|
+
* It updates the editable page state when content changes are detected in the UVE,
|
|
7
|
+
* ensuring your React components always display the latest content when editing in DotCMS.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Import the hook and the client
|
|
12
|
+
* import { useEditableDotCMSPage } from '@dotcms/react';
|
|
13
|
+
* import { createDotCMSClient } from '@dotcms/client';
|
|
14
|
+
*
|
|
15
|
+
* // Create the client
|
|
16
|
+
* const client = createDotCMSClient({
|
|
17
|
+
* dotcmsURL: 'https://your-dotcms-instance.com',
|
|
18
|
+
* authToken: 'your-auth-token'
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // Get the page
|
|
22
|
+
* const page = await client.page.get('/', {
|
|
23
|
+
* languageId: '1',
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Use the hook to get an editable version of the page
|
|
27
|
+
* const editablePage = useEditableDotCMSPage(page);
|
|
28
|
+
*
|
|
29
|
+
* // Then use the page data in your component
|
|
30
|
+
* return (
|
|
31
|
+
* <div>
|
|
32
|
+
* <h1>{editablePage.page.title}</h1>
|
|
33
|
+
* <div dangerouslySetInnerHTML={{ __html: editablePage.page.body }} />
|
|
34
|
+
* </div>
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Import the hook and the client
|
|
41
|
+
* import { useEditableDotCMSPage } from '@dotcms/react';
|
|
42
|
+
* import { createDotCMSClient } from '@dotcms/client';
|
|
43
|
+
*
|
|
44
|
+
* // Create the client
|
|
45
|
+
* const client = createDotCMSClient({
|
|
46
|
+
* dotcmsURL: 'https://your-dotcms-instance.com',
|
|
47
|
+
* authToken: 'your-auth-token'
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Get the page with GraphQL content
|
|
51
|
+
* const page = await client.page.get('/', {
|
|
52
|
+
* languageId: '1',
|
|
53
|
+
* graphql: {
|
|
54
|
+
* content: {
|
|
55
|
+
* products: `ProductCollection(query: "+title:snow", limit: 10, offset: 0, sortBy: "score") {
|
|
56
|
+
* title
|
|
57
|
+
* urlMap
|
|
58
|
+
* category {
|
|
59
|
+
* name
|
|
60
|
+
* inode
|
|
61
|
+
* }
|
|
62
|
+
* retailPrice
|
|
63
|
+
* image {
|
|
64
|
+
* versionPath
|
|
65
|
+
* }
|
|
66
|
+
* }`
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Use the hook to get an editable version of the page and its content
|
|
72
|
+
* const editablePage = useEditableDotCMSPage(page);
|
|
73
|
+
*
|
|
74
|
+
* // Access both page data and GraphQL content
|
|
75
|
+
* const { page: pageData, content } = editablePage;
|
|
76
|
+
*
|
|
77
|
+
* // Use the products from GraphQL content
|
|
78
|
+
* return (
|
|
79
|
+
* <div>
|
|
80
|
+
* <h1>{pageData.title}</h1>
|
|
81
|
+
* <ProductList products={content.products} />
|
|
82
|
+
* </div>
|
|
83
|
+
* );
|
|
84
|
+
* ```
|
|
85
|
+
* @param {DotCMSEditablePage} editablePage - The initial editable page data from client.page.get().
|
|
86
|
+
*
|
|
87
|
+
* @returns {DotCMSEditablePage} The updated editable page state that reflects any changes made in the UVE.
|
|
88
|
+
* The structure includes page data and any GraphQL content that was requested.
|
|
89
|
+
*/
|
|
90
|
+
export declare const useEditableDotCMSPage: (editablePage: DotCMSEditablePage) => DotCMSEditablePage;
|
package/src/next.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { DotCMSLayoutBody } from './lib/next/components/DotCMSLayoutBody/DotCMSLayoutBody';
|
|
2
2
|
export { DotCMSShow } from './lib/next/components/DotCMSShow/DotCMSShow';
|
|
3
3
|
export { useDotCMSShowWhen } from './lib/next/hooks/useDotCMSShowWhen';
|
|
4
|
+
export { useEditableDotCMSPage } from './lib/next/hooks/useEditableDotCMSPage';
|