index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. 生成图片
  18. </CusBtn>
  19. <CusBtn
  20. :loading="loading"
  21. @click="save"
  22. >
  23. 保存
  24. </CusBtn>
  25. </div>
  26. </div>
  27. <div class="bs-custom-component-content">
  28. <div class="bs-custom-component-content-code">
  29. <div class="left-vue-code component-code">
  30. <div class="code-tab-header">
  31. <div class="code-tab">
  32. VUE组件
  33. </div>
  34. <div class="upload-btn">
  35. <CusBtn @click="upload('vueContent')">
  36. 上传
  37. </CusBtn>
  38. </div>
  39. </div>
  40. <div class="code-tab-content">
  41. <!-- <MonacoEditor
  42. ref="vueContent"
  43. v-model="form.vueContent"
  44. class="editor"
  45. language="html"
  46. /> -->
  47. <codemirror
  48. v-model="form.vueContent"
  49. :options="vueOptions"
  50. />
  51. </div>
  52. </div>
  53. <div class="right-setting-code component-code">
  54. <div class="code-tab-header">
  55. <div class="code-tab">
  56. 组件配置
  57. </div>
  58. <div class="upload-btn">
  59. <CusBtn @click="upload('settingContent')">
  60. 上传
  61. </CusBtn>
  62. </div>
  63. </div>
  64. <div class="code-tab-content">
  65. <!-- <MonacoEditor
  66. ref="settingContent"
  67. v-model="form.settingContent"
  68. class="editor"
  69. language="javascript"
  70. /> -->
  71. <codemirror
  72. v-model="form.settingContent"
  73. :options="settingOptions"
  74. />
  75. </div>
  76. </div>
  77. </div>
  78. <div class="bs-custom-component-content-preview">
  79. <div class="bs-preview-inner">
  80. <BizComponentPreview
  81. :vue-content="form.vueContent"
  82. :setting-content="form.settingContent"
  83. />
  84. </div>
  85. </div>
  86. <!-- 通过计算属性发现accept有问题 -->
  87. <input
  88. ref="vueContentFile"
  89. style="display: none"
  90. type="file"
  91. name="file"
  92. accept=".vue"
  93. @change="handleBatchUpload"
  94. >
  95. <input
  96. ref="settingContentFile"
  97. style="display: none"
  98. type="file"
  99. name="file"
  100. accept=".js"
  101. @change="handleBatchUpload"
  102. >
  103. </div>
  104. </div>
  105. </template>
  106. <script>
  107. import CusBtn from 'packages/BigScreenDesign/BtnLoading'
  108. // import MonacoEditor from 'packages/MonacoEditor'
  109. import BizComponentPreview from './Preview'
  110. import { getBizComponentInfo, updateBizComponent } from 'packages/js/api/bigScreenApi'
  111. import { defaultSettingContent, defaultVueContent } from './config/defaultBizConfig'
  112. import { codemirror } from 'vue-codemirror'
  113. import 'codemirror/lib/codemirror.css'
  114. import 'codemirror/theme/material-darker.css'
  115. import 'codemirror/addon/selection/active-line.js'
  116. import 'codemirror/mode/vue/vue.js'
  117. export default {
  118. name: 'BizComponentDesign',
  119. components: {
  120. CusBtn,
  121. // MonacoEditor,
  122. codemirror,
  123. BizComponentPreview
  124. },
  125. props: {},
  126. data () {
  127. return {
  128. form: {
  129. name: '',
  130. coverPicture: '',
  131. settingContent: '',
  132. vueContent: ''
  133. },
  134. currentContentType: 'vueContent',
  135. loading: false,
  136. vueOptions: {
  137. foldGutter: true,
  138. lineWrapping: true,
  139. gutters: [
  140. 'CodeMirror-linenumbers',
  141. 'CodeMirror-foldgutter',
  142. 'CodeMirror-lint-markers'
  143. ],
  144. theme: 'material-darker',
  145. tabSize: 4,
  146. lineNumbers: true,
  147. line: true,
  148. indentWithTabs: true,
  149. smartIndent: true,
  150. autofocus: false,
  151. matchBrackets: true,
  152. mode: 'text/x-vue',
  153. hintOptions: {
  154. completeSingle: false
  155. },
  156. lint: true
  157. },
  158. settingOptions: {
  159. foldGutter: true,
  160. lineWrapping: true,
  161. gutters: [
  162. 'CodeMirror-linenumbers',
  163. 'CodeMirror-foldgutter',
  164. 'CodeMirror-lint-markers'
  165. ],
  166. theme: 'material-darker',
  167. tabSize: 4,
  168. lineNumbers: true,
  169. line: true,
  170. indentWithTabs: true,
  171. smartIndent: true,
  172. autofocus: false,
  173. matchBrackets: true,
  174. mode: 'text/javascript',
  175. hintOptions: {
  176. completeSingle: false
  177. },
  178. lint: true
  179. }
  180. }
  181. },
  182. computed: {
  183. },
  184. mounted () {
  185. this.getBizComponentInfo()
  186. },
  187. methods: {
  188. getBizComponentInfo () {
  189. const code = this.$route.query.code
  190. if (code) {
  191. getBizComponentInfo(code).then(data => {
  192. this.form = {
  193. ...data,
  194. name: data.name,
  195. coverPicture: data.coverPicture,
  196. settingContent: data.settingContent || defaultSettingContent,
  197. vueContent: data.vueContent || defaultVueContent
  198. }
  199. // this.$refs.vueContent.editor.setValue(this.form.vueContent)
  200. // this.$refs.settingContent.editor.setValue(this.form.settingContent)
  201. })
  202. }
  203. },
  204. upload (type) {
  205. this.currentContentType = type
  206. this.$refs[`${this.currentContentType}File`].click()
  207. },
  208. handleBatchUpload (source) {
  209. const file = source.target.files
  210. const reader = new FileReader() // 新建一个FileReader
  211. reader.readAsText(file[0], 'UTF-8') // 读取文件
  212. reader.onload = (event) => {
  213. const sileString = event.target.result // 读取文件内容
  214. this.form[this.currentContentType] = sileString
  215. // input通过onchange事件来触发js代码的,由于两次文件是重复的,所以这个时候onchange事件是没有触发到的,所以需要手动清空input的值
  216. source.target.value = ''
  217. }
  218. },
  219. backManagement () {
  220. this.$router.push({
  221. path: window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components'
  222. })
  223. },
  224. save () {
  225. this.loading = true
  226. updateBizComponent(this.form).then(() => {
  227. this.$message.success('保存成功')
  228. }).catch(() => {
  229. this.$message.error('保存失败')
  230. }).finally(() => {
  231. this.loading = false
  232. })
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. .bs-custom-components {
  239. position: absolute;
  240. display: flex;
  241. flex-direction: column;
  242. width: 100%;
  243. height: 100vh;
  244. color: var(--bs-el-text);
  245. background: var(--bs-background-2);
  246. overflow: hidden;
  247. > * {
  248. box-sizing: border-box;
  249. }
  250. .bs-custom-component-header {
  251. display: flex;
  252. align-items: center;
  253. justify-content: space-between;
  254. height: 50px;
  255. padding: 0 16px;
  256. border-bottom: 4px solid var(--bs-background-1);
  257. background: var(--bs-background-2);
  258. .left-title {
  259. font-size: 16px;
  260. color: var(--bs-el-title);
  261. .logo-wrap {
  262. display: flex;
  263. align-items: center;
  264. }
  265. .menu-img {
  266. width: 18px;
  267. height: 18px;
  268. margin-right: 15px;
  269. margin-left: 9px;
  270. cursor: pointer;
  271. }
  272. }
  273. .right-btn-wrap {
  274. display: flex;
  275. align-items: center;
  276. height: 100%;
  277. }
  278. }
  279. .bs-custom-component-content {
  280. flex: 1;
  281. background: var(--bs-background-2);
  282. display: flex;
  283. flex-direction: column;
  284. .bs-custom-component-content-code {
  285. display: flex;
  286. justify-content: space-between;
  287. width: 100%;
  288. height: 400px;
  289. padding: 16px;
  290. .left-vue-code {
  291. width: 60%;
  292. height: 100%;
  293. /* background: var(--bs-background-1); */
  294. }
  295. .right-setting-code {
  296. width: calc(40% - 16px);
  297. height: 100%;
  298. /* background: var(--bs-background-1); */
  299. }
  300. .component-code {
  301. .code-tab-header {
  302. display: flex;
  303. align-items: center;
  304. justify-content: space-between;
  305. height: 40px;
  306. .code-tab {
  307. font-size: 14px;
  308. display: flex;
  309. align-items: center;
  310. justify-content: center;
  311. width: 120px;
  312. height: 100%;
  313. color: var(--bs-el-title);
  314. background: var(--bs-background-1);
  315. }
  316. }
  317. .code-tab-content {
  318. height: calc(100% - 40px);
  319. background: var(--bs-background-1);
  320. }
  321. }
  322. }
  323. .bs-custom-component-content-preview {
  324. flex: 1;
  325. width: 100%;
  326. height: 50%;
  327. padding: 0 16px 16px;
  328. .bs-preview-inner {
  329. width: 100%;
  330. height: 100%;
  331. background: var(--bs-background-1);
  332. position: relative;
  333. }
  334. }
  335. }
  336. }
  337. </style>
  338. <style>
  339. .cm-s-material-darker.CodeMirror,
  340. .cm-s-material-darker .CodeMirror-gutters
  341. {
  342. background: var(--bs-background-1) !important;
  343. }
  344. .CodeMirror-scroll {
  345. background-color: var(--bs-background-1) !important;
  346. }
  347. .CodeMirror-gutters {
  348. border-right: 1px solid var(--bs-background-1) !important;
  349. background-color: var(--bs-background-1) !important;
  350. }
  351. </style>