feat: add lifecycle hook in editor provider

This commit is contained in:
QiShaoXuan
2022-12-14 16:47:33 +08:00
parent 7734fe8928
commit 9253e9af0b
13 changed files with 155 additions and 61 deletions
@@ -142,12 +142,8 @@ const HeaderRight = () => {
};
export const Header = ({ children }: PropsWithChildren<{}>) => {
const { pageList } = useEditor();
const [showWarning, setShowWarning] = useState(shouldShowWarning());
const router = useRouter();
const currentPageMeta = pageList.find(p => p.id === router.query.pageId);
const isTrash = !!currentPageMeta?.trash;
return (
<StyledHeaderContainer hasWarning={showWarning}>
<BrowserWarning
@@ -18,18 +18,17 @@ export const PageHeader = () => {
const [title, setTitle] = useState('');
const [isHover, setIsHover] = useState(false);
const { editor } = useEditor();
const { editor, onPropsUpdated } = useEditor();
const { triggerQuickSearchModal } = useModal();
useEffect(() => {
if (editor?.model) {
setTitle(editor.model.title || 'Untitled');
editor.model.propsUpdated.on(() => {
setTitle(editor.model.title);
});
}
return () => {
editor?.model?.propsUpdated.dispose();
};
onPropsUpdated(editor => {
setTitle(editor.model.title);
});
}, [onPropsUpdated]);
useEffect(() => {
setTitle(editor?.model.title || 'Untitled');
}, [editor]);
return (