Preview.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if(ret && ret.data){
  77. if(!ret.data.background){
  78. ret.data.background = '#1e2430';
  79. }
  80. if(!ret.data.color){
  81. ret.data.color = '#bdc7db';
  82. }
  83. if(!ret.data.width){ret.data.width= 1920};
  84. if(!ret.data.height){ret.data.height= 1080};
  85. meta2d.open(ret.data);
  86. }
  87. } else {
  88. let data: any = await localforage.getItem(localStorageName);
  89. if (data) {
  90. data = JSON.parse(data);
  91. data.locked = 1;
  92. data.rule = false;
  93. if(!data.background){
  94. data.background = '#1e2430';
  95. }
  96. if(!data.color){
  97. data.color = '#bdc7db';
  98. }
  99. if(!data.width){data.width= 1920};
  100. if(!data.height){data.height= 1080};
  101. meta2d.open(data);
  102. bgColor.value = data.background;
  103. }
  104. }
  105. };
  106. const opened = () => {
  107. meta2d.store.options.shadowColor = '#0000'
  108. meta2d.canvas.parentElement.style.background = meta2d.store.data.background|| meta2d.store.theme[meta2d.store.data.theme].background;
  109. let fit: any =
  110. (meta2d.store.data as any).scaleMode === '3'
  111. ? 'height'
  112. : (meta2d.store.data as any).scaleMode === '2'
  113. ? 'width'
  114. : true;
  115. meta2d.fitSizeView(fit, 0);
  116. meta2d.setOptions({
  117. scroll: (meta2d.store.data as any).scroll,
  118. disableScale: (meta2d.store.data as any).isDisableScale,
  119. disableTranslate: (meta2d.store.data as any).isDisableTranslate,
  120. });
  121. };
  122. //获取下载列表
  123. const doDownload =async (path: string) => {
  124. const list = getDownloadList(meta2d.data(), path);
  125. window.parent.postMessage(
  126. JSON.stringify({
  127. name: 'download',
  128. data: [...list],
  129. type: 1,
  130. }),
  131. '*'
  132. );
  133. };
  134. //获取框架下载列表
  135. const doGetFrameDownload = (frame: Frame,path:string) => {
  136. const list = getFrameDownloadList(meta2d.data(),path, frame);
  137. window.parent.postMessage(
  138. JSON.stringify({
  139. name: 'download',
  140. data: [...list],
  141. type: 1,
  142. }),
  143. '*'
  144. );
  145. };
  146. //获取付费图元列表
  147. const doGetPayList = () => {
  148. const list = getPayList(meta2d.data());
  149. window.parent.postMessage(
  150. JSON.stringify({
  151. name: 'payList',
  152. data: list,
  153. type: 1,
  154. }),
  155. '*'
  156. );
  157. };
  158. onUnmounted(() => {
  159. watcher();
  160. if (meta2d) {
  161. meta2d.off('opened', opened);
  162. meta2d.destroy();
  163. }
  164. window.removeEventListener('message', dealWithMessage);
  165. });
  166. </script>
  167. <style lang="postcss" scoped>
  168. .preview {
  169. width: 100vw;
  170. height: 100vh;
  171. /* background-color: var(--color-background-editor); */
  172. .meta2d-canvas {
  173. width: 100%;
  174. height: 100%;
  175. }
  176. }
  177. </style>