index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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="save"
  19. >
  20. 保存
  21. </CusBtn>
  22. </div>
  23. </div>
  24. <div class="bs-custom-component-content">
  25. <div class="bs-custom-component-content-code">
  26. <div class="left-vue-code component-code">
  27. <div class="code-tab-header">
  28. <div class="code-tab-left">
  29. <div class="code-tab">
  30. 组件模板
  31. </div>
  32. <div
  33. class="code-tab-btn"
  34. @click="change('echart')"
  35. >
  36. echarts组件
  37. </div>
  38. <div
  39. class="code-tab-btn"
  40. @click="change('g2plot')"
  41. >
  42. G2Plot组件
  43. </div>
  44. <div
  45. class="code-tab-btn"
  46. @click="change('native')"
  47. >
  48. 原生组件
  49. </div>
  50. <div
  51. class="code-tab-btn"
  52. @click="change('3DEchart')"
  53. >
  54. 3D组件
  55. </div>
  56. </div>
  57. <!-- <div class="upload-btn">
  58. <CusBtn @click="upload('vueContent')">
  59. 上传
  60. </CusBtn>
  61. </div> -->
  62. </div>
  63. <div class="code-tab-content">
  64. <!-- <MonacoEditor
  65. ref="vueContent"
  66. v-model="form.vueContent"
  67. class="editor"
  68. language="html"
  69. /> -->
  70. <codemirror
  71. v-model="form.vueContent"
  72. :options="vueOptions"
  73. />
  74. </div>
  75. </div>
  76. <div class="right-setting-code component-code">
  77. <div class="code-tab-header">
  78. <div class="code-tab">
  79. 组件配置
  80. </div>
  81. <!-- <div class="upload-btn">
  82. <CusBtn @click="upload('settingContent')">
  83. 上传
  84. </CusBtn>
  85. </div> -->
  86. </div>
  87. <div class="code-tab-content">
  88. <!-- <MonacoEditor
  89. ref="settingContent"
  90. v-model="form.settingContent"
  91. class="editor"
  92. language="javascript"
  93. /> -->
  94. <codemirror
  95. v-model="form.settingContent"
  96. :options="settingOptions"
  97. />
  98. </div>
  99. </div>
  100. </div>
  101. <div class="bs-custom-component-content-preview">
  102. <div class="bs-preview-inner">
  103. <div class="code-tab-header">
  104. <div class="code-tab">
  105. 效果预览
  106. </div>
  107. <div class="upload-btn">
  108. <CusBtn
  109. :loading="loading"
  110. @click.native="createdImg()"
  111. >
  112. 生成图片
  113. </CusBtn>
  114. </div>
  115. </div>
  116. <BizComponentPreview
  117. :vue-content="form.vueContent"
  118. :setting-content="form.settingContent"
  119. />
  120. </div>
  121. </div>
  122. <!-- 通过计算属性发现accept有问题 -->
  123. <input
  124. ref="vueContentFile"
  125. style="display: none"
  126. type="file"
  127. name="file"
  128. accept=".vue"
  129. @change="handleBatchUpload"
  130. >
  131. <input
  132. ref="settingContentFile"
  133. style="display: none"
  134. type="file"
  135. name="file"
  136. accept=".js"
  137. @change="handleBatchUpload"
  138. >
  139. </div>
  140. </div>
  141. </template>
  142. <script>
  143. import { toJpeg } from 'html-to-image'
  144. import CusBtn from 'data-room-ui/BigScreenDesign/BtnLoading'
  145. // import MonacoEditor from 'data-room-ui/MonacoEditor'
  146. import BizComponentPreview from './Preview'
  147. import { getBizComponentInfo, updateBizComponent } from 'data-room-ui/js/api/bigScreenApi'
  148. import { defaultSettingContent, defaultVueContent } from './config/defaultBizConfig'
  149. import { defaultEchartsSettingContent, defaultEchartsVueContent } from './config/defaultEchartsConfig'
  150. import { defaultG2SettingContent, defaultG2VueContent } from './config/defaultG2Config'
  151. import { codemirror } from 'vue-codemirror'
  152. import 'codemirror/lib/codemirror.css'
  153. import 'codemirror/theme/material-darker.css'
  154. import 'codemirror/addon/selection/active-line.js'
  155. import 'codemirror/mode/vue/vue.js'
  156. import {
  157. showSize,
  158. compressImage
  159. // dataURLtoBlob,
  160. // translateBlobToBase64
  161. } from 'data-room-ui/js/utils/compressImg'
  162. // import * as imageConversion from 'image-conversion'
  163. export default {
  164. name: 'BizComponentDesign',
  165. components: {
  166. CusBtn,
  167. // MonacoEditor,
  168. codemirror,
  169. BizComponentPreview
  170. },
  171. props: {},
  172. data () {
  173. return {
  174. initialCoverPicture: '',
  175. form: {
  176. name: '',
  177. coverPicture: '',
  178. settingContent: '',
  179. vueContent: ''
  180. },
  181. currentContentType: 'vueContent',
  182. loading: false,
  183. vueOptions: {
  184. foldGutter: true,
  185. lineWrapping: true,
  186. gutters: [
  187. 'CodeMirror-linenumbers',
  188. 'CodeMirror-foldgutter',
  189. 'CodeMirror-lint-markers'
  190. ],
  191. theme: 'material-darker',
  192. tabSize: 4,
  193. lineNumbers: true,
  194. line: true,
  195. indentWithTabs: true,
  196. smartIndent: true,
  197. autofocus: false,
  198. matchBrackets: true,
  199. mode: 'text/x-vue',
  200. hintOptions: {
  201. completeSingle: false
  202. },
  203. lint: true
  204. },
  205. settingOptions: {
  206. foldGutter: true,
  207. lineWrapping: true,
  208. gutters: [
  209. 'CodeMirror-linenumbers',
  210. 'CodeMirror-foldgutter',
  211. 'CodeMirror-lint-markers'
  212. ],
  213. theme: 'material-darker',
  214. tabSize: 4,
  215. lineNumbers: true,
  216. line: true,
  217. indentWithTabs: true,
  218. smartIndent: true,
  219. autofocus: false,
  220. matchBrackets: true,
  221. mode: 'text/javascript',
  222. hintOptions: {
  223. completeSingle: false
  224. },
  225. lint: true
  226. }
  227. }
  228. },
  229. computed: {
  230. },
  231. mounted () {
  232. this.getBizComponentInfo()
  233. },
  234. methods: {
  235. getBizComponentInfo () {
  236. const code = this.$route.query.code
  237. const type = this.$route.query.type
  238. if (code) {
  239. getBizComponentInfo(code).then(data => {
  240. this.initialCoverPicture = data.coverPicture || ''
  241. if (type && type === 'g2plot') {
  242. this.form = {
  243. ...data,
  244. name: data.name,
  245. coverPicture: data.coverPicture,
  246. settingContent: data.settingContent || defaultG2SettingContent,
  247. vueContent: data.vueContent || defaultG2VueContent
  248. }
  249. } else if (type && type === 'echart') {
  250. this.form = {
  251. ...data,
  252. name: data.name,
  253. coverPicture: data.coverPicture,
  254. settingContent: data.settingContent || defaultEchartsSettingContent,
  255. vueContent: data.vueContent || defaultEchartsVueContent
  256. }
  257. } else {
  258. this.form = {
  259. ...data,
  260. name: data.name,
  261. coverPicture: data.coverPicture,
  262. settingContent: data.settingContent || defaultSettingContent,
  263. vueContent: data.vueContent || defaultVueContent
  264. }
  265. }
  266. // this.$refs.vueContent.editor.setValue(this.form.vueContent)
  267. // this.$refs.settingContent.editor.setValue(this.form.settingContent)
  268. })
  269. }
  270. },
  271. changeTemp (val) {
  272. if (val == 'g2plot') {
  273. this.form.settingContent = defaultG2SettingContent
  274. this.form.vueContent = defaultG2VueContent
  275. } else if (val == 'native') {
  276. this.form.settingContent = defaultSettingContent
  277. this.form.vueContent = defaultVueContent
  278. } else if (val == 'echart') {
  279. this.form.settingContent = defaultEchartsSettingContent
  280. this.form.vueContent = defaultEchartsVueContent
  281. }
  282. },
  283. change (val) {
  284. if (val === '3DEchart') {
  285. return this.$confirm('开发中。。。。', '提示', {
  286. distinguishCancelAndClose: true,
  287. confirmButtonText: '确定',
  288. cancelButtonText: '取消',
  289. cancelButtonClass: 'cancel-btn',
  290. type: 'warning',
  291. customClass: 'bs-el-message-box'
  292. }).then(() => {
  293. }).catch((action) => {
  294. })
  295. }
  296. this.$confirm('确定替换为选中模板吗?未保存的代码将被覆盖!', '提示', {
  297. distinguishCancelAndClose: true,
  298. confirmButtonText: '确定',
  299. showCancelButton: false,
  300. cancelButtonClass: 'cancel-btn',
  301. type: 'warning',
  302. customClass: 'bs-el-message-box'
  303. }).then(() => {
  304. this.changeTemp(val)
  305. }).catch((action) => {
  306. })
  307. },
  308. // upload (type) {
  309. // this.currentContentType = type
  310. // this.$refs[`${this.currentContentType}File`].click()
  311. // },
  312. handleBatchUpload (source) {
  313. const file = source.target.files
  314. const reader = new FileReader() // 新建一个FileReader
  315. reader.readAsText(file[0], 'UTF-8') // 读取文件
  316. reader.onload = (event) => {
  317. const sileString = event.target.result // 读取文件内容
  318. this.form[this.currentContentType] = sileString
  319. // input通过onchange事件来触发js代码的,由于两次文件是重复的,所以这个时候onchange事件是没有触发到的,所以需要手动清空input的值
  320. source.target.value = ''
  321. }
  322. },
  323. backManagement () {
  324. // 给出一个确认框提示,提示如下确定返回主页面吗?未保存的配置将会丢失。3个按钮 : 留在页面 、离开页面、保存后离开页面
  325. this.$confirm('确定返回主页面吗?未保存的配置将会丢失。', '提示', {
  326. distinguishCancelAndClose: true,
  327. confirmButtonText: '保存后离开页面',
  328. cancelButtonText: '离开页面',
  329. cancelButtonClass: 'cancel-btn',
  330. type: 'warning',
  331. customClass: 'bs-el-message-box'
  332. }).then(() => {
  333. this.save(true)
  334. }).catch((action) => {
  335. if (action === 'cancel') {
  336. this.pageJump()
  337. }
  338. })
  339. },
  340. async save (pageJump = false) {
  341. this.loading = true
  342. let dataUrl = ''
  343. const node = document.querySelector('.remote-preview-inner-wrap')
  344. // 获取node下的第一个子节点
  345. const childrenNode = node.children[0]
  346. try {
  347. dataUrl = await toJpeg(childrenNode, { quality: 0.2 })
  348. } catch (error) {
  349. console.info(error)
  350. }
  351. if (dataUrl) {
  352. if (showSize(dataUrl) > 200) {
  353. // const url = dataURLtoBlob(dataUrl)
  354. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  355. // imageConversion.compressAccurately(
  356. // url,
  357. // {
  358. // size: 200, // 图片大小压缩到100kb
  359. // width: 1280, // 宽度压缩到1280
  360. // height: 720 // 高度压缩到720
  361. // }
  362. // ).then((res) => {
  363. // translateBlobToBase64(res, (e) => {
  364. // this.form.coverPicture = e.result
  365. // })
  366. // })
  367. this.$message.info('由于封面图片过大,进行压缩中')
  368. this.form.coverPicture = await compressImage(dataUrl, { width: 1280, height: 720, size: 400, quality: 1 })
  369. } else {
  370. this.form.coverPicture = dataUrl
  371. }
  372. } else {
  373. this.$message.warning('保存封面失败,将使用上次保存的封面')
  374. this.form.coverPicture = this.initialCoverPicture
  375. }
  376. updateBizComponent(this.form).then(() => {
  377. this.$message({
  378. message: '保存成功',
  379. type: 'success',
  380. duration: 800,
  381. onClose: () => {
  382. // 此处写提示关闭后需要执行的函数
  383. if (pageJump) {
  384. this.pageJump()
  385. }
  386. }
  387. })
  388. this.loading = false
  389. }).catch((error) => {
  390. console.info(error)
  391. this.loading = false
  392. })
  393. },
  394. createdImg () {
  395. this.loading = true
  396. const node = document.querySelector('.remote-preview-inner-wrap')
  397. // 获取node下的第一个子节点
  398. const childrenNode = node.children[0]
  399. // 为childrenNode添加一个背景颜色
  400. childrenNode.style.backgroundColor = 'var(--bs-background-1)'
  401. toJpeg(childrenNode)
  402. .then((dataUrl) => {
  403. const link = document.createElement('a')
  404. link.download = `${this.form.name}.png`
  405. link.href = dataUrl
  406. link.click()
  407. link.addEventListener('click', () => {
  408. link.remove()
  409. })
  410. this.loading = false
  411. })
  412. .catch((error) => {
  413. this.loading = false
  414. if (error.type === 'error') {
  415. // 判断的error.currentTarget是img标签,如果是的,就弹出消息说是图片跨域
  416. if (error.currentTarget.tagName.toLowerCase() === 'img') {
  417. // 确认框
  418. this.$confirm('图片资源跨域导致使用toDataURL API生成图片失败,请将图片上传到资源库,然后在组件中使用资源库中的图片资源,确保没有跨域问题。', '提示', {
  419. confirmButtonText: '确定',
  420. showCancelButton: false,
  421. type: 'warning',
  422. customClass: 'bs-el-message-box'
  423. }).then(() => { }).catch(() => { })
  424. }
  425. } else {
  426. this.$message.warning('出现未知错误,请重试')
  427. }
  428. })
  429. },
  430. pageJump () {
  431. const data = { componentsManagementType: 'bizComponent' }
  432. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  433. this.$router.push({ path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components' })
  434. }
  435. }
  436. }
  437. </script>
  438. <style lang="scss" scoped>
  439. .bs-custom-components {
  440. position: absolute;
  441. display: flex;
  442. flex-direction: column;
  443. width: 100%;
  444. height: 100vh;
  445. color: var(--bs-el-text);
  446. background: var(--bs-background-2);
  447. overflow: hidden;
  448. >* {
  449. box-sizing: border-box;
  450. }
  451. .bs-custom-component-header {
  452. display: flex;
  453. align-items: center;
  454. justify-content: space-between;
  455. height: 50px;
  456. padding: 0 16px;
  457. border-bottom: 4px solid var(--bs-background-1);
  458. background: var(--bs-background-2);
  459. .left-title {
  460. font-size: 16px;
  461. color: var(--bs-el-title);
  462. .logo-wrap {
  463. display: flex;
  464. align-items: center;
  465. }
  466. .menu-img {
  467. width: 18px;
  468. height: 18px;
  469. margin-right: 15px;
  470. margin-left: 9px;
  471. cursor: pointer;
  472. }
  473. }
  474. .right-btn-wrap {
  475. display: flex;
  476. align-items: center;
  477. height: 100%;
  478. }
  479. }
  480. .bs-custom-component-content {
  481. flex: 1;
  482. background: var(--bs-background-2);
  483. display: flex;
  484. flex-direction: column;
  485. .bs-custom-component-content-code {
  486. display: flex;
  487. justify-content: space-between;
  488. width: 100%;
  489. height: 354px;
  490. padding: 5px 16px;
  491. .left-vue-code {
  492. width: 60%;
  493. height: 100%;
  494. /* background: var(--bs-background-1); */
  495. }
  496. .right-setting-code {
  497. width: calc(40% - 16px);
  498. height: 100%;
  499. /* background: var(--bs-background-1); */
  500. }
  501. .component-code {
  502. .code-tab-header {
  503. display: flex;
  504. align-items: center;
  505. justify-content: space-between;
  506. height: 40px;
  507. .code-tab-left {
  508. height: 100%;
  509. width: 450px;
  510. display: flex;
  511. flex-direction: row;
  512. align-items: center;
  513. justify-content: space-between;
  514. .code-tab-btn {
  515. // width: 90px;
  516. cursor: pointer;
  517. text-align: center;
  518. }
  519. .code-tab {
  520. font-size: 14px;
  521. align-items: center;
  522. justify-content: center;
  523. width: 120px;
  524. height: 100%;
  525. color: var(--bs-el-title);
  526. background: var(--bs-background-1);
  527. }
  528. }
  529. .code-tab {
  530. font-size: 14px;
  531. display: flex;
  532. align-items: center;
  533. justify-content: center;
  534. width: 120px;
  535. height: 100%;
  536. color: var(--bs-el-title);
  537. background: var(--bs-background-1);
  538. }
  539. }
  540. .code-tab-content {
  541. height: calc(100% - 88px);
  542. background: var(--bs-background-1);
  543. }
  544. }
  545. }
  546. .bs-custom-component-content-preview {
  547. flex: 1;
  548. width: 100%;
  549. height: 50%;
  550. padding: 0 16px 16px;
  551. .bs-preview-inner {
  552. width: 100%;
  553. height: 100%;
  554. background: var(--bs-background-1);
  555. position: relative;
  556. .code-tab-header {
  557. height: 40px;
  558. display: flex;
  559. flex-direction: row;
  560. align-items: center;
  561. background-color: var(--bs-background-2);
  562. .code-tab {
  563. font-size: 14px;
  564. align-items: center;
  565. justify-content: center;
  566. display: flex;
  567. width: 120px;
  568. margin-right: 20px;
  569. height: 100%;
  570. color: var(--bs-el-title);
  571. background: var(--bs-background-1);
  572. }
  573. }
  574. }
  575. }
  576. }
  577. }
  578. </style>
  579. <style>
  580. .cm-s-material-darker.CodeMirror,
  581. .cm-s-material-darker .CodeMirror-gutters {
  582. background: var(--bs-background-1) !important;
  583. }
  584. .CodeMirror-scroll {
  585. background-color: var(--bs-background-1) !important;
  586. }
  587. .CodeMirror-gutters {
  588. border-right: 1px solid var(--bs-background-1) !important;
  589. background-color: var(--bs-background-1) !important;
  590. }
  591. .CodeMirror-vscrollbar {
  592. right: 0;
  593. top: 0;
  594. overflow-x: hidden;
  595. overflow-y: scroll;
  596. margin-right: 4px;
  597. }
  598. /* Webkit浏览器滚动条样式 */
  599. .CodeMirror-vscrollbar::-webkit-scrollbar {
  600. width: 6px;
  601. /* 滚动条宽度 */
  602. }
  603. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
  604. background-color: #444851;
  605. /* 滚动条滑块颜色 */
  606. border-radius: 4px;
  607. /* 滚动条滑块圆角 */
  608. }
  609. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover {
  610. background-color: #444851;
  611. /* 滚动条滑块悬停时颜色 */
  612. }
  613. /* Firefox和新版Chrome浏览器滚动条样式 */
  614. .CodeMirror-vscrollbar {
  615. scrollbar-width: thin;
  616. /* 滚动条宽度 */
  617. scrollbar-color: #444851 #444851;
  618. /* 滚动条颜色 */
  619. }
  620. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
  621. background-color: #444851;
  622. /* 滚动条滑块颜色 */
  623. }
  624. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover {
  625. background-color: #444851;
  626. /* 滚动条滑块悬停时颜色 */
  627. }
  628. </style>