mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-06 11:59:51 +08:00
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import type { BlockSuiteFeatureFlags } from '@affine/env';
|
|
import { config } from '@affine/env';
|
|
import type { AffinePublicWorkspace } from '@affine/workspace/type';
|
|
import { WorkspaceFlavour } from '@affine/workspace/type';
|
|
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
|
import { atom } from 'jotai';
|
|
|
|
import { BlockSuiteWorkspace } from '../../shared';
|
|
import { affineApis } from '../../shared/apis';
|
|
|
|
function createPublicWorkspace(
|
|
workspaceId: string,
|
|
binary: ArrayBuffer,
|
|
singlePage = false
|
|
): AffinePublicWorkspace {
|
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
|
workspaceId,
|
|
WorkspaceFlavour.AFFINE,
|
|
{
|
|
workspaceApis: affineApis,
|
|
cachePrefix: WorkspaceFlavour.PUBLIC + (singlePage ? '-single-page' : ''),
|
|
}
|
|
);
|
|
BlockSuiteWorkspace.Y.applyUpdate(
|
|
blockSuiteWorkspace.doc,
|
|
new Uint8Array(binary)
|
|
);
|
|
Object.entries(config.editorFlags).forEach(([key, value]) => {
|
|
blockSuiteWorkspace.awarenessStore.setFlag(
|
|
key as keyof BlockSuiteFeatureFlags,
|
|
value
|
|
);
|
|
});
|
|
// force disable some features
|
|
blockSuiteWorkspace.awarenessStore.setFlag('enable_block_hub', false);
|
|
blockSuiteWorkspace.awarenessStore.setFlag('enable_drag_handle', false);
|
|
return {
|
|
flavour: WorkspaceFlavour.PUBLIC,
|
|
id: workspaceId,
|
|
blockSuiteWorkspace,
|
|
// maybe we can add some sync providers here
|
|
providers: [],
|
|
};
|
|
}
|
|
|
|
export const publicWorkspaceIdAtom = atom<string | null>(null);
|
|
export const publicWorkspacePageIdAtom = atom<string | null>(null);
|
|
export const publicPageBlockSuiteAtom = atom<Promise<AffinePublicWorkspace>>(
|
|
async get => {
|
|
const workspaceId = get(publicWorkspaceIdAtom);
|
|
const pageId = get(publicWorkspacePageIdAtom);
|
|
if (!workspaceId || !pageId) {
|
|
throw new Error('No workspace id or page id');
|
|
}
|
|
const binary = await affineApis.downloadPublicWorkspacePage(
|
|
workspaceId,
|
|
pageId
|
|
);
|
|
return createPublicWorkspace(workspaceId, binary, true);
|
|
}
|
|
);
|
|
export const publicWorkspaceAtom = atom<Promise<AffinePublicWorkspace>>(
|
|
async get => {
|
|
const workspaceId = get(publicWorkspaceIdAtom);
|
|
if (!workspaceId) {
|
|
throw new Error('No workspace id');
|
|
}
|
|
const binary = await affineApis.downloadWorkspace(workspaceId, true);
|
|
return createPublicWorkspace(workspaceId, binary, false);
|
|
}
|
|
);
|