import React from "react"; import { useEffect } from "react"; import { useRef } from "react"; import { useFeature } from "@webiny/app"; import { createReactiveComponent } from "@webiny/app-admin"; import { ContentEntryForm } from "@webiny/app-headless-cms/presentation/contentEntries/views/layout/index.js"; import { useContentEntryFormPresenter } from "@webiny/app-headless-cms/presentation/contentEntries/form/useContentEntryFormPresenter.js"; import { useCmsEntryReview } from "~/features/editorReview/useCmsEntryReview.js"; import { CmsEntryFormFieldLockFeature } from "~/features/formFieldLock/feature.js"; import { isReviewableModel } from "~/utils/isReviewableModel.js"; interface FormLockProps { isLocked: boolean; children: React.ReactNode; } /* * One stable wrapper, always rendered, so toggling the lock never remounts the form. A disabled * fieldset stops native inputs; `inert` stops everything else (rich text editors, custom field * renderers). Trade-off: `inert` also blocks text selection and copy inside the form while it is * locked; nothing else can stop contenteditable editors from being edited, and the server stays * authoritative (`TargetLocked`). */ const FormLock = ({ isLocked, children }: FormLockProps) => { const wrapper = useRef(null); useEffect(() => { if (wrapper.current) { wrapper.current.inert = isLocked; } }, [isLocked]); return (
{children}
); }; export const CmsEntryReviewFormLock = ContentEntryForm.createDecorator(Original => { return createReactiveComponent(function CmsEntryReviewFormLockDecorator() { const formPresenter = useContentEntryFormPresenter(); const session = useCmsEntryReview(); const { entry, model } = formPresenter.vm; const isReviewed = entry !== null && isReviewableModel(model); const isLocked = isReviewed && !session.presenter.vm.allowsEditing; const { fieldLock } = useFeature(CmsEntryFormFieldLockFeature); useEffect(() => { fieldLock.setLocked(isLocked); }, [fieldLock, isLocked]); // Leaving the editor never leaves the next form locked. useEffect(() => { return () => { fieldLock.setLocked(false); }; }, [fieldLock]); return ( ); }); });