createContextMenu.ts 1.8 KB
Newer Older
V
Vben 已提交
1
import contextMenuVue from './ContextMenu.vue';
V
vben 已提交
2
import { isClient } from '/@/utils/is';
V
Vben 已提交
3
import { CreateContextOptions, ContextMenuProps } from './typing';
V
vben 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import { createVNode, render } from 'vue';

const menuManager: {
  domList: Element[];
  resolve: Fn;
} = {
  domList: [],
  resolve: () => {},
};

export const createContextMenu = function (options: CreateContextOptions) {
  const { event } = options || {};

  event && event?.preventDefault();

V
Vben 已提交
19 20 21
  if (!isClient) {
    return;
  }
V
vben 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  return new Promise((resolve) => {
    const body = document.body;

    const container = document.createElement('div');
    const propsData: Partial<ContextMenuProps> = {};
    if (options.styles) {
      propsData.styles = options.styles;
    }

    if (options.items) {
      propsData.items = options.items;
    }

    if (options.event) {
      propsData.customEvent = event;
      propsData.axis = { x: event.clientX, y: event.clientY };
    }

    const vm = createVNode(contextMenuVue, propsData);
    render(vm, container);

    const handleClick = function () {
      menuManager.resolve('');
    };

    menuManager.domList.push(container);

    const remove = function () {
      menuManager.domList.forEach((dom: Element) => {
        try {
          dom && body.removeChild(dom);
V
vben 已提交
53 54 55
        } catch (error) {
          //
        }
V
vben 已提交
56 57 58 59 60
      });
      body.removeEventListener('click', handleClick);
      body.removeEventListener('scroll', handleClick);
    };

V
Vben 已提交
61
    menuManager.resolve = function (arg) {
V
vben 已提交
62
      remove();
V
Vben 已提交
63
      resolve(arg);
V
vben 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    };
    remove();
    body.appendChild(container);
    body.addEventListener('click', handleClick);
    body.addEventListener('scroll', handleClick);
  });
};

export const destroyContextMenu = function () {
  if (menuManager) {
    menuManager.resolve('');
    menuManager.domList = [];
  }
};