From 14a3cdff41fc1e23474a9d4ec218c92501be71e6 Mon Sep 17 00:00:00 2001 From: mitsuha Date: Fri, 2 Sep 2022 15:59:24 +0800 Subject: [PATCH] 1.adjust export/import database; --- .../layout/src/header/FileSystem.tsx | 42 +++------ .../layout/src/header/LayoutHeader.tsx | 3 - .../Settings/use-setting-flags.ts | 4 +- .../settings-sidebar/Settings/use-settings.ts | 16 ++-- .../Settings/util/inspector-workspace.ts | 8 +- .../db-service/src/services/database/index.ts | 2 +- .../src/services/workspace/user-config.ts | 2 +- libs/datasource/jwt/src/index.ts | 2 +- libs/datasource/jwt/src/yjs/index.ts | 88 ++++++++++--------- libs/datasource/state/src/user.ts | 38 ++------ 10 files changed, 89 insertions(+), 116 deletions(-) diff --git a/libs/components/layout/src/header/FileSystem.tsx b/libs/components/layout/src/header/FileSystem.tsx index 30c2b68816..c143e6f3f1 100644 --- a/libs/components/layout/src/header/FileSystem.tsx +++ b/libs/components/layout/src/header/FileSystem.tsx @@ -80,34 +80,20 @@ export const FileSystem = () => { if (apiSupported && !selected) { return ( - <> - setError(false)} - > - - - } - /> - { - try { - await requestPermission('AFFiNE'); - onSelected(); - } catch (e) { - onError(); - } - }} - > - {t('Sync to Disk')} - - + setError(false)} + > + + + } + /> ); } return null; diff --git a/libs/components/layout/src/header/LayoutHeader.tsx b/libs/components/layout/src/header/LayoutHeader.tsx index 9619c97c84..4c17564d9c 100644 --- a/libs/components/layout/src/header/LayoutHeader.tsx +++ b/libs/components/layout/src/header/LayoutHeader.tsx @@ -70,9 +70,6 @@ export const LayoutHeader = () => { - - {warningTips} - ); }; diff --git a/libs/components/layout/src/settings-sidebar/Settings/use-setting-flags.ts b/libs/components/layout/src/settings-sidebar/Settings/use-setting-flags.ts index e0a7fb697c..ff3136b230 100644 --- a/libs/components/layout/src/settings-sidebar/Settings/use-setting-flags.ts +++ b/libs/components/layout/src/settings-sidebar/Settings/use-setting-flags.ts @@ -2,8 +2,8 @@ import { useFlag } from '@toeverything/datasource/feature-flags'; export const useSettingFlags = () => { const booleanFullWidthChecked = useFlag('BooleanFullWidthChecked', false); - const booleanExportWorkspace = useFlag('BooleanExportWorkspace', false); - const booleanImportWorkspace = useFlag('BooleanImportWorkspace', false); + const booleanExportWorkspace = useFlag('BooleanExportAffineDb', true); + const booleanImportWorkspace = useFlag('BooleanImportAffineDb', true); const booleanExportHtml = useFlag('BooleanExportHtml', false); const booleanExportPdf = useFlag('BooleanExportPdf', false); const booleanExportMarkdown = useFlag('BooleanExportMarkdown', false); diff --git a/libs/components/layout/src/settings-sidebar/Settings/use-settings.ts b/libs/components/layout/src/settings-sidebar/Settings/use-settings.ts index dfd8918c49..2b32794424 100644 --- a/libs/components/layout/src/settings-sidebar/Settings/use-settings.ts +++ b/libs/components/layout/src/settings-sidebar/Settings/use-settings.ts @@ -145,12 +145,19 @@ export const useSettings = (): SettingItem[] => { type: 'separator', key: 'separator2', }, + { + type: 'button', + name: t('Clear Workspace'), + key: 'Clear Workspace', + onClick: () => clearWorkspace(workspaceId), + flag: 'booleanClearWorkspace', + }, { type: 'button', name: t('Import Workspace'), key: 'Import Workspace', onClick: () => importWorkspace(workspaceId), - flag: 'booleanImportWorkspace', + flag: 'booleanClearWorkspace', }, { type: 'button', @@ -159,13 +166,6 @@ export const useSettings = (): SettingItem[] => { onClick: () => exportWorkspace(), flag: 'booleanExportWorkspace', }, - { - type: 'button', - name: t('Clear Workspace'), - key: 'Clear Workspace', - onClick: () => clearWorkspace(workspaceId), - flag: 'booleanClearWorkspace', - }, ]; return filterSettings(settings, settingFlags); diff --git a/libs/components/layout/src/settings-sidebar/Settings/util/inspector-workspace.ts b/libs/components/layout/src/settings-sidebar/Settings/util/inspector-workspace.ts index ee851dc90c..25adfdd669 100644 --- a/libs/components/layout/src/settings-sidebar/Settings/util/inspector-workspace.ts +++ b/libs/components/layout/src/settings-sidebar/Settings/util/inspector-workspace.ts @@ -6,8 +6,12 @@ export const importWorkspace = (workspaceId: string) => { window.client .inspector() .load() - .then(() => { - window.location.href = `/${workspaceId}/`; + .then(status => { + if (status) { + if (window.confirm('Your currently open data will be lost.')) { + window.location.href = `/${workspaceId}/`; + } + } }); }; diff --git a/libs/datasource/db-service/src/services/database/index.ts b/libs/datasource/db-service/src/services/database/index.ts index 3d70345f0f..dbbe23bb9e 100644 --- a/libs/datasource/db-service/src/services/database/index.ts +++ b/libs/datasource/db-service/src/services/database/index.ts @@ -39,7 +39,7 @@ async function _getCurrentToken() { const _enabled = { demo: [], - AFFiNE: ['sqlite'], + AFFiNE: ['idb'], } as any; async function _getBlockDatabase( diff --git a/libs/datasource/db-service/src/services/workspace/user-config.ts b/libs/datasource/db-service/src/services/workspace/user-config.ts index 5a52581c21..fbe74ce83d 100644 --- a/libs/datasource/db-service/src/services/workspace/user-config.ts +++ b/libs/datasource/db-service/src/services/workspace/user-config.ts @@ -36,7 +36,7 @@ export class UserConfig extends ServiceBaseClass { } const db = await this.database.getDatabase(workspace); - const newPage = await db.get('page'); + const newPage = await db.get_named_block('start-page'); await this.get_dependency(PageTree).addPage(workspace, newPage.id); await this.addRecentPage(workspace, userId, newPage.id); diff --git a/libs/datasource/jwt/src/index.ts b/libs/datasource/jwt/src/index.ts index e8f3483e23..5c74ae8b99 100644 --- a/libs/datasource/jwt/src/index.ts +++ b/libs/datasource/jwt/src/index.ts @@ -357,7 +357,7 @@ export class BlockClient< * @param name block name * @returns block instance */ - private async get_named_block( + public async get_named_block( name: string, options?: { workspace?: boolean } ): Promise> { diff --git a/libs/datasource/jwt/src/yjs/index.ts b/libs/datasource/jwt/src/yjs/index.ts index 7d0f30e4e3..b94f7009ea 100644 --- a/libs/datasource/jwt/src/yjs/index.ts +++ b/libs/datasource/jwt/src/yjs/index.ts @@ -324,53 +324,59 @@ export class YjsAdapter implements AsyncDatabaseAdapter { const binary = encodeStateAsUpdate(this._doc); saveAs( new Blob([binary]), - `affine_workspace_${new Date().toDateString()}.apk` + `affine_workspace_${new Date().toDateString()}.affine` ); }, load: async () => { - const handles = await window.showOpenFilePicker({ - types: [ - { - description: 'AFFiNE Package', - accept: { - // eslint-disable-next-line @typescript-eslint/naming-convention - 'application/affine': ['.apk'], + try { + const handles = await window.showOpenFilePicker({ + types: [ + { + description: 'AFFiNE Package', + accept: { + // eslint-disable-next-line @typescript-eslint/naming-convention + 'application/affine': ['.affine'], + }, }, - }, - ], - }); - const [file] = (await fromEvent(handles)) as File[]; - const binary = await file?.arrayBuffer(); - // await this._provider.idb.clearData(); - const doc = new Doc({ autoLoad: true, shouldLoad: true }); - let updated = 0; - let isUpdated = false; - doc.on('update', () => { - isUpdated = true; - updated += 1; - }); - setInterval(() => { - if (updated > 0) { - updated -= 1; - } - }, 500); - - const update_check = new Promise(resolve => { - const check = async () => { - while (!isUpdated || updated > 0) { - await sleep(); + ], + }); + const [file] = (await fromEvent(handles)) as File[]; + const binary = await file?.arrayBuffer(); + // await this._provider.idb.clearData(); + const doc = new Doc({ autoLoad: true, shouldLoad: true }); + let updated = 0; + let isUpdated = false; + doc.on('update', () => { + isUpdated = true; + updated += 1; + }); + setInterval(() => { + if (updated > 0) { + updated -= 1; } - resolve(); - }; - check(); - }); - // await new IndexedDBProvider(this._provider.idb.name, doc) - // .whenSynced; - if (binary) { - applyUpdate(doc, new Uint8Array(binary)); - await update_check; + }, 500); + + const update_check = new Promise(resolve => { + const check = async () => { + while (!isUpdated || updated > 0) { + await sleep(); + } + resolve(); + }; + check(); + }); + // await new IndexedDBProvider(this._provider.idb.name, doc) + // .whenSynced; + if (binary) { + applyUpdate(doc, new Uint8Array(binary)); + await update_check; + } + console.log('load success'); + + return true; + } catch (err) { + return false; } - console.log('load success'); }, parse: () => this._doc.toJSON(), // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/libs/datasource/state/src/user.ts b/libs/datasource/state/src/user.ts index 61893471de..953603ef2b 100644 --- a/libs/datasource/state/src/user.ts +++ b/libs/datasource/state/src/user.ts @@ -74,41 +74,21 @@ const _useUserAndSpacesForFreeLogin = () => { if (location.pathname.startsWith('/local')) { navigate('/local'); } else { - navigate('/demo'); + navigate(`/${BRAND_ID}`); } setLoading(false); } }, []); useEffect(() => { - if (localTrigger) { - setUser({ - photo: '', - id: BRAND_ID, - username: BRAND_ID, - nickname: BRAND_ID, - email: '', - }); - } else { - if (location.pathname.startsWith('/local')) { - setUser({ - photo: '', - id: 'local', - username: 'local', - nickname: 'local', - email: '', - }); - } else { - setUser({ - photo: '', - id: 'demo', - username: 'demo', - nickname: 'demo', - email: '', - }); - } - } - }, [localTrigger, location, setLoading, setUser]); + setUser({ + photo: '', + id: BRAND_ID, + username: BRAND_ID, + nickname: BRAND_ID, + email: '', + }); + }, [setUser]); const currentSpaceId: string | undefined = useMemo(() => user?.id, [user]);