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
@@ -75,18 +75,14 @@ const toolbarList2 = [
const UndoRedo = () => {
const [canUndo, setCanUndo] = useState(false);
const [canRedo, setCanRedo] = useState(false);
const { currentPage } = useEditor();
useEffect(() => {
if (!currentPage) return;
const { onHistoryUpdated, page } = useEditor();
currentPage.signals.historyUpdated.on(() => {
setCanUndo(currentPage.canUndo);
setCanRedo(currentPage.canRedo);
useEffect(() => {
onHistoryUpdated(page => {
setCanUndo(page.canUndo);
setCanRedo(page.canRedo);
});
return () => {
currentPage.signals.historyUpdated.dispose();
};
}, [currentPage]);
}, [onHistoryUpdated]);
return (
<StyledToolbarWrapper>
@@ -94,7 +90,7 @@ const UndoRedo = () => {
<StyledToolbarItem
disable={!canUndo}
onClick={() => {
currentPage?.undo();
page?.undo();
}}
>
<UndoIcon />
@@ -104,7 +100,7 @@ const UndoRedo = () => {
<StyledToolbarItem
disable={!canRedo}
onClick={() => {
currentPage?.redo();
page?.redo();
}}
>
<RedoIcon />
+1 -1
View File
@@ -3,7 +3,7 @@ import { useEditor, initDefaultContent } from '@/providers/editor-provider';
export const Editor = () => {
const editorContainer = useRef<HTMLDivElement>(null);
const { editor } = useEditor();
const { editor, onHistoryUpdated } = useEditor();
const ref = useRef<any>();
useEffect(() => {
if (editor && ref.current?.page.id !== editor?.page.id) {
@@ -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 (
@@ -16,7 +16,9 @@ export const DateCell = ({
// dayjs().format('L LT');
return (
<TableCell ellipsis={true}>
{dayjs(pageMeta[dateKey] as string).format('YYYY-MM-DD HH:MM')}
{pageMeta[dateKey] === undefined
? '--'
: dayjs(pageMeta[dateKey] as string).format('YYYY-MM-DD HH:MM')}
</TableCell>
);
};
@@ -74,7 +74,7 @@ export const PageList = ({
<DateCell pageMeta={pageMeta} dateKey="createDate" />
<DateCell
pageMeta={pageMeta}
dateKey={isTrash ? 'trashDate' : 'createDate'}
dateKey={isTrash ? 'trashDate' : 'updatedDate'}
/>
<TableCell style={{ padding: 0 }}>
{isTrash ? (