index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. <BizComponentPreview
  84. :vue-content="form.vueContent"
  85. :setting-content="form.settingContent"
  86. />
  87. </div>
  88. </div>
  89. <!-- 通过计算属性发现accept有问题 -->
  90. <input
  91. ref="vueContentFile"
  92. style="display: none"
  93. type="file"
  94. name="file"
  95. accept=".vue"
  96. @change="handleBatchUpload"
  97. >
  98. <input
  99. ref="settingContentFile"
  100. style="display: none"
  101. type="file"
  102. name="file"
  103. accept=".js"
  104. @change="handleBatchUpload"
  105. >
  106. </div>
  107. </div>
  108. </template>
  109. <script>
  110. import { toJpeg, toPng } from 'html-to-image'
  111. import CusBtn from 'packages/BigScreenDesign/BtnLoading'
  112. // import MonacoEditor from 'packages/MonacoEditor'
  113. import BizComponentPreview from './Preview'
  114. import { getBizComponentInfo, updateBizComponent } from 'packages/js/api/bigScreenApi'
  115. import { defaultSettingContent, defaultVueContent } from './config/defaultBizConfig'
  116. import { codemirror } from 'vue-codemirror'
  117. import 'codemirror/lib/codemirror.css'
  118. import 'codemirror/theme/material-darker.css'
  119. import 'codemirror/addon/selection/active-line.js'
  120. import 'codemirror/mode/vue/vue.js'
  121. import {
  122. showSize,
  123. dataURLtoBlob,
  124. translateBlobToBase64
  125. } from 'packages/js/utils/compressImg'
  126. import * as imageConversion from 'image-conversion'
  127. export default {
  128. name: 'BizComponentDesign',
  129. components: {
  130. CusBtn,
  131. // MonacoEditor,
  132. codemirror,
  133. BizComponentPreview
  134. },
  135. props: {},
  136. beforeRouteLeave (to, from, next) {
  137. // 在离开当前页面之前执行逻辑
  138. const data = { componentsManagementType: 'bizComponent' }
  139. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  140. next() // 继续导航
  141. },
  142. data () {
  143. return {
  144. form: {
  145. name: '',
  146. coverPicture: '',
  147. settingContent: '',
  148. vueContent: ''
  149. },
  150. currentContentType: 'vueContent',
  151. loading: false,
  152. vueOptions: {
  153. foldGutter: true,
  154. lineWrapping: true,
  155. gutters: [
  156. 'CodeMirror-linenumbers',
  157. 'CodeMirror-foldgutter',
  158. 'CodeMirror-lint-markers'
  159. ],
  160. theme: 'material-darker',
  161. tabSize: 4,
  162. lineNumbers: true,
  163. line: true,
  164. indentWithTabs: true,
  165. smartIndent: true,
  166. autofocus: false,
  167. matchBrackets: true,
  168. mode: 'text/x-vue',
  169. hintOptions: {
  170. completeSingle: false
  171. },
  172. lint: true
  173. },
  174. settingOptions: {
  175. foldGutter: true,
  176. lineWrapping: true,
  177. gutters: [
  178. 'CodeMirror-linenumbers',
  179. 'CodeMirror-foldgutter',
  180. 'CodeMirror-lint-markers'
  181. ],
  182. theme: 'material-darker',
  183. tabSize: 4,
  184. lineNumbers: true,
  185. line: true,
  186. indentWithTabs: true,
  187. smartIndent: true,
  188. autofocus: false,
  189. matchBrackets: true,
  190. mode: 'text/javascript',
  191. hintOptions: {
  192. completeSingle: false
  193. },
  194. lint: true
  195. }
  196. }
  197. },
  198. computed: {
  199. },
  200. mounted () {
  201. this.getBizComponentInfo()
  202. },
  203. methods: {
  204. getBizComponentInfo () {
  205. const code = this.$route.query.code
  206. if (code) {
  207. getBizComponentInfo(code).then(data => {
  208. this.form = {
  209. ...data,
  210. name: data.name,
  211. coverPicture: data.coverPicture,
  212. settingContent: data.settingContent || defaultSettingContent,
  213. vueContent: data.vueContent || defaultVueContent
  214. }
  215. // this.$refs.vueContent.editor.setValue(this.form.vueContent)
  216. // this.$refs.settingContent.editor.setValue(this.form.settingContent)
  217. })
  218. }
  219. },
  220. upload (type) {
  221. this.currentContentType = type
  222. this.$refs[`${this.currentContentType}File`].click()
  223. },
  224. handleBatchUpload (source) {
  225. const file = source.target.files
  226. const reader = new FileReader() // 新建一个FileReader
  227. reader.readAsText(file[0], 'UTF-8') // 读取文件
  228. reader.onload = (event) => {
  229. const sileString = event.target.result // 读取文件内容
  230. this.form[this.currentContentType] = sileString
  231. // input通过onchange事件来触发js代码的,由于两次文件是重复的,所以这个时候onchange事件是没有触发到的,所以需要手动清空input的值
  232. source.target.value = ''
  233. }
  234. },
  235. backManagement () {
  236. this.$router.push({
  237. path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components'
  238. })
  239. },
  240. save () {
  241. this.loading = true
  242. const node = document.querySelector('.bs-preview-inner')
  243. toJpeg(node, { quality: 0.2 })
  244. .then((dataUrl) => {
  245. const that = this
  246. if (showSize(dataUrl) > 200) {
  247. const url = dataURLtoBlob(dataUrl)
  248. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  249. imageConversion
  250. .compressAccurately(url, {
  251. size: 200, // 图片大小压缩到100kb
  252. width: 1280, // 宽度压缩到1280
  253. height: 720 // 高度压缩到720
  254. })
  255. .then((res) => {
  256. translateBlobToBase64(res, function (e) {
  257. this.form.coverPicture = e.result
  258. updateBizComponent(this.form)
  259. .then((res) => {
  260. that.$message.success('保存成功')
  261. })
  262. .finally(() => {
  263. that.loading = false
  264. })
  265. })
  266. })
  267. } else {
  268. this.form.coverPicture = dataUrl
  269. updateBizComponent(this.form)
  270. .then(() => {
  271. this.$message.success('保存成功')
  272. })
  273. .finally(() => {
  274. this.loading = false
  275. })
  276. }
  277. })
  278. .catch(() => {
  279. this.loading = false
  280. })
  281. },
  282. createdImg () {
  283. this.loading = true
  284. const node = document.querySelector('.bs-preview-inner')
  285. toPng(node)
  286. .then((dataUrl) => {
  287. const link = document.createElement('a')
  288. link.download = `${this.form.name}.png`
  289. link.href = dataUrl
  290. link.click()
  291. link.addEventListener('click', () => {
  292. link.remove()
  293. })
  294. this.loading = false
  295. })
  296. .catch(() => {
  297. this.$message.warning('出现未知错误,请重试')
  298. this.loading = false
  299. })
  300. }
  301. }
  302. }
  303. </script>
  304. <style lang="scss" scoped>
  305. .bs-custom-components {
  306. position: absolute;
  307. display: flex;
  308. flex-direction: column;
  309. width: 100%;
  310. height: 100vh;
  311. color: var(--bs-el-text);
  312. background: var(--bs-background-2);
  313. overflow: hidden;
  314. > * {
  315. box-sizing: border-box;
  316. }
  317. .bs-custom-component-header {
  318. display: flex;
  319. align-items: center;
  320. justify-content: space-between;
  321. height: 50px;
  322. padding: 0 16px;
  323. border-bottom: 4px solid var(--bs-background-1);
  324. background: var(--bs-background-2);
  325. .left-title {
  326. font-size: 16px;
  327. color: var(--bs-el-title);
  328. .logo-wrap {
  329. display: flex;
  330. align-items: center;
  331. }
  332. .menu-img {
  333. width: 18px;
  334. height: 18px;
  335. margin-right: 15px;
  336. margin-left: 9px;
  337. cursor: pointer;
  338. }
  339. }
  340. .right-btn-wrap {
  341. display: flex;
  342. align-items: center;
  343. height: 100%;
  344. }
  345. }
  346. .bs-custom-component-content {
  347. flex: 1;
  348. background: var(--bs-background-2);
  349. display: flex;
  350. flex-direction: column;
  351. .bs-custom-component-content-code {
  352. display: flex;
  353. justify-content: space-between;
  354. width: 100%;
  355. height: 400px;
  356. padding: 16px;
  357. .left-vue-code {
  358. width: 60%;
  359. height: 100%;
  360. /* background: var(--bs-background-1); */
  361. }
  362. .right-setting-code {
  363. width: calc(40% - 16px);
  364. height: 100%;
  365. /* background: var(--bs-background-1); */
  366. }
  367. .component-code {
  368. .code-tab-header {
  369. display: flex;
  370. align-items: center;
  371. justify-content: space-between;
  372. height: 40px;
  373. .code-tab {
  374. font-size: 14px;
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. width: 120px;
  379. height: 100%;
  380. color: var(--bs-el-title);
  381. background: var(--bs-background-1);
  382. }
  383. }
  384. .code-tab-content {
  385. height: calc(100% - 40px);
  386. background: var(--bs-background-1);
  387. }
  388. }
  389. }
  390. .bs-custom-component-content-preview {
  391. flex: 1;
  392. width: 100%;
  393. height: 50%;
  394. padding: 0 16px 16px;
  395. .bs-preview-inner {
  396. width: 100%;
  397. height: 100%;
  398. background: var(--bs-background-1);
  399. position: relative;
  400. }
  401. }
  402. }
  403. }
  404. </style>
  405. <style>
  406. .cm-s-material-darker.CodeMirror,
  407. .cm-s-material-darker .CodeMirror-gutters
  408. {
  409. background: var(--bs-background-1) !important;
  410. }
  411. .CodeMirror-scroll {
  412. background-color: var(--bs-background-1) !important;
  413. }
  414. .CodeMirror-gutters {
  415. border-right: 1px solid var(--bs-background-1) !important;
  416. background-color: var(--bs-background-1) !important;
  417. }
  418. </style>