deleteItem.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable */
  2. export default function(G6) {
  3. G6.registerBehavior('deleteItem', {
  4. getEvents() {
  5. return {
  6. 'keydown': 'onKeydown',
  7. 'canvas:mouseleave': 'onCanvasLeave',
  8. 'canvas:mouseenter': 'onCanvasFocus',
  9. }
  10. },
  11. onKeydown(e) {
  12. const items = this.graph.get('selectedItems');
  13. const focus = this.graph.get('focusGraphWrapper');
  14. console.log(e.keyCode)
  15. if (e.keyCode === 46 && items && items.length > 0 && focus) {
  16. if (this.graph.executeCommand) {
  17. this.graph.executeCommand('delete', {});
  18. } else {
  19. this.graph.remove(items[0]);
  20. }
  21. this.graph.set('selectedItems', []);
  22. this.graph.emit('afteritemselected', []);
  23. }
  24. if (e.ctrlKey == true && e.keyCode == 90) { //Ctrl+z
  25. e.returnvalue = false;
  26. if (this.graph.executeCommand) {
  27. this.graph.executeCommand('undo', {});
  28. }
  29. }
  30. if (e.ctrlKey == true && e.keyCode == 89) { //Ctrl+y
  31. e.returnvalue = false;
  32. if (this.graph.executeCommand) {
  33. this.graph.executeCommand('redo', {});
  34. }
  35. }
  36. if (e.ctrlKey == true && e.keyCode == 67) { //Ctrl+c
  37. e.returnvalue = false;
  38. if (this.graph.executeCommand) {
  39. this.graph.executeCommand('copy', {});
  40. }
  41. }
  42. if (e.ctrlKey == true && e.keyCode == 86) { //Ctrl+v
  43. e.returnvalue = false;
  44. if (this.graph.executeCommand) {
  45. this.graph.executeCommand('paste', {});
  46. }
  47. }
  48. },
  49. onCanvasLeave(e) {
  50. this.graph.set('focusGraphWrapper', false);
  51. },
  52. onCanvasFocus() {
  53. this.graph.set('focusGraphWrapper', true);
  54. }
  55. });
  56. }