index.vue 13 KB

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