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:
doouding
2025-04-22 08:18:23 +00:00
parent 52953ce8e3
commit 21bf009553
6 changed files with 92 additions and 23 deletions
@@ -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']