From d51554344487772971a5c1492bf1277ca42533f9 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sun, 3 Apr 2022 11:13:21 +0800 Subject: [PATCH] fix: sync title in template editor --- .../client/src/components/template/editor/editor.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/client/src/components/template/editor/editor.tsx b/packages/client/src/components/template/editor/editor.tsx index bc7b9ae..84486ca 100644 --- a/packages/client/src/components/template/editor/editor.tsx +++ b/packages/client/src/components/template/editor/editor.tsx @@ -24,7 +24,6 @@ import { User } from 'components/user'; import { DocumentStyle } from 'components/document/style'; import { useDocumentStyle } from 'hooks/use-document-style'; import { useWindowSize } from 'hooks/use-window-size'; -import { safeJSONParse } from 'helpers/json'; import styles from './index.module.scss'; const { Text } = Typography; @@ -41,6 +40,7 @@ interface IProps { export const Editor: React.FC = ({ user, data, loading, error, updateTemplate, deleteTemplate }) => { if (!user) return null; const { width: windowWidth } = useWindowSize(); + const [title, setTitle] = useState(data.title); const provider = useMemo(() => { return getProvider({ targetId: data.id, @@ -53,9 +53,13 @@ export const Editor: React.FC = ({ user, data, loading, error, updateTem const editor = useEditor({ editable: true, extensions: [...BaseKit, DocumentWithTitle, getCollaborationExtension(provider)], - content: safeJSONParse(data && data.content), + onTransaction: ({ transaction }) => { + try { + const title = transaction.doc.content.firstChild.content.firstChild.textContent; + setTitle(title); + } catch (e) {} + }, }); - const [isPublic, setPublic] = useState(false); const { width, fontSize } = useDocumentStyle(); const editorWrapClassNames = useMemo(() => { @@ -100,7 +104,7 @@ export const Editor: React.FC = ({ user, data, loading, error, updateTem