index.vue 18 KB

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