2024-05-11 12:46:49 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Control, Controller } from "react-hook-form";
|
|
|
|
// document editor
|
|
|
|
import {
|
|
|
|
DocumentEditorWithRef,
|
|
|
|
DocumentReadOnlyEditorWithRef,
|
|
|
|
EditorReadOnlyRefApi,
|
|
|
|
EditorRefApi,
|
|
|
|
IMarking,
|
|
|
|
} from "@plane/document-editor";
|
|
|
|
// types
|
|
|
|
import { IUserLite, TPage } from "@plane/types";
|
|
|
|
// components
|
2024-05-03 17:42:44 +00:00
|
|
|
import { PageContentBrowser, PageContentLoader, PageEditorTitle } from "@/components/pages";
|
2024-04-11 15:58:59 +00:00
|
|
|
// helpers
|
|
|
|
import { cn } from "@/helpers/common.helper";
|
|
|
|
// hooks
|
|
|
|
import { useMember, useMention, useUser, useWorkspace } from "@/hooks/store";
|
2024-05-01 12:40:39 +00:00
|
|
|
import { usePageFilters } from "@/hooks/use-page-filters";
|
2024-04-11 15:58:59 +00:00
|
|
|
import useReloadConfirmations from "@/hooks/use-reload-confirmation";
|
|
|
|
// services
|
|
|
|
import { FileService } from "@/services/file.service";
|
2024-05-11 12:46:49 +00:00
|
|
|
import { PageService } from "@/services/page.service";
|
|
|
|
const pageService = new PageService();
|
2024-04-11 15:58:59 +00:00
|
|
|
// store
|
|
|
|
import { IPageStore } from "@/store/pages/page.store";
|
|
|
|
|
|
|
|
const fileService = new FileService();
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
control: Control<TPage, any>;
|
|
|
|
editorRef: React.RefObject<EditorRefApi>;
|
|
|
|
readOnlyEditorRef: React.RefObject<EditorReadOnlyRefApi>;
|
|
|
|
handleSubmit: () => void;
|
|
|
|
markings: IMarking[];
|
|
|
|
pageStore: IPageStore;
|
|
|
|
sidePeekVisible: boolean;
|
|
|
|
handleEditorReady: (value: boolean) => void;
|
|
|
|
handleReadOnlyEditorReady: (value: boolean) => void;
|
|
|
|
updateMarkings: (description_html: string) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PageEditorBody: React.FC<Props> = observer((props) => {
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
handleReadOnlyEditorReady,
|
|
|
|
handleEditorReady,
|
|
|
|
editorRef,
|
|
|
|
markings,
|
|
|
|
readOnlyEditorRef,
|
2024-05-11 12:46:49 +00:00
|
|
|
// handleSubmit,
|
2024-04-11 15:58:59 +00:00
|
|
|
pageStore,
|
|
|
|
sidePeekVisible,
|
|
|
|
updateMarkings,
|
|
|
|
} = props;
|
2024-05-11 12:46:49 +00:00
|
|
|
// states
|
|
|
|
const [descriptionYJS, setDescriptionYJS] = useState<Uint8Array | null>(null);
|
2024-04-11 15:58:59 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
// store hooks
|
2024-05-08 17:31:20 +00:00
|
|
|
const { data: currentUser } = useUser();
|
2024-04-11 15:58:59 +00:00
|
|
|
const { getWorkspaceBySlug } = useWorkspace();
|
|
|
|
const {
|
|
|
|
getUserDetails,
|
|
|
|
project: { getProjectMemberIds },
|
|
|
|
} = useMember();
|
|
|
|
// derived values
|
|
|
|
const workspaceId = workspaceSlug ? getWorkspaceBySlug(workspaceSlug.toString())?.id ?? "" : "";
|
2024-05-11 12:46:49 +00:00
|
|
|
const pageId = pageStore?.id ?? "";
|
2024-04-11 15:58:59 +00:00
|
|
|
const pageTitle = pageStore?.name ?? "";
|
2024-05-03 17:42:44 +00:00
|
|
|
const pageDescription = pageStore?.description_html;
|
2024-04-11 15:58:59 +00:00
|
|
|
const { description_html, isContentEditable, updateTitle, isSubmitting, setIsSubmitting } = pageStore;
|
|
|
|
const projectMemberIds = projectId ? getProjectMemberIds(projectId.toString()) : [];
|
|
|
|
const projectMemberDetails = projectMemberIds?.map((id) => getUserDetails(id) as IUserLite);
|
|
|
|
// use-mention
|
|
|
|
const { mentionHighlights, mentionSuggestions } = useMention({
|
|
|
|
workspaceSlug: workspaceSlug?.toString() ?? "",
|
|
|
|
projectId: projectId?.toString() ?? "",
|
|
|
|
members: projectMemberDetails,
|
|
|
|
user: currentUser ?? undefined,
|
|
|
|
});
|
2024-05-01 12:40:39 +00:00
|
|
|
// page filters
|
|
|
|
const { isFullWidth } = usePageFilters();
|
2024-04-11 15:58:59 +00:00
|
|
|
|
2024-05-11 12:46:49 +00:00
|
|
|
useReloadConfirmations(isSubmitting === "submitting");
|
|
|
|
|
|
|
|
// const { data: pageDescriptionYJS } = useSWR(
|
|
|
|
// workspaceSlug && projectId && pageId ? `PAGE_DESCRIPTION_${workspaceSlug}_${projectId}_${pageId}` : null,
|
|
|
|
// workspaceSlug && projectId && pageId
|
|
|
|
// ? () => pageService.fetchDescriptionYJS(workspaceSlug.toString(), projectId.toString(), pageId.toString())
|
|
|
|
// : null
|
|
|
|
// );
|
|
|
|
|
|
|
|
const handleDescriptionChange = useCallback(
|
|
|
|
(binaryString: string, descriptionHTML: string) => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
|
|
|
pageService.updateDescriptionYJS(workspaceSlug.toString(), projectId.toString(), pageId.toString(), {
|
|
|
|
description_yjs: binaryString,
|
|
|
|
description_html: descriptionHTML,
|
|
|
|
});
|
|
|
|
// setIsSubmitting("submitting");
|
|
|
|
// setShowAlert(true);
|
|
|
|
// onChange(description_html);
|
|
|
|
// handleSubmit();
|
|
|
|
},
|
|
|
|
[pageId, projectId, workspaceSlug]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchDescription = async () => {
|
|
|
|
if (!workspaceSlug || !projectId || !pageId) return;
|
|
|
|
console.log("fetching...");
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
`http://localhost:8000/api/workspaces/${workspaceSlug}/projects/${projectId}/pages/${pageId}/description/`,
|
|
|
|
{
|
|
|
|
credentials: "include",
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const data = await response.arrayBuffer();
|
|
|
|
setDescriptionYJS(new Uint8Array(data));
|
|
|
|
// __AUTO_GENERATED_PRINT_VAR_START__
|
|
|
|
console.log("fetchById data: %s", data); // __AUTO_GENERATED_PRINT_VAR_END__
|
|
|
|
// if (data.byteLength === 0) {
|
|
|
|
// const yjs = await fetchByIdIfExists(workspaceSlug, projectId, pageId);
|
|
|
|
// if (yjs) {
|
|
|
|
// console.log("not found in db:", yjs, yjs instanceof Uint8Array);
|
|
|
|
// return yjs;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
|
2024-05-11 12:50:07 +00:00
|
|
|
// Fetch the description immediately
|
|
|
|
fetchDescription();
|
2024-05-11 12:46:49 +00:00
|
|
|
|
2024-05-11 12:50:07 +00:00
|
|
|
// Then fetch the description every 10 seconds
|
|
|
|
const intervalId = setInterval(fetchDescription, 10000);
|
|
|
|
|
|
|
|
// Clear the interval when the component is unmounted
|
|
|
|
return () => clearInterval(intervalId);
|
2024-05-11 12:46:49 +00:00
|
|
|
}, [pageId, projectId, workspaceSlug]);
|
2024-04-11 15:58:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
updateMarkings(description_html ?? "<p></p>");
|
|
|
|
}, [description_html, updateMarkings]);
|
|
|
|
|
2024-05-11 12:46:49 +00:00
|
|
|
if (pageDescription === undefined || pageId === undefined || !descriptionYJS) return <PageContentLoader />;
|
2024-05-03 17:42:44 +00:00
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
return (
|
|
|
|
<div className="flex items-center h-full w-full overflow-y-auto">
|
|
|
|
<div
|
|
|
|
className={cn("sticky top-0 hidden h-full flex-shrink-0 -translate-x-full p-5 duration-200 md:block", {
|
|
|
|
"translate-x-0": sidePeekVisible,
|
2024-05-01 12:31:30 +00:00
|
|
|
"w-40 lg:w-56": !isFullWidth,
|
|
|
|
"w-[5%]": isFullWidth,
|
2024-04-11 15:58:59 +00:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{!isFullWidth && (
|
|
|
|
<PageContentBrowser
|
|
|
|
editorRef={(isContentEditable ? editorRef : readOnlyEditorRef)?.current}
|
|
|
|
markings={markings}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={cn("h-full w-full pt-5", {
|
2024-05-01 12:31:30 +00:00
|
|
|
"md:w-[calc(100%-10rem)] xl:w-[calc(100%-14rem-14rem)]": !isFullWidth,
|
|
|
|
"md:w-[90%]": isFullWidth,
|
2024-04-11 15:58:59 +00:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div className="h-full w-full flex flex-col gap-y-7 overflow-y-auto overflow-x-hidden">
|
2024-04-28 19:22:43 +00:00
|
|
|
<div className="relative w-full flex-shrink-0 md:pl-5 px-4">
|
2024-04-11 15:58:59 +00:00
|
|
|
<PageEditorTitle
|
|
|
|
editorRef={editorRef}
|
|
|
|
title={pageTitle}
|
|
|
|
updateTitle={updateTitle}
|
|
|
|
readOnly={!isContentEditable}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isContentEditable ? (
|
|
|
|
<Controller
|
|
|
|
name="description_html"
|
|
|
|
control={control}
|
2024-05-11 12:46:49 +00:00
|
|
|
render={() => (
|
2024-04-11 15:58:59 +00:00
|
|
|
<DocumentEditorWithRef
|
2024-05-11 12:46:49 +00:00
|
|
|
id={pageId}
|
2024-04-11 15:58:59 +00:00
|
|
|
fileHandler={{
|
|
|
|
cancel: fileService.cancelUpload,
|
|
|
|
delete: fileService.getDeleteImageFunction(workspaceId),
|
|
|
|
restore: fileService.getRestoreImageFunction(workspaceId),
|
|
|
|
upload: fileService.getUploadFileFunction(workspaceSlug as string, setIsSubmitting),
|
|
|
|
}}
|
|
|
|
handleEditorReady={handleEditorReady}
|
2024-05-11 12:46:49 +00:00
|
|
|
value={descriptionYJS}
|
2024-04-11 15:58:59 +00:00
|
|
|
ref={editorRef}
|
|
|
|
containerClassName="p-0 pb-64"
|
2024-04-28 19:22:43 +00:00
|
|
|
editorClassName="lg:px-10 pl-8"
|
2024-05-11 12:46:49 +00:00
|
|
|
onChange={handleDescriptionChange}
|
2024-04-11 15:58:59 +00:00
|
|
|
mentionHandler={{
|
|
|
|
highlights: mentionHighlights,
|
|
|
|
suggestions: mentionSuggestions,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<DocumentReadOnlyEditorWithRef
|
|
|
|
ref={readOnlyEditorRef}
|
2024-05-03 17:42:44 +00:00
|
|
|
initialValue={pageDescription ?? "<p></p>"}
|
2024-04-11 15:58:59 +00:00
|
|
|
handleEditorReady={handleReadOnlyEditorReady}
|
|
|
|
containerClassName="p-0 pb-64 border-none"
|
2024-04-28 19:22:43 +00:00
|
|
|
editorClassName="lg:px-10 pl-8"
|
2024-04-11 15:58:59 +00:00
|
|
|
mentionHandler={{
|
|
|
|
highlights: mentionHighlights,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
2024-04-28 19:22:43 +00:00
|
|
|
className={cn("hidden xl:block flex-shrink-0", {
|
2024-05-01 12:31:30 +00:00
|
|
|
"w-40 lg:w-56": !isFullWidth,
|
|
|
|
"w-[5%]": isFullWidth,
|
2024-04-11 15:58:59 +00:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|