index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. if (dataUrl) {
  349. if (showSize(dataUrl) > 200) {
  350. const url = dataURLtoBlob(dataUrl)
  351. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  352. imageConversion.compressAccurately(
  353. url,
  354. {
  355. size: 200, // 图片大小压缩到100kb
  356. width: 1280, // 宽度压缩到1280
  357. height: 720 // 高度压缩到720
  358. }
  359. ).then((res) => {
  360. translateBlobToBase64(res, (e) => {
  361. this.form.coverPicture = e.result
  362. })
  363. })
  364. } else {
  365. this.form.coverPicture = dataUrl
  366. }
  367. } else {
  368. this.form.coverPicture = ''
  369. }
  370. updateBizComponent(this.form).then(() => {
  371. this.$message({
  372. message: '保存成功',
  373. type: 'success',
  374. duration: 800,
  375. onClose: () => {
  376. // 此处写提示关闭后需要执行的函数
  377. if (pageJump) {
  378. this.pageJump()
  379. }
  380. }
  381. })
  382. this.loading = false
  383. }).catch((error) => {
  384. console.info(error)
  385. this.loading = false
  386. })
  387. },
  388. createdImg () {
  389. this.loading = true
  390. const node = document.querySelector('.remote-preview-inner-wrap')
  391. // 获取node下的第一个子节点
  392. const childrenNode = node.children[0]
  393. // 为childrenNode添加一个背景颜色
  394. childrenNode.style.backgroundColor = 'var(--bs-background-1)'
  395. toJpeg(childrenNode)
  396. .then((dataUrl) => {
  397. const link = document.createElement('a')
  398. link.download = `${this.form.name}.png`
  399. link.href = dataUrl
  400. link.click()
  401. link.addEventListener('click', () => {
  402. link.remove()
  403. })
  404. this.loading = false
  405. })
  406. .catch((error) => {
  407. console.info(error)
  408. this.$message.warning('出现未知错误,请重试')
  409. this.loading = false
  410. })
  411. },
  412. pageJump () {
  413. const data = { componentsManagementType: 'bizComponent' }
  414. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  415. this.$router.push({ path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components' })
  416. }
  417. }
  418. }
  419. </script>
  420. <style lang="scss" scoped>
  421. .bs-custom-components {
  422. position: absolute;
  423. display: flex;
  424. flex-direction: column;
  425. width: 100%;
  426. height: 100vh;
  427. color: var(--bs-el-text);
  428. background: var(--bs-background-2);
  429. overflow: hidden;
  430. >* {
  431. box-sizing: border-box;
  432. }
  433. .bs-custom-component-header {
  434. display: flex;
  435. align-items: center;
  436. justify-content: space-between;
  437. height: 50px;
  438. padding: 0 16px;
  439. border-bottom: 4px solid var(--bs-background-1);
  440. background: var(--bs-background-2);
  441. .left-title {
  442. font-size: 16px;
  443. color: var(--bs-el-title);
  444. .logo-wrap {
  445. display: flex;
  446. align-items: center;
  447. }
  448. .menu-img {
  449. width: 18px;
  450. height: 18px;
  451. margin-right: 15px;
  452. margin-left: 9px;
  453. cursor: pointer;
  454. }
  455. }
  456. .right-btn-wrap {
  457. display: flex;
  458. align-items: center;
  459. height: 100%;
  460. }
  461. }
  462. .bs-custom-component-content {
  463. flex: 1;
  464. background: var(--bs-background-2);
  465. display: flex;
  466. flex-direction: column;
  467. .bs-custom-component-content-code {
  468. display: flex;
  469. justify-content: space-between;
  470. width: 100%;
  471. height: 354px;
  472. padding: 5px 16px;
  473. .left-vue-code {
  474. width: 60%;
  475. height: 100%;
  476. /* background: var(--bs-background-1); */
  477. }
  478. .right-setting-code {
  479. width: calc(40% - 16px);
  480. height: 100%;
  481. /* background: var(--bs-background-1); */
  482. }
  483. .component-code {
  484. .code-tab-header {
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. height: 40px;
  489. .code-tab-left {
  490. height: 100%;
  491. width: 450px;
  492. display: flex;
  493. flex-direction: row;
  494. align-items: center;
  495. justify-content: space-between;
  496. .code-tab-btn {
  497. // width: 90px;
  498. cursor: pointer;
  499. text-align: center;
  500. }
  501. .code-tab {
  502. font-size: 14px;
  503. align-items: center;
  504. justify-content: center;
  505. width: 120px;
  506. height: 100%;
  507. color: var(--bs-el-title);
  508. background: var(--bs-background-1);
  509. }
  510. }
  511. .code-tab {
  512. font-size: 14px;
  513. display: flex;
  514. align-items: center;
  515. justify-content: center;
  516. width: 120px;
  517. height: 100%;
  518. color: var(--bs-el-title);
  519. background: var(--bs-background-1);
  520. }
  521. }
  522. .code-tab-content {
  523. height: calc(100% - 88px);
  524. background: var(--bs-background-1);
  525. }
  526. }
  527. }
  528. .bs-custom-component-content-preview {
  529. flex: 1;
  530. width: 100%;
  531. height: 50%;
  532. padding: 0 16px 16px;
  533. .bs-preview-inner {
  534. width: 100%;
  535. height: 100%;
  536. background: var(--bs-background-1);
  537. position: relative;
  538. .code-tab-header {
  539. height: 40px;
  540. display: flex;
  541. flex-direction: row;
  542. align-items: center;
  543. background-color: var(--bs-background-2);
  544. .code-tab {
  545. font-size: 14px;
  546. align-items: center;
  547. justify-content: center;
  548. display: flex;
  549. width: 120px;
  550. margin-right: 20px;
  551. height: 100%;
  552. color: var(--bs-el-title);
  553. background: var(--bs-background-1);
  554. }
  555. }
  556. }
  557. }
  558. }
  559. }
  560. </style>
  561. <style>
  562. .cm-s-material-darker.CodeMirror,
  563. .cm-s-material-darker .CodeMirror-gutters {
  564. background: var(--bs-background-1) !important;
  565. }
  566. .CodeMirror-scroll {
  567. background-color: var(--bs-background-1) !important;
  568. }
  569. .CodeMirror-gutters {
  570. border-right: 1px solid var(--bs-background-1) !important;
  571. background-color: var(--bs-background-1) !important;
  572. }
  573. .CodeMirror-vscrollbar {
  574. right: 0;
  575. top: 0;
  576. overflow-x: hidden;
  577. overflow-y: scroll;
  578. margin-right: 4px;
  579. }
  580. /* Webkit浏览器滚动条样式 */
  581. .CodeMirror-vscrollbar::-webkit-scrollbar {
  582. width: 6px;
  583. /* 滚动条宽度 */
  584. }
  585. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
  586. background-color: #444851;
  587. /* 滚动条滑块颜色 */
  588. border-radius: 4px;
  589. /* 滚动条滑块圆角 */
  590. }
  591. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover {
  592. background-color: #444851;
  593. /* 滚动条滑块悬停时颜色 */
  594. }
  595. /* Firefox和新版Chrome浏览器滚动条样式 */
  596. .CodeMirror-vscrollbar {
  597. scrollbar-width: thin;
  598. /* 滚动条宽度 */
  599. scrollbar-color: #444851 #444851;
  600. /* 滚动条颜色 */
  601. }
  602. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
  603. background-color: #444851;
  604. /* 滚动条滑块颜色 */
  605. }
  606. .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover {
  607. background-color: #444851;
  608. /* 滚动条滑块悬停时颜色 */
  609. }
  610. </style>