import type { ExpectedLayout } from '@affine/sdk/entry'; import { assertExists } from '@blocksuite/global/utils'; import type { Workspace } from '@blocksuite/store'; import { atom, createStore } from 'jotai/vanilla'; import { getActiveBlockSuiteWorkspaceAtom } from './__internal__/workspace'; // global store let rootStore = createStore(); export function getCurrentStore() { return rootStore; } /** * @internal do not use this function unless you know what you are doing */ export function _setCurrentStore(store: ReturnType) { rootStore = store; } export const loadedPluginNameAtom = atom([]); export const currentWorkspaceIdAtom = atom(null); export const currentPageIdAtom = atom(null); export const currentWorkspaceAtom = atom>(async get => { const workspaceId = get(currentWorkspaceIdAtom); assertExists(workspaceId); const currentWorkspaceAtom = getActiveBlockSuiteWorkspaceAtom(workspaceId); return get(currentWorkspaceAtom); }); const contentLayoutBaseAtom = atom('editor'); type SetStateAction = Value | ((prev: Value) => Value); export const contentLayoutAtom = atom< ExpectedLayout, [SetStateAction], void >( get => get(contentLayoutBaseAtom), (_, set, layout) => { set(contentLayoutBaseAtom, prev => { let setV: (prev: ExpectedLayout) => ExpectedLayout; if (typeof layout !== 'function') { setV = () => layout; } else { setV = layout; } const nextValue = setV(prev); if (nextValue === 'editor') { return nextValue; } if (nextValue.first !== 'editor') { throw new Error('The first element of the layout should be editor.'); } if (nextValue.splitPercentage && nextValue.splitPercentage < 70) { throw new Error('The split percentage should be greater than 70.'); } return nextValue; }); } );