Skip to content

Commit

Permalink
Merge pull request #358 from starker-xp/fix-context-menu-on-node
Browse files Browse the repository at this point in the history
fix(ContextMenu): Context menu not working on node
  • Loading branch information
newcat authored Jan 13, 2024
2 parents 8a82bcf + 8b35e1a commit 316b310
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
5 changes: 2 additions & 3 deletions packages/renderer-vue/src/commands/hotkeyHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ref } from "vue";

const INPUT_ELEMENT_TAGS = ["INPUT", "TEXTAREA", "SELECT"];
import { isInputElement } from "../utility";

export function useHotkeyHandler(executeCommand: (name: string) => void) {
const pressedKeys = ref<string[]>([]);
Expand All @@ -11,7 +10,7 @@ export function useHotkeyHandler(executeCommand: (name: string) => void) {
pressedKeys.value.push(ev.key);
}

if (INPUT_ELEMENT_TAGS.includes(document.activeElement?.tagName ?? "")) {
if (document.activeElement && isInputElement(document.activeElement)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/renderer-vue/src/editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@wheel.self="panZoom.onMouseWheel"
@keydown="keyDown"
@keyup="keyUp"
@contextmenu.self.prevent="contextMenu.open"
@contextmenu="contextMenu.open"
>
<slot name="background">
<Background />
Expand Down
22 changes: 19 additions & 3 deletions packages/renderer-vue/src/editor/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Ref, computed, ref, reactive } from "vue";
import { AbstractNode } from "@baklavajs/core";
import { IMenuItem } from "../contextmenu";
import { IBaklavaViewModel } from "../viewModel";
import { useNodeCategories, useTransform } from "../utility";
import { isInputElement, useNodeCategories, useTransform } from "../utility";

export function useContextMenu(viewModel: Ref<IBaklavaViewModel>) {
const show = ref(false);
Expand Down Expand Up @@ -63,9 +63,25 @@ export function useContextMenu(viewModel: Ref<IBaklavaViewModel>) {
});

function open(ev: MouseEvent) {
if (ev.target instanceof Element && isInputElement(ev.target)) {
return;
}

ev.preventDefault();
show.value = true;
x.value = ev.offsetX;
y.value = ev.offsetY;
const target = ev.target as Element;
const element = target.closest(".baklava-node");
if (!element) {
x.value = ev.offsetX;
y.value = ev.offsetY;
return;
}
const bounding = target.getBoundingClientRect();
const editor = document.querySelector(".baklava-editor") as Element;

const editorBounding = editor.getBoundingClientRect();
x.value = bounding.x + ev.offsetX - editorBounding.x;
y.value = bounding.y + ev.offsetY - editorBounding.y;
}

function onClick(value: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/renderer-vue/src/utility/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./isInputElements";
export * from "./nodePosition";
export * from "./useDragMove";
export * from "./useGraph";
Expand Down
5 changes: 5 additions & 0 deletions packages/renderer-vue/src/utility/isInputElements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const INPUT_ELEMENT_TAGS = ["INPUT", "TEXTAREA", "SELECT"];

export function isInputElement(el: Element) {
return INPUT_ELEMENT_TAGS.includes(el.tagName);
}

0 comments on commit 316b310

Please sign in to comment.