提交 547801ce authored 作者: wangjiahao's avatar wangjiahao

feat:预览增加自适应屏幕尺寸功能

上级 f8f504f3
<template> <template>
<div v-if="show" class="bg"> <div ref="element" class="bg">
<el-button class="close" @click="close">关闭</el-button> <ComponentWrapper
<div class="canvas-container"> v-for="(item, index) in componentDataInfo"
<div :key="index"
class="canvas" :config="item"
:style="{ />
width: changeStyleWithScale(canvasStyleData.width) + 'px',
height: changeStyleWithScale(canvasStyleData.height) + 'px',
}"
>
<ComponentWrapper
v-for="(item, index) in componentData"
:key="index"
:config="item"
/>
</div>
</div>
</div> </div>
</template> </template>
...@@ -25,6 +14,9 @@ import { mapState } from 'vuex' ...@@ -25,6 +14,9 @@ import { mapState } from 'vuex'
import ComponentWrapper from './ComponentWrapper' import ComponentWrapper from './ComponentWrapper'
import { changeStyleWithScale } from '@/components/canvas/utils/translate' import { changeStyleWithScale } from '@/components/canvas/utils/translate'
import { uuid } from 'vue-uuid' import { uuid } from 'vue-uuid'
import { deepCopy } from '@/components/canvas/utils/utils'
import eventBus from '@/components/canvas/utils/eventBus'
import { get } from '@/api/panel/panel'
export default { export default {
components: { ComponentWrapper }, components: { ComponentWrapper },
...@@ -38,39 +30,87 @@ export default { ...@@ -38,39 +30,87 @@ export default {
default: false default: false
} }
}, },
created() { data() {
return {
isShowPreview: false,
panelId: '',
needToChangeHeight: [
'top',
'height',
'fontSize',
'borderWidth'
],
needToChangeWidth: [
'left',
'width'
],
scaleWidth: '100',
scaleHeight: '100',
timer: null,
componentData: {},
canvasStyleData: {}
}
},
computed: {
componentDataInfo() {
return this.componentData
}
},
mounted() {
// 加载数据
this.restore() this.restore()
window.onresize = () => {
debugger
this.resize()
}
// this.resize()
}, },
computed: mapState([
'componentData',
'canvasStyleData'
]),
methods: { methods: {
changeStyleWithScale, changeStyleWithScale,
getStyle, getStyle,
close() { close() {
this.$emit('change', false) this.$emit('change', false)
}, },
resize() {
this.scaleWidth = window.innerWidth * 100 / parseInt(this.canvasStyleData.width)// 获取宽度比
this.scaleHeight = window.innerHeight * 100 / parseInt(this.canvasStyleData.height)// 获取高度比
this.handleScaleChange()
},
restore() { restore() {
debugger debugger
// 用保存的数据恢复画布 this.panelId = this.$route.path.split('/')[2]
if (localStorage.getItem('canvasData')) { // 加载视图数据
this.componentData = this.resetID(JSON.parse(localStorage.getItem('canvasData'))) get('panel/group/findOne/' + this.panelId).then(response => {
} this.componentData = this.resetID(JSON.parse(response.data.panelData))
this.canvasStyleData = JSON.parse(response.data.panelStyle)
if (localStorage.getItem('canvasStyle')) { this.resize()
this.canvasStyleData = JSON.parse(localStorage.getItem('canvasStyle')) })
this.$store.commit('setCanvasStyle', JSON.parse(localStorage.getItem('canvasStyle')))
}
}, },
resetID(data) { resetID(data) {
data.forEach(item => { data.forEach(item => {
item.id = uuid.v1() item.id = uuid.v1()
}) })
return data return data
},
format(value, scale) {
return value * parseInt(scale) / 100
},
handleScaleChange() {
const componentData = deepCopy(this.componentData)
componentData.forEach(component => {
Object.keys(component.style).forEach(key => {
if (this.needToChangeHeight.includes(key)) {
component.style[key] = this.format(component.style[key], this.scaleHeight)
}
if (this.needToChangeWidth.includes(key)) {
component.style[key] = this.format(component.style[key], this.scaleWidth)
}
})
})
this.componentData = componentData
eventBus.$emit('resizing', '')
} }
} }
} }
...@@ -80,14 +120,8 @@ export default { ...@@ -80,14 +120,8 @@ export default {
.bg { .bg {
width: 100%; width: 100%;
height: 100%; height: 100%;
.canvas-container { overflow: auto;
width: 100%; position: relative;
height: 100%; margin: 0;
overflow: auto;
.canvas {
position: relative;
margin: auto;
}
}
} }
</style> </style>
...@@ -3,7 +3,7 @@ import store from '@/store' ...@@ -3,7 +3,7 @@ import store from '@/store'
// 角度转弧度 // 角度转弧度
// Math.PI = 180 度 // Math.PI = 180 度
function angleToRadian(angle) { function angleToRadian(angle) {
return angle * Math.PI / 180 return angle * Math.PI / 180
} }
/** /**
...@@ -15,7 +15,7 @@ function angleToRadian(angle) { ...@@ -15,7 +15,7 @@ function angleToRadian(angle) {
* https://www.zhihu.com/question/67425734/answer/252724399 旋转矩阵公式 * https://www.zhihu.com/question/67425734/answer/252724399 旋转矩阵公式
*/ */
export function calculateRotatedPointCoordinate(point, center, rotate) { export function calculateRotatedPointCoordinate(point, center, rotate) {
/** /**
* 旋转公式: * 旋转公式:
* 点a(x, y) * 点a(x, y)
* 旋转中心c(x, y) * 旋转中心c(x, y)
...@@ -25,10 +25,10 @@ export function calculateRotatedPointCoordinate(point, center, rotate) { ...@@ -25,10 +25,10 @@ export function calculateRotatedPointCoordinate(point, center, rotate) {
* ny = sinθ * (ax - cx) + cosθ * (ay - cy) + cy * ny = sinθ * (ax - cx) + cosθ * (ay - cy) + cy
*/ */
return { return {
x: (point.x - center.x) * Math.cos(angleToRadian(rotate)) - (point.y - center.y) * Math.sin(angleToRadian(rotate)) + center.x, x: (point.x - center.x) * Math.cos(angleToRadian(rotate)) - (point.y - center.y) * Math.sin(angleToRadian(rotate)) + center.x,
y: (point.x - center.x) * Math.sin(angleToRadian(rotate)) + (point.y - center.y) * Math.cos(angleToRadian(rotate)) + center.y, y: (point.x - center.x) * Math.sin(angleToRadian(rotate)) + (point.y - center.y) * Math.cos(angleToRadian(rotate)) + center.y
} }
} }
/** /**
...@@ -39,89 +39,93 @@ export function calculateRotatedPointCoordinate(point, center, rotate) { ...@@ -39,89 +39,93 @@ export function calculateRotatedPointCoordinate(point, center, rotate) {
* @return {Object} 旋转后的点坐标 * @return {Object} 旋转后的点坐标
*/ */
export function getRotatedPointCoordinate(style, center, name) { export function getRotatedPointCoordinate(style, center, name) {
let point // point 是未旋转前的坐标 let point // point 是未旋转前的坐标
switch (name) { switch (name) {
case 't': case 't':
point = { point = {
x: style.left + (style.width / 2), x: style.left + (style.width / 2),
y: style.top, y: style.top
} }
break break
case 'b': case 'b':
point = { point = {
x: style.left + (style.width / 2), x: style.left + (style.width / 2),
y: style.top + style.height, y: style.top + style.height
} }
break break
case 'l': case 'l':
point = { point = {
x: style.left, x: style.left,
y: style.top + style.height / 2, y: style.top + style.height / 2
} }
break break
case 'r': case 'r':
point = { point = {
x: style.left + style.width, x: style.left + style.width,
y: style.top + style.height / 2, y: style.top + style.height / 2
} }
break break
case 'lt': case 'lt':
point = { point = {
x: style.left, x: style.left,
y: style.top, y: style.top
} }
break break
case 'rt': case 'rt':
point = { point = {
x: style.left + style.width, x: style.left + style.width,
y: style.top, y: style.top
} }
break break
case 'lb': case 'lb':
point = { point = {
x: style.left, x: style.left,
y: style.top + style.height, y: style.top + style.height
} }
break break
default: // rb default: // rb
point = { point = {
x: style.left + style.width, x: style.left + style.width,
y: style.top+ style.height, y: style.top + style.height
} }
break break
} }
return calculateRotatedPointCoordinate(point, center, style.rotate) return calculateRotatedPointCoordinate(point, center, style.rotate)
} }
// 求两点之间的中点坐标 // 求两点之间的中点坐标
export function getCenterPoint(p1, p2) { export function getCenterPoint(p1, p2) {
return { return {
x: p1.x + ((p2.x - p1.x) / 2), x: p1.x + ((p2.x - p1.x) / 2),
y: p1.y + ((p2.y - p1.y) / 2), y: p1.y + ((p2.y - p1.y) / 2)
} }
} }
export function sin(rotate) { export function sin(rotate) {
return Math.abs(Math.sin(angleToRadian(rotate))) return Math.abs(Math.sin(angleToRadian(rotate)))
} }
export function cos(rotate) { export function cos(rotate) {
return Math.abs(Math.cos(angleToRadian(rotate))) return Math.abs(Math.cos(angleToRadian(rotate)))
} }
export function mod360(deg) { export function mod360(deg) {
return (deg + 360) % 360 return (deg + 360) % 360
} }
export function changeStyleWithScale(value) { export function changeStyleWithScale(value) {
return value * parseInt(store.state.canvasStyleData.scale) / 100 return value * parseInt(store.state.canvasStyleData.scale) / 100
}
export function changeStyleWithScaleIn(value, scale) {
return value * parseInt(scale) / 100
} }
...@@ -72,8 +72,8 @@ export const constantRoutes = [ ...@@ -72,8 +72,8 @@ export const constantRoutes = [
] ]
}, },
{ {
path: '/preview', path: '/preview/:reportId',
component: () => import('@/components/canvas/components/Editor/PreviewFullScreen'), component: () => import('@/components/canvas/components/Editor/PreviewEject'),
hidden: true hidden: true
}, },
......
...@@ -417,6 +417,7 @@ export default { ...@@ -417,6 +417,7 @@ export default {
nodeClick(data, node) { nodeClick(data, node) {
if (data.nodeType === 'panel') { if (data.nodeType === 'panel') {
this.$store.dispatch('panel/setPanelInfo', data)
this.currGroup = data this.currGroup = data
// 加载视图数据 // 加载视图数据
this.$nextTick(() => { this.$nextTick(() => {
......
...@@ -48,9 +48,10 @@ export default { ...@@ -48,9 +48,10 @@ export default {
}, },
methods: { methods: {
clickPreview() { clickPreview() {
debugger
localStorage.setItem('canvasData', JSON.stringify(this.componentData)) localStorage.setItem('canvasData', JSON.stringify(this.componentData))
localStorage.setItem('canvasStyle', JSON.stringify(this.canvasStyleData)) localStorage.setItem('canvasStyle', JSON.stringify(this.canvasStyleData))
const url = '#/preview' const url = '#/preview/' + this.$store.state.panel.panelInfo.id
window.open(url, '_blank') window.open(url, '_blank')
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论