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(ContextMenu): Context menu not working on node #358

Merged
merged 3 commits into from
Jan 13, 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
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);
}