FileProps.vue 14 KB

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