mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-18 23:07:02 +08:00
feat: add @affine/bookmark-block plugin (#2618)
This commit is contained in:
@@ -12,12 +12,22 @@
|
||||
"dependencies": {
|
||||
"@affine/component": "workspace:*",
|
||||
"@affine/env": "workspace:*",
|
||||
"@affine/workspace": "workspace:*"
|
||||
"@affine/workspace": "workspace:*",
|
||||
"@blocksuite/blocks": "0.0.0-20230531080915-ca9c55a2-nightly",
|
||||
"@blocksuite/editor": "0.0.0-20230531080915-ca9c55a2-nightly",
|
||||
"@blocksuite/global": "0.0.0-20230531080915-ca9c55a2-nightly",
|
||||
"@blocksuite/lit": "0.0.0-20230531080915-ca9c55a2-nightly",
|
||||
"@blocksuite/store": "0.0.0-20230531080915-ca9c55a2-nightly"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jotai": "^2.1.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@blocksuite/blocks": "*",
|
||||
"@blocksuite/editor": "*",
|
||||
"@blocksuite/global": "*",
|
||||
"@blocksuite/lit": "*",
|
||||
"@blocksuite/store": "*",
|
||||
"jotai": "*",
|
||||
"react": "*",
|
||||
"react-dom": "*"
|
||||
|
||||
@@ -4,27 +4,53 @@ import { atom } from 'jotai';
|
||||
|
||||
import type { AffinePlugin, Definition } from './type';
|
||||
import type { Loader, PluginUIAdapter } from './type';
|
||||
import type { PluginBlockSuiteAdapter } from './type';
|
||||
|
||||
// todo: for now every plugin is enabled by default
|
||||
export const affinePluginsAtom = atom<Record<string, AffinePlugin<string>>>({});
|
||||
|
||||
const pluginLogger = new DebugLogger('affine:plugin');
|
||||
import { config } from '@affine/env';
|
||||
|
||||
export function definePlugin<ID extends string>(
|
||||
definition: Definition<ID>,
|
||||
uiAdapterLoader?: Loader<Partial<PluginUIAdapter>>
|
||||
uiAdapterLoader?: Loader<Partial<PluginUIAdapter>>,
|
||||
blockSuiteAdapter?: Loader<Partial<PluginBlockSuiteAdapter>>
|
||||
) {
|
||||
if (!config.enablePlugin) {
|
||||
return;
|
||||
}
|
||||
const basePlugin = {
|
||||
definition,
|
||||
uiAdapter: {},
|
||||
blockSuiteAdapter: {},
|
||||
};
|
||||
|
||||
rootStore.set(affinePluginsAtom, plugins => ({
|
||||
...plugins,
|
||||
[definition.id]: basePlugin,
|
||||
}));
|
||||
|
||||
if (blockSuiteAdapter) {
|
||||
const updateAdapter = (adapter: Partial<PluginBlockSuiteAdapter>) => {
|
||||
rootStore.set(affinePluginsAtom, plugins => ({
|
||||
...plugins,
|
||||
[definition.id]: {
|
||||
...basePlugin,
|
||||
blockSuiteAdapter: adapter,
|
||||
},
|
||||
}));
|
||||
};
|
||||
|
||||
blockSuiteAdapter
|
||||
.load()
|
||||
.then(({ default: adapter }) => updateAdapter(adapter));
|
||||
|
||||
if (import.meta.webpackHot) {
|
||||
blockSuiteAdapter.hotModuleReload(async _ => {
|
||||
const adapter = (await _).default;
|
||||
updateAdapter(adapter);
|
||||
pluginLogger.info('[HMR] Plugin', definition.id, 'hot reloaded.');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (uiAdapterLoader) {
|
||||
const updateAdapter = (adapter: Partial<PluginUIAdapter>) => {
|
||||
rootStore.set(affinePluginsAtom, plugins => ({
|
||||
@@ -39,6 +65,7 @@ export function definePlugin<ID extends string>(
|
||||
uiAdapterLoader
|
||||
.load()
|
||||
.then(({ default: adapter }) => updateAdapter(adapter));
|
||||
|
||||
if (import.meta.webpackHot) {
|
||||
uiAdapterLoader.hotModuleReload(async _ => {
|
||||
const adapter = (await _).default;
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
* AFFiNE Plugin System Types
|
||||
*/
|
||||
|
||||
import type { EditorContainer } from '@blocksuite/editor';
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
import type { Page } from '@playwright/test';
|
||||
import type { WritableAtom } from 'jotai';
|
||||
import type { ReactElement } from 'react';
|
||||
import type { MosaicDirection, MosaicNode } from 'react-mosaic-component';
|
||||
@@ -152,6 +155,14 @@ export type PluginUIAdapter = {
|
||||
debugContent: Adapter<Record<string, unknown>>;
|
||||
};
|
||||
|
||||
type Cleanup = () => void;
|
||||
|
||||
export type PluginBlockSuiteAdapter = {
|
||||
storeDecorator: (currentWorkspace: Workspace) => Promise<void>;
|
||||
pageDecorator: (currentPage: Page) => Cleanup;
|
||||
uiDecorator: (root: EditorContainer) => Cleanup;
|
||||
};
|
||||
|
||||
export type PluginAdapterCreator = (
|
||||
context: AffinePluginContext
|
||||
) => PluginUIAdapter;
|
||||
@@ -159,4 +170,5 @@ export type PluginAdapterCreator = (
|
||||
export type AffinePlugin<ID extends string> = {
|
||||
definition: Definition<ID>;
|
||||
uiAdapter: Partial<PluginUIAdapter>;
|
||||
blockSuiteAdapter: Partial<PluginBlockSuiteAdapter>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user