Files
upstream-affine/blocksuite/framework/std/src/gfx/interactivity/extension/base.ts
upstream-mirror 702cc698a7
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
snapshot of https://github.com/toeverything/AFFiNE.git (canary, 2026-09-16; blobs > 131072B omitted)
2026-09-16 04:01:20 +05:00

212 lines
5.4 KiB
TypeScript

import { type Container, createIdentifier } from '@blocksuite/global/di';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Extension } from '@blocksuite/store';
import type { GfxController } from '../../controller.js';
import { GfxControllerIdentifier } from '../../identifiers.js';
import type { GfxModel } from '../../model/model.js';
import type { GfxInteractivityContext, SupportedEvents } from '../event.js';
import type { ExtensionElementsCloneContext } from '../types/clone.js';
import type {
DragExtensionInitializeContext,
ExtensionDragEndContext,
ExtensionDragMoveContext,
ExtensionDragStartContext,
} from '../types/drag.js';
import type {
ExtensionElementResizeContext,
ExtensionElementResizeEndContext,
ExtensionElementResizeMoveContext,
ExtensionElementResizeStartContext,
} from '../types/resize.js';
import type { ExtensionElementSelectContext } from '../types/select.js';
export const InteractivityExtensionIdentifier =
createIdentifier<InteractivityExtension>('interactivity-extension');
export class InteractivityExtension extends Extension {
static key: string;
get std() {
return this.gfx.std;
}
event: Omit<InteractivityEventAPI, 'emit'> = new InteractivityEventAPI();
action: Omit<InteractivityActionAPI, 'emit'> = new InteractivityActionAPI();
constructor(protected readonly gfx: GfxController) {
super();
}
mounted() {}
/**
* Override this method should call `super.unmounted()`
*/
unmounted() {
this.event.destroy();
this.action.destroy();
}
static override setup(di: Container) {
if (!this.key) {
throw new BlockSuiteError(
ErrorCode.ValueNotExists,
'key is not defined in the InteractivityExtension'
);
}
di.add(
this as unknown as { new (gfx: GfxController): InteractivityExtension },
[GfxControllerIdentifier]
);
di.addImpl(InteractivityExtensionIdentifier(this.key), provider =>
provider.get(this)
);
}
}
export class InteractivityEventAPI {
private readonly _handlersMap = new Map<
SupportedEvents,
((evt: GfxInteractivityContext) => void)[]
>();
on(
eventName: SupportedEvents,
handler: (evt: GfxInteractivityContext) => void
) {
const handlers = this._handlersMap.get(eventName) ?? [];
handlers.push(handler);
this._handlersMap.set(eventName, handlers);
return () => {
const idx = handlers.indexOf(handler);
if (idx > -1) {
handlers.splice(idx, 1);
}
};
}
emit(eventName: SupportedEvents, evt: GfxInteractivityContext) {
const handlers = this._handlersMap.get(eventName);
if (!handlers) {
return;
}
for (const handler of handlers) {
handler(evt);
}
}
destroy() {
this._handlersMap.clear();
}
}
export type ActionContextMap = {
dragInitialize: {
context: DragExtensionInitializeContext;
returnType: {
onDragStart?: (context: ExtensionDragStartContext) => void;
onDragMove?: (context: ExtensionDragMoveContext) => void;
onDragEnd?: (context: ExtensionDragEndContext) => void;
clear?: () => void;
};
};
elementsClone: {
context: ExtensionElementsCloneContext;
returnType: Promise<
| {
elements: GfxModel[];
}
| undefined
>;
};
elementResize: {
context: ExtensionElementResizeContext;
returnType: {
onResizeStart?: (context: ExtensionElementResizeStartContext) => void;
onResizeMove?: (context: ExtensionElementResizeMoveContext) => void;
onResizeEnd?: (context: ExtensionElementResizeEndContext) => void;
};
};
elementSelect: {
context: ExtensionElementSelectContext;
returnType: void;
};
};
export class InteractivityActionAPI {
private readonly _handlers: Partial<{
[K in keyof ActionContextMap]: (
ctx: ActionContextMap[K]['context']
) => ActionContextMap[K]['returnType'];
}> = {};
onDragInitialize(
handler: (
ctx: ActionContextMap['dragInitialize']['context']
) => ActionContextMap['dragInitialize']['returnType']
) {
this._handlers['dragInitialize'] = handler;
return () => {
delete this._handlers['dragInitialize'];
};
}
onElementResize(
handler: (
ctx: ActionContextMap['elementResize']['context']
) => ActionContextMap['elementResize']['returnType']
) {
this._handlers['elementResize'] = handler;
return () => {
return delete this._handlers['elementResize'];
};
}
onRequestElementsClone(
handler: (
ctx: ActionContextMap['elementsClone']['context']
) => ActionContextMap['elementsClone']['returnType']
) {
this._handlers['elementsClone'] = handler;
return () => {
return delete this._handlers['elementsClone'];
};
}
onElementSelect(
handler: (
ctx: ActionContextMap['elementSelect']['context']
) => ActionContextMap['elementSelect']['returnType']
) {
this._handlers['elementSelect'] = handler;
return () => {
return delete this._handlers['elementSelect'];
};
}
emit<K extends keyof ActionContextMap>(
event: K,
context: ActionContextMap[K]['context']
): ActionContextMap[K]['returnType'] | undefined {
const handler = this._handlers[event];
return handler?.(context);
}
destroy() {
for (const key in this._handlers) {
delete this._handlers[key as keyof typeof this._handlers];
}
}
}