Skip to content

Commit

Permalink
Show native context menu on input elements
Browse files Browse the repository at this point in the history
  • Loading branch information
newcat committed Jan 13, 2024
1 parent b3d9a04 commit 8b35e1a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 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.prevent="contextMenu.open"
@contextmenu="contextMenu.open"
>
<slot name="background">
<Background />
Expand Down
7 changes: 6 additions & 1 deletion 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,6 +63,11 @@ export function useContextMenu(viewModel: Ref<IBaklavaViewModel>) {
});

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

ev.preventDefault();
show.value = true;
const target = ev.target as Element;
const element = target.closest(".baklava-node");
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 8b35e1a

Please sign in to comment.