index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 'data-room-ui/BigScreenDesign/BtnLoading'
  112. // import MonacoEditor from 'data-room-ui/MonacoEditor'
  113. import BizComponentPreview from './Preview'
  114. import { getBizComponentInfo, updateBizComponent } from 'data-room-ui/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 'data-room-ui/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({ path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components' })
  231. const data = { componentsManagementType: 'bizComponent' }
  232. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  233. },
  234. save () {
  235. this.loading = true
  236. const node = document.querySelector('.bs-preview-inner')
  237. toJpeg(node, { quality: 0.2 })
  238. .then((dataUrl) => {
  239. const that = this
  240. if (showSize(dataUrl) > 200) {
  241. const url = dataURLtoBlob(dataUrl)
  242. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  243. imageConversion
  244. .compressAccurately(url, {
  245. size: 200, // 图片大小压缩到100kb
  246. width: 1280, // 宽度压缩到1280
  247. height: 720 // 高度压缩到720
  248. })
  249. .then((res) => {
  250. translateBlobToBase64(res, function (e) {
  251. this.form.coverPicture = e.result
  252. updateBizComponent(this.form)
  253. .then((res) => {
  254. that.$message.success('保存成功')
  255. })
  256. .finally(() => {
  257. that.loading = false
  258. })
  259. })
  260. })
  261. } else {
  262. this.form.coverPicture = dataUrl
  263. updateBizComponent(this.form)
  264. .then(() => {
  265. this.$message.success('保存成功')
  266. })
  267. .finally(() => {
  268. this.loading = false
  269. })
  270. }
  271. })
  272. .catch(() => {
  273. this.loading = false
  274. })
  275. },
  276. createdImg () {
  277. this.loading = true
  278. const node = document.querySelector('.bs-preview-inner')
  279. toPng(node)
  280. .then((dataUrl) => {
  281. const link = document.createElement('a')
  282. link.download = `${this.form.name}.png`
  283. link.href = dataUrl
  284. link.click()
  285. link.addEventListener('click', () => {
  286. link.remove()
  287. })
  288. this.loading = false
  289. })
  290. .catch(() => {
  291. this.$message.warning('出现未知错误,请重试')
  292. this.loading = false
  293. })
  294. }
  295. }
  296. }
  297. </script>
  298. <style lang="scss" scoped>
  299. .bs-custom-components {
  300. position: absolute;
  301. display: flex;
  302. flex-direction: column;
  303. width: 100%;
  304. height: 100vh;
  305. color: var(--bs-el-text);
  306. background: var(--bs-background-2);
  307. overflow: hidden;
  308. > * {
  309. box-sizing: border-box;
  310. }
  311. .bs-custom-component-header {
  312. display: flex;
  313. align-items: center;
  314. justify-content: space-between;
  315. height: 50px;
  316. padding: 0 16px;
  317. border-bottom: 4px solid var(--bs-background-1);
  318. background: var(--bs-background-2);
  319. .left-title {
  320. font-size: 16px;
  321. color: var(--bs-el-title);
  322. .logo-wrap {
  323. display: flex;
  324. align-items: center;
  325. }
  326. .menu-img {
  327. width: 18px;
  328. height: 18px;
  329. margin-right: 15px;
  330. margin-left: 9px;
  331. cursor: pointer;
  332. }
  333. }
  334. .right-btn-wrap {
  335. display: flex;
  336. align-items: center;
  337. height: 100%;
  338. }
  339. }
  340. .bs-custom-component-content {
  341. flex: 1;
  342. background: var(--bs-background-2);
  343. display: flex;
  344. flex-direction: column;
  345. .bs-custom-component-content-code {
  346. display: flex;
  347. justify-content: space-between;
  348. width: 100%;
  349. height: 400px;
  350. padding: 16px;
  351. .left-vue-code {
  352. width: 60%;
  353. height: 100%;
  354. /* background: var(--bs-background-1); */
  355. }
  356. .right-setting-code {
  357. width: calc(40% - 16px);
  358. height: 100%;
  359. /* background: var(--bs-background-1); */
  360. }
  361. .component-code {
  362. .code-tab-header {
  363. display: flex;
  364. align-items: center;
  365. justify-content: space-between;
  366. height: 40px;
  367. .code-tab {
  368. font-size: 14px;
  369. display: flex;
  370. align-items: center;
  371. justify-content: center;
  372. width: 120px;
  373. height: 100%;
  374. color: var(--bs-el-title);
  375. background: var(--bs-background-1);
  376. }
  377. }
  378. .code-tab-content {
  379. height: calc(100% - 40px);
  380. background: var(--bs-background-1);
  381. }
  382. }
  383. }
  384. .bs-custom-component-content-preview {
  385. flex: 1;
  386. width: 100%;
  387. height: 50%;
  388. padding: 0 16px 16px;
  389. .bs-preview-inner {
  390. width: 100%;
  391. height: 100%;
  392. background: var(--bs-background-1);
  393. position: relative;
  394. }
  395. }
  396. }
  397. }
  398. </style>
  399. <style>
  400. .cm-s-material-darker.CodeMirror,
  401. .cm-s-material-darker .CodeMirror-gutters
  402. {
  403. background: var(--bs-background-1) !important;
  404. }
  405. .CodeMirror-scroll {
  406. background-color: var(--bs-background-1) !important;
  407. }
  408. .CodeMirror-gutters {
  409. border-right: 1px solid var(--bs-background-1) !important;
  410. background-color: var(--bs-background-1) !important;
  411. }
  412. </style>