fix(core): upgrade page when previewing/reverting page snapshot (#5435)

This commit is contained in:
Peng Xiao
2023-12-29 07:26:58 +00:00
parent 3082d63948
commit f65c5dbfa7
2 changed files with 19 additions and 22 deletions

View File

@@ -146,12 +146,12 @@ export const usePageHistory = (
export const useSnapshotPage = ( export const useSnapshotPage = (
workspaceId: string, workspaceId: string,
pageDocId: string, pageDocId: string,
ts?: string, ts?: string
snapshot?: ArrayBuffer
) => { ) => {
const snapshot = usePageHistory(workspaceId, pageDocId, ts);
const page = useMemo(() => { const page = useMemo(() => {
if (!ts) { if (!ts) {
return null; return;
} }
const pageId = pageDocId + '-' + ts; const pageId = pageDocId + '-' + ts;
const historyShellWorkspace = getOrCreateWorkspace(workspaceId); const historyShellWorkspace = getOrCreateWorkspace(workspaceId);
@@ -163,10 +163,13 @@ export const useSnapshotPage = (
page.awarenessStore.setReadonly(page, true); page.awarenessStore.setReadonly(page, true);
const spaceDoc = page.spaceDoc; const spaceDoc = page.spaceDoc;
page page
.load(() => applyUpdate(spaceDoc, new Uint8Array(snapshot))) .load(() => {
applyUpdate(spaceDoc, new Uint8Array(snapshot));
historyShellWorkspace.schema.upgradePage(0, {}, spaceDoc);
})
.catch(console.error); // must load before applyUpdate .catch(console.error); // must load before applyUpdate
} }
return page; return page ?? undefined;
}, [pageDocId, snapshot, ts, workspaceId]); }, [pageDocId, snapshot, ts, workspaceId]);
return page; return page;

View File

@@ -8,7 +8,7 @@ import { ConfirmModal, Modal } from '@affine/component/ui/modal';
import type { PageMode } from '@affine/core/atoms'; import type { PageMode } from '@affine/core/atoms';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { waitForCurrentWorkspaceAtom } from '@affine/workspace/atom'; import { waitForCurrentWorkspaceAtom } from '@affine/workspace/atom';
import type { Workspace } from '@blocksuite/store'; import type { Page, Workspace } from '@blocksuite/store';
import type { DialogContentProps } from '@radix-ui/react-dialog'; import type { DialogContentProps } from '@radix-ui/react-dialog';
import { useAsyncCallback } from '@toeverything/hooks/affine-async-hooks'; import { useAsyncCallback } from '@toeverything/hooks/affine-async-hooks';
import { useAtom, useAtomValue } from 'jotai'; import { useAtom, useAtomValue } from 'jotai';
@@ -20,6 +20,7 @@ import {
useMemo, useMemo,
useState, useState,
} from 'react'; } from 'react';
import { encodeStateAsUpdate } from 'yjs';
import { currentModeAtom } from '../../../atoms/mode'; import { currentModeAtom } from '../../../atoms/mode';
import { pageHistoryModalAtom } from '../../../atoms/page-history'; import { pageHistoryModalAtom } from '../../../atoms/page-history';
@@ -31,7 +32,6 @@ import {
import { AffineErrorBoundary } from '../affine-error-boundary'; import { AffineErrorBoundary } from '../affine-error-boundary';
import { import {
historyListGroupByDay, historyListGroupByDay,
usePageHistory,
usePageSnapshotList, usePageSnapshotList,
useRestorePage, useRestorePage,
useSnapshotPage, useSnapshotPage,
@@ -90,10 +90,8 @@ const timestampToLocalTime = (ts: string) => {
}; };
interface HistoryEditorPreviewProps { interface HistoryEditorPreviewProps {
workspaceId: string;
pageDocId: string;
ts?: string; ts?: string;
snapshot?: ArrayBuffer; snapshotPage?: Page;
mode: PageMode; mode: PageMode;
onModeChange: (mode: PageMode) => void; onModeChange: (mode: PageMode) => void;
title: string; title: string;
@@ -101,11 +99,9 @@ interface HistoryEditorPreviewProps {
const HistoryEditorPreview = ({ const HistoryEditorPreview = ({
ts, ts,
snapshot, snapshotPage,
onModeChange, onModeChange,
mode, mode,
workspaceId,
pageDocId,
title, title,
}: HistoryEditorPreviewProps) => { }: HistoryEditorPreviewProps) => {
const onSwitchToPageMode = useCallback(() => { const onSwitchToPageMode = useCallback(() => {
@@ -114,7 +110,6 @@ const HistoryEditorPreview = ({
const onSwitchToEdgelessMode = useCallback(() => { const onSwitchToEdgelessMode = useCallback(() => {
onModeChange('edgeless'); onModeChange('edgeless');
}, [onModeChange]); }, [onModeChange]);
const page = useSnapshotPage(workspaceId, pageDocId, ts, snapshot);
return ( return (
<div className={styles.previewWrapper}> <div className={styles.previewWrapper}>
@@ -139,11 +134,11 @@ const HistoryEditorPreview = ({
</div> </div>
</div> </div>
{page ? ( {snapshotPage ? (
<BlockSuiteEditor <BlockSuiteEditor
className={styles.editor} className={styles.editor}
mode={mode} mode={mode}
page={page} page={snapshotPage}
onModeChange={onModeChange} onModeChange={onModeChange}
/> />
) : ( ) : (
@@ -306,7 +301,7 @@ const PageHistoryManager = ({
return workspace.getPage(pageId)?.spaceDoc.guid ?? pageId; return workspace.getPage(pageId)?.spaceDoc.guid ?? pageId;
}, [pageId, workspace]); }, [pageId, workspace]);
const snapshot = usePageHistory(workspaceId, pageDocId, activeVersion); const snapshotPage = useSnapshotPage(workspaceId, pageDocId, activeVersion);
const t = useAFFiNEI18N(); const t = useAFFiNEI18N();
@@ -314,14 +309,15 @@ const PageHistoryManager = ({
const handleRestore = useMemo( const handleRestore = useMemo(
() => async () => { () => async () => {
if (!activeVersion || !snapshot) { if (!activeVersion || !snapshotPage) {
return; return;
} }
const snapshot = encodeStateAsUpdate(snapshotPage.spaceDoc);
await onRestore(activeVersion, new Uint8Array(snapshot)); await onRestore(activeVersion, new Uint8Array(snapshot));
// close the modal after restore // close the modal after restore
onClose(); onClose();
}, },
[activeVersion, onClose, onRestore, snapshot] [activeVersion, onClose, onRestore, snapshotPage]
); );
const defaultPreviewPageMode = useAtomValue(currentModeAtom); const defaultPreviewPageMode = useAtomValue(currentModeAtom);
@@ -352,10 +348,8 @@ const PageHistoryManager = ({
<div className={styles.root}> <div className={styles.root}>
<div className={styles.modalContent} data-empty={!activeVersion}> <div className={styles.modalContent} data-empty={!activeVersion}>
<HistoryEditorPreview <HistoryEditorPreview
workspaceId={workspaceId}
pageDocId={pageDocId}
ts={activeVersion} ts={activeVersion}
snapshot={snapshot} snapshotPage={snapshotPage}
mode={mode} mode={mode}
onModeChange={setMode} onModeChange={setMode}
title={title} title={title}