mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
feat(editor): bookmark and attachment extension (#11824)
Closes: BS-3188 Closes: BS-3189
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
type StoreExtensionContext,
|
||||
StoreExtensionProvider,
|
||||
} from '../store-provider';
|
||||
import { ViewExtensionManager } from '../view-manager';
|
||||
import {
|
||||
type ViewExtensionContext,
|
||||
ViewExtensionProvider,
|
||||
@@ -165,3 +166,56 @@ it('should extension manager be able to be injected', () => {
|
||||
const provider = container.provider();
|
||||
expect(provider.get(StoreExtensionManagerIdentifier)).toBe(manager);
|
||||
});
|
||||
|
||||
it('should effect only run once', () => {
|
||||
const effect1 = vi.fn();
|
||||
const effect2 = vi.fn();
|
||||
class ViewExt1 extends ViewExtensionProvider {
|
||||
override name = 'ViewExt1';
|
||||
|
||||
override effect() {
|
||||
super.effect();
|
||||
effect1();
|
||||
}
|
||||
|
||||
override setup(context: ViewExtensionContext) {
|
||||
super.setup(context);
|
||||
context.register(Ext1);
|
||||
}
|
||||
}
|
||||
|
||||
class ViewExt2 extends ViewExtensionProvider {
|
||||
override name = 'ViewExt2';
|
||||
|
||||
override effect() {
|
||||
super.effect();
|
||||
effect2();
|
||||
}
|
||||
|
||||
override setup(context: ViewExtensionContext) {
|
||||
super.setup(context);
|
||||
context.register(Ext2);
|
||||
}
|
||||
}
|
||||
|
||||
const manager = new ViewExtensionManager([ViewExt1]);
|
||||
|
||||
expect(ViewExt1.effectRunned).toBe(false);
|
||||
expect(ViewExt2.effectRunned).toBe(false);
|
||||
|
||||
manager.get('page');
|
||||
|
||||
expect(ViewExt1.effectRunned).toBe(true);
|
||||
expect(ViewExt2.effectRunned).toBe(false);
|
||||
|
||||
expect(effect1).toHaveBeenCalledTimes(1);
|
||||
expect(effect2).toHaveBeenCalledTimes(0);
|
||||
|
||||
manager.get('edgeless');
|
||||
|
||||
expect(ViewExt1.effectRunned).toBe(true);
|
||||
expect(ViewExt2.effectRunned).toBe(false);
|
||||
|
||||
expect(effect1).toHaveBeenCalledTimes(1);
|
||||
expect(effect2).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
@@ -45,6 +45,12 @@ export type ViewScope =
|
||||
* 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');
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
@@ -53,6 +59,36 @@ export class ViewExtensionProvider<
|
||||
> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user