mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
@@ -94,6 +94,9 @@ export {
|
||||
type GfxToolsFullOptionValue,
|
||||
type GfxToolsMap,
|
||||
type GfxToolsOption,
|
||||
type ToolOptions,
|
||||
type ToolOptionWithType,
|
||||
type ToolType,
|
||||
} from './tool/tool.js';
|
||||
export { MouseButton, ToolController } from './tool/tool-controller.js';
|
||||
export {
|
||||
|
||||
@@ -10,10 +10,10 @@ import type { GfxController } from '../controller.js';
|
||||
import { GfxExtension, GfxExtensionIdentifier } from '../extension.js';
|
||||
import {
|
||||
type BaseTool,
|
||||
type GfxToolsFullOptionValue,
|
||||
type GfxToolsMap,
|
||||
type GfxToolsOption,
|
||||
ToolIdentifier,
|
||||
type ToolOptions,
|
||||
type ToolOptionWithType,
|
||||
type ToolType,
|
||||
} from './tool.js';
|
||||
|
||||
type BuiltInHookEvent<T> = {
|
||||
@@ -23,9 +23,9 @@ type BuiltInHookEvent<T> = {
|
||||
|
||||
type BuiltInEventMap = {
|
||||
beforeToolUpdate: BuiltInHookEvent<{
|
||||
toolName: keyof GfxToolsMap;
|
||||
toolName: string;
|
||||
}>;
|
||||
toolUpdate: BuiltInHookEvent<{ toolName: keyof GfxToolsMap }>;
|
||||
toolUpdate: BuiltInHookEvent<{ toolName: string }>;
|
||||
};
|
||||
|
||||
type BuiltInSlotContext = {
|
||||
@@ -84,13 +84,11 @@ export class ToolController extends GfxExtension {
|
||||
|
||||
private readonly _disposableGroup = new DisposableGroup();
|
||||
|
||||
private readonly _toolOption$ = new Signal<GfxToolsFullOptionValue>(
|
||||
{} as GfxToolsFullOptionValue
|
||||
);
|
||||
private readonly _toolOption$ = new Signal<ToolOptionWithType>({});
|
||||
|
||||
private readonly _tools = new Map<string, BaseTool>();
|
||||
|
||||
readonly currentToolName$ = new Signal<keyof GfxToolsMap>();
|
||||
readonly currentToolName$ = new Signal<string>();
|
||||
|
||||
readonly dragging$ = new Signal<boolean>(false);
|
||||
|
||||
@@ -157,23 +155,11 @@ export class ToolController extends GfxExtension {
|
||||
|
||||
return {
|
||||
peek() {
|
||||
const option = self._toolOption$.peek() as unknown as { type: string };
|
||||
|
||||
if (!option.type) {
|
||||
option.type = '';
|
||||
}
|
||||
|
||||
return option as GfxToolsFullOptionValue;
|
||||
return self._toolOption$.peek();
|
||||
},
|
||||
|
||||
get value(): GfxToolsFullOptionValue {
|
||||
const option = self._toolOption$.value as unknown as { type: string };
|
||||
|
||||
if (!option.type) {
|
||||
option.type = '';
|
||||
}
|
||||
|
||||
return option as GfxToolsFullOptionValue;
|
||||
get value() {
|
||||
return self._toolOption$.value;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -481,9 +467,16 @@ export class ToolController extends GfxExtension {
|
||||
tools.mounted();
|
||||
}
|
||||
|
||||
get<K extends keyof GfxToolsMap>(key: K): GfxToolsMap[K] {
|
||||
return this._tools.get(key) as GfxToolsMap[K];
|
||||
}
|
||||
get = <T extends BaseTool>(type: ToolType<T>): T => {
|
||||
const instance = this._tools.get(type.toolName) as T | undefined;
|
||||
if (!instance) {
|
||||
throw new BlockSuiteError(
|
||||
BlockSuiteError.ErrorCode.ValueNotExists,
|
||||
`Trying to get tool "${type.toolName}" is not registered`
|
||||
);
|
||||
}
|
||||
return instance;
|
||||
};
|
||||
|
||||
override mounted(): void {
|
||||
const { addHook } = this._initializeEvents();
|
||||
@@ -499,24 +492,11 @@ export class ToolController extends GfxExtension {
|
||||
});
|
||||
}
|
||||
|
||||
setTool(toolName: GfxToolsFullOptionValue, ...args: [void]): void;
|
||||
setTool<K extends keyof GfxToolsMap>(
|
||||
toolName: K,
|
||||
...args: K extends keyof GfxToolsOption
|
||||
? [option: GfxToolsOption[K]]
|
||||
: [void]
|
||||
): void;
|
||||
setTool<K extends keyof GfxToolsMap>(
|
||||
toolName: K | GfxToolsFullOptionValue,
|
||||
...args: K extends keyof GfxToolsOption
|
||||
? [option: GfxToolsOption[K]]
|
||||
: [void]
|
||||
): void {
|
||||
const option = typeof toolName === 'string' ? args[0] : toolName;
|
||||
const toolNameStr =
|
||||
typeof toolName === 'string'
|
||||
? toolName
|
||||
: ((toolName as { type: string }).type as K);
|
||||
setTool = <T extends BaseTool>(
|
||||
toolType: ToolType<T>,
|
||||
options?: ToolOptions<T>
|
||||
): void => {
|
||||
const toolNameStr = toolType.toolName;
|
||||
|
||||
const beforeUpdateCtx = this._createBuiltInHookCtx('beforeToolUpdate', {
|
||||
toolName: toolNameStr,
|
||||
@@ -541,18 +521,18 @@ export class ToolController extends GfxExtension {
|
||||
);
|
||||
}
|
||||
|
||||
currentTool.activatedOption = option ?? {};
|
||||
currentTool.activatedOption = options ?? {};
|
||||
this._toolOption$.value = {
|
||||
...currentTool.activatedOption,
|
||||
type: toolNameStr,
|
||||
} as GfxToolsFullOptionValue;
|
||||
toolType,
|
||||
options: currentTool.activatedOption,
|
||||
};
|
||||
currentTool.activate(currentTool.activatedOption);
|
||||
|
||||
const afterUpdateCtx = this._createBuiltInHookCtx('toolUpdate', {
|
||||
toolName: toolNameStr,
|
||||
});
|
||||
this._builtInHookSlot.next(afterUpdateCtx.slotCtx);
|
||||
}
|
||||
};
|
||||
|
||||
override unmounted(): void {
|
||||
this.currentTool$.peek()?.deactivate();
|
||||
|
||||
@@ -111,6 +111,19 @@ export abstract class BaseTool<
|
||||
|
||||
export const ToolIdentifier = createIdentifier<BaseTool>('GfxTool');
|
||||
|
||||
export type ToolType<T extends BaseTool = BaseTool> = {
|
||||
new (gfx: GfxController): T;
|
||||
toolName: string;
|
||||
};
|
||||
|
||||
export type ToolOptions<T extends BaseTool> =
|
||||
T extends BaseTool<infer O> ? O : never;
|
||||
|
||||
export type ToolOptionWithType<T extends BaseTool = BaseTool> = {
|
||||
toolType?: ToolType<T>;
|
||||
options?: ToolOptions<T>;
|
||||
};
|
||||
|
||||
export interface GfxToolsMap {}
|
||||
|
||||
export interface GfxToolsOption {}
|
||||
|
||||
Reference in New Issue
Block a user