mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
refactor(editor): config the extension provider directly (#12252)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added new export paths to improve module accessibility for foundation store and view components. - Introduced new extension points for telemetry, font configuration, link preview cache, and peek view services in the view extension foundation. - **Improvements** - Enhanced flexibility by allowing optional configuration of placeholders in paragraph view extensions. - Switched to runtime schema validation for font configuration, increasing reliability. - Streamlined service registration for peek view providers. - **Refactor** - Centralized and simplified extension management by removing redundant extension files and consolidating setup logic. - Updated internal integration of telemetry, font, and peek view services for improved maintainability. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -274,7 +274,9 @@
|
||||
"./model": "./src/model/index.ts",
|
||||
"./sync": "./src/sync/index.ts",
|
||||
"./extensions/store": "./src/extensions/store.ts",
|
||||
"./extensions/view": "./src/extensions/view.ts"
|
||||
"./extensions/view": "./src/extensions/view.ts",
|
||||
"./foundation/store": "./src/foundation/store.ts",
|
||||
"./foundation/view": "./src/foundation/view.ts"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
|
||||
1
blocksuite/affine/all/src/foundation/store.ts
Normal file
1
blocksuite/affine/all/src/foundation/store.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from '@blocksuite/affine-foundation/store';
|
||||
1
blocksuite/affine/all/src/foundation/view.ts
Normal file
1
blocksuite/affine/all/src/foundation/view.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from '@blocksuite/affine-foundation/view';
|
||||
@@ -28,10 +28,9 @@ import { z } from 'zod';
|
||||
import { effects } from './effects';
|
||||
|
||||
const optionsSchema = z.object({
|
||||
getPlaceholder: z
|
||||
.function()
|
||||
.args(z.instanceof(ParagraphBlockModel))
|
||||
.returns(z.string()),
|
||||
getPlaceholder: z.optional(
|
||||
z.function().args(z.instanceof(ParagraphBlockModel)).returns(z.string())
|
||||
),
|
||||
});
|
||||
|
||||
export class ParagraphViewExtension extends ViewExtensionProvider<
|
||||
@@ -50,7 +49,7 @@ export class ParagraphViewExtension extends ViewExtensionProvider<
|
||||
context: ViewExtensionContext,
|
||||
options?: z.infer<typeof optionsSchema>
|
||||
) {
|
||||
super.setup(context);
|
||||
super.setup(context, options);
|
||||
const getPlaceholder =
|
||||
options?.getPlaceholder ?? (model => placeholders[model.props.type]);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ export const PeekViewProvider = createIdentifier<PeekViewService>(
|
||||
export function PeekViewExtension(service: PeekViewService): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(PeekViewProvider, () => service);
|
||||
di.override(PeekViewProvider, () => service);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { FileDropExtension } from '@blocksuite/affine-components/drop-indicator';
|
||||
import {
|
||||
PeekViewExtension,
|
||||
type PeekViewService,
|
||||
} from '@blocksuite/affine-components/peek';
|
||||
import {
|
||||
type ViewExtensionContext,
|
||||
ViewExtensionProvider,
|
||||
@@ -12,28 +16,49 @@ import {
|
||||
EditPropsStore,
|
||||
EmbedOptionService,
|
||||
FileSizeLimitService,
|
||||
FontConfigExtension,
|
||||
fontConfigSchema,
|
||||
FontLoaderService,
|
||||
LinkPreviewCache,
|
||||
LinkPreviewCacheConfigSchema,
|
||||
LinkPreviewCacheExtension,
|
||||
LinkPreviewService,
|
||||
PageViewportServiceExtension,
|
||||
TelemetryExtension,
|
||||
type TelemetryService,
|
||||
ThemeService,
|
||||
ToolbarRegistryExtension,
|
||||
} from '@blocksuite/affine-shared/services';
|
||||
import { InteractivityManager, ToolController } from '@blocksuite/std/gfx';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { clipboardConfigs } from './clipboard';
|
||||
import { effects } from './effects';
|
||||
|
||||
export class FoundationViewExtension extends ViewExtensionProvider {
|
||||
const optionsSchema = z.object({
|
||||
linkPreviewCacheConfig: z.optional(LinkPreviewCacheConfigSchema),
|
||||
fontConfig: z.optional(z.array(fontConfigSchema)),
|
||||
telemetry: z.optional(z.custom<TelemetryService>()),
|
||||
peekView: z.optional(z.custom<PeekViewService>()),
|
||||
});
|
||||
|
||||
export type FoundationViewExtensionOptions = z.infer<typeof optionsSchema>;
|
||||
|
||||
export class FoundationViewExtension extends ViewExtensionProvider<FoundationViewExtensionOptions> {
|
||||
override name = 'foundation';
|
||||
|
||||
override schema = optionsSchema;
|
||||
|
||||
override effect() {
|
||||
super.effect();
|
||||
effects();
|
||||
}
|
||||
|
||||
override setup(context: ViewExtensionContext) {
|
||||
super.setup(context);
|
||||
override setup(
|
||||
context: ViewExtensionContext,
|
||||
options?: FoundationViewExtensionOptions
|
||||
) {
|
||||
super.setup(context, options);
|
||||
context.register([
|
||||
DocDisplayMetaService,
|
||||
EditPropsStore,
|
||||
@@ -56,5 +81,21 @@ export class FoundationViewExtension extends ViewExtensionProvider {
|
||||
if (this.isEdgeless(context.scope)) {
|
||||
context.register([InteractivityManager, ToolController]);
|
||||
}
|
||||
const fontConfig = options?.fontConfig;
|
||||
if (fontConfig) {
|
||||
context.register(FontConfigExtension(fontConfig));
|
||||
}
|
||||
const linkPreviewCacheConfig = options?.linkPreviewCacheConfig;
|
||||
if (linkPreviewCacheConfig) {
|
||||
context.register(LinkPreviewCacheExtension(linkPreviewCacheConfig));
|
||||
}
|
||||
const telemetry = options?.telemetry;
|
||||
if (telemetry) {
|
||||
context.register(TelemetryExtension(telemetry));
|
||||
}
|
||||
const peekView = options?.peekView;
|
||||
if (peekView) {
|
||||
context.register(PeekViewExtension(peekView));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { FontFamily, FontStyle, FontWeight } from '@blocksuite/affine-model';
|
||||
import { z } from 'zod';
|
||||
|
||||
export interface FontConfig {
|
||||
font: string;
|
||||
weight: string;
|
||||
url: string;
|
||||
style: string;
|
||||
}
|
||||
export const fontConfigSchema = z.object({
|
||||
font: z.string(),
|
||||
weight: z.string(),
|
||||
url: z.string(),
|
||||
style: z.string(),
|
||||
});
|
||||
|
||||
export type FontConfig = z.infer<typeof fontConfigSchema>;
|
||||
|
||||
export const AffineCanvasTextFonts: FontConfig[] = [
|
||||
// Inter, https://fonts.cdnfonts.com/css/inter?styles=29139,29134,29135,29136,29140,29141
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
import type { ExtensionType } from '@blocksuite/store';
|
||||
|
||||
import type { OutDatabaseAllEvents } from './database.js';
|
||||
import type { LinkToolbarEvents } from './link.js';
|
||||
@@ -46,3 +47,13 @@ export interface TelemetryService {
|
||||
export const TelemetryProvider = createIdentifier<TelemetryService>(
|
||||
'AffineTelemetryService'
|
||||
);
|
||||
|
||||
export const TelemetryExtension = (
|
||||
service: TelemetryService
|
||||
): ExtensionType => {
|
||||
return {
|
||||
setup: di => {
|
||||
di.override(TelemetryProvider, () => service);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user