refactor(editor): remove service global type (#10129)

Closes: [BS-2566](https://linear.app/affine-design/issue/BS-2566/remove-global-types-in-service)
This commit is contained in:
Saul-Mirone
2025-02-12 11:46:10 +00:00
parent 6730122108
commit e7cc710f8e
11 changed files with 14 additions and 77 deletions

View File

@@ -1,7 +1,3 @@
import type * as SurfaceEffects from '@blocksuite/affine-block-surface/effects';
declare type _GLOBAL_ = typeof SurfaceEffects;
export * from './adapters/notion-html'; export * from './adapters/notion-html';
export * from './attachment-block'; export * from './attachment-block';
export * from './attachment-service'; export * from './attachment-service';

View File

@@ -1,5 +1,3 @@
import type * as CommandsType from '@blocksuite/affine-shared/commands';
import { CodeBlockComponent } from './code-block'; import { CodeBlockComponent } from './code-block';
import type { CodeBlockConfig } from './code-block-config'; import type { CodeBlockConfig } from './code-block-config';
import { import {
@@ -18,8 +16,6 @@ export function effects() {
customElements.define('affine-code', CodeBlockComponent); customElements.define('affine-code', CodeBlockComponent);
} }
declare type _GLOBAL_ = typeof CommandsType;
declare global { declare global {
namespace BlockSuite { namespace BlockSuite {
interface BlockConfigs { interface BlockConfigs {

View File

@@ -1,30 +1,18 @@
import type * as SurfaceEffects from '@blocksuite/affine-block-surface/effects';
import { EmbedFigmaBlockComponent } from './embed-figma-block'; import { EmbedFigmaBlockComponent } from './embed-figma-block';
import { EmbedEdgelessBlockComponent } from './embed-figma-block/embed-edgeless-figma-block'; import { EmbedEdgelessBlockComponent } from './embed-figma-block/embed-edgeless-figma-block';
import type { EmbedFigmaBlockService } from './embed-figma-block/embed-figma-service'; import { EmbedGithubBlockComponent } from './embed-github-block';
import {
EmbedGithubBlockComponent,
type EmbedGithubBlockService,
} from './embed-github-block';
import { EmbedEdgelessGithubBlockComponent } from './embed-github-block/embed-edgeless-github-block'; import { EmbedEdgelessGithubBlockComponent } from './embed-github-block/embed-edgeless-github-block';
import { EmbedHtmlBlockComponent } from './embed-html-block'; import { EmbedHtmlBlockComponent } from './embed-html-block';
import { EmbedHtmlFullscreenToolbar } from './embed-html-block/components/fullscreen-toolbar'; import { EmbedHtmlFullscreenToolbar } from './embed-html-block/components/fullscreen-toolbar';
import { EmbedEdgelessHtmlBlockComponent } from './embed-html-block/embed-edgeless-html-block'; import { EmbedEdgelessHtmlBlockComponent } from './embed-html-block/embed-edgeless-html-block';
import { EmbedLinkedDocBlockComponent } from './embed-linked-doc-block'; import { EmbedLinkedDocBlockComponent } from './embed-linked-doc-block';
import { EmbedEdgelessLinkedDocBlockComponent } from './embed-linked-doc-block/embed-edgeless-linked-doc-block'; import { EmbedEdgelessLinkedDocBlockComponent } from './embed-linked-doc-block/embed-edgeless-linked-doc-block';
import { import { EmbedLoomBlockComponent } from './embed-loom-block';
EmbedLoomBlockComponent,
type EmbedLoomBlockService,
} from './embed-loom-block';
import { EmbedEdgelessLoomBlockComponent } from './embed-loom-block/embed-edgeless-loom-bock'; import { EmbedEdgelessLoomBlockComponent } from './embed-loom-block/embed-edgeless-loom-bock';
import { EmbedSyncedDocBlockComponent } from './embed-synced-doc-block'; import { EmbedSyncedDocBlockComponent } from './embed-synced-doc-block';
import { EmbedSyncedDocCard } from './embed-synced-doc-block/components/embed-synced-doc-card'; import { EmbedSyncedDocCard } from './embed-synced-doc-block/components/embed-synced-doc-card';
import { EmbedEdgelessSyncedDocBlockComponent } from './embed-synced-doc-block/embed-edgeless-synced-doc-block'; import { EmbedEdgelessSyncedDocBlockComponent } from './embed-synced-doc-block/embed-edgeless-synced-doc-block';
import { import { EmbedYoutubeBlockComponent } from './embed-youtube-block';
EmbedYoutubeBlockComponent,
type EmbedYoutubeBlockService,
} from './embed-youtube-block';
import { EmbedEdgelessYoutubeBlockComponent } from './embed-youtube-block/embed-edgeless-youtube-block'; import { EmbedEdgelessYoutubeBlockComponent } from './embed-youtube-block/embed-edgeless-youtube-block';
export function effects() { export function effects() {
@@ -86,8 +74,6 @@ export function effects() {
); );
} }
declare type _GLOBAL = typeof SurfaceEffects;
declare global { declare global {
interface HTMLElementTagNameMap { interface HTMLElementTagNameMap {
'affine-embed-figma-block': EmbedFigmaBlockComponent; 'affine-embed-figma-block': EmbedFigmaBlockComponent;
@@ -107,13 +93,4 @@ declare global {
'affine-embed-linked-doc-block': EmbedLinkedDocBlockComponent; 'affine-embed-linked-doc-block': EmbedLinkedDocBlockComponent;
'affine-embed-edgeless-linked-doc-block': EmbedEdgelessLinkedDocBlockComponent; 'affine-embed-edgeless-linked-doc-block': EmbedEdgelessLinkedDocBlockComponent;
} }
namespace BlockSuite {
interface BlockServices {
'affine:embed-figma': EmbedFigmaBlockService;
'affine:embed-github': EmbedGithubBlockService;
'affine:embed-loom': EmbedLoomBlockService;
'affine:embed-youtube': EmbedYoutubeBlockService;
}
}
} }

View File

@@ -1,7 +1,3 @@
import type * as SurfaceEffects from '@blocksuite/affine-block-surface/effects';
declare type _GLOBAL_ = typeof SurfaceEffects;
export * from './adapters'; export * from './adapters';
export * from './commands'; export * from './commands';
export * from './image-block'; export * from './image-block';

View File

@@ -7,7 +7,6 @@ import {
AFFINE_EDGELESS_NOTE, AFFINE_EDGELESS_NOTE,
EdgelessNoteBlockComponent, EdgelessNoteBlockComponent,
} from './note-edgeless-block'; } from './note-edgeless-block';
import type { NoteBlockService } from './note-service';
export function effects() { export function effects() {
customElements.define('affine-note', NoteBlockComponent); customElements.define('affine-note', NoteBlockComponent);
@@ -19,9 +18,6 @@ export function effects() {
declare global { declare global {
namespace BlockSuite { namespace BlockSuite {
interface BlockServices {
'affine:note': NoteBlockService;
}
interface BlockConfigs { interface BlockConfigs {
'affine:note': NoteConfig; 'affine:note': NoteConfig;
} }

View File

@@ -1,6 +1,5 @@
import { effects as ParagraphHeadingIconEffects } from './heading-icon.js'; import { effects as ParagraphHeadingIconEffects } from './heading-icon.js';
import { ParagraphBlockComponent } from './paragraph-block.js'; import { ParagraphBlockComponent } from './paragraph-block.js';
import type { ParagraphBlockService } from './paragraph-service.js';
export function effects() { export function effects() {
ParagraphHeadingIconEffects(); ParagraphHeadingIconEffects();
@@ -8,11 +7,6 @@ export function effects() {
} }
declare global { declare global {
namespace BlockSuite {
interface BlockServices {
'affine:paragraph': ParagraphBlockService;
}
}
interface HTMLElementTagNameMap { interface HTMLElementTagNameMap {
'affine-paragraph': ParagraphBlockComponent; 'affine-paragraph': ParagraphBlockComponent;
} }

View File

@@ -1,7 +1,3 @@
import type * as SurfaceEffects from '@blocksuite/affine-block-surface/effects';
declare type _GLOBAL_ = typeof SurfaceEffects;
export * from './consts'; export * from './consts';
export * from './drag-handle'; export * from './drag-handle';
export * from './utils'; export * from './utils';

View File

@@ -143,7 +143,6 @@ import {
PageRootBlockComponent, PageRootBlockComponent,
PreviewRootBlockComponent, PreviewRootBlockComponent,
type RootBlockConfig, type RootBlockConfig,
type RootService,
} from './root-block/index.js'; } from './root-block/index.js';
import { AIFinishTip } from './root-block/widgets/ai-panel/components/finish-tip.js'; import { AIFinishTip } from './root-block/widgets/ai-panel/components/finish-tip.js';
import { GeneratingPlaceholder } from './root-block/widgets/ai-panel/components/generating-placeholder.js'; import { GeneratingPlaceholder } from './root-block/widgets/ai-panel/components/generating-placeholder.js';
@@ -400,8 +399,5 @@ declare global {
interface BlockConfigs { interface BlockConfigs {
'affine:page': RootBlockConfig; 'affine:page': RootBlockConfig;
} }
interface BlockServices {
'affine:page': RootService;
}
} }
} }

View File

@@ -12,13 +12,11 @@ import { Clipboard } from '../clipboard/index.js';
import { CommandManager } from '../command/index.js'; import { CommandManager } from '../command/index.js';
import { UIEventDispatcher } from '../event/index.js'; import { UIEventDispatcher } from '../event/index.js';
import { DndController } from '../extension/dnd/index.js'; import { DndController } from '../extension/dnd/index.js';
import type { BlockService } from '../extension/index.js';
import { GfxController } from '../gfx/controller.js'; import { GfxController } from '../gfx/controller.js';
import { GfxSelectionManager } from '../gfx/selection.js'; import { GfxSelectionManager } from '../gfx/selection.js';
import { SurfaceMiddlewareExtension } from '../gfx/surface-middleware.js'; import { SurfaceMiddlewareExtension } from '../gfx/surface-middleware.js';
import { ViewManager } from '../gfx/view/view-manager.js'; import { ViewManager } from '../gfx/view/view-manager.js';
import { import {
BlockServiceIdentifier,
BlockViewIdentifier, BlockViewIdentifier,
ConfigIdentifier, ConfigIdentifier,
LifeCycleWatcherIdentifier, LifeCycleWatcherIdentifier,
@@ -152,18 +150,6 @@ export class BlockStdScope {
return config; return config;
} }
/**
* @deprecated
* BlockService will be removed in the future.
*/
getService<Key extends BlockSuite.ServiceKeys>(
flavour: Key
): BlockSuite.BlockServices[Key] | null;
getService<Service extends BlockService>(flavour: string): Service | null;
getService(flavour: string): BlockService | null {
return this.getOptional(BlockServiceIdentifier(flavour));
}
getView(flavour: string) { getView(flavour: string) {
return this.getOptional(BlockViewIdentifier(flavour)); return this.getOptional(BlockViewIdentifier(flavour));
} }
@@ -208,10 +194,8 @@ export class BlockStdScope {
declare global { declare global {
namespace BlockSuite { namespace BlockSuite {
interface BlockServices {}
interface BlockConfigs {} interface BlockConfigs {}
type ServiceKeys = string & keyof BlockServices;
type ConfigKeys = string & keyof BlockConfigs; type ConfigKeys = string & keyof BlockConfigs;
} }
} }

View File

@@ -11,6 +11,7 @@ import { html } from 'lit/static-html.js';
import type { EventName, UIEventHandler } from '../../event/index.js'; import type { EventName, UIEventHandler } from '../../event/index.js';
import type { BlockService } from '../../extension/index.js'; import type { BlockService } from '../../extension/index.js';
import { BlockServiceIdentifier } from '../../identifier.js';
import type { BlockStdScope } from '../../scope/index.js'; import type { BlockStdScope } from '../../scope/index.js';
import { BlockSelection } from '../../selection/index.js'; import { BlockSelection } from '../../selection/index.js';
import { PropTypes, requiredProperties } from '../decorators/index.js'; import { PropTypes, requiredProperties } from '../decorators/index.js';
@@ -146,9 +147,11 @@ export class BlockComponent<
if (this._service) { if (this._service) {
return this._service; return this._service;
} }
const service = this.std.getService(this.model.flavour) as Service; const service = this.std.getOptional(
this._service = service; BlockServiceIdentifier(this.model.flavour)
return service; );
this._service = service as Service;
return service as Service;
} }
get topContenteditableElement(): BlockComponent | null { get topContenteditableElement(): BlockComponent | null {

View File

@@ -1,4 +1,5 @@
import type { SurfaceBlockModel } from '@blocksuite/blocks'; import { BlockServiceIdentifier } from '@blocksuite/block-std';
import type { PageRootService, SurfaceBlockModel } from '@blocksuite/blocks';
import type { PointLocation } from '@blocksuite/global/utils'; import type { PointLocation } from '@blocksuite/global/utils';
import { beforeEach, expect, test } from 'vitest'; import { beforeEach, expect, test } from 'vitest';
@@ -24,7 +25,9 @@ const fieldChecker: Record<string, (value: any) => boolean> = {
const skipFields = new Set(['_lastXYWH']); const skipFields = new Set(['_lastXYWH']);
const snapshotTest = async (snapshotUrl: string, elementsCount: number) => { const snapshotTest = async (snapshotUrl: string, elementsCount: number) => {
const pageService = window.editor.host!.std.getService('affine:page'); const pageService = window.editor.host!.std.getOptional(
BlockServiceIdentifier('affine:page')
) as PageRootService;
if (!pageService) { if (!pageService) {
throw new Error('page service not found'); throw new Error('page service not found');
} }