index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <div
  3. ref="bs-render-wrap"
  4. :key="`${pageInfo.pageConfig.w}${pageInfo.pageConfig.h}`"
  5. class="bs-render-wrap design-drag-wrap render-theme-wrap"
  6. :style="{
  7. width: pageInfo.pageConfig.w + 'px',
  8. height: pageInfo.pageConfig.h + 'px',
  9. backgroundColor: pageInfo.pageConfig.bgColor,
  10. backgroundImage: `url(${pageInfo.pageConfig.bg})`
  11. }"
  12. @drop="drop($event)"
  13. @dragover.prevent
  14. >
  15. <vdr
  16. v-for="chart in chartList"
  17. :id="chart.code"
  18. :key="chart.updateKey || chart.code"
  19. class="drag-item"
  20. :class="{
  21. 'multiple-selected': activeCodes.includes(chart.code),
  22. }"
  23. :scale-ratio="scale"
  24. :x="chart.x"
  25. :y="chart.y"
  26. :w="chart.w"
  27. :h="chart.h"
  28. :min-width="10"
  29. :min-height="10"
  30. :draggable="!chart.locked"
  31. :resizable="!chart.locked"
  32. :parent="true"
  33. :debug="false"
  34. :is-conflict-check="false"
  35. :snap="true"
  36. :snap-tolerance="2"
  37. :style="{
  38. zIndex: chart.z || 0,
  39. }"
  40. :grid="[1,1]"
  41. @activated="activated(...arguments, chart)"
  42. @dragging="onDrag(...arguments, chart)"
  43. @resizing="onResize(...arguments, chart)"
  44. @resizestop="resizestop(...arguments, chart)"
  45. @dragstop="dragstop(...arguments, chart)"
  46. @refLineParams="getRefLineParams"
  47. @mouseleave.native="resetPresetLineDelay"
  48. >
  49. <Configuration
  50. v-if="isInit"
  51. :config="chart"
  52. @openRightPanel="openRightPanel"
  53. >
  54. <RenderCard
  55. :ref="'RenderCard' + chart.code"
  56. :config="chart"
  57. />
  58. </Configuration>
  59. </vdr>
  60. <span
  61. v-for="(vl, index) in vLine"
  62. v-show="vl.display"
  63. :key="index + 'vLine'"
  64. class="ref-line v-line"
  65. :style="{ left: vl.position, top: vl.origin, height: vl.lineLength }"
  66. />
  67. <span
  68. v-for="(hl, index) in hLine"
  69. v-show="hl.display"
  70. :key="index + 'hLine'"
  71. class="ref-line h-line"
  72. :style="{ top: hl.position, left: hl.origin, width: hl.lineLength }"
  73. />
  74. </div>
  75. </template>
  76. <script>
  77. import { mapState, mapMutations } from 'vuex'
  78. import RenderCard from './RenderCard.vue'
  79. import Configuration from './Configuration.vue'
  80. // import _ from 'lodash'
  81. import cloneDeep from 'lodash/cloneDeep'
  82. import vdr from 'vue-draggable-resizable-gorkys'
  83. import 'vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css'
  84. import { randomString } from '../js/utils'
  85. import { compile } from 'tiny-sass-compiler/dist/tiny-sass-compiler.esm-browser.prod.js'
  86. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  87. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  88. export default {
  89. name: 'BigScreenRender',
  90. components: {
  91. RenderCard,
  92. Configuration,
  93. vdr
  94. },
  95. props: {
  96. ruleKey: {
  97. type: Number,
  98. default: 0
  99. }
  100. },
  101. data () {
  102. return {
  103. vLine: [],
  104. hLine: [],
  105. themeCss: '',
  106. // 临时冻结拖拽
  107. freeze: false,
  108. plotList,
  109. rawChart: []
  110. }
  111. },
  112. computed: {
  113. ...mapState({
  114. pageConfig: (state) => state.bigScreen.pageInfo.pageConfig,
  115. pageInfo: (state) => state.bigScreen.pageInfo,
  116. chartList: (state) => state.bigScreen.pageInfo.chartList,
  117. activeCode: (state) => state.bigScreen.activeCode,
  118. activeCodes: (state) => state.bigScreen.activeCodes,
  119. hoverCode: (state) => state.bigScreen.hoverCode,
  120. themeJson: (state) => state.bigScreen.pageInfo.pageConfig.themeJson,
  121. isInit: (state) => !state.bigScreen.pageLoading,
  122. scale: (state) => state.bigScreen.zoom / 100
  123. })
  124. },
  125. watch: {
  126. pageConfig: {
  127. handler (pageConfig) {
  128. this.$nextTick(() => {
  129. const style = document.createElement('style')
  130. if (
  131. pageConfig &&
  132. pageConfig.themeJson &&
  133. pageConfig.themeJson.themeCss
  134. ) {
  135. const themeCss = pageConfig.themeJson.themeCss
  136. if (themeCss) {
  137. const themeStr = compile(themeCss).code
  138. style.type = 'text/css'
  139. style.innerText = themeStr
  140. document.getElementsByTagName('head')[0].appendChild(style)
  141. }
  142. }
  143. })
  144. },
  145. deep: true,
  146. immediate: true
  147. }
  148. },
  149. mounted () {
  150. this.styleSet()
  151. this.plotList = [...this.plotList, ...getCustomPlots()]
  152. },
  153. methods: {
  154. ...mapMutations('bigScreen', [
  155. 'changeLayout',
  156. 'changeActiveCode',
  157. 'changeChartConfig',
  158. 'changeActiveItemConfig',
  159. 'changeActiveItemWH',
  160. 'addItem',
  161. 'delItem',
  162. 'resetPresetLine',
  163. 'changeGridShow',
  164. 'setPresetLine',
  165. 'saveTimeLine'
  166. ]),
  167. // 获取到后端传来的主题样式并进行修改
  168. styleSet () {
  169. const style = document.createElement('style')
  170. if (this.themeJson && this.themeJson.themeCss) {
  171. const styleStr = this.themeJson.themeCss
  172. const themeCss = compile(styleStr).code
  173. style.type = 'text/css'
  174. style.innerText = themeCss
  175. document.getElementsByTagName('head')[0].appendChild(style)
  176. } else {
  177. style.remove()
  178. }
  179. },
  180. resetPresetLineDelay () {
  181. setTimeout(() => {
  182. this.resetPresetLine()
  183. }, 500)
  184. },
  185. // 点击当前组件时打开右侧面板
  186. openRightPanel (config) {
  187. this.$emit('openRightPanel', config)
  188. },
  189. drop (e) {
  190. e.preventDefault()
  191. // 解决:火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片的问题。
  192. e.stopPropagation()
  193. const transferData = e.dataTransfer.getData('dragComponent')
  194. if (transferData) {
  195. this.addChart(transferData, { x: e?.x, y: e?.y })
  196. }
  197. },
  198. /**
  199. * 改变组件大小
  200. * @param x
  201. * @param y
  202. * @param width
  203. * @param height
  204. * @param chart
  205. */
  206. onResize (x, y, width, height, chart) {
  207. chart.x = x
  208. chart.y = y
  209. chart.w = width
  210. chart.h = height
  211. this.changeGridShow(true)
  212. this.setPresetLine({
  213. ...chart
  214. })
  215. },
  216. /**
  217. *
  218. * @param x
  219. * @param y
  220. * @param chart
  221. */
  222. onDrag (x, y, chart) {
  223. // 防止事件冒泡
  224. event.stopPropagation()
  225. if (chart.group) {
  226. // 查找和自己是一个组合的组件
  227. this.dragGroupChart(x, y, chart)
  228. } else {
  229. chart.x = x
  230. chart.y = y
  231. }
  232. this.changeGridShow(true)
  233. this.setPresetLine({
  234. ...chart
  235. })
  236. },
  237. resizestop (left, top, width, height, chart) {
  238. this.changeChartConfig({
  239. ...chart,
  240. w: width,
  241. h: height,
  242. x: left,
  243. y: top
  244. })
  245. this.changeActiveItemConfig({
  246. ...chart,
  247. w: width,
  248. h: height,
  249. x: left,
  250. y: top
  251. })
  252. if (chart.code === this.activeCode) {
  253. this.changeActiveItemWH({
  254. code: chart.code,
  255. w: width,
  256. h: height
  257. })
  258. }
  259. this.saveTimeLine(`改变${chart?.title}大小`)
  260. this.changeGridShow(false)
  261. },
  262. activated (chart) {
  263. this.rawChart = cloneDeep(chart)
  264. },
  265. dragstop (left, top, chart) {
  266. if (!this.freeze) {
  267. if (this.rawChart.x !== left || this.rawChart.y !== top) {
  268. this.changeChartConfig({
  269. ...chart,
  270. x: left,
  271. y: top
  272. })
  273. this.changeActiveItemConfig({
  274. ...chart,
  275. x: left,
  276. y: top
  277. })
  278. if (chart.code === this.activeCode) {
  279. this.changeActiveItemWH({
  280. code: chart.code,
  281. x: left,
  282. y: top
  283. })
  284. }
  285. this.rawChart = cloneDeep(chart)
  286. }
  287. } else {
  288. const index = this.chartList.findIndex(
  289. (_chart) => _chart.code === chart.code
  290. )
  291. this.$set(this.chartList, index, chart)
  292. this.changeChartConfig({
  293. ...chart,
  294. updateKey: new Date().getTime()
  295. })
  296. }
  297. this.changeGridShow(false)
  298. this.freeze = false
  299. this.saveTimeLine(`拖拽${chart?.title}`)
  300. },
  301. // 辅助线
  302. getRefLineParams (params) {
  303. const { vLine, hLine } = params
  304. this.vLine = vLine
  305. this.hLine = hLine
  306. },
  307. // 新增元素
  308. addChart (chart, position, isComponent) {
  309. const { left, top } = this.$el.getBoundingClientRect()
  310. const _chart = !chart.code ? JSON.parse(chart) : chart
  311. let option = _chart.option
  312. if (_chart.type === 'customComponent') {
  313. option = {
  314. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  315. theme: this.pageConfig.customTheme
  316. }
  317. }
  318. const config = {
  319. ..._chart,
  320. x: parseInt(!chart.code
  321. ? (position.x - left - _chart.offsetX) / this.scale
  322. : position.x),
  323. y: parseInt(!chart.code
  324. ? (position.y - top - _chart.offsetX) / this.scale
  325. : position.y),
  326. width: 200 * this.scale,
  327. height: 200 * this.scale,
  328. code: !chart.code ? randomString(8) : chart.code,
  329. option
  330. }
  331. config.key = config.code
  332. // 1、从左侧新增时会初始化theme的内容 2、从组件库添加自定义组件时不用初始化
  333. if (!isComponent) {
  334. config.theme = settingToTheme(config, 'dark')
  335. config.theme = settingToTheme(config, 'light')
  336. }
  337. this.addItem(config)
  338. },
  339. addSourceChart (chart, position) {
  340. const { left, top } = this.$el.getBoundingClientRect()
  341. const _chart = JSON.parse(chart)
  342. let option = _chart.option
  343. if (_chart.type === 'customComponent') {
  344. option = {
  345. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  346. theme: this.pageConfig.customTheme
  347. }
  348. }
  349. const config = {
  350. ..._chart,
  351. x: parseInt((position.x - left) / this.scale),
  352. y: parseInt((position.y - top) / this.scale),
  353. width: 200 * this.scale,
  354. height: 200 * this.scale,
  355. code: randomString(8),
  356. option
  357. }
  358. config.key = config.code
  359. this.addItem(config)
  360. },
  361. /**
  362. * 拖拽相同组合的组件
  363. * @param x 组合元素当前x
  364. * @param y 组合元素当前y
  365. * @param chart
  366. */
  367. dragGroupChart (x, y, chart) {
  368. if (chart.group) {
  369. const diffX = x - chart.x
  370. const diffY = y - chart.y
  371. const group = chart.group
  372. // 找到相同group的组件,并找到边界
  373. const groupChartList = this.chartList.filter(
  374. (groupChart) => groupChart.group === group
  375. )
  376. const groupMinX = Math.min(
  377. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  378. )
  379. const groupMinY = Math.min(
  380. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  381. )
  382. const groupMaxX = Math.max(
  383. ...groupChartList?.map(
  384. (groupChart) => groupChart.x + diffX + groupChart.w
  385. )
  386. )
  387. const groupMaxY = Math.max(
  388. ...groupChartList?.map(
  389. (groupChart) => groupChart.y + diffY + groupChart.h
  390. )
  391. )
  392. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  393. if (
  394. (groupMinX <= 0 ||
  395. groupMinY <= 0 ||
  396. groupMaxX >= this.pageConfig.w ||
  397. groupMaxY >= this.pageConfig.h) &&
  398. // 偏移的绝对值要大于0
  399. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  400. ) {
  401. this.freeze = true
  402. return
  403. }
  404. // 移动相应的diff距离
  405. groupChartList?.map((groupChart) => {
  406. this.changeChartConfig({
  407. ...groupChart,
  408. x: groupChart.x + diffX,
  409. y: groupChart.y + diffY
  410. })
  411. })
  412. }
  413. }
  414. }
  415. }
  416. </script>
  417. <style lang="scss" scoped>
  418. .bs-render-wrap {
  419. position: relative;
  420. background-size: cover;
  421. .drag-item {
  422. cursor: move;
  423. }
  424. ::v-deep .vdr {
  425. border: none;
  426. }
  427. .h-line {
  428. border-bottom: 1px dashed #0089d0;
  429. }
  430. .v-line {
  431. border-left: 1px dashed #0089d0;
  432. }
  433. .ref-line {
  434. background-color: transparent;
  435. }
  436. }
  437. .design-drag-wrap {
  438. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  439. }
  440. .multiple-selected {
  441. border: 1px dashed #fff !important;
  442. }
  443. </style>