Preview.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div class="preview" :style="{ background: bgColor }">
  3. <div class="meta2d-canvas" ref="meta2dDom"></div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import { ref, onMounted, watch, onUnmounted } from 'vue';
  8. import localforage from 'localforage';
  9. import { localStorageName } from '@/services/utils';
  10. import { defaultFormat } from '@/services/defaults';
  11. import { useRouter, useRoute } from 'vue-router';
  12. import { Meta2d, Options, Pen } from '@meta2d/core';
  13. import { registerBasicDiagram } from '@/services/register';
  14. import { cdn, getLe5leV } from '@/services/api';
  15. import { getDownloadList, getPayList,getFrameDownloadList, Frame} from '@/services/download';
  16. const route = useRoute();
  17. const meta2dDom = ref('');
  18. const meta2dOptions: Options = {
  19. cdn,
  20. background: '#1e2430',
  21. x: 10,
  22. y: 10,
  23. width: 1920,
  24. color: '#bdc7db',
  25. height: 1080,
  26. defaultFormat: { ...defaultFormat },
  27. };
  28. const bgColor = ref('#1e2430');
  29. onMounted(() => {
  30. meta2d = new Meta2d(meta2dDom.value, meta2dOptions);
  31. registerBasicDiagram();
  32. open();
  33. meta2d.on('opened', opened);
  34. window.addEventListener('message', dealWithMessage);
  35. });
  36. const dealWithMessage = (e) => {
  37. if (typeof e.data !== 'string'||!e.data ||
  38. e.data.startsWith('setImmediate')) {
  39. return;
  40. }
  41. try {
  42. let data = JSON.parse(e.data);
  43. if (typeof data === 'object') {
  44. if (data.type) {
  45. if (data.name === 'downloadHtml') {
  46. //处理下载
  47. doDownload(data.path);
  48. } else if (data.name === 'prePayList') {
  49. doGetPayList();
  50. } else if (data.name === 'downloadVue3') {
  51. doGetFrameDownload(Frame.vue3,data.path);
  52. } else if (data.name === 'downloadVue2') {
  53. doGetFrameDownload(Frame.vue2,data.path);
  54. } else if (data.name === 'downloadReact') {
  55. doGetFrameDownload(Frame.react,data.path);
  56. }
  57. } else {
  58. meta2d.emit(data.name);
  59. }
  60. } else {
  61. meta2d.emit(data);
  62. }
  63. } catch (e) {
  64. console.info(e);
  65. }
  66. };
  67. const watcher = watch(
  68. () => route.query.id,
  69. async () => {
  70. open();
  71. }
  72. );
  73. const open = async () => {
  74. if (route.query.id) {
  75. const ret: any = await getLe5leV(route.query.id + '');
  76. ret && ret.data && meta2d.open(ret.data);
  77. } else {
  78. let data: any = await localforage.getItem(localStorageName);
  79. if (data) {
  80. data = JSON.parse(data);
  81. data.locked = 1;
  82. data.rule = false;
  83. meta2d.open(data);
  84. bgColor.value = data.background;
  85. }
  86. }
  87. };
  88. const opened = () => {
  89. let fit: any =
  90. (meta2d.store.data as any).scaleMode === '3'
  91. ? 'height'
  92. : (meta2d.store.data as any).scaleMode === '2'
  93. ? 'width'
  94. : true;
  95. meta2d.fitSizeView(fit, 0);
  96. meta2d.setOptions({
  97. scroll: (meta2d.store.data as any).scroll,
  98. disableScale: (meta2d.store.data as any).isDisableScale,
  99. disableTranslate: (meta2d.store.data as any).isDisableTranslate,
  100. });
  101. };
  102. //获取下载列表
  103. const doDownload = (path: string) => {
  104. const list = getDownloadList(meta2d.data(), path);
  105. window.parent.postMessage(
  106. JSON.stringify({
  107. name: 'download',
  108. data: [...list],
  109. type: 1,
  110. }),
  111. '*'
  112. );
  113. };
  114. //获取框架下载列表
  115. const doGetFrameDownload = (frame: Frame,path:string) => {
  116. const list = getFrameDownloadList(meta2d.data(),path, frame);
  117. window.parent.postMessage(
  118. JSON.stringify({
  119. name: 'download',
  120. data: [...list],
  121. type: 1,
  122. }),
  123. '*'
  124. );
  125. };
  126. //获取付费图元列表
  127. const doGetPayList = () => {
  128. const list = getPayList(meta2d.data());
  129. window.parent.postMessage(
  130. JSON.stringify({
  131. name: 'payList',
  132. data: list,
  133. type: 1,
  134. }),
  135. '*'
  136. );
  137. };
  138. onUnmounted(() => {
  139. watcher();
  140. if (meta2d) {
  141. meta2d.off('opened', opened);
  142. meta2d.destroy();
  143. }
  144. window.removeEventListener('message', dealWithMessage);
  145. });
  146. </script>
  147. <style lang="postcss" scoped>
  148. .preview {
  149. width: 100vw;
  150. height: 100vh;
  151. /* background-color: var(--color-background-editor); */
  152. .meta2d-canvas {
  153. width: 100%;
  154. height: 100%;
  155. }
  156. }
  157. </style>