context-menu-tool.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {ToolsView} from "@antv/x6";
  2. import {createApp} from "vue";
  3. import contextMenuTool from './context-menu-tool.vue'
  4. let app: any = null
  5. let timer: any = null;
  6. const ID = 'graph-dropdown'
  7. if (!document.getElementById(ID)) {
  8. const dom = document.createElement('div')
  9. dom.id = ID
  10. document.body.appendChild(dom)
  11. }
  12. class ContextMenuTool extends ToolsView.ToolItem {
  13. toggleContextMenu(visible, pos = {x: 0, y: 0}) {
  14. const dom = document.getElementById(ID)
  15. if (app) {
  16. // 清空上次内容
  17. app.unmount();
  18. if (dom) {
  19. dom.innerHTML = '';
  20. }
  21. app = null
  22. }
  23. document.removeEventListener('mousedown', this.onMouseDown)
  24. if (visible && pos) {
  25. console.log(this.options)
  26. app = createApp(contextMenuTool, {
  27. data: this.options.data,
  28. onClose: this.onMouseDown
  29. })
  30. // 减去本身元素的宽高
  31. if (dom) {
  32. dom.style = `left: ${pos.x}px;top: ${pos.y}px;position: absolute;z-index: 100;`
  33. }
  34. app.mount(`#${ID}`)
  35. document.addEventListener('mousedown', this.onMouseDown)
  36. }
  37. }
  38. onMouseDown = (e, bool = false) => {
  39. if (bool || !e.target.getAttribute('context-menu-tools')) {
  40. timer = window.setTimeout(() => {
  41. this.toggleContextMenu(false)
  42. },200)
  43. }
  44. }
  45. onContextMenu({ e }) {
  46. if (timer) {
  47. clearTimeout(timer)
  48. timer = 0
  49. }
  50. this.toggleContextMenu(true, { x: e.pageX, y: e.pageY })
  51. }
  52. delegateEvents() {
  53. this.cellView.on('cell:contextmenu', this.onContextMenu, this)
  54. return super.delegateEvents()
  55. }
  56. onRemove() {
  57. this.cellView.off('cell:contextmenu', this.onContextMenu, this)
  58. }
  59. }
  60. ContextMenuTool.config({
  61. tagName: 'div',
  62. isSVGElement: false,
  63. data: {},
  64. })
  65. export {
  66. ContextMenuTool
  67. }