index.vue 18 KB

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