index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <template>
  2. <div class="bs-custom-components">
  3. <div class="bs-custom-component-header">
  4. <div class="left-title">
  5. <div class="logo-wrap item-wrap">
  6. <img
  7. class="menu-img"
  8. src="../BigScreenDesign/images/app.png"
  9. alt="返回"
  10. @click="backManagement"
  11. >
  12. <span class="logo-text name-span">{{ form.name }}</span>
  13. </div>
  14. </div>
  15. <div class="right-btn-wrap">
  16. <CusBtn
  17. :loading="loading"
  18. @click.native="createdImg()"
  19. >
  20. 生成图片
  21. </CusBtn>
  22. <CusBtn
  23. :loading="loading"
  24. @click="save"
  25. >
  26. 保存
  27. </CusBtn>
  28. </div>
  29. </div>
  30. <div class="bs-custom-component-content">
  31. <div class="bs-custom-component-content-code">
  32. <div class="left-vue-code component-code">
  33. <div class="code-tab-header">
  34. <div class="code-tab">
  35. VUE组件
  36. </div>
  37. <div class="upload-btn">
  38. <CusBtn @click="upload('vueContent')">
  39. 上传
  40. </CusBtn>
  41. </div>
  42. </div>
  43. <div class="code-tab-content">
  44. <!-- <MonacoEditor
  45. ref="vueContent"
  46. v-model="form.vueContent"
  47. class="editor"
  48. language="html"
  49. /> -->
  50. <codemirror
  51. v-model="form.vueContent"
  52. :options="vueOptions"
  53. />
  54. </div>
  55. </div>
  56. <div class="right-setting-code component-code">
  57. <div class="code-tab-header">
  58. <div class="code-tab">
  59. 组件配置
  60. </div>
  61. <div class="upload-btn">
  62. <CusBtn @click="upload('settingContent')">
  63. 上传
  64. </CusBtn>
  65. </div>
  66. </div>
  67. <div class="code-tab-content">
  68. <!-- <MonacoEditor
  69. ref="settingContent"
  70. v-model="form.settingContent"
  71. class="editor"
  72. language="javascript"
  73. /> -->
  74. <codemirror
  75. v-model="form.settingContent"
  76. :options="settingOptions"
  77. />
  78. </div>
  79. </div>
  80. </div>
  81. <div class="bs-custom-component-content-preview">
  82. <div class="bs-preview-inner">
  83. <div class="code-tab-header">
  84. <div class="code-tab">
  85. 效果预览
  86. </div>
  87. </div>
  88. <BizComponentPreview
  89. :vue-content="form.vueContent"
  90. :setting-content="form.settingContent"
  91. />
  92. </div>
  93. </div>
  94. <!-- 通过计算属性发现accept有问题 -->
  95. <input
  96. ref="vueContentFile"
  97. style="display: none"
  98. type="file"
  99. name="file"
  100. accept=".vue"
  101. @change="handleBatchUpload"
  102. >
  103. <input
  104. ref="settingContentFile"
  105. style="display: none"
  106. type="file"
  107. name="file"
  108. accept=".js"
  109. @change="handleBatchUpload"
  110. >
  111. </div>
  112. </div>
  113. </template>
  114. <script>
  115. import { toJpeg, toPng } from 'html-to-image'
  116. import CusBtn from 'data-room-ui/BigScreenDesign/BtnLoading'
  117. // import MonacoEditor from 'data-room-ui/MonacoEditor'
  118. import BizComponentPreview from './Preview'
  119. import { getBizComponentInfo, updateBizComponent } from 'data-room-ui/js/api/bigScreenApi'
  120. import { defaultSettingContent, defaultVueContent } from './config/defaultBizConfig'
  121. import { codemirror } from 'vue-codemirror'
  122. import 'codemirror/lib/codemirror.css'
  123. import 'codemirror/theme/material-darker.css'
  124. import 'codemirror/addon/selection/active-line.js'
  125. import 'codemirror/mode/vue/vue.js'
  126. import {
  127. showSize,
  128. dataURLtoBlob,
  129. translateBlobToBase64
  130. } from 'data-room-ui/js/utils/compressImg'
  131. import * as imageConversion from 'image-conversion'
  132. export default {
  133. name: 'BizComponentDesign',
  134. components: {
  135. CusBtn,
  136. // MonacoEditor,
  137. codemirror,
  138. BizComponentPreview
  139. },
  140. props: {},
  141. data () {
  142. return {
  143. form: {
  144. name: '',
  145. coverPicture: '',
  146. settingContent: '',
  147. vueContent: ''
  148. },
  149. currentContentType: 'vueContent',
  150. loading: false,
  151. vueOptions: {
  152. foldGutter: true,
  153. lineWrapping: true,
  154. gutters: [
  155. 'CodeMirror-linenumbers',
  156. 'CodeMirror-foldgutter',
  157. 'CodeMirror-lint-markers'
  158. ],
  159. theme: 'material-darker',
  160. tabSize: 4,
  161. lineNumbers: true,
  162. line: true,
  163. indentWithTabs: true,
  164. smartIndent: true,
  165. autofocus: false,
  166. matchBrackets: true,
  167. mode: 'text/x-vue',
  168. hintOptions: {
  169. completeSingle: false
  170. },
  171. lint: true
  172. },
  173. settingOptions: {
  174. foldGutter: true,
  175. lineWrapping: true,
  176. gutters: [
  177. 'CodeMirror-linenumbers',
  178. 'CodeMirror-foldgutter',
  179. 'CodeMirror-lint-markers'
  180. ],
  181. theme: 'material-darker',
  182. tabSize: 4,
  183. lineNumbers: true,
  184. line: true,
  185. indentWithTabs: true,
  186. smartIndent: true,
  187. autofocus: false,
  188. matchBrackets: true,
  189. mode: 'text/javascript',
  190. hintOptions: {
  191. completeSingle: false
  192. },
  193. lint: true
  194. }
  195. }
  196. },
  197. computed: {
  198. },
  199. mounted () {
  200. this.getBizComponentInfo()
  201. },
  202. methods: {
  203. getBizComponentInfo () {
  204. const code = this.$route.query.code
  205. if (code) {
  206. getBizComponentInfo(code).then(data => {
  207. this.form = {
  208. ...data,
  209. name: data.name,
  210. coverPicture: data.coverPicture,
  211. settingContent: data.settingContent || defaultSettingContent,
  212. vueContent: data.vueContent || defaultVueContent
  213. }
  214. // this.$refs.vueContent.editor.setValue(this.form.vueContent)
  215. // this.$refs.settingContent.editor.setValue(this.form.settingContent)
  216. })
  217. }
  218. },
  219. upload (type) {
  220. this.currentContentType = type
  221. this.$refs[`${this.currentContentType}File`].click()
  222. },
  223. handleBatchUpload (source) {
  224. const file = source.target.files
  225. const reader = new FileReader() // 新建一个FileReader
  226. reader.readAsText(file[0], 'UTF-8') // 读取文件
  227. reader.onload = (event) => {
  228. const sileString = event.target.result // 读取文件内容
  229. this.form[this.currentContentType] = sileString
  230. // input通过onchange事件来触发js代码的,由于两次文件是重复的,所以这个时候onchange事件是没有触发到的,所以需要手动清空input的值
  231. source.target.value = ''
  232. }
  233. },
  234. backManagement () {
  235. // 给出一个确认框提示,提示如下确定返回主页面吗?未保存的配置将会丢失。3个按钮 : 留在页面 、离开页面、保存后离开页面
  236. this.$confirm('确定返回主页面吗?未保存的配置将会丢失。', '提示', {
  237. distinguishCancelAndClose: true,
  238. confirmButtonText: '保存后离开页面',
  239. cancelButtonText: '离开页面',
  240. cancelButtonClass: 'cancel-btn',
  241. type: 'warning',
  242. customClass: 'bs-el-message-box'
  243. }).then(() => {
  244. this.save(true)
  245. }).catch((action) => {
  246. if (action === 'cancel') {
  247. this.pageJump()
  248. }
  249. })
  250. },
  251. save (pageJump = false) {
  252. this.loading = true
  253. const node = document.querySelector('.remote-preview-inner-wrap')
  254. // 获取node下的第一个子节点
  255. const childrenNode = node.children[0]
  256. toJpeg(childrenNode, { quality: 0.2 })
  257. .then((dataUrl) => {
  258. const that = this
  259. if (showSize(dataUrl) > 200) {
  260. const url = dataURLtoBlob(dataUrl)
  261. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  262. imageConversion
  263. .compressAccurately(url, {
  264. size: 200, // 图片大小压缩到100kb
  265. width: 1280, // 宽度压缩到1280
  266. height: 720 // 高度压缩到720
  267. })
  268. .then((res) => {
  269. translateBlobToBase64(res, function (e) {
  270. this.form.coverPicture = e.result
  271. updateBizComponent(this.form)
  272. .then(() => {
  273. this.$message({
  274. message: '保存成功',
  275. type: 'success',
  276. onClose: () => {
  277. // 此处写提示关闭后需要执行的函数
  278. if (pageJump) {
  279. this.pageJump()
  280. }
  281. }
  282. })
  283. })
  284. .finally(() => {
  285. that.loading = false
  286. })
  287. })
  288. })
  289. } else {
  290. this.form.coverPicture = dataUrl
  291. updateBizComponent(this.form)
  292. .then(() => {
  293. this.$message({
  294. message: '保存成功',
  295. type: 'success',
  296. onClose: () => {
  297. // 此处写提示关闭后需要执行的函数
  298. if (pageJump) {
  299. this.pageJump()
  300. }
  301. }
  302. })
  303. })
  304. .finally(() => {
  305. this.loading = false
  306. })
  307. }
  308. })
  309. .catch(() => {
  310. this.loading = false
  311. })
  312. },
  313. createdImg () {
  314. this.loading = true
  315. const node = document.querySelector('.remote-preview-inner-wrap')
  316. // 获取node下的第一个子节点
  317. const childrenNode = node.children[0]
  318. toPng(childrenNode)
  319. .then((dataUrl) => {
  320. const link = document.createElement('a')
  321. link.download = `${this.form.name}.png`
  322. link.href = dataUrl
  323. link.click()
  324. link.addEventListener('click', () => {
  325. link.remove()
  326. })
  327. this.loading = false
  328. })
  329. .catch(() => {
  330. this.$message.warning('出现未知错误,请重试')
  331. this.loading = false
  332. })
  333. },
  334. pageJump () {
  335. this.$router.push({ path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components' })
  336. const data = { componentsManagementType: 'bizComponent' }
  337. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  338. }
  339. }
  340. }
  341. </script>
  342. <style lang="scss" scoped>
  343. .bs-custom-components {
  344. position: absolute;
  345. display: flex;
  346. flex-direction: column;
  347. width: 100%;
  348. height: 100vh;
  349. color: var(--bs-el-text);
  350. background: var(--bs-background-2);
  351. overflow: hidden;
  352. > * {
  353. box-sizing: border-box;
  354. }
  355. .bs-custom-component-header {
  356. display: flex;
  357. align-items: center;
  358. justify-content: space-between;
  359. height: 50px;
  360. padding: 0 16px;
  361. border-bottom: 4px solid var(--bs-background-1);
  362. background: var(--bs-background-2);
  363. .left-title {
  364. font-size: 16px;
  365. color: var(--bs-el-title);
  366. .logo-wrap {
  367. display: flex;
  368. align-items: center;
  369. }
  370. .menu-img {
  371. width: 18px;
  372. height: 18px;
  373. margin-right: 15px;
  374. margin-left: 9px;
  375. cursor: pointer;
  376. }
  377. }
  378. .right-btn-wrap {
  379. display: flex;
  380. align-items: center;
  381. height: 100%;
  382. }
  383. }
  384. .bs-custom-component-content {
  385. flex: 1;
  386. background: var(--bs-background-2);
  387. display: flex;
  388. flex-direction: column;
  389. .bs-custom-component-content-code {
  390. display: flex;
  391. justify-content: space-between;
  392. width: 100%;
  393. height: 400px;
  394. padding: 16px;
  395. .left-vue-code {
  396. width: 60%;
  397. height: 100%;
  398. /* background: var(--bs-background-1); */
  399. }
  400. .right-setting-code {
  401. width: calc(40% - 16px);
  402. height: 100%;
  403. /* background: var(--bs-background-1); */
  404. }
  405. .component-code {
  406. .code-tab-header {
  407. display: flex;
  408. align-items: center;
  409. justify-content: space-between;
  410. height: 40px;
  411. .code-tab {
  412. font-size: 14px;
  413. display: flex;
  414. align-items: center;
  415. justify-content: center;
  416. width: 120px;
  417. height: 100%;
  418. color: var(--bs-el-title);
  419. background: var(--bs-background-1);
  420. }
  421. }
  422. .code-tab-content {
  423. height: calc(100% - 40px);
  424. background: var(--bs-background-1);
  425. }
  426. }
  427. }
  428. .bs-custom-component-content-preview {
  429. flex: 1;
  430. width: 100%;
  431. height: 50%;
  432. padding: 0 16px 16px;
  433. .bs-preview-inner {
  434. width: 100%;
  435. height: 100%;
  436. background: var(--bs-background-1);
  437. position: relative;
  438. .code-tab-header{
  439. height: 40px;
  440. background-color: var(--bs-background-2);
  441. line-height: 40px;
  442. .code-tab{
  443. width: 120px;
  444. height:40px;
  445. text-align: center;
  446. background-color: var(--bs-background-1);
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. </style>
  454. <style>
  455. .cm-s-material-darker.CodeMirror,
  456. .cm-s-material-darker .CodeMirror-gutters
  457. {
  458. background: var(--bs-background-1) !important;
  459. }
  460. .CodeMirror-scroll {
  461. background-color: var(--bs-background-1) !important;
  462. }
  463. .CodeMirror-gutters {
  464. border-right: 1px solid var(--bs-background-1) !important;
  465. background-color: var(--bs-background-1) !important;
  466. }
  467. </style>