fix(plugin-infra): react as peer dependency (#3260)
This commit is contained in:
@@ -19,7 +19,7 @@ import {
|
|||||||
import { createIndexedDBDownloadProvider } from '@affine/workspace/providers';
|
import { createIndexedDBDownloadProvider } from '@affine/workspace/providers';
|
||||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||||
import { nanoid } from '@blocksuite/store';
|
import { nanoid } from '@blocksuite/store';
|
||||||
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BlockSuitePageList,
|
BlockSuitePageList,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
|||||||
import type { RootWorkspaceMetadata } from '@affine/workspace/atom';
|
import type { RootWorkspaceMetadata } from '@affine/workspace/atom';
|
||||||
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
||||||
import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name';
|
import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name';
|
||||||
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { useAtomValue } from 'jotai';
|
import { useAtomValue } from 'jotai';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { WorkspaceDetailSkeleton } from '@affine/component/setting-components';
|
import { WorkspaceDetailSkeleton } from '@affine/component/setting-components';
|
||||||
import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
import { useSetAtom } from 'jotai';
|
import { useSetAtom } from 'jotai';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { Suspense, useCallback } from 'react';
|
import { Suspense, useCallback } from 'react';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import type { Workspace } from '@blocksuite/store';
|
import type { Workspace } from '@blocksuite/store';
|
||||||
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
import type { Atom } from 'jotai';
|
import type { Atom } from 'jotai';
|
||||||
import { atom, useAtomValue } from 'jotai';
|
import { atom, useAtomValue } from 'jotai';
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import {
|
|||||||
useSensors,
|
useSensors,
|
||||||
} from '@dnd-kit/core';
|
} from '@dnd-kit/core';
|
||||||
import { useBlockSuiteWorkspaceHelper } from '@toeverything/hooks/use-block-suite-workspace-helper';
|
import { useBlockSuiteWorkspaceHelper } from '@toeverything/hooks/use-block-suite-workspace-helper';
|
||||||
import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
import {
|
import {
|
||||||
currentPageIdAtom,
|
currentPageIdAtom,
|
||||||
currentWorkspaceIdAtom,
|
currentWorkspaceIdAtom,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
LocalWorkspaceIcon as DefaultLocalWorkspaceIcon,
|
LocalWorkspaceIcon as DefaultLocalWorkspaceIcon,
|
||||||
} from '@blocksuite/icons';
|
} from '@blocksuite/icons';
|
||||||
import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name';
|
import { useBlockSuiteWorkspaceName } from '@toeverything/hooks/use-block-suite-workspace-name';
|
||||||
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { useStaticBlockSuiteWorkspace } from '@toeverything/plugin-infra/__internal__/react';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,11 @@
|
|||||||
"type": "./dist/__internal__/workspace.d.ts",
|
"type": "./dist/__internal__/workspace.d.ts",
|
||||||
"import": "./dist/__internal__/workspace.js",
|
"import": "./dist/__internal__/workspace.js",
|
||||||
"require": "./dist/__internal__/workspace.cjs"
|
"require": "./dist/__internal__/workspace.cjs"
|
||||||
|
},
|
||||||
|
"./__internal__/react": {
|
||||||
|
"type": "./dist/__internal__/react.d.ts",
|
||||||
|
"import": "./dist/__internal__/react.js",
|
||||||
|
"require": "./dist/__internal__/react.cjs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
35
packages/plugin-infra/src/__internal__/react.ts
Normal file
35
packages/plugin-infra/src/__internal__/react.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import type { Workspace } from '@blocksuite/store';
|
||||||
|
import { type PassiveDocProvider } from '@blocksuite/store';
|
||||||
|
import { useAtomValue } from 'jotai/react';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
import {
|
||||||
|
getActiveBlockSuiteWorkspaceAtom,
|
||||||
|
workspacePassiveEffectWeakMap,
|
||||||
|
} from './workspace';
|
||||||
|
|
||||||
|
export function useStaticBlockSuiteWorkspace(id: string): Workspace {
|
||||||
|
return useAtomValue(getActiveBlockSuiteWorkspaceAtom(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function usePassiveWorkspaceEffect(workspace: Workspace) {
|
||||||
|
useEffect(() => {
|
||||||
|
if (workspacePassiveEffectWeakMap.get(workspace) === true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const providers = workspace.providers.filter(
|
||||||
|
(provider): provider is PassiveDocProvider =>
|
||||||
|
'passive' in provider && provider.passive === true
|
||||||
|
);
|
||||||
|
providers.forEach(provider => {
|
||||||
|
provider.connect();
|
||||||
|
});
|
||||||
|
workspacePassiveEffectWeakMap.set(workspace, true);
|
||||||
|
return () => {
|
||||||
|
providers.forEach(provider => {
|
||||||
|
provider.disconnect();
|
||||||
|
});
|
||||||
|
workspacePassiveEffectWeakMap.delete(workspace);
|
||||||
|
};
|
||||||
|
}, [workspace]);
|
||||||
|
}
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
import type { ActiveDocProvider, Workspace } from '@blocksuite/store';
|
import type { ActiveDocProvider, Workspace } from '@blocksuite/store';
|
||||||
import type { PassiveDocProvider } from '@blocksuite/store';
|
|
||||||
import { useAtomValue } from 'jotai/react';
|
|
||||||
import type { Atom } from 'jotai/vanilla';
|
import type { Atom } from 'jotai/vanilla';
|
||||||
import { atom } from 'jotai/vanilla';
|
import { atom } from 'jotai/vanilla';
|
||||||
import { useEffect } from 'react';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DO NOT ACCESS THIS MAP IN PRODUCTION, OR YOU WILL BE FIRED
|
* DO NOT ACCESS THIS MAP IN PRODUCTION, OR YOU WILL BE FIRED
|
||||||
@@ -60,29 +57,3 @@ export function getActiveBlockSuiteWorkspaceAtom(
|
|||||||
}
|
}
|
||||||
return workspacePassiveAtomWeakMap.get(workspace) as Atom<Promise<Workspace>>;
|
return workspacePassiveAtomWeakMap.get(workspace) as Atom<Promise<Workspace>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useStaticBlockSuiteWorkspace(id: string): Workspace {
|
|
||||||
return useAtomValue(getActiveBlockSuiteWorkspaceAtom(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function usePassiveWorkspaceEffect(workspace: Workspace) {
|
|
||||||
useEffect(() => {
|
|
||||||
if (workspacePassiveEffectWeakMap.get(workspace) === true) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const providers = workspace.providers.filter(
|
|
||||||
(provider): provider is PassiveDocProvider =>
|
|
||||||
'passive' in provider && provider.passive === true
|
|
||||||
);
|
|
||||||
providers.forEach(provider => {
|
|
||||||
provider.connect();
|
|
||||||
});
|
|
||||||
workspacePassiveEffectWeakMap.set(workspace, true);
|
|
||||||
return () => {
|
|
||||||
providers.forEach(provider => {
|
|
||||||
provider.disconnect();
|
|
||||||
});
|
|
||||||
workspacePassiveEffectWeakMap.delete(workspace);
|
|
||||||
};
|
|
||||||
}, [workspace]);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import { getDefaultStore } from 'jotai/vanilla';
|
|||||||
import { expect, test, vi } from 'vitest';
|
import { expect, test, vi } from 'vitest';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getActiveBlockSuiteWorkspaceAtom,
|
|
||||||
INTERNAL_BLOCKSUITE_HASH_MAP,
|
|
||||||
usePassiveWorkspaceEffect,
|
usePassiveWorkspaceEffect,
|
||||||
useStaticBlockSuiteWorkspace,
|
useStaticBlockSuiteWorkspace,
|
||||||
|
} from '../__internal__/react';
|
||||||
|
import {
|
||||||
|
getActiveBlockSuiteWorkspaceAtom,
|
||||||
|
INTERNAL_BLOCKSUITE_HASH_MAP,
|
||||||
} from '../__internal__/workspace';
|
} from '../__internal__/workspace';
|
||||||
|
|
||||||
test('useStaticBlockSuiteWorkspace', async () => {
|
test('useStaticBlockSuiteWorkspace', async () => {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export default defineConfig({
|
|||||||
root,
|
root,
|
||||||
'src/__internal__/workspace.ts'
|
'src/__internal__/workspace.ts'
|
||||||
),
|
),
|
||||||
|
'__internal__/react': resolve(root, 'src/__internal__/react.ts'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
|
|||||||
Reference in New Issue
Block a user