Preview.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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,getComponents } 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. //js线性图元
  33. if (globalThis.meta2dTools?.length !== globalThis.jsDiagramNum) {
  34. let jsLoadInterval = setInterval(() => {
  35. if (globalThis.meta2dTools?.length == globalThis.jsDiagramNum) {
  36. if (globalThis.registerToolsNew) {
  37. globalThis.registerToolsNew();
  38. }
  39. clearInterval(jsLoadInterval);
  40. }
  41. }, 500);
  42. } else {
  43. if (globalThis.registerToolsNew) {
  44. globalThis.registerToolsNew();
  45. }
  46. }
  47. open();
  48. meta2d.on('opened', opened);
  49. window.addEventListener('message', dealWithMessage);
  50. });
  51. const dealWithMessage = (e) => {
  52. if (typeof e.data !== 'string'||!e.data ||
  53. e.data.startsWith('setImmediate')) {
  54. return;
  55. }
  56. try {
  57. let data = JSON.parse(e.data);
  58. if (typeof data === 'object') {
  59. if (data.type) {
  60. if (data.name === 'downloadHtml') {
  61. //处理下载
  62. doDownload(data.path);
  63. } else if (data.name === 'prePayList') {
  64. doGetPayList();
  65. } else if (data.name === 'downloadVue3') {
  66. doGetFrameDownload(Frame.vue3,data.path);
  67. } else if (data.name === 'downloadVue2') {
  68. doGetFrameDownload(Frame.vue2,data.path);
  69. } else if (data.name === 'downloadReact') {
  70. doGetFrameDownload(Frame.react,data.path);
  71. }
  72. } else {
  73. // meta2d.emit(data.name);
  74. }
  75. } else {
  76. // meta2d.emit(data);
  77. }
  78. } catch (e) {
  79. console.info(e);
  80. }
  81. };
  82. const watcher = watch(
  83. () => route.query.id,
  84. async () => {
  85. open();
  86. }
  87. );
  88. const open = async () => {
  89. if (route.query.id) {
  90. // const ret: any = await getLe5leV(route.query.id + '');
  91. let ret: any;
  92. if (route.query.c) {
  93. ret = await getComponents(route.query.id + '');
  94. } else {
  95. ret = await getLe5leV(route.query.id + '',undefined , route.query.historyId as string);
  96. }
  97. if(ret && ret.data){
  98. if(!ret.data.background){
  99. ret.data.background = '#1e2430';
  100. }
  101. if(!ret.data.color){
  102. ret.data.color = '#bdc7db';
  103. }
  104. ret.data.locked = 1;
  105. if(!ret.data.width){ret.data.width= 1920};
  106. if(!ret.data.height){ret.data.height= 1080};
  107. meta2d.open(ret.data);
  108. }
  109. } else {
  110. let data: any = await localforage.getItem(localStorageName);
  111. if (data) {
  112. data = JSON.parse(data);
  113. data.locked = 1;
  114. data.rule = false;
  115. if(!data.background){
  116. data.background = '#1e2430';
  117. }
  118. if(!data.color){
  119. data.color = '#bdc7db';
  120. }
  121. if(!data.width){data.width= 1920};
  122. if(!data.height){data.height= 1080};
  123. meta2d.open(data);
  124. bgColor.value = data.background;
  125. }
  126. }
  127. };
  128. const opened = () => {
  129. meta2d.store.options.shadowColor = '#0000'
  130. meta2d.canvas.parentElement.style.background = meta2d.store.data.background|| meta2d.store.theme[meta2d.store.data.theme].background;
  131. let fit: any =
  132. (meta2d.store.data as any).scaleMode === '3'
  133. ? 'height'
  134. : (meta2d.store.data as any).scaleMode === '2'
  135. ? 'width'
  136. : true;
  137. meta2d.fitSizeView(fit, 0);
  138. meta2d.setOptions({
  139. scroll: (meta2d.store.data as any).scroll,
  140. disableScale: (meta2d.store.data as any).isDisableScale,
  141. disableTranslate: (meta2d.store.data as any).isDisableTranslate,
  142. });
  143. };
  144. //获取下载列表
  145. const doDownload =async (path: string) => {
  146. const list = getDownloadList(meta2d.data(), path);
  147. window.parent.postMessage(
  148. JSON.stringify({
  149. name: 'download',
  150. data: [...list],
  151. type: 1,
  152. }),
  153. '*'
  154. );
  155. };
  156. //获取框架下载列表
  157. const doGetFrameDownload = (frame: Frame,path:string) => {
  158. const list = getFrameDownloadList(meta2d.data(),path, frame);
  159. window.parent.postMessage(
  160. JSON.stringify({
  161. name: 'download',
  162. data: [...list],
  163. type: 1,
  164. }),
  165. '*'
  166. );
  167. };
  168. //获取付费图元列表
  169. const doGetPayList = () => {
  170. const list = getPayList(meta2d.data());
  171. window.parent.postMessage(
  172. JSON.stringify({
  173. name: 'payList',
  174. data: list,
  175. type: 1,
  176. }),
  177. '*'
  178. );
  179. };
  180. onUnmounted(() => {
  181. watcher();
  182. if (meta2d) {
  183. meta2d.off('opened', opened);
  184. meta2d.destroy();
  185. }
  186. window.removeEventListener('message', dealWithMessage);
  187. });
  188. </script>
  189. <style lang="postcss" scoped>
  190. .preview {
  191. width: 100vw;
  192. height: 100vh;
  193. /* background-color: var(--color-background-editor); */
  194. .meta2d-canvas {
  195. width: 100%;
  196. height: 100%;
  197. }
  198. }
  199. </style>