mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-10-28 07:37:36 +09:00
34 lines
987 B
TypeScript
34 lines
987 B
TypeScript
import { selectorIdAttribute } from "@webstudio-is/react-sdk";
|
|
import {
|
|
$instanceContextMenu,
|
|
$textEditingInstanceSelector,
|
|
} from "~/shared/nano-states";
|
|
|
|
export const subscribeInstanceContextMenu = () => {
|
|
const handleContextMenu = (event: MouseEvent) => {
|
|
// Allow native context menu when editing text content
|
|
if ($textEditingInstanceSelector.get() !== undefined) {
|
|
return;
|
|
}
|
|
|
|
const target = event.target as HTMLElement;
|
|
const element = target.closest(`[${selectorIdAttribute}]`);
|
|
const selectorId = element?.getAttribute(selectorIdAttribute);
|
|
|
|
if (selectorId) {
|
|
event.preventDefault();
|
|
const instanceSelector = selectorId.split(",");
|
|
$instanceContextMenu.set({
|
|
position: { x: event.clientX, y: event.clientY },
|
|
instanceSelector,
|
|
});
|
|
}
|
|
};
|
|
|
|
document.addEventListener("contextmenu", handleContextMenu);
|
|
|
|
return () => {
|
|
document.removeEventListener("contextmenu", handleContextMenu);
|
|
};
|
|
};
|