mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
refactor(editor): should not rely on doc collection type (#9501)
This commit is contained in:
6
packages/common/env/src/constant.ts
vendored
6
packages/common/env/src/constant.ts
vendored
@@ -1,7 +1,7 @@
|
||||
// This file should has not side effect
|
||||
// oxlint-disable-next-line
|
||||
// @ts-ignore FIXME: typecheck error
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
|
||||
declare global {
|
||||
// oxlint-disable-next-line no-var
|
||||
@@ -91,10 +91,10 @@ export const Messages = {
|
||||
};
|
||||
|
||||
export class PageNotFoundError extends TypeError {
|
||||
readonly docCollection: DocCollection;
|
||||
readonly docCollection: Workspace;
|
||||
readonly pageId: string;
|
||||
|
||||
constructor(docCollection: DocCollection, pageId: string) {
|
||||
constructor(docCollection: Workspace, pageId: string) {
|
||||
super();
|
||||
this.docCollection = docCollection;
|
||||
this.pageId = pageId;
|
||||
|
||||
5
packages/common/env/src/filter.ts
vendored
5
packages/common/env/src/filter.ts
vendored
@@ -1,4 +1,4 @@
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { DocsPropertiesMeta } from '@blocksuite/affine/store';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const literalValueSchema: z.ZodType<LiteralValue, z.ZodTypeDef> =
|
||||
@@ -29,7 +29,6 @@ export type Ref = {
|
||||
name: keyof VariableMap;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface VariableMap {}
|
||||
|
||||
export const literalSchema = z.object({
|
||||
@@ -85,4 +84,4 @@ export const tagSchema = z.object({
|
||||
});
|
||||
export type Tag = z.input<typeof tagSchema>;
|
||||
|
||||
export type PropertiesMeta = DocCollection['meta']['properties'];
|
||||
export type PropertiesMeta = DocsPropertiesMeta;
|
||||
|
||||
@@ -145,18 +145,9 @@ export const markdownToSnapshot = async (
|
||||
middlewares: [defaultImageProxyMiddleware, pasteMiddleware(host.std)],
|
||||
});
|
||||
const markdownAdapter = new MixTextAdapter(job, host.std.provider);
|
||||
const { blockVersions, workspaceVersion, pageVersion } =
|
||||
host.std.doc.collection.meta;
|
||||
if (!blockVersions || !workspaceVersion || !pageVersion)
|
||||
throw new Error(
|
||||
'Need blockVersions, workspaceVersion, pageVersion meta information to get slice'
|
||||
);
|
||||
const payload = {
|
||||
file: markdown,
|
||||
assets: job.assetsManager,
|
||||
blockVersions,
|
||||
pageVersion,
|
||||
workspaceVersion,
|
||||
workspaceId: host.std.doc.collection.id,
|
||||
pageId: host.std.doc.id,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { useI18n } from '@affine/i18n';
|
||||
import { track } from '@affine/track';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import { ArrowRightBigIcon } from '@blocksuite/icons/rc';
|
||||
import type { createStore } from 'jotai';
|
||||
|
||||
@@ -19,7 +19,7 @@ export function registerAffineNavigationCommands({
|
||||
t: ReturnType<typeof useI18n>;
|
||||
store: ReturnType<typeof createStore>;
|
||||
navigationHelper: ReturnType<typeof useNavigateHelper>;
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
globalDialogService: GlobalDialogService;
|
||||
}) {
|
||||
const unsubs: Array<() => void> = [];
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { ListHistoryQuery } from '@affine/graphql';
|
||||
import { listHistoryQuery, recoverDocMutation } from '@affine/graphql';
|
||||
import { i18nTime } from '@affine/i18n';
|
||||
import { assertEquals } from '@blocksuite/affine/global/utils';
|
||||
import { DocCollection } from '@blocksuite/affine/store';
|
||||
import { DocCollection, type Workspace } from '@blocksuite/affine/store';
|
||||
import { useService } from '@toeverything/infra';
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import useSWRImmutable from 'swr/immutable';
|
||||
@@ -99,7 +99,7 @@ const snapshotFetcher = async (
|
||||
// so that we do not need to worry about providers etc
|
||||
// TODO(@Peng): fix references to the page (the referenced page will shown as deleted)
|
||||
// if we simply clone the current workspace, it maybe time consuming right?
|
||||
const docCollectionMap = new Map<string, DocCollection>();
|
||||
const docCollectionMap = new Map<string, Workspace>();
|
||||
|
||||
// assume the workspace is a cloud workspace since the history feature is only enabled for cloud workspace
|
||||
const getOrCreateShellWorkspace = (
|
||||
@@ -147,7 +147,7 @@ export const usePageHistory = (
|
||||
|
||||
// workspace id + page id + timestamp + snapshot -> Page (to be used for rendering in blocksuite editor)
|
||||
export const useSnapshotPage = (
|
||||
docCollection: DocCollection,
|
||||
docCollection: Workspace,
|
||||
pageDocId: string,
|
||||
ts?: string
|
||||
) => {
|
||||
@@ -253,10 +253,7 @@ export function revertUpdate(
|
||||
applyUpdate(doc, revertChangesSinceSnapshotUpdate);
|
||||
}
|
||||
|
||||
export const useRestorePage = (
|
||||
docCollection: DocCollection,
|
||||
pageId: string
|
||||
) => {
|
||||
export const useRestorePage = (docCollection: Workspace, pageId: string) => {
|
||||
const page = useDocCollectionPage(docCollection, pageId);
|
||||
const mutateQueryResource = useMutateQueryResource();
|
||||
const { trigger: recover, isMutating } = useMutation({
|
||||
|
||||
@@ -11,10 +11,7 @@ import { WorkspaceService } from '@affine/core/modules/workspace';
|
||||
import { i18nTime, Trans, useI18n } from '@affine/i18n';
|
||||
import { track } from '@affine/track';
|
||||
import type { DocMode } from '@blocksuite/affine/blocks';
|
||||
import type {
|
||||
Doc as BlockSuiteDoc,
|
||||
DocCollection,
|
||||
} from '@blocksuite/affine/store';
|
||||
import type { Doc as BlockSuiteDoc, Workspace } from '@blocksuite/affine/store';
|
||||
import { CloseIcon, ToggleCollapseIcon } from '@blocksuite/icons/rc';
|
||||
import * as Collapsible from '@radix-ui/react-collapsible';
|
||||
import type { DialogContentProps } from '@radix-ui/react-dialog';
|
||||
@@ -48,7 +45,7 @@ import * as styles from './styles.css';
|
||||
export interface PageHistoryModalProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
pageId: string;
|
||||
}
|
||||
|
||||
@@ -400,7 +397,7 @@ const PageHistoryManager = ({
|
||||
pageId,
|
||||
onClose,
|
||||
}: {
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
pageId: string;
|
||||
onClose: () => void;
|
||||
}) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { useInsidePeekView } from '@affine/core/modules/peek-view/view/modal-con
|
||||
import { WorkbenchLink } from '@affine/core/modules/workbench';
|
||||
import { track } from '@affine/track';
|
||||
import type { DocMode } from '@blocksuite/affine/blocks';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import { LiveData, useLiveData, useService } from '@toeverything/infra';
|
||||
import clsx from 'clsx';
|
||||
import { nanoid } from 'nanoid';
|
||||
@@ -167,7 +167,7 @@ export function AffineSharedPageReference({
|
||||
Icon,
|
||||
onClick: userOnClick,
|
||||
}: AffinePageReferenceProps & {
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
}) {
|
||||
const journalService = useService(JournalService);
|
||||
const isJournal = !!useLiveData(journalService.journalDate$(pageId));
|
||||
|
||||
@@ -5,11 +5,11 @@ import { DocsService } from '@affine/core/modules/doc';
|
||||
import { EditorSettingService } from '@affine/core/modules/editor-setting';
|
||||
import { WorkbenchService } from '@affine/core/modules/workbench';
|
||||
import { type DocMode } from '@blocksuite/affine/blocks';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import { useServices } from '@toeverything/infra';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
|
||||
export const usePageHelper = (docCollection: DocCollection) => {
|
||||
export const usePageHelper = (docCollection: Workspace) => {
|
||||
const {
|
||||
docsService,
|
||||
workbenchService,
|
||||
|
||||
@@ -6,13 +6,13 @@ import { ShareDocsListService } from '@affine/core/modules/share-doc';
|
||||
import { WorkspaceService } from '@affine/core/modules/workspace';
|
||||
import { PublicPageMode } from '@affine/graphql';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import type { DocCollection, DocMeta } from '@blocksuite/affine/store';
|
||||
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
|
||||
import { useLiveData, useService } from '@toeverything/infra';
|
||||
import { type ReactNode, useCallback, useEffect, useMemo } from 'react';
|
||||
|
||||
export type AllPageListConfig = {
|
||||
allPages: DocMeta[];
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
/**
|
||||
* Return `undefined` if the page is not public
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { DeltaInsert } from '@blocksuite/affine/inline';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
export function useReferenceLinkHelper(docCollection: DocCollection) {
|
||||
export function useReferenceLinkHelper(docCollection: Workspace) {
|
||||
const addReferenceLink = useCallback(
|
||||
(pageId: string, referenceId: string) => {
|
||||
const page = docCollection?.getDoc(pageId);
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import type { DocCollection, DocMeta } from '@blocksuite/affine/store';
|
||||
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
|
||||
import type { Atom } from 'jotai';
|
||||
import { atom, useAtomValue } from 'jotai';
|
||||
|
||||
const weakMap = new WeakMap<DocCollection, Atom<DocMeta[]>>();
|
||||
const weakMap = new WeakMap<Workspace, Atom<DocMeta[]>>();
|
||||
|
||||
// this hook is extracted from './use-block-suite-page-meta.ts' to avoid circular dependency
|
||||
export function useAllBlockSuiteDocMeta(
|
||||
docCollection: DocCollection
|
||||
): DocMeta[] {
|
||||
export function useAllBlockSuiteDocMeta(docCollection: Workspace): DocMeta[] {
|
||||
if (!weakMap.has(docCollection)) {
|
||||
const baseAtom = atom<DocMeta[]>([...docCollection.meta.docMetas]);
|
||||
weakMap.set(docCollection, baseAtom);
|
||||
baseAtom.onMount = set => {
|
||||
set([...docCollection.meta.docMetas]);
|
||||
const dispose = docCollection.meta.docMetaUpdated.on(() => {
|
||||
const dispose = docCollection.slots.docListUpdated.on(() => {
|
||||
set([...docCollection.meta.docMetas]);
|
||||
});
|
||||
return () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { DocsService } from '@affine/core/modules/doc';
|
||||
import { WorkspaceService } from '@affine/core/modules/workspace';
|
||||
import type { DocCollection, DocMeta } from '@blocksuite/affine/store';
|
||||
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
|
||||
import { useService } from '@toeverything/infra';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
|
||||
@@ -13,7 +13,7 @@ import { useJournalInfoHelper } from './use-journal';
|
||||
* If you want to get all pageMetas, use `useAllBlockSuitePageMeta` instead
|
||||
* @returns
|
||||
*/
|
||||
export function useBlockSuiteDocMeta(docCollection: DocCollection) {
|
||||
export function useBlockSuiteDocMeta(docCollection: Workspace) {
|
||||
const pageMetas = useAllBlockSuiteDocMeta(docCollection);
|
||||
const { isPageJournal } = useJournalInfoHelper();
|
||||
return useMemo(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Doc, DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Doc, Workspace } from '@blocksuite/affine/store';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
export function useDocCollectionHelper(docCollection: DocCollection) {
|
||||
export function useDocCollectionHelper(docCollection: Workspace) {
|
||||
return useMemo(
|
||||
() => ({
|
||||
createDoc: (pageId?: string): Doc => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import { DisposableGroup } from '@blocksuite/affine/global/utils';
|
||||
import type { Doc, DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Doc, Workspace } from '@blocksuite/affine/store';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
const logger = new DebugLogger('use-doc-collection-page');
|
||||
|
||||
export function useDocCollectionPage(
|
||||
docCollection: DocCollection,
|
||||
docCollection: Workspace,
|
||||
pageId: string | null
|
||||
): Doc | null {
|
||||
const [page, setPage] = useState(
|
||||
|
||||
@@ -2,7 +2,7 @@ import { DocDisplayMetaService } from '@affine/core/modules/doc-display-meta';
|
||||
import type { Tag } from '@affine/env/filter';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import { assertExists } from '@blocksuite/affine/global/utils';
|
||||
import type { DocCollection, DocMeta } from '@blocksuite/affine/store';
|
||||
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
|
||||
import { ToggleCollapseIcon, ViewLayersIcon } from '@blocksuite/icons/rc';
|
||||
import * as Collapsible from '@radix-ui/react-collapsible';
|
||||
import { useLiveData, useService } from '@toeverything/infra';
|
||||
@@ -274,7 +274,7 @@ export const TagListItemRenderer = memo(function TagListItemRenderer(
|
||||
|
||||
function tagIdToTagOption(
|
||||
tagId: string,
|
||||
docCollection: DocCollection
|
||||
docCollection: Workspace
|
||||
): Tag | undefined {
|
||||
return docCollection.meta.properties.tags?.options.find(
|
||||
opt => opt.id === tagId
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Collection, Tag } from '@affine/env/filter';
|
||||
import type { DocCollection, DocMeta } from '@blocksuite/affine/store';
|
||||
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
|
||||
import type { JSX, PropsWithChildren, ReactNode } from 'react';
|
||||
import type { To } from 'react-router-dom';
|
||||
|
||||
@@ -91,7 +91,7 @@ export type PageGroupByType =
|
||||
export interface ListProps<T> {
|
||||
// required data:
|
||||
items: T[];
|
||||
docCollection: DocCollection;
|
||||
docCollection: Workspace;
|
||||
className?: string;
|
||||
hideHeader?: boolean; // whether or not to hide the header. default is false (showing header)
|
||||
groupBy?: ItemGroupDefinition<T>[];
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import { DisposableGroup } from '@blocksuite/affine/global/utils';
|
||||
import type { Doc, DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Doc, Workspace } from '@blocksuite/affine/store';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
const logger = new DebugLogger('useBlockSuiteWorkspacePage');
|
||||
|
||||
export function useDocCollectionPage(
|
||||
docCollection: DocCollection,
|
||||
docCollection: Workspace,
|
||||
pageId: string | null
|
||||
): Doc | null {
|
||||
const [page, setPage] = useState(
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
openFileOrFiles,
|
||||
ZipTransformer,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import {
|
||||
ExportToHtmlIcon,
|
||||
ExportToMarkdownIcon,
|
||||
@@ -51,7 +51,7 @@ type ImportResult = {
|
||||
type ImportConfig = {
|
||||
fileOptions: { acceptType: AcceptType; multiple: boolean };
|
||||
importFunction: (
|
||||
docCollection: DocCollection,
|
||||
docCollection: Workspace,
|
||||
file: File | File[]
|
||||
) => Promise<ImportResult>;
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ export class TagStore extends Store {
|
||||
|
||||
subscribe(cb: () => void) {
|
||||
const disposable =
|
||||
this.workspaceService.workspace.docCollection.meta.docMetaUpdated.on(cb);
|
||||
this.workspaceService.workspace.docCollection.slots.docListUpdated.on(cb);
|
||||
return disposable.dispose;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { DocCollection } from '@blocksuite/affine/store';
|
||||
import {
|
||||
DocCollection,
|
||||
type Workspace as BSWorkspace,
|
||||
} from '@blocksuite/affine/store';
|
||||
import { Entity, LiveData } from '@toeverything/infra';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { Observable } from 'rxjs';
|
||||
@@ -22,7 +25,7 @@ export class Workspace extends Entity {
|
||||
|
||||
readonly flavour = this.meta.flavour;
|
||||
|
||||
_docCollection: DocCollection | null = null;
|
||||
_docCollection: BSWorkspace | null = null;
|
||||
|
||||
get docCollection() {
|
||||
if (!this._docCollection) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace as BSWorkspace } from '@blocksuite/affine/store';
|
||||
import {
|
||||
type AwarenessConnection,
|
||||
type BlobStorage,
|
||||
@@ -27,7 +27,7 @@ export interface WorkspaceFlavourProvider {
|
||||
|
||||
createWorkspace(
|
||||
initial: (
|
||||
docCollection: DocCollection,
|
||||
docCollection: BSWorkspace,
|
||||
blobStorage: BlobStorage,
|
||||
docStorage: DocStorage
|
||||
) => Promise<void>
|
||||
|
||||
@@ -3,8 +3,6 @@ import { Scope } from '@toeverything/infra';
|
||||
import type { WorkspaceOpenOptions } from '../open-options';
|
||||
import type { WorkspaceEngineProvider } from '../providers/flavour';
|
||||
|
||||
export type { DocCollection } from '@blocksuite/affine/store';
|
||||
|
||||
export class WorkspaceScope extends Scope<{
|
||||
openOptions: WorkspaceOpenOptions;
|
||||
engineProvider: WorkspaceEngineProvider;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import {
|
||||
type BlobStorage,
|
||||
type DocStorage,
|
||||
@@ -21,7 +21,7 @@ export class WorkspaceFactoryService extends Service {
|
||||
create = async (
|
||||
flavour: string,
|
||||
initial: (
|
||||
docCollection: DocCollection,
|
||||
docCollection: Workspace,
|
||||
blobStorage: BlobStorage,
|
||||
docStorage: DocStorage
|
||||
) => Promise<void> = () => Promise.resolve()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type { Collection } from '@affine/env/filter';
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { Map as YMap } from 'yjs';
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
export class UserSetting {
|
||||
constructor(
|
||||
private readonly docCollection: DocCollection,
|
||||
private readonly docCollection: Workspace,
|
||||
private readonly userId: string
|
||||
) {}
|
||||
|
||||
@@ -38,9 +38,6 @@ export class UserSetting {
|
||||
}
|
||||
}
|
||||
|
||||
export const getUserSetting = (
|
||||
docCollection: DocCollection,
|
||||
userId: string
|
||||
) => {
|
||||
export const getUserSetting = (docCollection: Workspace, userId: string) => {
|
||||
return new UserSetting(docCollection, userId);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user