Skip to content

Commit aee10c3

Browse files
committed
fix: 修复空格键进入节点编辑状态的用户退出编辑状态时,左键框选和点击失灵的问题。
1 parent 724157c commit aee10c3

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

app/src/core/service/controlService/keyboardOnlyEngine/keyboardOnlyEngine.tsx

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { toast } from "sonner";
1212
@service("keyboardOnlyEngine")
1313
export class KeyboardOnlyEngine {
1414
constructor(private readonly project: Project) {
15-
this.bindKeyEvents();
15+
this.project.canvas.element.addEventListener("keydown", this.onKeyDown);
16+
this.project.canvas.element.addEventListener("keyup", this.onKeyUp);
1617
}
1718

1819
/**
@@ -26,59 +27,64 @@ export class KeyboardOnlyEngine {
2627
return this.openning;
2728
}
2829

29-
/**
30-
* 开始绑定按键事件
31-
* 仅在最开始调用一次
32-
*/
33-
private bindKeyEvents() {
34-
const startEditNode = (event: KeyboardEvent, selectedNode: TextNode) => {
35-
event.preventDefault(); // 这个prevent必须开启,否则会立刻在刚创建的输入框里输入一个换行符。
36-
this.addSuccessEffect();
37-
// 编辑节点
38-
setTimeout(() => {
39-
this.project.controllerUtils.editTextNode(selectedNode, Settings.textNodeSelectAllWhenStartEditByKeyboard);
40-
}, 1); // 上面的prevent似乎不生效了,但这里加个1毫秒就能解决了
41-
};
30+
public dispose() {
31+
// 销毁服务
32+
this.project.canvas.element.removeEventListener("keydown", this.onKeyDown);
33+
this.project.canvas.element.removeEventListener("keyup", this.onKeyUp);
34+
}
4235

43-
this.project.canvas.element.addEventListener("keydown", (event) => {
44-
if (event.key === "Enter") {
45-
const enterKeyDetail = getEnterKey(event);
46-
if (Settings.textNodeStartEditMode === enterKeyDetail) {
47-
// 这个还必须在down的位置上,因为在up上会导致无限触发
48-
const selectedNode = this.project.stageManager.getTextNodes().find((node) => node.isSelected);
49-
if (!selectedNode) return;
50-
startEditNode(event, selectedNode);
51-
} else {
52-
// 用户可能记错了快捷键
53-
this.addFailEffect();
54-
}
55-
} else if (event.key === " ") {
56-
if (Settings.textNodeStartEditMode === "space") {
57-
// 用户设置了空格键进入节点编辑状态(3群用户:神奈川)
58-
const selectedNode = this.project.stageManager.getTextNodes().find((node) => node.isSelected);
59-
if (!selectedNode) return;
60-
if (this.project.controller.isMouseDown[0]) {
61-
// 不要在可能拖动节点的情况下按空格
62-
toast.warning("请不要在拖动节点的过程中按空格");
63-
return;
64-
}
65-
startEditNode(event, selectedNode);
66-
}
67-
} else if (event.key === "Escape") {
68-
// 取消全部选择
69-
for (const stageObject of this.project.stageManager.getStageObjects()) {
70-
stageObject.isSelected = false;
36+
private startEditNode = (event: KeyboardEvent, selectedNode: TextNode) => {
37+
event.preventDefault(); // 这个prevent必须开启,否则会立刻在刚创建的输入框里输入一个换行符。
38+
this.addSuccessEffect();
39+
// 编辑节点
40+
setTimeout(() => {
41+
this.project.controllerUtils.editTextNode(selectedNode, Settings.textNodeSelectAllWhenStartEditByKeyboard);
42+
}, 1); // 上面的prevent似乎不生效了,但这里加个1毫秒就能解决了
43+
};
44+
45+
private onKeyUp = (event: KeyboardEvent) => {
46+
// 把空格键进入节点编辑状态的时机绑定到keyup上,这样就巧妙的解决了退出编辑状态后左键框选和点击失灵的问题。
47+
if (event.key === " ") {
48+
if (Settings.textNodeStartEditMode === "space") {
49+
// 用户设置了空格键进入节点编辑状态(3群用户:神奈川)
50+
const selectedNode = this.project.stageManager.getTextNodes().find((node) => node.isSelected);
51+
if (!selectedNode) return;
52+
if (this.project.controller.isMouseDown[0]) {
53+
// 不要在可能拖动节点的情况下按空格
54+
toast.warning("请不要在拖动节点的过程中按空格");
55+
return;
7156
}
72-
} else if (event.key === "F2") {
57+
this.startEditNode(event, selectedNode);
58+
}
59+
}
60+
};
61+
62+
private onKeyDown = (event: KeyboardEvent) => {
63+
if (event.key === "Enter") {
64+
const enterKeyDetail = getEnterKey(event);
65+
if (Settings.textNodeStartEditMode === enterKeyDetail) {
66+
// 这个还必须在down的位置上,因为在up上会导致无限触发
7367
const selectedNode = this.project.stageManager.getTextNodes().find((node) => node.isSelected);
7468
if (!selectedNode) return;
75-
// 编辑节点
76-
this.project.controllerUtils.editTextNode(selectedNode);
69+
this.startEditNode(event, selectedNode);
7770
} else {
78-
// SelectChangeEngine.listenKeyDown(event);
71+
// 用户可能记错了快捷键
72+
this.addFailEffect();
7973
}
80-
});
81-
}
74+
} else if (event.key === "Escape") {
75+
// 取消全部选择
76+
for (const stageObject of this.project.stageManager.getStageObjects()) {
77+
stageObject.isSelected = false;
78+
}
79+
} else if (event.key === "F2") {
80+
const selectedNode = this.project.stageManager.getTextNodes().find((node) => node.isSelected);
81+
if (!selectedNode) return;
82+
// 编辑节点
83+
this.project.controllerUtils.editTextNode(selectedNode);
84+
} else {
85+
// SelectChangeEngine.listenKeyDown(event);
86+
}
87+
};
8288

8389
private addSuccessEffect() {
8490
const textNodes = this.project.stageManager.getTextNodes().filter((textNode) => textNode.isSelected);

0 commit comments

Comments
 (0)