Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat: add clear all workspaces

MingLiang Wang512c0a791e66
diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts
index a065228670..d88762b436 100644
--- a/packages/data-center/src/datacenter.ts
+++ b/packages/data-center/src/datacenter.ts
@@ -279,6 +279,16 @@ export class DataCenter {
return await this._transWorkspaceProvider(workspace, 'affine');
}
+ /**
+ * @deprecated
+ * clear all workspaces and data
+ */
+ public async clear() {
+ for (const provider of this.providerMap.values()) {
+ await provider.clear();
+ }
+ }
+
/**
* Select a file to import the workspace
* @param {File} file file of workspace.
diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts
index c1caa4048d..c2bea657db 100644
--- a/packages/data-center/src/provider/affine/affine.ts
+++ b/packages/data-center/src/provider/affine/affine.ts
@@ -185,12 +185,21 @@ export class AffineProvider extends BaseProvider {
}
public override async delete(id: string): Promise<void> {
- // TODO delete workspace all local data
+ await this.close(id);
+ IndexedDBProvider.delete(id);
await deleteWorkspace({ id });
}
public override async clear(): Promise<void> {
- // TODO: clear all workspaces source
+ for (const w of this._workspacesCache.values()) {
+ if (w.room) {
+ try {
+ await this.delete(w.room);
+ } catch (e) {
+ this._logger('has a problem of delete workspace ', e);
+ }
+ }
+ }
this._workspacesCache.clear();
}
diff --git a/packages/data-center/src/provider/local/local.ts b/packages/data-center/src/provider/local/local.ts
index 58dfd7acd6..20ed420ba1 100644
--- a/packages/data-center/src/provider/local/local.ts
+++ b/packages/data-center/src/provider/local/local.ts
@@ -38,22 +38,23 @@ export class LocalProvider extends BaseProvider {
return workspace;
}
- override loadWorkspaces() {
+ override loadWorkspaces(): Promise<WS[]> {
const workspaceStr = storage.getItem(WORKSPACE_KEY);
+ let workspaces: WS[] = [];
if (workspaceStr) {
try {
- return JSON.parse(workspaceStr);
+ workspaces = JSON.parse(workspaceStr) as WS[];
} catch (error) {
this._logger(`Failed to parse workspaces from storage`);
}
}
- return [];
+ return Promise.resolve(workspaces);
}
public override async delete(id: string): Promise<void> {
const index = this._workspacesList.findIndex(ws => ws.id === id);
if (index !== -1) {
- // TODO delete workspace all data
+ IndexedDBProvider.delete(id);
this._workspacesList.splice(index, 1);
this._storeWorkspaces(this._workspacesList);
} else {
@@ -75,7 +76,8 @@ export class LocalProvider extends BaseProvider {
}
public override async clear(): Promise<void> {
- // TODO: clear all data
+ const workspaces = await this.loadWorkspaces();
+ workspaces.forEach(ws => IndexedDBProvider.delete(ws.id));
this._storeWorkspaces([]);
}
}