mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
refactor: migrate elements clone from default-tool to extension (#11703)
### Changed - Add `onRequestElementsClone` action to allow extension to define how to clone gfx elements
This commit is contained in:
@@ -5,7 +5,9 @@ import { Extension } from '@blocksuite/store';
|
||||
import type { PointerEventState } from '../../../event/index.js';
|
||||
import type { GfxController } from '../../controller.js';
|
||||
import { GfxControllerIdentifier } from '../../identifiers.js';
|
||||
import type { GfxModel } from '../../model/model.js';
|
||||
import type { SupportedEvents } from '../event.js';
|
||||
import type { ExtensionElementsCloneContext } from '../types/clone.js';
|
||||
import type {
|
||||
DragExtensionInitializeContext,
|
||||
ExtensionDragEndContext,
|
||||
@@ -105,11 +107,22 @@ type ActionContextMap = {
|
||||
clear?: () => void;
|
||||
};
|
||||
};
|
||||
elementsClone: {
|
||||
context: ExtensionElementsCloneContext;
|
||||
returnType: Promise<
|
||||
| {
|
||||
elements: GfxModel[];
|
||||
}
|
||||
| undefined
|
||||
>;
|
||||
};
|
||||
};
|
||||
|
||||
export class InteractivityActionAPI {
|
||||
private readonly _handlers: Partial<{
|
||||
dragInitialize: Parameters<InteractivityActionAPI['onDragInitialize']>[0];
|
||||
[K in keyof ActionContextMap]: (
|
||||
ctx: ActionContextMap[K]['context']
|
||||
) => ActionContextMap[K]['returnType'];
|
||||
}> = {};
|
||||
|
||||
onDragInitialize(
|
||||
@@ -124,6 +137,18 @@ export class InteractivityActionAPI {
|
||||
};
|
||||
}
|
||||
|
||||
onRequestElementsClone(
|
||||
handler: (
|
||||
ctx: ActionContextMap['elementsClone']['context']
|
||||
) => ActionContextMap['elementsClone']['returnType']
|
||||
) {
|
||||
this._handlers['elementsClone'] = handler;
|
||||
|
||||
return () => {
|
||||
return delete this._handlers['elementsClone'];
|
||||
};
|
||||
}
|
||||
|
||||
emit<K extends keyof ActionContextMap>(
|
||||
event: K,
|
||||
context: ActionContextMap[K]['context']
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
InteractivityExtensionIdentifier,
|
||||
} from './extension/base.js';
|
||||
import { GfxViewEventManager } from './gfx-view-event-handler.js';
|
||||
import type { RequestElementsCloneContext } from './types/clone.js';
|
||||
import type {
|
||||
DragExtensionInitializeContext,
|
||||
DragInitializationOption,
|
||||
@@ -288,4 +289,21 @@ export class InteractivityManager extends GfxExtension {
|
||||
listenEvent();
|
||||
dragStart();
|
||||
}
|
||||
|
||||
requestElementsClone(options: RequestElementsCloneContext) {
|
||||
const extensions = this.interactExtensions;
|
||||
|
||||
for (let ext of extensions.values()) {
|
||||
const cloneData = (ext.action as InteractivityActionAPI).emit(
|
||||
'elementsClone',
|
||||
options
|
||||
);
|
||||
|
||||
if (cloneData) {
|
||||
return cloneData;
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { GfxModel } from '../../model/model';
|
||||
|
||||
export type ExtensionElementsCloneContext = {
|
||||
elements: GfxModel[];
|
||||
};
|
||||
|
||||
export type RequestElementsCloneContext = ExtensionElementsCloneContext;
|
||||
Reference in New Issue
Block a user