refactor(core): desktop project struct (#8334)

This commit is contained in:
EYHN
2024-11-05 11:00:33 +08:00
committed by GitHub
parent 89d09fd5e9
commit 902635e60f
343 changed files with 3846 additions and 3508 deletions

View File

@@ -0,0 +1,55 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { DocMode } from '@blocksuite/affine/blocks';
import type { WorkspaceMetadata } from '@toeverything/infra';
export type SettingTab =
| 'shortcuts'
| 'appearance'
| 'about'
| 'plans'
| 'billing'
| 'experimental-features'
| 'editor'
| 'account'
| `workspace:${'preference' | 'properties'}`;
export type GLOBAL_DIALOG_SCHEMA = {
'create-workspace': () => {
metadata: WorkspaceMetadata;
defaultDocId?: string;
};
'import-workspace': () => {
workspace: WorkspaceMetadata;
};
'import-template': (props: {
templateName: string;
templateMode: DocMode;
snapshotUrl: string;
}) => void;
import: () => void;
setting: (props: {
activeTab?: SettingTab;
workspaceMetadata?: WorkspaceMetadata | null;
scrollAnchor?: string;
}) => void;
};
export type WORKSPACE_DIALOG_SCHEMA = {
'doc-info': (props: { docId: string }) => void;
'doc-selector': (props: {
init: string[];
onBeforeConfirm?: (ids: string[], cb: () => void) => void;
}) => string[];
'collection-selector': (props: {
init: string[];
onBeforeConfirm?: (ids: string[], cb: () => void) => void;
}) => string[];
'collection-editor': (props: {
collectionId: string;
mode?: 'page' | 'rule';
}) => void;
'tag-selector': (props: {
init: string[];
onBeforeConfirm?: (ids: string[], cb: () => void) => void;
}) => string[];
};

View File

@@ -0,0 +1,17 @@
import type { Framework } from '@toeverything/infra';
import { WorkspaceScope } from '@toeverything/infra';
import { GlobalDialogService } from './services/dialog';
import { WorkspaceDialogService } from './services/workspace-dialog';
export type { GLOBAL_DIALOG_SCHEMA, WORKSPACE_DIALOG_SCHEMA } from './constant';
export { GlobalDialogService } from './services/dialog';
export { WorkspaceDialogService } from './services/workspace-dialog';
export type { DialogComponentProps } from './types';
export function configureDialogModule(framework: Framework) {
framework
.service(GlobalDialogService)
.scope(WorkspaceScope)
.service(WorkspaceDialogService);
}

View File

@@ -0,0 +1,41 @@
import { LiveData, Service } from '@toeverything/infra';
import { nanoid } from 'nanoid';
import type { GLOBAL_DIALOG_SCHEMA } from '../constant';
import type { DialogProps, DialogResult, OpenedDialog } from '../types';
export class GlobalDialogService extends Service {
readonly dialogs$ = new LiveData<OpenedDialog<GLOBAL_DIALOG_SCHEMA>[]>([]);
open<T extends keyof GLOBAL_DIALOG_SCHEMA>(
type: T,
props: DialogProps<GLOBAL_DIALOG_SCHEMA[T]>,
callback?: (result?: DialogResult<GLOBAL_DIALOG_SCHEMA[T]>) => void
): string {
const id = nanoid();
this.dialogs$.next([
...this.dialogs$.value,
{
type,
props,
callback,
id,
} as OpenedDialog<GLOBAL_DIALOG_SCHEMA>,
]);
return id;
}
close(id: string, result?: unknown) {
this.dialogs$.next(
this.dialogs$.value.filter(dialog => {
if (dialog.id === id) {
if (dialog.callback) {
dialog.callback(result as any);
}
return false;
}
return true;
})
);
}
}

View File

@@ -0,0 +1,41 @@
import { LiveData, Service } from '@toeverything/infra';
import { nanoid } from 'nanoid';
import type { WORKSPACE_DIALOG_SCHEMA } from '../constant';
import type { DialogProps, DialogResult, OpenedDialog } from '../types';
export class WorkspaceDialogService extends Service {
readonly dialogs$ = new LiveData<OpenedDialog<WORKSPACE_DIALOG_SCHEMA>[]>([]);
open<T extends keyof WORKSPACE_DIALOG_SCHEMA>(
type: T,
props: DialogProps<WORKSPACE_DIALOG_SCHEMA[T]>,
callback?: (result?: DialogResult<WORKSPACE_DIALOG_SCHEMA[T]>) => void
): string {
const id = nanoid();
this.dialogs$.next([
...this.dialogs$.value,
{
type,
props,
callback,
id,
} as OpenedDialog<WORKSPACE_DIALOG_SCHEMA>,
]);
return id;
}
close(id: string, result?: unknown) {
this.dialogs$.next(
this.dialogs$.value.filter(dialog => {
if (dialog.id === id) {
if (dialog.callback) {
dialog.callback(result as any);
}
return false;
}
return true;
})
);
}
}

View File

@@ -0,0 +1,16 @@
export type OpenedDialog<T> = {
[key in keyof T]: {
type: key;
props: DialogProps<T[key]>;
callback: (result?: DialogResult<T[key]>) => void;
};
}[keyof T] & { id: string };
export type DialogProps<T> = T extends (props: infer P) => void ? P : undefined;
export type DialogResult<T> = T extends (...args: any[]) => infer R
? R
: undefined;
export type DialogComponentProps<T> = DialogProps<T> & {
close: (result?: DialogResult<T>) => void;
};