From c0bbb07824140fa84371e0ec915ffae755ef4d78 Mon Sep 17 00:00:00 2001 From: Yanyan-Wang Date: Tue, 21 Jul 2020 19:09:42 +0800 Subject: [PATCH] fix: Cannot read property getModel of null problem on contextmenu when the target is not an item. closes: #1815. --- CHANGELOG.md | 3 ++- src/plugins/menu/index.ts | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 508c00ba07..564ede48f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ - fix: combo edge with uncorrect end points; - fix: combo polyline edge with wrong path; - fix: getViewCenter with padding problem; -- feat: allow user to configure the initial positions for empty combos. +- feat: allow user to configure the initial positions for empty combos; +- fix: Cannot read property 'getModel' of null problem on contextmenu when the target is not an item. #### 3.5.10 - fix: fitView and fitCenter with animate in the initial state; diff --git a/src/plugins/menu/index.ts b/src/plugins/menu/index.ts index bda0816de7..d082682c8e 100644 --- a/src/plugins/menu/index.ts +++ b/src/plugins/menu/index.ts @@ -46,7 +46,7 @@ export default class Menu extends Base {
  • 菜单项2
  • ` - }, + }, // 菜单隐藏事件 onHide() { return true; @@ -71,7 +71,7 @@ export default class Menu extends Base { protected onMenuShow(e: IG6GraphEvent) { const self = this; - + const container = this.get('menu') const getContent = this.get('getContent') let menu = getContent(e) @@ -94,26 +94,26 @@ export default class Menu extends Base { const bbox = container.getBoundingClientRect(); - let x = e.item.getModel().x; - let y = e.item.getModel().y; + let x = e.item.getModel().x || e.x; + let y = e.item.getModel().y || e.y; // 若菜单超出画布范围,反向 if (x + bbox.width > width) { x = width - bbox.width; } - + if (y + bbox.height > height) { y = height - bbox.height; } - + const point = graph.getClientByPoint(x, y) e.canvasX = point.x; e.canvasY = point.y; modifyCSS(container, { - top: `${point.y}px`, - left: `${point.x}px`, - visibility: 'visible' + top: `${point.y}px`, + left: `${point.x}px`, + visibility: 'visible' }); const handler = (evt) => { @@ -133,7 +133,7 @@ export default class Menu extends Base { // 隐藏菜单后需要移除事件监听 document.body.removeEventListener('click', this.get('handler')); - + const handleMenuClick = this.get('handleMenuClick') if (handleMenuClick) { container.removeEventListener('click', handleMenuClick) @@ -148,7 +148,7 @@ export default class Menu extends Base { if (handleMenuClick) { menu.removeEventListener('click', handleMenuClick) } - + if (menu) { document.body.removeChild(menu); }