♻️(frontend) move editor store to useEditorStore
Previous changes migrated the editor store to doc-management, we move it back doc-editor and simplify it.
This commit is contained in:
@@ -11,11 +11,11 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { Box, TextErrors } from '@/components';
|
import { Box, TextErrors } from '@/components';
|
||||||
import { mediaUrl } from '@/core';
|
import { mediaUrl } from '@/core';
|
||||||
import { useAuthStore } from '@/core/auth';
|
import { useAuthStore } from '@/core/auth';
|
||||||
import { Doc, useDocStore } from '@/features/docs/doc-management';
|
import { Doc } from '@/features/docs/doc-management';
|
||||||
|
|
||||||
import { useCreateDocAttachment } from '../api/useCreateDocUpload';
|
import { useCreateDocAttachment } from '../api/useCreateDocUpload';
|
||||||
import useSaveDoc from '../hook/useSaveDoc';
|
import useSaveDoc from '../hook/useSaveDoc';
|
||||||
import { useHeadingStore } from '../stores';
|
import { useEditorStore, useHeadingStore } from '../stores';
|
||||||
import { randomColor } from '../utils';
|
import { randomColor } from '../utils';
|
||||||
|
|
||||||
import { BlockNoteToolbar } from './BlockNoteToolbar';
|
import { BlockNoteToolbar } from './BlockNoteToolbar';
|
||||||
@@ -86,11 +86,10 @@ export const BlockNoteEditor = ({
|
|||||||
}: BlockNoteEditorProps) => {
|
}: BlockNoteEditorProps) => {
|
||||||
const isVersion = doc.id !== storeId;
|
const isVersion = doc.id !== storeId;
|
||||||
const { userData } = useAuthStore();
|
const { userData } = useAuthStore();
|
||||||
const { setStore, docsStore } = useDocStore();
|
const { setEditor } = useEditorStore();
|
||||||
|
|
||||||
const readOnly = !doc.abilities.partial_update || isVersion;
|
const readOnly = !doc.abilities.partial_update || isVersion;
|
||||||
useSaveDoc(doc.id, provider.document, !readOnly);
|
useSaveDoc(doc.id, provider.document, !readOnly);
|
||||||
const storedEditor = docsStore?.[storeId]?.editor;
|
|
||||||
const {
|
const {
|
||||||
mutateAsync: createDocAttachment,
|
mutateAsync: createDocAttachment,
|
||||||
isError: isErrorAttachment,
|
isError: isErrorAttachment,
|
||||||
@@ -132,8 +131,12 @@ export const BlockNoteEditor = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setStore(storeId, { editor });
|
setEditor(editor);
|
||||||
}, [setStore, storeId, editor]);
|
|
||||||
|
return () => {
|
||||||
|
setEditor(undefined);
|
||||||
|
};
|
||||||
|
}, [setEditor, editor]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setHeadings(editor);
|
setHeadings(editor);
|
||||||
@@ -160,7 +163,7 @@ export const BlockNoteEditor = ({
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<BlockNoteView
|
<BlockNoteView
|
||||||
editor={storedEditor ?? editor}
|
editor={editor}
|
||||||
formattingToolbar={false}
|
formattingToolbar={false}
|
||||||
editable={!readOnly}
|
editable={!readOnly}
|
||||||
theme="light"
|
theme="light"
|
||||||
|
|||||||
@@ -127,9 +127,7 @@ export const PanelEditor = ({
|
|||||||
</BoxButton>
|
</BoxButton>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
{isPanelTableContentOpen && (
|
{isPanelTableContentOpen && <TableContent headings={headings} />}
|
||||||
<TableContent doc={doc} headings={headings} />
|
|
||||||
)}
|
|
||||||
{!isPanelTableContentOpen && doc.abilities.versions_list && (
|
{!isPanelTableContentOpen && doc.abilities.versions_list && (
|
||||||
<VersionList doc={doc} />
|
<VersionList doc={doc} />
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
|
export * from './useEditorStore';
|
||||||
export * from './useHeadingStore';
|
export * from './useHeadingStore';
|
||||||
export * from './usePanelEditorStore';
|
export * from './usePanelEditorStore';
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { BlockNoteEditor } from '@blocknote/core';
|
||||||
|
import { create } from 'zustand';
|
||||||
|
|
||||||
|
export interface UseEditorstore {
|
||||||
|
editor?: BlockNoteEditor;
|
||||||
|
setEditor: (editor: BlockNoteEditor | undefined) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useEditorStore = create<UseEditorstore>((set) => ({
|
||||||
|
editor: undefined,
|
||||||
|
setEditor: (editor) => {
|
||||||
|
set({ editor });
|
||||||
|
},
|
||||||
|
}));
|
||||||
@@ -8,17 +8,18 @@ import { useTranslation } from 'react-i18next';
|
|||||||
|
|
||||||
import { Box, DropButton, IconOptions } from '@/components';
|
import { Box, DropButton, IconOptions } from '@/components';
|
||||||
import { useAuthStore } from '@/core';
|
import { useAuthStore } from '@/core';
|
||||||
import { usePanelEditorStore } from '@/features/docs/doc-editor/';
|
import {
|
||||||
|
useEditorStore,
|
||||||
|
usePanelEditorStore,
|
||||||
|
} from '@/features/docs/doc-editor/';
|
||||||
import {
|
import {
|
||||||
Doc,
|
Doc,
|
||||||
ModalRemoveDoc,
|
ModalRemoveDoc,
|
||||||
ModalShare,
|
ModalShare,
|
||||||
useDocStore,
|
|
||||||
} from '@/features/docs/doc-management';
|
} from '@/features/docs/doc-management';
|
||||||
|
import { ModalVersion, Versions } from '@/features/docs/doc-versioning';
|
||||||
import { useResponsiveStore } from '@/stores';
|
import { useResponsiveStore } from '@/stores';
|
||||||
|
|
||||||
import { ModalVersion, Versions } from '../../doc-versioning';
|
|
||||||
|
|
||||||
import { ModalPDF } from './ModalExport';
|
import { ModalPDF } from './ModalExport';
|
||||||
|
|
||||||
interface DocToolBoxProps {
|
interface DocToolBoxProps {
|
||||||
@@ -36,13 +37,12 @@ export const DocToolBox = ({ doc, versionId }: DocToolBoxProps) => {
|
|||||||
const [isModalVersionOpen, setIsModalVersionOpen] = useState(false);
|
const [isModalVersionOpen, setIsModalVersionOpen] = useState(false);
|
||||||
const { isSmallMobile } = useResponsiveStore();
|
const { isSmallMobile } = useResponsiveStore();
|
||||||
const { authenticated } = useAuthStore();
|
const { authenticated } = useAuthStore();
|
||||||
const { docsStore } = useDocStore();
|
const { editor } = useEditorStore();
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
|
|
||||||
const copyCurrentEditorToClipboard = async (
|
const copyCurrentEditorToClipboard = async (
|
||||||
asFormat: 'html' | 'markdown',
|
asFormat: 'html' | 'markdown',
|
||||||
) => {
|
) => {
|
||||||
const editor = docsStore[doc.id]?.editor;
|
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
toast(t('Editor unavailable'), VariantType.ERROR, { duration: 3000 });
|
toast(t('Editor unavailable'), VariantType.ERROR, { duration: 3000 });
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import { t } from 'i18next';
|
|||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { Box, Text } from '@/components';
|
import { Box, Text } from '@/components';
|
||||||
import { Doc, useDocStore } from '@/features/docs/doc-management';
|
import { useEditorStore } from '@/features/docs/doc-editor';
|
||||||
|
import { Doc } from '@/features/docs/doc-management';
|
||||||
|
|
||||||
import { useExport } from '../api/useExport';
|
import { useExport } from '../api/useExport';
|
||||||
import { TemplatesOrdering, useTemplates } from '../api/useTemplates';
|
import { TemplatesOrdering, useTemplates } from '../api/useTemplates';
|
||||||
@@ -30,7 +31,7 @@ export const ModalPDF = ({ onClose, doc }: ModalPDFProps) => {
|
|||||||
ordering: TemplatesOrdering.BY_CREATED_ON_DESC,
|
ordering: TemplatesOrdering.BY_CREATED_ON_DESC,
|
||||||
});
|
});
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
const { docsStore } = useDocStore();
|
const { editor } = useEditorStore();
|
||||||
const {
|
const {
|
||||||
mutate: createExport,
|
mutate: createExport,
|
||||||
data: documentGenerated,
|
data: documentGenerated,
|
||||||
@@ -103,8 +104,6 @@ export const ModalPDF = ({ onClose, doc }: ModalPDFProps) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const editor = docsStore[doc.id].editor;
|
|
||||||
|
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
toast(t('No editor found'), VariantType.ERROR);
|
toast(t('No editor found'), VariantType.ERROR);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { BlockNoteEditor } from '@blocknote/core';
|
|
||||||
import { HocuspocusProvider } from '@hocuspocus/provider';
|
import { HocuspocusProvider } from '@hocuspocus/provider';
|
||||||
import * as Y from 'yjs';
|
import * as Y from 'yjs';
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
@@ -8,7 +7,6 @@ import { Base64, Doc, blocksToYDoc } from '@/features/docs/doc-management';
|
|||||||
|
|
||||||
interface DocStore {
|
interface DocStore {
|
||||||
provider: HocuspocusProvider;
|
provider: HocuspocusProvider;
|
||||||
editor?: BlockNoteEditor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UseDocStore {
|
export interface UseDocStore {
|
||||||
|
|||||||
@@ -2,22 +2,19 @@ import React, { useEffect, useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { Box, BoxButton, Text } from '@/components';
|
import { Box, BoxButton, Text } from '@/components';
|
||||||
import { HeadingBlock } from '@/features/docs/doc-editor';
|
import { HeadingBlock, useEditorStore } from '@/features/docs/doc-editor';
|
||||||
import { Doc, useDocStore } from '@/features/docs/doc-management';
|
|
||||||
import { useResponsiveStore } from '@/stores';
|
import { useResponsiveStore } from '@/stores';
|
||||||
|
|
||||||
import { Heading } from './Heading';
|
import { Heading } from './Heading';
|
||||||
|
|
||||||
interface TableContentProps {
|
interface TableContentProps {
|
||||||
doc: Doc;
|
|
||||||
headings: HeadingBlock[];
|
headings: HeadingBlock[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TableContent = ({ doc, headings }: TableContentProps) => {
|
export const TableContent = ({ headings }: TableContentProps) => {
|
||||||
const { docsStore } = useDocStore();
|
const { editor } = useEditorStore();
|
||||||
const { isMobile } = useResponsiveStore();
|
const { isMobile } = useResponsiveStore();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const editor = docsStore?.[doc.id]?.editor;
|
|
||||||
const [headingIdHighlight, setHeadingIdHighlight] = useState<string>();
|
const [headingIdHighlight, setHeadingIdHighlight] = useState<string>();
|
||||||
|
|
||||||
// To highlight the first heading in the viewport
|
// To highlight the first heading in the viewport
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export const ModalVersion = ({
|
|||||||
}: ModalVersionProps) => {
|
}: ModalVersionProps) => {
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { docsStore, setStore } = useDocStore();
|
const { docsStore } = useDocStore();
|
||||||
const { mutate: updateDoc } = useUpdateDoc({
|
const { mutate: updateDoc } = useUpdateDoc({
|
||||||
listInvalideQueries: [KEY_LIST_DOC_VERSIONS],
|
listInvalideQueries: [KEY_LIST_DOC_VERSIONS],
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
@@ -46,10 +46,6 @@ export const ModalVersion = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setStore(docId, {
|
|
||||||
editor: undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
revertUpdate(
|
revertUpdate(
|
||||||
docsStore[docId].provider.document,
|
docsStore[docId].provider.document,
|
||||||
docsStore[docId].provider.document,
|
docsStore[docId].provider.document,
|
||||||
|
|||||||
Reference in New Issue
Block a user