Skip to content

Commit

Permalink
refactor(editor): use spec builder (#9424)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 30, 2024
1 parent 4fee0e2 commit b66a64b
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 150 deletions.
9 changes: 5 additions & 4 deletions blocksuite/affine/shared/src/utils/spec/spec-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export class SpecBuilder {
this._value = this._value.filter(extension => extension !== target);
}

replace(target: ExtensionType, newExtension: ExtensionType) {
this._value = this._value.map(extension =>
extension === target ? newExtension : extension
);
replace(target: ExtensionType[], newExtension: ExtensionType[]) {
this._value = [
...this._value.filter(extension => !target.includes(extension)),
...newExtension,
];
}
}
6 changes: 4 additions & 2 deletions blocksuite/blocks/src/_specs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
RichTextExtensions,
} from '@blocksuite/affine-components/rich-text';
import {
DocDisplayMetaService,
EditPropsStore,
FontLoaderService,
} from '@blocksuite/affine-shared/services';
Expand All @@ -36,6 +37,7 @@ import {
} from '../surface-ref-block/surface-ref-spec.js';

export const CommonBlockSpecs: ExtensionType[] = [
DocDisplayMetaService,
RefNodeSlotsExtension,
EditPropsStore,
RichTextExtensions,
Expand All @@ -54,15 +56,15 @@ export const CommonBlockSpecs: ExtensionType[] = [
].flat();

export const PageFirstPartyBlockSpecs: ExtensionType[] = [
...CommonBlockSpecs,
CommonBlockSpecs,
NoteBlockSpec,
PageSurfaceBlockSpec,
PageSurfaceRefBlockSpec,
FontLoaderService,
].flat();

export const EdgelessFirstPartyBlockSpecs: ExtensionType[] = [
...CommonBlockSpecs,
CommonBlockSpecs,

EdgelessNoteBlockSpec,
EdgelessSurfaceBlockSpec,
Expand Down
44 changes: 0 additions & 44 deletions blocksuite/blocks/src/_specs/group/common.ts

This file was deleted.

16 changes: 0 additions & 16 deletions blocksuite/blocks/src/_specs/group/edgeless.ts

This file was deleted.

6 changes: 0 additions & 6 deletions blocksuite/blocks/src/_specs/group/page.ts

This file was deleted.

1 change: 0 additions & 1 deletion blocksuite/blocks/src/_specs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './group/common.js';
export * from './preset/edgeless-specs.js';
export * from './preset/mobile-patch.js';
export * from './preset/page-specs.js';
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/blocks/src/_specs/preset/edgeless-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const EdgelessBuiltInManager: ExtensionType[] = [

export const EdgelessEditorBlockSpecs: ExtensionType[] = [
EdgelessRootBlockSpec,
...EdgelessFirstPartyBlockSpecs,
EdgelessFirstPartyBlockSpecs,
EdgelessToolExtension,
EdgelessBuiltInManager,
].flat();
1 change: 1 addition & 0 deletions blocksuite/blocks/src/data-view-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { DataViewBlockModel } from './data-view-model.js';

export * from './data-view-block.js';
export * from './data-view-model.js';
export * from './data-view-spec.js';

declare global {
namespace BlockSuite {
Expand Down
1 change: 1 addition & 0 deletions blocksuite/blocks/src/database-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type { DatabaseOptionsConfig } from './config.js';
export * from './data-source.js';
export * from './database-block.js';
export * from './database-service.js';
export * from './database-spec.js';
export { databaseBlockColumns } from './properties/index.js';
declare global {
namespace BlockSuite {
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/blocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export * from './_common/test-utils/test-utils.js';
export * from './_common/transformers/index.js';
export { type AbstractEditor } from './_common/types.js';
export * from './_specs/index.js';
export * from './data-view-block/index.js';
export * from './database-block/index.js';
export * from './data-view-block';
export * from './database-block';
export { EdgelessTemplatePanel } from './root-block/edgeless/components/toolbar/template/template-panel.js';
export type {
Template,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FileDropExtension } from '@blocksuite/affine-components/drag-indicator';
import {
DNDAPIExtension,
DocDisplayMetaService,
DocModeService,
EmbedOptionService,
PageViewportServiceExtension,
Expand Down Expand Up @@ -99,7 +98,6 @@ const EdgelessCommonExtension: ExtensionType[] = [
ToolController,
DNDAPIExtension,
PageViewportServiceExtension,
DocDisplayMetaService,
RootBlockAdapterExtensions,
FileDropExtension,
].flat();
Expand Down
2 changes: 0 additions & 2 deletions blocksuite/blocks/src/root-block/page/page-root-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FileDropExtension } from '@blocksuite/affine-components/drag-indicator';
import {
DNDAPIExtension,
DocDisplayMetaService,
DocModeService,
EmbedOptionService,
PageViewportServiceExtension,
Expand Down Expand Up @@ -70,7 +69,6 @@ const PageCommonExtension: ExtensionType[] = [
DocModeService,
ThemeService,
EmbedOptionService,
DocDisplayMetaService,
PageViewportServiceExtension,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DividerBlockSpec,
EditPropsStore,
EmbedExtensions,
FontLoaderService,
ImageBlockSpec,
LatexBlockSpec,
ListBlockSpec,
Expand All @@ -36,6 +37,7 @@ const CommonBlockSpecs: ExtensionType[] = [
BookmarkBlockSpec,
AttachmentBlockSpec,
AdapterFactoryExtensions,
FontLoaderService,
].flat();

export const DefaultBlockSpecs: ExtensionType[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
AIEdgelessRootBlockSpec,
AIPageRootBlockSpec,
AICodeBlockSpec,
AIImageBlockSpec,
AIParagraphBlockSpec,
} from '@affine/core/blocksuite/presets/ai';
import { AIChatBlockSpec } from '@affine/core/blocksuite/presets/blocks';
import { DocService, DocsService } from '@affine/core/modules/doc';
import { DocDisplayMetaService } from '@affine/core/modules/doc-display-meta';
import { EditorSettingService } from '@affine/core/modules/editor-setting';
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
import { AppThemeService } from '@affine/core/modules/theme';
import { mixpanel } from '@affine/track';
import {
Expand All @@ -18,18 +19,17 @@ import type {
DocDisplayMetaExtension,
DocDisplayMetaParams,
RootBlockConfig,
SpecBuilder,
TelemetryEventMap,
ThemeExtension,
} from '@blocksuite/affine/blocks';
import {
CodeBlockSpec,
ColorScheme,
DocDisplayMetaProvider,
EdgelessBuiltInManager,
EdgelessRootBlockSpec,
EdgelessToolExtension,
EditorSettingExtension,
FontLoaderService,
PageRootBlockSpec,
ImageBlockSpec,
ParagraphBlockSpec,
TelemetryProvider,
ThemeExtensionIdentifier,
} from '@blocksuite/affine/blocks';
Expand Down Expand Up @@ -258,36 +258,24 @@ export const extendEdgelessPreviewSpec = (function () {
};
})();

export function createPageRootBlockSpec(
framework: FrameworkProvider
): ExtensionType[] {
const featureFlagService = framework.get(FeatureFlagService);
const enableAI = featureFlagService.flags.enable_ai.value;
return [
enableAI ? AIPageRootBlockSpec : PageRootBlockSpec,
FontLoaderService,
getThemeExtension(framework),
getFontConfigExtension(),
getTelemetryExtension(),
getEditorConfigExtension(framework),
buildDocDisplayMetaExtension(framework),
].flat();
export function enableAffineExtension(
framework: FrameworkProvider,
specBuilder: SpecBuilder
): void {
specBuilder.extend(
[
getThemeExtension(framework),
getFontConfigExtension(),
getTelemetryExtension(),
getEditorConfigExtension(framework),
buildDocDisplayMetaExtension(framework),
].flat()
);
}

export function createEdgelessRootBlockSpec(
framework: FrameworkProvider
): ExtensionType[] {
const featureFlagService = framework.get(FeatureFlagService);
const enableAI = featureFlagService.flags.enable_ai.value;
return [
enableAI ? AIEdgelessRootBlockSpec : EdgelessRootBlockSpec,
FontLoaderService,
getThemeExtension(framework),
EdgelessToolExtension,
EdgelessBuiltInManager,
getFontConfigExtension(),
getTelemetryExtension(),
getEditorConfigExtension(framework),
buildDocDisplayMetaExtension(framework),
].flat();
export function enableAIExtension(specBuilder: SpecBuilder): void {
specBuilder.replace(CodeBlockSpec, AICodeBlockSpec);
specBuilder.replace(ImageBlockSpec, AIImageBlockSpec);
specBuilder.replace(ParagraphBlockSpec, AIParagraphBlockSpec);
specBuilder.extend(AIChatBlockSpec);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import { AIEdgelessRootBlockSpec } from '@affine/core/blocksuite/presets/ai';
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
import { builtInTemplates as builtInEdgelessTemplates } from '@affine/templates/edgeless';
import { builtInTemplates as builtInStickersTemplates } from '@affine/templates/stickers';
import type { ExtensionType } from '@blocksuite/affine/block-std';
import type { TemplateManager } from '@blocksuite/affine/blocks';
import {
EdgelessNoteBlockSpec,
EdgelessSurfaceBlockSpec,
EdgelessSurfaceRefBlockSpec,
EdgelessRootBlockSpec,
EdgelessTemplatePanel,
EdgelessTextBlockSpec,
FrameBlockSpec,
SpecProvider,
} from '@blocksuite/affine/blocks';
import { type FrameworkProvider } from '@toeverything/infra';

import { AIBlockSpecs, DefaultBlockSpecs } from './common';
import { createEdgelessRootBlockSpec } from './custom/root-block';
import { enableAffineExtension, enableAIExtension } from './custom/root-block';

export function createEdgelessModeSpecs(
framework: FrameworkProvider
): ExtensionType[] {
const featureFlagService = framework.get(FeatureFlagService);
const enableAI = featureFlagService.flags.enable_ai.value;
return [
...(enableAI ? AIBlockSpecs : DefaultBlockSpecs),
EdgelessSurfaceBlockSpec,
EdgelessSurfaceRefBlockSpec,
FrameBlockSpec,
EdgelessTextBlockSpec,
EdgelessNoteBlockSpec,
// special
createEdgelessRootBlockSpec(framework),
].flat();
const edgelessSpec = SpecProvider.getInstance().getSpec('edgeless');
enableAffineExtension(framework, edgelessSpec);
if (enableAI) {
enableAIExtension(edgelessSpec);
edgelessSpec.replace(EdgelessRootBlockSpec, AIEdgelessRootBlockSpec);
}

return edgelessSpec.value;
}

export function effects() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { AIPageRootBlockSpec } from '@affine/core/blocksuite/presets/ai';
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
import type { ExtensionType } from '@blocksuite/affine/block-std';
import {
NoteBlockSpec,
PageSurfaceBlockSpec,
PageSurfaceRefBlockSpec,
} from '@blocksuite/affine/blocks';
import { PageRootBlockSpec, SpecProvider } from '@blocksuite/affine/blocks';
import { type FrameworkProvider } from '@toeverything/infra';

import { AIBlockSpecs, DefaultBlockSpecs } from './common';
import { createPageRootBlockSpec } from './custom/root-block';
import { enableAffineExtension, enableAIExtension } from './custom/root-block';

export function createPageModeSpecs(
framework: FrameworkProvider
): ExtensionType[] {
const featureFlagService = framework.get(FeatureFlagService);
const enableAI = featureFlagService.flags.enable_ai.value;
return [
...(enableAI ? AIBlockSpecs : DefaultBlockSpecs),
PageSurfaceBlockSpec,
PageSurfaceRefBlockSpec,
NoteBlockSpec,
// special
createPageRootBlockSpec(framework),
].flat();
const provider = SpecProvider.getInstance();
const pageSpec = provider.getSpec('page');
enableAffineExtension(framework, pageSpec);
if (enableAI) {
enableAIExtension(pageSpec);
pageSpec.replace(PageRootBlockSpec, AIPageRootBlockSpec);
}
return pageSpec.value;
}

0 comments on commit b66a64b

Please sign in to comment.