view.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { NodeView } from '@antv/x6';
  2. import { isVue2, isVue3, createApp, h, Vue2 } from 'vue-demi';
  3. import { shapeMaps } from './registry';
  4. import { isActive, connect, disconnect } from './teleport';
  5. export class VueShapeView extends NodeView {
  6. getComponentContainer() {
  7. return this.selectors && this.selectors.foContent;
  8. }
  9. confirmUpdate(flag) {
  10. const ret = super.confirmUpdate(flag);
  11. return this.handleAction(ret, VueShapeView.action, () => {
  12. this.renderVueComponent();
  13. });
  14. }
  15. targetId() {
  16. return `${this.graph.view.cid}:${this.cell.id}`;
  17. }
  18. renderVueComponent() {
  19. this.unmountVueComponent();
  20. const root = this.getComponentContainer();
  21. const node = this.cell;
  22. const graph = this.graph;
  23. if (root) {
  24. const { component } = shapeMaps[node.shape];
  25. if (component) {
  26. if (isVue2) {
  27. const Vue = Vue2;
  28. this.vm = new Vue({
  29. el: root,
  30. render(h) {
  31. return h(component, { node, graph });
  32. },
  33. provide() {
  34. return {
  35. getNode: () => node,
  36. getGraph: () => graph,
  37. };
  38. },
  39. });
  40. }
  41. else if (isVue3) {
  42. if (isActive()) {
  43. connect(this.targetId(), component, root, node, graph);
  44. }
  45. else {
  46. this.vm = createApp({
  47. render() {
  48. return h(component, { node, graph });
  49. },
  50. provide() {
  51. return {
  52. getNode: () => node,
  53. getGraph: () => graph,
  54. };
  55. },
  56. });
  57. this.vm.mount(root);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. unmountVueComponent() {
  64. const root = this.getComponentContainer();
  65. if (this.vm) {
  66. isVue2 && this.vm.$destroy();
  67. isVue3 && this.vm.unmount();
  68. this.vm = null;
  69. }
  70. if (root) {
  71. root.innerHTML = '';
  72. }
  73. return root;
  74. }
  75. onMouseDown(e, x, y) {
  76. const target = e.target;
  77. const tagName = target.tagName.toLowerCase();
  78. if (tagName === 'input') {
  79. const type = target.getAttribute('type');
  80. if (type == null ||
  81. [
  82. 'text',
  83. 'password',
  84. 'number',
  85. 'email',
  86. 'search',
  87. 'tel',
  88. 'url',
  89. ].includes(type)) {
  90. return;
  91. }
  92. }
  93. super.onMouseDown(e, x, y);
  94. }
  95. unmount() {
  96. if (isActive()) {
  97. disconnect(this.targetId());
  98. }
  99. this.unmountVueComponent();
  100. super.unmount();
  101. return this;
  102. }
  103. }
  104. (function (VueShapeView) {
  105. VueShapeView.action = 'vue';
  106. VueShapeView.config({
  107. bootstrap: [VueShapeView.action],
  108. actions: {
  109. component: VueShapeView.action,
  110. },
  111. });
  112. NodeView.registry.register('vue-shape-view', VueShapeView, true);
  113. })(VueShapeView || (VueShapeView = {}));
  114. //# sourceMappingURL=view.js.map