editor-modal-drawio.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template lang='pug'>
  2. v-card.editor-modal-drawio.animated.fadeIn(flat, tile)
  3. iframe(
  4. ref='drawio'
  5. src='https://embed.diagrams.net/?embed=1&proto=json&spin=1&saveAndExit=1&noSaveBtn=1&noExitBtn=0'
  6. frameborder='0'
  7. )
  8. </template>
  9. <script>
  10. import { sync, get } from 'vuex-pathify'
  11. // const xmlTest = `<?xml version="1.0" encoding="UTF-8"?>
  12. // <mxfile version="13.4.2">
  13. // <diagram id="SgbkCjxR32CZT1FvBvkp" name="Page-1">
  14. // <mxGraphModel dx="2062" dy="1123" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
  15. // <root>
  16. // <mxCell id="0" />
  17. // <mxCell id="1" parent="0" />
  18. // <mxCell id="5gE3BTvRYS_8FoJnOusC-1" value="" style="whiteSpace=wrap;html=1;aspect=fixed;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
  19. // <mxGeometry x="380" y="530" width="80" height="80" as="geometry" />
  20. // </mxCell>
  21. // </root>
  22. // </mxGraphModel>
  23. // </diagram>
  24. // </mxfile>
  25. // `
  26. export default {
  27. data() {
  28. return {
  29. content: ''
  30. }
  31. },
  32. computed: {
  33. editorKey: get('editor/editorKey'),
  34. activeModal: sync('editor/activeModal')
  35. },
  36. methods: {
  37. close () {
  38. this.activeModal = ''
  39. },
  40. overwriteAndClose() {
  41. this.$root.$emit('overwriteEditorContent')
  42. this.$root.$emit('resetEditorConflict')
  43. this.close()
  44. },
  45. send (msg) {
  46. this.$refs.drawio.contentWindow.postMessage(JSON.stringify(msg), '*')
  47. },
  48. receive (evt) {
  49. if (evt.frame === null || evt.source !== this.$refs.drawio.contentWindow || evt.data.length < 1) {
  50. return
  51. }
  52. try {
  53. const msg = JSON.parse(evt.data)
  54. switch (msg.event) {
  55. case 'init': {
  56. this.send({
  57. action: 'load',
  58. autosave: 0,
  59. modified: 'unsavedChanges',
  60. xml: this.$store.get('editor/activeModalData'),
  61. title: this.$store.get('page/title')
  62. })
  63. this.$store.set('editor/activeModalData', null)
  64. break
  65. }
  66. case 'save': {
  67. if (msg.exit) {
  68. this.send({
  69. action: 'export',
  70. format: 'xmlsvg'
  71. })
  72. }
  73. break
  74. }
  75. case 'export': {
  76. const svgDataStart = msg.data.indexOf('base64,') + 7
  77. this.$root.$emit('editorInsert', {
  78. kind: 'DIAGRAM',
  79. text: msg.data.slice(svgDataStart)
  80. // text: msg.xml.replace(/ agent="(.*?)"/, '').replace(/ host="(.*?)"/, '').replace(/ etag="(.*?)"/, '')
  81. })
  82. this.close()
  83. break
  84. }
  85. case 'exit': {
  86. this.close()
  87. break
  88. }
  89. }
  90. } catch (err) {
  91. console.error(err)
  92. }
  93. }
  94. },
  95. async mounted () {
  96. window.addEventListener('message', this.receive)
  97. },
  98. beforeDestroy () {
  99. window.removeEventListener('message', this.receive)
  100. }
  101. }
  102. </script>
  103. <style lang='scss'>
  104. .editor-modal-drawio {
  105. position: fixed !important;
  106. top: 0;
  107. left: 0;
  108. z-index: 10;
  109. width: 100%;
  110. height: 100vh;
  111. background-color: rgba(255,255,255, 1) !important;
  112. overflow: hidden;
  113. > iframe {
  114. width: 100%;
  115. height: 100vh;
  116. border: 0;
  117. padding: 0;
  118. background-color: #FFF;
  119. }
  120. }
  121. </style>