Skip to content

Commit

Permalink
style: rename tool interface hook
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Mar 1, 2024
1 parent 475042e commit 7be38bd
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 66 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_drag_canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ export class DragCanvasTool implements ITool {
cursor: ICursor = 'grab';

constructor(private editor: Editor) {}
active() {
onActive() {
this.editor.canvasDragger.disableDragBySpace();
this.editor.canvasDragger.active();
}
inactive() {
onInactive() {
this.editor.canvasDragger.inactive();
this.editor.canvasDragger.enableDragBySpace();
}
moveExcludeDrag() {
// noop
}
start() {
onStart() {
// noop
}
drag() {
onDrag() {
// noop
}
end() {
onEnd() {
// noop
}
afterEnd() {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_draw_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class DrawGraphTool implements ITool {
private unbindEvent: () => void = noop;

constructor(protected editor: Editor) {}
active() {
onActive() {
const editor = this.editor;
const hotkeysManager = editor.hostEventManager;
const updateRect = () => {
Expand Down Expand Up @@ -82,14 +82,14 @@ export abstract class DrawGraphTool implements ITool {
hotkeysManager.off('spaceToggle', handleSpaceToggle);
};
}
inactive() {
onInactive() {
this.unbindEvent();
}
moveExcludeDrag() {
// do nothing;
}

start(e: PointerEvent) {
onStart(e: PointerEvent) {
this.startPoint = this.editor.getSceneCursorXY(
e,
this.editor.setting.get('snapToPixelGrid'),
Expand All @@ -100,7 +100,7 @@ export abstract class DrawGraphTool implements ITool {
this.lastDragPointWhenSpaceDown = null;
}

drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
this.editor.hostEventManager.disableDelete();
this.editor.hostEventManager.disableContextmenu();
if (this.editor.hostEventManager.isDraggingCanvasBySpace) {
Expand Down Expand Up @@ -214,7 +214,7 @@ export abstract class DrawGraphTool implements ITool {
sceneGraph.render();
}

end(e: PointerEvent) {
onEnd(e: PointerEvent) {
if (this.editor.hostEventManager.isDraggingCanvasBySpace) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_draw_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export class DrawPathTool implements ITool {
private currCursorScenePoint: IPoint | null = null;

constructor(private editor: Editor) {}
active() {
onActive() {
if (this.editor.pathEditor.getActive()) {
this.path = this.editor.pathEditor.getPath()!;
this.pathIdx = this.path.pathData.length;
}
}
inactive() {
onInactive() {
this.editor.commandManager.batchCommandEnd();

this.editor.pathEditor.updateControlHandles();
Expand All @@ -58,7 +58,7 @@ export class DrawPathTool implements ITool {
}
}

start(e: PointerEvent) {
onStart(e: PointerEvent) {
const point = this.editor.getSceneCursorXY(e);
const pathEditor = this.editor.pathEditor;
if (this.editor.setting.get('snapToPixelGrid')) {
Expand Down Expand Up @@ -147,7 +147,7 @@ export class DrawPathTool implements ITool {
this.editor.render();
}

drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
if (!this.startPoint) {
console.warn('startPoint is null, check start()');
return;
Expand All @@ -174,7 +174,7 @@ export class DrawPathTool implements ITool {
this.editor.render();
}

end() {
onEnd() {
this.editor.commandManager.pushCommand(
new SetGraphsAttrsCmd(
'Update Path Data',
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_draw_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export class DrawTextTool implements ITool {
cursor: ICursor = 'crosshair';

constructor(private editor: Editor) {}
active() {
onActive() {
// noop
}
inactive() {
onInactive() {
// noop
}
moveExcludeDrag() {
// do nothing
}
start() {
onStart() {
// do nothing
}
drag() {
onDrag() {
// do nothing
}

end(e: PointerEvent) {
onEnd(e: PointerEvent) {
const { x, y } = this.editor.getCursorXY(e);

// 让一个 input 元素出现在光标位置,然后输入内容回车。
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/tools/tool_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class ToolManager {
throw new Error('there is no active tool');
}
startPos = { x: e.clientX, y: e.clientY };
this.currentTool.start(e);
this.currentTool.onStart(e);
});
};
const handleMove = (e: PointerEvent) => {
Expand All @@ -158,7 +158,7 @@ export class ToolManager {
if (this._isDragging) {
this.enableSwitchTool = false;
this.editor.canvasDragger.disableDragBySpace();
this.currentTool.drag(e);
this.currentTool.onDrag(e);
}
} else {
const isOutsideCanvas = this.editor.canvasElement !== e.target;
Expand All @@ -178,7 +178,7 @@ export class ToolManager {
if (isPressing) {
this.editor.canvasDragger.enableDragBySpace();
isPressing = false;
this.currentTool.end(e, this._isDragging);
this.currentTool.onEnd(e, this._isDragging);
this.currentTool.afterEnd(e);
}

Expand Down Expand Up @@ -231,9 +231,9 @@ export class ToolManager {
}
const currentTool = (this.currentTool = new currentToolCtor(this.editor));

prevTool && prevTool.inactive();
prevTool && prevTool.onInactive();
this.setCursorWhenActive();
currentTool.active();
currentTool.onActive();
this.eventEmitter.emit('switchTool', currentTool.type);
}
on<K extends keyof Events>(eventName: K, handler: Events[K]) {
Expand All @@ -243,7 +243,7 @@ export class ToolManager {
this.eventEmitter.off(eventName, handler);
}
destroy() {
this.currentTool?.inactive();
this.currentTool?.onInactive();
}
setCursorWhenActive() {
if (this.currentTool) {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_path_select/tool_path_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class PathSelectTool implements ITool {
private indexInfo: Readonly<ISelectedIdxInfo> | null = null;

constructor(private editor: Editor) {}
active() {
onActive() {
console.log('路径选择工具');
}
inactive() {
onInactive() {
// noop
}
start(e: PointerEvent) {
onStart(e: PointerEvent) {
const startPoint = this.editor.getSceneCursorXY(e);

const control =
Expand All @@ -54,7 +54,7 @@ export class PathSelectTool implements ITool {
}
}
}
drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
const point = this.editor.getSceneCursorXY(e);
if (this.startPoint) {
const path = this.editor.pathEditor.getPath()!;
Expand All @@ -68,7 +68,7 @@ export class PathSelectTool implements ITool {
}
}
}
end() {
onEnd() {
const path = this.editor.pathEditor.getPath()!;
this.editor.commandManager.pushCommand(
new SetGraphsAttrsCmd(
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/tools/tool_select/tool_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export class SelectTool implements ITool {
// TODO: resetHoverItem after drag canvas end
};

active() {
onActive() {
this.editor.selectedElements.on(
'hoverItemChange',
this.handleHoverItemChange,
);
this.editor.hostEventManager.on('spaceToggle', this.handleSpaceToggle);
}
inactive() {
onInactive() {
this.editor.selectedElements.off(
'hoverItemChange',
this.handleHoverItemChange,
Expand Down Expand Up @@ -101,7 +101,7 @@ export class SelectTool implements ITool {
}
}, 20);

start(e: PointerEvent) {
onStart(e: PointerEvent) {
this.currStrategy = null;
this.topHitElementWhenStart = null;
this.isDragHappened = false;
Expand Down Expand Up @@ -167,26 +167,26 @@ export class SelectTool implements ITool {
}

if (this.currStrategy) {
this.currStrategy.active();
this.currStrategy.start(e);
this.currStrategy.onActive();
this.currStrategy.onStart(e);
} else {
throw new Error('没有根据判断选择策略,代码有问题');
}
}
drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
this.isDragHappened = true;

if (this.editor.hostEventManager.isDraggingCanvasBySpace) {
return;
}

if (this.currStrategy) {
this.currStrategy.drag(e);
this.currStrategy.onDrag(e);
} else {
throw new Error('没有根据判断选择策略,代码有问题');
}
}
end(e: PointerEvent, isDragHappened: boolean) {
onEnd(e: PointerEvent, isDragHappened: boolean) {
this.editor.controlHandleManager.showCustomHandles();

if (this.editor.hostEventManager.isDraggingCanvasBySpace) {
Expand All @@ -200,8 +200,8 @@ export class SelectTool implements ITool {

const currStrategy = this.currStrategy;
if (currStrategy) {
currStrategy.end(e, isDragHappened);
currStrategy.inactive();
currStrategy.onEnd(e, isDragHappened);
currStrategy.onInactive();
} else {
throw new Error('没有根据判断选择策略,代码有问题');
}
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_select/tool_select_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class SelectMoveTool implements IBaseTool {
unbindEvents = noop;

constructor(private editor: Editor) {}
active() {
onActive() {
const hotkeysManager = this.editor.hostEventManager;
const moveWhenToggleShift = () => {
if (this.dragPoint) {
Expand All @@ -33,10 +33,10 @@ export class SelectMoveTool implements IBaseTool {
hotkeysManager.off('shiftToggle', moveWhenToggleShift);
};
}
inactive() {
onInactive() {
this.unbindEvents();
}
start(e: PointerEvent) {
onStart(e: PointerEvent) {
this.editor.controlHandleManager.hideCustomHandles();

this.startPoint = this.editor.getSceneCursorXY(e);
Expand All @@ -61,7 +61,7 @@ export class SelectMoveTool implements IBaseTool {

this.editor.refLine.cacheXYToBbox();
}
drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
this.dragPoint = this.editor.getCursorXY(e);
this.move();
}
Expand Down Expand Up @@ -119,7 +119,7 @@ export class SelectMoveTool implements IBaseTool {

this.editor.sceneGraph.render();
}
end(e: PointerEvent, isDragHappened: boolean) {
onEnd(e: PointerEvent, isDragHappened: boolean) {
const selectedItems = this.editor.selectedElements.getItems({
excludeLocked: true,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tools/tool_select/tool_select_resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class SelectResizeTool implements IBaseTool {

constructor(private editor: Editor) {}

active() {
onActive() {
const handler = () => {
this.resize();
};
Expand All @@ -36,11 +36,11 @@ export class SelectResizeTool implements IBaseTool {
this.editor.hostEventManager.off('altToggle', handler);
};
}
inactive() {
onInactive() {
this.unbind();
this.unbind = noop;
}
start(e: PointerEvent) {
onStart(e: PointerEvent) {
this.startPoint = this.editor.getSceneCursorXY(e);
const handleInfo = this.editor.controlHandleManager.getHandleInfoByPoint(
this.startPoint,
Expand Down Expand Up @@ -71,7 +71,7 @@ export class SelectResizeTool implements IBaseTool {
}
this.handleName = handleInfo.handleName;
}
drag(e: PointerEvent) {
onDrag(e: PointerEvent) {
this.editor.commandManager.disableRedoUndo();
this.editor.hostEventManager.disableDelete();

Expand Down Expand Up @@ -143,7 +143,7 @@ export class SelectResizeTool implements IBaseTool {

this.editor.sceneGraph.render();
}
end(_e: PointerEvent, isDragHappened: boolean) {
onEnd(_e: PointerEvent, isDragHappened: boolean) {
if (this.editor.selectedElements.size() === 0 || !isDragHappened) {
return;
}
Expand Down
Loading

0 comments on commit 7be38bd

Please sign in to comment.