FileProps.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <div class="props">
  3. <t-tabs v-model="data.tab">
  4. <t-tab-panel :value="1" label="画布">
  5. <t-space direction="vertical" size="small" class="panel">
  6. <div class="form-item">
  7. <label>画布尺寸</label>
  8. <t-input-number
  9. label="W"
  10. :value="data.meta2dData.width"
  11. placeholder="宽"
  12. theme="normal"
  13. min="1"
  14. style="width: 80px"
  15. @change="changeValue($event, 'width')"
  16. />
  17. <t-input-number
  18. class="ml-8"
  19. label="H"
  20. placeholder="高"
  21. theme="normal"
  22. min="1"
  23. :value="data.meta2dData.height"
  24. style="width: 80px"
  25. @change="changeValue($event, 'height')"
  26. />
  27. <t-dropdown
  28. :minColumnWidth="180"
  29. :maxHeight="500"
  30. :delay2="[10, 150]"
  31. overlayClassName="header-dropdown"
  32. >
  33. <t-icon name="more" class="ml-8 hover" />
  34. <t-dropdown-menu>
  35. <t-dropdown-item
  36. v-for="item in screenList"
  37. @click="selectedSreen(item)"
  38. >
  39. <div class="flex between">
  40. {{ item.name }}
  41. <span class="desc">
  42. {{ item.width }}
  43. <span style="font-size: 10px; margin: 0 4px">x</span>
  44. {{ item.height }}
  45. </span>
  46. </div>
  47. </t-dropdown-item>
  48. </t-dropdown-menu>
  49. </t-dropdown>
  50. </div>
  51. <div class="form-item">
  52. <label>背景颜色</label>
  53. <t-color-picker
  54. class="w-full"
  55. format="CSS"
  56. :color-modes="['monochrome']"
  57. :show-primary-color-preview="false"
  58. :value="data.meta2dData.background"
  59. @change="changeValue($event, 'background')"
  60. />
  61. </div>
  62. <div class="form-item">
  63. <label>背景图片</label>
  64. <t-upload
  65. class="ml-8"
  66. v-model="data.background"
  67. action="/api/image/upload"
  68. theme="image"
  69. accept="image/*"
  70. :headers="headers"
  71. :data="updataData"
  72. :auto-upload="true"
  73. :upload-all-files-in-one-request="false"
  74. @success="fileSuccessed"
  75. @remove="fileRemoved"
  76. />
  77. </div>
  78. </t-space>
  79. <t-space direction="vertical" size="small" class="mt-8">
  80. <t-collapse
  81. :defaultValue="['1', '2']"
  82. expandIconPlacement="right"
  83. :borderless="true"
  84. >
  85. <t-collapse-panel value="1" header="预览设置">
  86. <t-space direction="vertical" size="small">
  87. <div class="form-item">
  88. <label>缩放方式</label>
  89. <t-radio-group
  90. class="ml-8"
  91. v-model="data.meta2dData.scaleMode"
  92. @change="changeValue($event, 'scaleMode')"
  93. >
  94. <t-radio
  95. value="1"
  96. title="宽高中较小的铺满"
  97. style="width: 200px"
  98. >
  99. 自动铺满
  100. </t-radio>
  101. <t-radio
  102. value="2"
  103. title="高度等比例缩放"
  104. style="width: 200px"
  105. >
  106. 宽度铺满
  107. </t-radio>
  108. <t-radio value="3" title="宽度等比例缩放">高度铺满</t-radio>
  109. </t-radio-group>
  110. </div>
  111. <div class="form-item">
  112. <label>显示滚动条</label>
  113. <t-checkbox
  114. v-model="data.meta2dData.scroll"
  115. @change="changeValue($event, 'scroll')"
  116. class="ml-8"
  117. />
  118. </div>
  119. <!-- <template v-if="data.scroll">
  120. <div class="form-item">
  121. <label>水平对齐</label>
  122. <t-radio-group class="ml-8" default-value="2">
  123. <t-radio value="1" style="width: 200px">左对齐</t-radio>
  124. <t-radio value="2" style="width: 200px">居中</t-radio>
  125. <t-radio value="3" style="width: 200px">右对齐</t-radio>
  126. </t-radio-group>
  127. </div>
  128. <div class="form-item">
  129. <label>垂直对齐</label>
  130. <t-radio-group class="ml-8" default-value="2">
  131. <t-radio value="1" style="width: 200px">顶部对齐</t-radio>
  132. <t-radio value="2" style="width: 200px">居中</t-radio>
  133. <t-radio value="3" style="width: 200px">底部对齐</t-radio>
  134. </t-radio-group>
  135. </div>
  136. </template> -->
  137. </t-space>
  138. </t-collapse-panel>
  139. <t-collapse-panel value="2" header="进阶设置">
  140. <t-space direction="vertical" size="small">
  141. <div class="form-item">
  142. <label>
  143. 图标URL
  144. <span>
  145. <label
  146. class="vip-label"
  147. style="font-size: 10px; padding: 0 2px"
  148. >
  149. VIP
  150. </label>
  151. </span>
  152. </label>
  153. <div class="px-8" style="width: 200px">
  154. <template v-if="data.meta2dData.iconUrls">
  155. <div
  156. v-for="(icon, i) of data.meta2dData.iconUrls"
  157. class="flex middle between"
  158. style="height: 30px"
  159. >
  160. <div>
  161. {{ icon.substring(0, 8) }}...{{ icon.substr(-16) }}
  162. </div>
  163. <t-icon
  164. name="close"
  165. class="hover"
  166. @click="removeIconUrl(i)"
  167. />
  168. </div>
  169. </template>
  170. <div class="flex middle" v-if="user.isVip">
  171. <t-input
  172. v-model="data.iconUrl"
  173. placeholder="Font class方式URL"
  174. style="width: 150px; margin-left: -8px"
  175. />
  176. <t-button
  177. variant="outline"
  178. style="padding: 4px 8px; margin-left: 8px"
  179. @click="addIconUrl"
  180. >
  181. 添加
  182. </t-button>
  183. </div>
  184. <div v-else class="desc mt-4">
  185. 支持添加iconfont。
  186. <a :href="assets.account" target="_blank">VIP</a> 功能
  187. </div>
  188. </div>
  189. </div>
  190. <div class="form-item">
  191. <label>初始化动作</label>
  192. <t-button
  193. variant="outline"
  194. style="padding: 0 4px; margin: 2px 8px"
  195. @click="showInitFnDialog"
  196. >
  197. <t-icon name="ellipsis" />
  198. </t-button>
  199. </div>
  200. <div class="form-item">
  201. <label>数据监听</label>
  202. <t-button
  203. variant="outline"
  204. style="padding: 0 4px; margin: 2px 8px"
  205. @click="showDataTransformation"
  206. >
  207. <t-icon name="ellipsis" />
  208. </t-button>
  209. </div>
  210. </t-space>
  211. </t-collapse-panel>
  212. </t-collapse>
  213. </t-space>
  214. </t-tab-panel>
  215. <t-tab-panel :value="2" label="结构">
  216. <ElementTree />
  217. </t-tab-panel>
  218. </t-tabs>
  219. <t-dialog
  220. v-if="initFnDialog.show"
  221. :visible="true"
  222. header="初始化动作"
  223. @confirm="onOkInitFn"
  224. @close="initFnDialog.show = false"
  225. :width="700"
  226. >
  227. <CodeEditor v-model="initFnDialog.data" style="height: 300px" />
  228. </t-dialog>
  229. <t-dialog
  230. v-if="dataTransformationDialog.show"
  231. :visible="true"
  232. header="数据监听"
  233. @confirm="onOkDataTransformation"
  234. @close="dataTransformationDialog.show = false"
  235. :width="700"
  236. >
  237. <CodeEditor
  238. v-model="dataTransformationDialog.data"
  239. style="height: 300px"
  240. />
  241. </t-dialog>
  242. </div>
  243. </template>
  244. <script lang="ts" setup>
  245. import { onMounted, reactive, onUnmounted } from 'vue';
  246. import { useUser } from '@/services/user';
  247. import { getCookie } from '@/services/cookie';
  248. import ElementTree from './ElementTree.vue';
  249. import CodeEditor from '@/views/components/common/CodeEditor.vue';
  250. import { autoSave, inTreePanel, useAssets } from '@/services/common';
  251. import { MessagePlugin } from 'tdesign-vue-next';
  252. import { loadCss } from '@meta2d/core';
  253. const headers = {
  254. Authorization: 'Bearer ' + (localStorage.token || getCookie('token') || ''),
  255. };
  256. const updataData = { directory: '/项目' };
  257. const { assets } = useAssets();
  258. const { user } = useUser();
  259. const data = reactive<any>({
  260. tab: 1,
  261. background: [],
  262. meta2dData: {},
  263. iconUrl: '',
  264. });
  265. const screenList = reactive([
  266. {
  267. name: '大屏',
  268. width: 1920,
  269. height: 1080,
  270. },
  271. {
  272. name: '网页',
  273. width: 1440,
  274. height: 1024,
  275. },
  276. {
  277. name: '平板12.9"',
  278. width: 1024,
  279. height: 1366,
  280. },
  281. {
  282. name: '平板11"',
  283. width: 834,
  284. height: 1194,
  285. },
  286. {
  287. name: '平板Mini',
  288. width: 768,
  289. height: 1024,
  290. },
  291. {
  292. name: '华为P8',
  293. width: 360,
  294. height: 640,
  295. },
  296. {
  297. name: '华为P40',
  298. width: 395,
  299. height: 856,
  300. },
  301. {
  302. name: '手机1',
  303. width: 430,
  304. height: 932,
  305. },
  306. {
  307. name: '手机2',
  308. width: 375,
  309. height: 812,
  310. },
  311. ]);
  312. const initFnDialog = reactive<any>({
  313. show: false,
  314. data: '',
  315. });
  316. const dataTransformationDialog = reactive<any>({
  317. show: false,
  318. data: '',
  319. });
  320. const selectedSreen = (item: any) => {
  321. meta2d.store.data.width = item.width;
  322. meta2d.store.data.height = item.height;
  323. meta2d.store.patchFlagsBackground = true;
  324. meta2d.render();
  325. openData();
  326. };
  327. const fileSuccessed = async (content: any) => {
  328. // meta2d.store.patchFlagsBackground = true;
  329. meta2d.setBackgroundImage(content.response.url);
  330. setTimeout(() => {
  331. meta2d.store.patchFlagsBackground = true;
  332. meta2d.render();
  333. }, 1000);
  334. };
  335. const fileRemoved = () => {
  336. meta2d.setBackgroundImage('');
  337. meta2d.store.patchFlagsBackground = true;
  338. meta2d.render();
  339. data.background = [];
  340. };
  341. const changeValue = (e: any, key: string) => {
  342. // @ts-ignore
  343. meta2d.store.data[key] = e;
  344. if (key === 'background') {
  345. meta2d.store.patchFlagsBackground = true;
  346. }
  347. meta2d.render();
  348. openData();
  349. };
  350. onMounted(() => {
  351. if (inTreePanel.value) {
  352. data.tab = 2;
  353. }
  354. openData();
  355. meta2d.on('opened', openData);
  356. });
  357. onUnmounted(() => {
  358. meta2d.off('opened', openData);
  359. });
  360. function openData() {
  361. if (!(meta2d.store.data as any).scaleMode) {
  362. (meta2d.store.data as any).scaleMode = '1';
  363. }
  364. data.meta2dData = Object.assign({}, meta2d.store.data);
  365. if (meta2d.store.data.bkImage) {
  366. data.background = [
  367. {
  368. name: meta2d.store.data.bkImage,
  369. url: meta2d.store.data.bkImage,
  370. },
  371. ];
  372. }
  373. }
  374. const showInitFnDialog = () => {
  375. initFnDialog.data = meta2d.store.data.initJs;
  376. initFnDialog.show = true;
  377. };
  378. const onOkInitFn = () => {
  379. meta2d.store.data.initJs = initFnDialog.data;
  380. initFnDialog.show = false;
  381. };
  382. const showDataTransformation = () => {
  383. dataTransformationDialog.data = meta2d.store.data.socketCbJs;
  384. dataTransformationDialog.show = true;
  385. };
  386. const onOkDataTransformation = () => {
  387. meta2d.store.data.socketCbJs = dataTransformationDialog.data;
  388. dataTransformationDialog.show = false;
  389. };
  390. const addIconUrl = () => {
  391. if (!data.iconUrl || data.iconUrl.substr(-4) !== '.css') {
  392. MessagePlugin.error('请填写以.css结尾的font-class引用方式的URL地址');
  393. return;
  394. }
  395. if (!data.meta2dData.iconUrls) {
  396. data.meta2dData.iconUrls = [];
  397. }
  398. data.meta2dData.iconUrls.push(data.iconUrl);
  399. // @ts-ignore
  400. meta2d.store.data.iconUrls = data.meta2dData.iconUrls;
  401. if (meta2d.store.data.iconUrls) {
  402. for (const item of meta2d.store.data.iconUrls) {
  403. loadCss(item);
  404. }
  405. }
  406. data.iconUrl = '';
  407. autoSave(true);
  408. };
  409. const removeIconUrl = (i: number) => {
  410. data.meta2dData.iconUrls.splice(i, 1);
  411. // @ts-ignore
  412. meta2d.store.data.iconUrls = data.meta2dData.iconUrls;
  413. autoSave(true);
  414. };
  415. </script>
  416. <style lang="postcss" scoped>
  417. .props {
  418. }
  419. </style>