index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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.native="createdImg()"
  19. >
  20. 生成图片
  21. </CusBtn>
  22. <CusBtn
  23. :loading="loading"
  24. @click="save"
  25. >
  26. 保存
  27. </CusBtn>
  28. </div>
  29. </div>
  30. <div class="bs-custom-component-content">
  31. <div class="bs-custom-component-content-code">
  32. <div class="left-vue-code component-code">
  33. <div class="code-tab-header">
  34. <div class="code-tab">
  35. VUE组件
  36. </div>
  37. <div class="upload-btn">
  38. <CusBtn @click="upload('vueContent')">
  39. 上传
  40. </CusBtn>
  41. </div>
  42. </div>
  43. <div class="code-tab-content">
  44. <!-- <MonacoEditor
  45. ref="vueContent"
  46. v-model="form.vueContent"
  47. class="editor"
  48. language="html"
  49. /> -->
  50. <codemirror
  51. v-model="form.vueContent"
  52. :options="vueOptions"
  53. />
  54. </div>
  55. </div>
  56. <div class="right-setting-code component-code">
  57. <div class="code-tab-header">
  58. <div class="code-tab">
  59. 组件配置
  60. </div>
  61. <div class="upload-btn">
  62. <CusBtn @click="upload('settingContent')">
  63. 上传
  64. </CusBtn>
  65. </div>
  66. </div>
  67. <div class="code-tab-content">
  68. <!-- <MonacoEditor
  69. ref="settingContent"
  70. v-model="form.settingContent"
  71. class="editor"
  72. language="javascript"
  73. /> -->
  74. <codemirror
  75. v-model="form.settingContent"
  76. :options="settingOptions"
  77. />
  78. </div>
  79. </div>
  80. </div>
  81. <div class="bs-custom-component-content-preview">
  82. <div class="bs-preview-inner">
  83. <div class="code-tab-header">
  84. <div class="code-tab">
  85. 效果预览
  86. </div>
  87. </div>
  88. <BizComponentPreview
  89. :vue-content="form.vueContent"
  90. :setting-content="form.settingContent"
  91. />
  92. </div>
  93. </div>
  94. <!-- 通过计算属性发现accept有问题 -->
  95. <input
  96. ref="vueContentFile"
  97. style="display: none"
  98. type="file"
  99. name="file"
  100. accept=".vue"
  101. @change="handleBatchUpload"
  102. >
  103. <input
  104. ref="settingContentFile"
  105. style="display: none"
  106. type="file"
  107. name="file"
  108. accept=".js"
  109. @change="handleBatchUpload"
  110. >
  111. </div>
  112. </div>
  113. </template>
  114. <script>
  115. import { toJpeg, toPng } from 'html-to-image'
  116. import CusBtn from 'data-room-ui/BigScreenDesign/BtnLoading'
  117. // import MonacoEditor from 'data-room-ui/MonacoEditor'
  118. import BizComponentPreview from './Preview'
  119. import { getBizComponentInfo, updateBizComponent } from 'data-room-ui/js/api/bigScreenApi'
  120. import { defaultSettingContent, defaultVueContent } from './config/defaultBizConfig'
  121. import { codemirror } from 'vue-codemirror'
  122. import 'codemirror/lib/codemirror.css'
  123. import 'codemirror/theme/material-darker.css'
  124. import 'codemirror/addon/selection/active-line.js'
  125. import 'codemirror/mode/vue/vue.js'
  126. import {
  127. showSize,
  128. dataURLtoBlob,
  129. translateBlobToBase64
  130. } from 'data-room-ui/js/utils/compressImg'
  131. import * as imageConversion from 'image-conversion'
  132. export default {
  133. name: 'BizComponentDesign',
  134. components: {
  135. CusBtn,
  136. // MonacoEditor,
  137. codemirror,
  138. BizComponentPreview
  139. },
  140. props: {},
  141. data () {
  142. return {
  143. form: {
  144. name: '',
  145. coverPicture: '',
  146. settingContent: '',
  147. vueContent: ''
  148. },
  149. currentContentType: 'vueContent',
  150. loading: false,
  151. vueOptions: {
  152. foldGutter: true,
  153. lineWrapping: true,
  154. gutters: [
  155. 'CodeMirror-linenumbers',
  156. 'CodeMirror-foldgutter',
  157. 'CodeMirror-lint-markers'
  158. ],
  159. theme: 'material-darker',
  160. tabSize: 4,
  161. lineNumbers: true,
  162. line: true,
  163. indentWithTabs: true,
  164. smartIndent: true,
  165. autofocus: false,
  166. matchBrackets: true,
  167. mode: 'text/x-vue',
  168. hintOptions: {
  169. completeSingle: false
  170. },
  171. lint: true
  172. },
  173. settingOptions: {
  174. foldGutter: true,
  175. lineWrapping: true,
  176. gutters: [
  177. 'CodeMirror-linenumbers',
  178. 'CodeMirror-foldgutter',
  179. 'CodeMirror-lint-markers'
  180. ],
  181. theme: 'material-darker',
  182. tabSize: 4,
  183. lineNumbers: true,
  184. line: true,
  185. indentWithTabs: true,
  186. smartIndent: true,
  187. autofocus: false,
  188. matchBrackets: true,
  189. mode: 'text/javascript',
  190. hintOptions: {
  191. completeSingle: false
  192. },
  193. lint: true
  194. }
  195. }
  196. },
  197. computed: {
  198. },
  199. mounted () {
  200. this.getBizComponentInfo()
  201. },
  202. methods: {
  203. getBizComponentInfo () {
  204. const code = this.$route.query.code
  205. if (code) {
  206. getBizComponentInfo(code).then(data => {
  207. this.form = {
  208. ...data,
  209. name: data.name,
  210. coverPicture: data.coverPicture,
  211. settingContent: data.settingContent || defaultSettingContent,
  212. vueContent: data.vueContent || defaultVueContent
  213. }
  214. // this.$refs.vueContent.editor.setValue(this.form.vueContent)
  215. // this.$refs.settingContent.editor.setValue(this.form.settingContent)
  216. })
  217. }
  218. },
  219. upload (type) {
  220. this.currentContentType = type
  221. this.$refs[`${this.currentContentType}File`].click()
  222. },
  223. handleBatchUpload (source) {
  224. const file = source.target.files
  225. const reader = new FileReader() // 新建一个FileReader
  226. reader.readAsText(file[0], 'UTF-8') // 读取文件
  227. reader.onload = (event) => {
  228. const sileString = event.target.result // 读取文件内容
  229. this.form[this.currentContentType] = sileString
  230. // input通过onchange事件来触发js代码的,由于两次文件是重复的,所以这个时候onchange事件是没有触发到的,所以需要手动清空input的值
  231. source.target.value = ''
  232. }
  233. },
  234. backManagement () {
  235. // 给出一个确认框提示,提示如下确定返回主页面吗?未保存的配置将会丢失。3个按钮 : 留在页面 、离开页面、保存后离开页面
  236. this.$confirm('确定返回主页面吗?未保存的配置将会丢失。', '提示', {
  237. confirmButtonText: '保存后离开页面',
  238. cancelButtonText: '离开页面',
  239. cancelButtonClass: 'cancel-btn',
  240. cancelButtonColor: '#FF6F6F',
  241. showCancelButton: true,
  242. type: 'warning',
  243. customClass: 'bs-el-message-box'
  244. }).then(() => {
  245. this.save(true)
  246. }).catch(() => {
  247. this.pageJump()
  248. })
  249. },
  250. save (pageJump = false) {
  251. this.loading = true
  252. const node = document.querySelector('.remote-preview-inner-wrap')
  253. // 获取node下的第一个子节点
  254. const childrenNode = node.children[0]
  255. toJpeg(childrenNode, { quality: 0.2 })
  256. .then((dataUrl) => {
  257. const that = this
  258. if (showSize(dataUrl) > 200) {
  259. const url = dataURLtoBlob(dataUrl)
  260. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  261. imageConversion
  262. .compressAccurately(url, {
  263. size: 200, // 图片大小压缩到100kb
  264. width: 1280, // 宽度压缩到1280
  265. height: 720 // 高度压缩到720
  266. })
  267. .then((res) => {
  268. translateBlobToBase64(res, function (e) {
  269. this.form.coverPicture = e.result
  270. updateBizComponent(this.form)
  271. .then((res) => {
  272. that.$message.success('保存成功')
  273. console.log(pageJump)
  274. if (pageJump) {
  275. this.pageJump()
  276. }
  277. })
  278. .finally(() => {
  279. that.loading = false
  280. })
  281. })
  282. })
  283. } else {
  284. this.form.coverPicture = dataUrl
  285. updateBizComponent(this.form)
  286. .then(() => {
  287. console.log(1122)
  288. this.$message.success('保存成功')
  289. if (pageJump) {
  290. this.pageJump()
  291. }
  292. })
  293. .finally(() => {
  294. this.loading = false
  295. })
  296. }
  297. })
  298. .catch(() => {
  299. this.loading = false
  300. })
  301. },
  302. createdImg () {
  303. this.loading = true
  304. const node = document.querySelector('.remote-preview-inner-wrap')
  305. // 获取node下的第一个子节点
  306. const childrenNode = node.children[0]
  307. toPng(childrenNode)
  308. .then((dataUrl) => {
  309. const link = document.createElement('a')
  310. link.download = `${this.form.name}.png`
  311. link.href = dataUrl
  312. link.click()
  313. link.addEventListener('click', () => {
  314. link.remove()
  315. })
  316. this.loading = false
  317. })
  318. .catch(() => {
  319. this.$message.warning('出现未知错误,请重试')
  320. this.loading = false
  321. })
  322. },
  323. pageJump () {
  324. this.$router.push({ path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components' })
  325. const data = { componentsManagementType: 'bizComponent' }
  326. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  327. }
  328. }
  329. }
  330. </script>
  331. <style lang="scss" scoped>
  332. .bs-custom-components {
  333. position: absolute;
  334. display: flex;
  335. flex-direction: column;
  336. width: 100%;
  337. height: 100vh;
  338. color: var(--bs-el-text);
  339. background: var(--bs-background-2);
  340. overflow: hidden;
  341. > * {
  342. box-sizing: border-box;
  343. }
  344. .bs-custom-component-header {
  345. display: flex;
  346. align-items: center;
  347. justify-content: space-between;
  348. height: 50px;
  349. padding: 0 16px;
  350. border-bottom: 4px solid var(--bs-background-1);
  351. background: var(--bs-background-2);
  352. .left-title {
  353. font-size: 16px;
  354. color: var(--bs-el-title);
  355. .logo-wrap {
  356. display: flex;
  357. align-items: center;
  358. }
  359. .menu-img {
  360. width: 18px;
  361. height: 18px;
  362. margin-right: 15px;
  363. margin-left: 9px;
  364. cursor: pointer;
  365. }
  366. }
  367. .right-btn-wrap {
  368. display: flex;
  369. align-items: center;
  370. height: 100%;
  371. }
  372. }
  373. .bs-custom-component-content {
  374. flex: 1;
  375. background: var(--bs-background-2);
  376. display: flex;
  377. flex-direction: column;
  378. .bs-custom-component-content-code {
  379. display: flex;
  380. justify-content: space-between;
  381. width: 100%;
  382. height: 400px;
  383. padding: 16px;
  384. .left-vue-code {
  385. width: 60%;
  386. height: 100%;
  387. /* background: var(--bs-background-1); */
  388. }
  389. .right-setting-code {
  390. width: calc(40% - 16px);
  391. height: 100%;
  392. /* background: var(--bs-background-1); */
  393. }
  394. .component-code {
  395. .code-tab-header {
  396. display: flex;
  397. align-items: center;
  398. justify-content: space-between;
  399. height: 40px;
  400. .code-tab {
  401. font-size: 14px;
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. width: 120px;
  406. height: 100%;
  407. color: var(--bs-el-title);
  408. background: var(--bs-background-1);
  409. }
  410. }
  411. .code-tab-content {
  412. height: calc(100% - 40px);
  413. background: var(--bs-background-1);
  414. }
  415. }
  416. }
  417. .bs-custom-component-content-preview {
  418. flex: 1;
  419. width: 100%;
  420. height: 50%;
  421. padding: 0 16px 16px;
  422. .bs-preview-inner {
  423. width: 100%;
  424. height: 100%;
  425. background: var(--bs-background-1);
  426. position: relative;
  427. .code-tab-header{
  428. height: 40px;
  429. background-color: var(--bs-background-2);
  430. line-height: 40px;
  431. .code-tab{
  432. width: 120px;
  433. height:40px;
  434. text-align: center;
  435. background-color: var(--bs-background-1);
  436. }
  437. }
  438. }
  439. }
  440. }
  441. }
  442. </style>
  443. <style>
  444. .cm-s-material-darker.CodeMirror,
  445. .cm-s-material-darker .CodeMirror-gutters
  446. {
  447. background: var(--bs-background-1) !important;
  448. }
  449. .CodeMirror-scroll {
  450. background-color: var(--bs-background-1) !important;
  451. }
  452. .CodeMirror-gutters {
  453. border-right: 1px solid var(--bs-background-1) !important;
  454. background-color: var(--bs-background-1) !important;
  455. }
  456. </style>