index.vue 17 KB

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