Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(edgeless): tool controller potential problem #8675

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ export class CopilotTool extends BaseTool {

if (useCopilot) {
this.controller.setTool('copilot');
return false;
}

return;
});
}

onContainerPointerDown(e: PointerEventState): void {
override pointerDown(e: PointerEventState): void {
if (this.processing) {
e.raw.stopPropagation();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class PanTool extends BaseTool<PanToolOption> {
dispose();
}
});

return false;
});
}
}
Expand Down
45 changes: 35 additions & 10 deletions packages/framework/block-std/src/gfx/tool/tool-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export enum MouseButton {
}

export interface ToolEventTarget {
/**
* Add a hook before the event is handled by the tool.
* Return false to prevent the tool from handling the event.
* @param evtName
* @param handler
*/
addHook<K extends SupportedHooks | SupportedEvents>(
evtName: K,
handler: (
Expand Down Expand Up @@ -238,6 +244,10 @@ export class ToolController extends GfxExtension {
evtState: PointerEventState | BuiltInSlotContext
) => undefined | boolean)[]
> = {};
/**
* Invoke the hook and the tool handler.
* @returns false if the handler is prevented by the hook
*/
const invokeToolHandler = (
evtName: SupportedEvents,
evt: PointerEventState,
Expand All @@ -248,13 +258,15 @@ export class ToolController extends GfxExtension {
return pre || hook(evt) === false;
}, false);

if (stopHandler || !this.currentTool$.peek()) {
return;
tool = tool ?? this.currentTool$.peek();

if (stopHandler) {
return false;
}

tool = tool ?? this.currentTool$.peek();
try {
tool?.[evtName](evt);
return true;
} catch (e) {
throw new BlockSuiteError(
ErrorCode.ExecutionError,
Expand Down Expand Up @@ -324,13 +336,20 @@ export class ToolController extends GfxExtension {
h: 0,
};

invokeToolHandler('dragStart', evt);
// this means the dragEnd event is not even fired
// so we need to manually call the dragEnd method
if (dragContext?.tool) {
dragContext.tool.dragEnd(evt);
dragContext = null;
}

dragContext = this.currentTool$.peek()
? {
tool: this.currentTool$.peek()!,
}
: null;
if (invokeToolHandler('dragStart', evt)) {
dragContext = this.currentTool$.peek()
? {
tool: this.currentTool$.peek()!,
}
: null;
}
})
);

Expand Down Expand Up @@ -371,7 +390,13 @@ export class ToolController extends GfxExtension {
this.dragging$.value = false;
const evt = ctx.get('pointerState');

invokeToolHandler('dragEnd', evt, dragContext?.tool);
if (!invokeToolHandler('dragEnd', evt, dragContext?.tool)) {
// if the tool dragEnd is prevented by the hook, call the dragEnd method manually
// this guarantee the dragStart and dragEnd events are always called together
if (dragContext?.tool) {
dragContext.tool.dragEnd(evt);
}
}

dragContext = null;
this.draggingViewArea$.value = {
Expand Down
Loading