|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import type { Data } from "@puckeditor/core"; |
4 | | -import { Puck } from "@puckeditor/core"; |
| 4 | +import { Puck, blocksPlugin, outlinePlugin } from "@puckeditor/core"; |
5 | 5 | import config from "../../../../puck.config"; |
6 | | -import { useState, createContext, useContext, useCallback } from "react"; |
| 6 | +import { useState, useCallback } from "react"; |
7 | 7 | import { VersionPlugin } from "./VersionPlugin"; |
8 | | -import { blocksPlugin, outlinePlugin } from "@puckeditor/core"; |
9 | 8 | import { ActionBarOverride } from "./ActionBarOverride"; |
10 | 9 | import { SaveButton } from "./SaveButton"; |
11 | 10 | import { MediaProvider, type MediaWithUrl } from "@/components/puck/media-context"; |
12 | 11 | import type { Version } from "../../../../lib/types"; |
13 | | -import { useUnsavedChangesGuard, type ConfirmDiscardChangesOptions } from "./useUnsavedChangesGuard"; |
14 | | - |
15 | | -type DocumentContextType = { |
16 | | - documentId: number; |
17 | | - documentName: string; |
18 | | - versionId?: number; |
19 | | - publishedVersionId?: number; |
20 | | - versions: Version[]; |
21 | | - isArchived: boolean; |
22 | | - isDirty: boolean; |
23 | | - addVersion: (version: Version) => void; |
24 | | - confirmDiscardChanges: (options?: ConfirmDiscardChangesOptions) => Promise<boolean>; |
25 | | - clearUnsavedChangesGuard: () => Promise<void>; |
26 | | - setPublishedVersionId: (id: number) => void; |
27 | | -}; |
28 | | - |
29 | | -const DocumentContext = createContext<DocumentContextType>({ |
30 | | - documentId: 0, |
31 | | - documentName: "", |
32 | | - versions: [], |
33 | | - isArchived: false, |
34 | | - isDirty: false, |
35 | | - addVersion: () => {}, |
36 | | - confirmDiscardChanges: async () => true, |
37 | | - clearUnsavedChangesGuard: async () => {}, |
38 | | - setPublishedVersionId: () => {}, |
39 | | -}); |
40 | | - |
41 | | -export function useDocumentContext() { |
42 | | - return useContext(DocumentContext); |
43 | | -} |
| 12 | +import { useUnsavedChangesGuard } from "./useUnsavedChangesGuard"; |
| 13 | +import { DocumentContext } from "./document-context"; |
| 14 | +import { UnsavedChangesContext } from "./unsaved-changes-context"; |
44 | 15 |
|
45 | 16 | export function Client({ |
46 | 17 | documentId, |
@@ -75,25 +46,27 @@ export function Client({ |
75 | 46 |
|
76 | 47 | return ( |
77 | 48 | <MediaProvider media={media}> |
78 | | - <DocumentContext.Provider |
79 | | - value={{ documentId, documentName, versionId, publishedVersionId, versions, isArchived, isDirty, addVersion, confirmDiscardChanges, clearUnsavedChangesGuard, setPublishedVersionId }} |
80 | | - > |
81 | | - <Puck |
82 | | - config={config} |
83 | | - data={data} |
84 | | - onChange={() => setIsDirty(true)} |
85 | | - ui={{plugin: {current: "version-plugin"}}} |
86 | | - plugins={[VersionPlugin, blocksPlugin(), outlinePlugin()]} |
87 | | - permissions={isArchived |
88 | | - ? { drag: false, duplicate: false, delete: false, edit: false, insert: false } |
89 | | - : { duplicate: false } // We replace this with our own, to avoid an icon collision |
90 | | - } |
91 | | - overrides={{ |
92 | | - actionBar: ActionBarOverride, |
93 | | - headerActions: SaveButton |
94 | | - }} |
95 | | - /> |
96 | | - </DocumentContext.Provider> |
| 49 | + <DocumentContext.Provider |
| 50 | + value={{ documentId, documentName, versionId, publishedVersionId, versions, isArchived, isDirty, addVersion, setPublishedVersionId }} |
| 51 | + > |
| 52 | + <UnsavedChangesContext.Provider value={{ confirmDiscardChanges, clearUnsavedChangesGuard }}> |
| 53 | + <Puck |
| 54 | + config={config} |
| 55 | + data={data} |
| 56 | + onChange={() => setIsDirty(true)} |
| 57 | + ui={{ plugin: { current: "version-plugin" } }} |
| 58 | + plugins={[VersionPlugin, blocksPlugin(), outlinePlugin()]} |
| 59 | + permissions={isArchived |
| 60 | + ? { drag: false, duplicate: false, delete: false, edit: false, insert: false } |
| 61 | + : { duplicate: false } // We replace this with our own, to avoid an icon collision |
| 62 | + } |
| 63 | + overrides={{ |
| 64 | + actionBar: ActionBarOverride, |
| 65 | + headerActions: SaveButton |
| 66 | + }} |
| 67 | + /> |
| 68 | + </UnsavedChangesContext.Provider> |
| 69 | + </DocumentContext.Provider> |
97 | 70 | </MediaProvider> |
98 | 71 | ); |
99 | 72 | } |
0 commit comments