index.vue 11 KB

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