index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="flow-content">
  3. <iframe ref="myFlow" class="flow-iframe" :src="url"></iframe>
  4. <div v-if="loadIng" class="flow-loading"><w-loading></w-loading></div>
  5. </div>
  6. </template>
  7. <style lang="scss" scoped>
  8. .flow-content {
  9. position: absolute;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. .flow-iframe {
  15. position: absolute;
  16. top: 0;
  17. left: 0;
  18. width: 100%;
  19. height: 100%;
  20. background: 0 0;
  21. border: 0;
  22. float: none;
  23. margin: -1px 0 0;
  24. max-width: none;
  25. outline: 0;
  26. padding: 0;
  27. }
  28. .flow-loading {
  29. position: absolute;
  30. top: 50%;
  31. left: 50%;
  32. transform: translate(-50%, -50%);
  33. }
  34. }
  35. </style>
  36. <script>
  37. import JSPDF from "jspdf";
  38. export default {
  39. name: "Flow",
  40. props: {
  41. value: {
  42. type: Object,
  43. default: function () {
  44. return {}
  45. }
  46. },
  47. readOnly: {
  48. type: Boolean,
  49. default: false
  50. },
  51. },
  52. data() {
  53. return {
  54. loadIng: true,
  55. flow: null,
  56. url: window.location.origin + '/js/grapheditor/' + (this.readOnly ? 'viewer' : 'index') + '.html',
  57. }
  58. },
  59. mounted() {
  60. window.addEventListener('message', this.handleMessage)
  61. this.flow = this.$refs.myFlow.contentWindow;
  62. },
  63. activated() {
  64. window.addEventListener('message', this.handleMessage)
  65. this.flow = this.$refs.myFlow.contentWindow;
  66. },
  67. watch: {
  68. value: {
  69. handler() {
  70. this.updateContent();
  71. },
  72. deep: true
  73. }
  74. },
  75. methods: {
  76. updateContent() {
  77. this.flow.postMessage({
  78. act: 'setXml',
  79. params: Object.assign(this.value, typeof this.value.xml === "undefined" ? {
  80. xml: this.value.content
  81. } : {})
  82. }, '*')
  83. },
  84. handleMessage (event) {
  85. const data = event.data;
  86. switch (data.act) {
  87. case 'ready':
  88. this.loadIng = false;
  89. this.updateContent();
  90. break
  91. case 'change':
  92. this.$emit('input', data.params);
  93. break
  94. case 'save':
  95. this.$emit('saveData');
  96. break
  97. case 'imageContent':
  98. let pdf = new JSPDF({
  99. format: [data.params.width, data.params.height]
  100. });
  101. pdf.addImage(data.params.content, 'PNG', 0, 0, 0, 0);
  102. pdf.save(`${data.params.name}.pdf`);
  103. break
  104. }
  105. },
  106. exportPNG(name, scale = 10) {
  107. this.flow.postMessage({
  108. act: 'exportPNG',
  109. params: {
  110. name: name || this.$L('无标题'),
  111. scale: scale,
  112. type: 'png',
  113. }
  114. }, '*')
  115. },
  116. exportPDF(name, scale = 10) {
  117. this.flow.postMessage({
  118. act: 'exportPNG',
  119. params: {
  120. name: name || this.$L('无标题'),
  121. scale: scale,
  122. type: 'imageContent',
  123. }
  124. }, '*')
  125. }
  126. },
  127. }
  128. </script>