Files
AFFiNE-Mirror/blocksuite/affine/ext-loader/src/view-provider.ts
T
Saul-Mirone a6d0894ce1 feat(editor): bookmark and attachment extension (#11824)
Closes: BS-3188
Closes: BS-3189
2025-04-21 04:27:18 +00:00

99 lines
2.8 KiB
TypeScript

import {
BaseExtensionProvider,
type Context,
type Empty,
} from './base-provider';
/**
* Available view scopes for view-related extensions.
* Defines the different types of views where extensions can be applied.
*/
export type ViewScope =
| 'page' // Standard page view
| 'edgeless' // Edgeless (whiteboard) view
| 'preview-page' // Page preview view
| 'preview-edgeless' // Edgeless preview view
| 'mobile-page' // Mobile page view
| 'mobile-edgeless'; // Mobile edgeless view
/**
* A specialized extension provider for view-related functionality.
* Extends the base provider with view-specific scope and configuration.
*
* @typeParam Options - The type of configuration options for the view provider
*
* @example
* ```ts
* // Create a view provider with custom options
* class MyViewProvider extends ViewExtensionProvider<{ theme: string }> {
* override name = 'MyViewProvider';
*
* override schema = z.object({
* theme: z.enum(['light', 'dark'])
* });
*
* override setup(context: ViewExtensionContext, options?: { theme: string }) {
* super.setup(context, options);
*
* context.register([CommonExt]);
* if (context.scope === 'page') {
* context.register([PageExt]);
* } else if (context.scope === 'edgeless') {
* context.register([EdgelessExt]);
* }
* if (options?.theme === 'dark') {
* context.register([DarkModeExt]);
* }
* }
*
* // Override effect to run one-time initialization logic
* override effect() {
* // This will only run once per provider class
* console.log('Initializing MyViewProvider');
* }
* }
* ```
*/
export class ViewExtensionProvider<
Options extends object = Empty,
> extends BaseExtensionProvider<ViewScope, Options> {
/** The name of the view extension provider */
override name = 'ViewExtension';
/**
* Static flag to ensure effect is only run once per provider class
* @internal
*/
static effectRunned = false;
/**
* Override this method to implement one-time initialization logic for the provider.
* This method will be called automatically during setup, but only once per provider class.
*
* @example
* ```ts
* override effect() {
* super.effect();
* // Register lit elements
* registerLitElements();
* }
* ```
*/
effect(): void {}
override setup(context: ViewExtensionContext, options?: Options) {
super.setup(context, options);
const constructer = this.constructor as typeof ViewExtensionProvider;
if (!constructer.effectRunned) {
this.effect();
constructer.effectRunned = true;
}
}
}
/**
* Context type specifically for view-related extensions.
* Provides type safety for view extension registration and setup.
*/
export type ViewExtensionContext = Context<ViewScope>;