Commit 700872b0 authored by 郝聪敏's avatar 郝聪敏

feature: 添加根据背景图调整尺寸功能;添加样式转rem功能;暴露delete方法

parent e21bcd67
<template> <template>
<div class="top-bar" v-if="isShowTopBar"> <div class="top-bar">
<img class="top-bar-left" :src="leftImg" /> <img class="top-bar-left" :src="leftImg" />
<div class="top-bar-right"> <div class="top-bar-right">
<a :href="href"> <a :href="href">
<button class="top-bar-button">打开APP</button> <button class="top-bar-button">打开APP</button>
</a> </a>
<img @click="isShowTopBar = false" class="top-bar-close" src="./images/close@2x.png" /> <img class="top-bar-close" @click="close" src="./images/close@2x.png" />
</div> </div>
</div> </div>
</template> </template>
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
href: String, href: String,
leftImg: String leftImg: String
}, },
data() { methods: {
return { close() {
isShowTopBar: true, this.$emit('delete');
} }
} }
} }
......
import { cloneDeep } from 'lodash';
import { Component, Prop, Vue } from 'vue-property-decorator'; import { Component, Prop, Vue } from 'vue-property-decorator';
import { Action, Mutation, State, Getter } from 'vuex-class';
import LoginForm from '@/lib/Form/index.vue'; import LoginForm from '@/lib/Form/index.vue';
import { ContextMenu } from '@editor/mixins/contextMenu.mixin'; import { ContextMenu } from '@editor/mixins/contextMenu.mixin';
import { cloneDeep } from 'lodash'; import { resizeDiv } from '@/service/utils.service';
import { Action, Mutation, State } from 'vuex-class';
@Component({ components: { LoginForm }, name: 'FreedomContainer' }) @Component({ components: { LoginForm }, name: 'FreedomContainer' })
export default class FreedomContainer extends Vue { export default class FreedomContainer extends Vue {
@Getter('pageData') pageData;
@State(state => state.editor.curChildIndex) curChildIndex; @State(state => state.editor.curChildIndex) curChildIndex;
@Mutation('UPDATE_PAGE_INFO') updatePageInfo;
@Prop({type: Object, default: () => ({ child: [] })}) childItem; @Prop({type: Object, default: () => ({ child: [] })}) childItem;
@Prop(String) backgroundImage; @Prop(String) backgroundImage;
...@@ -14,11 +17,26 @@ export default class FreedomContainer extends Vue { ...@@ -14,11 +17,26 @@ export default class FreedomContainer extends Vue {
transformStyle(styleObj) { transformStyle(styleObj) {
const style = {}; const style = {};
for (const key of Object.keys(styleObj)) { for (const key of Object.keys(styleObj)) {
style[key] = typeof styleObj[key] !== 'string' ? `${styleObj[key]}px` : styleObj[key]; if ( typeof styleObj[key] === 'number') {
style[key] = `${(styleObj[key] / 37.5).toFixed(2)}rem`;
} else {
style[key] = styleObj[key].includes('px') ? `${(+(styleObj[key].slice(0, -2)) / 37.5).toFixed(2)}rem` : styleObj[key];
}
if (key === 'backgroundImage') { if (key === 'backgroundImage') {
style.backgroundImage = `url(${style.backgroundImage})`; style.backgroundImage = `url(${style.backgroundImage})`;
} }
} }
return style; return style;
} }
mounted() {
// 根据背景图设置元素高度
const index = this.pageData?.elements?.findIndex(v => v.point?.responsive);
const { props: { backgroundImage }, point } = this.pageData?.elements[index] || { props: {}};
if (backgroundImage) {
resizeDiv(backgroundImage, null, null, (height) => {
this.updatePageInfo({ containerIndex: index, data: { ...this.pageData?.elements[index], point: { ...point, h: height ?? point.h } } });
});
}
}
} }
\ No newline at end of file
...@@ -6,13 +6,14 @@ import GridLayout from '../../component/VueGridLayout/GridLayout.vue'; ...@@ -6,13 +6,14 @@ import GridLayout from '../../component/VueGridLayout/GridLayout.vue';
import GridItem from '../../component/VueGridLayout/GridItem.vue'; import GridItem from '../../component/VueGridLayout/GridItem.vue';
import LoginForm from '@/lib/Form/index.vue'; import LoginForm from '@/lib/Form/index.vue';
import DownloadGuide from '@/lib/DownloadGuide/index.vue'; import DownloadGuide from '@/lib/DownloadGuide/index.vue';
import { Getter, State } from 'vuex-class'; import { Getter, State, Mutation } from 'vuex-class';
@Component({ components: { FreedomContainer, GridLayout, GridItem, LoginForm, DownloadGuide }, name: 'Activity'}) @Component({ components: { FreedomContainer, GridLayout, GridItem, LoginForm, DownloadGuide }, name: 'Activity'})
export default class Activity extends Vue { export default class Activity extends Vue {
@Getter('pageData') pageData; @Getter('pageData') pageData;
@State(state => state.editor.pageInfo.pageName) pageName; @State(state => state.editor.pageInfo.pageName) pageName;
@State(state => state.editor.gridLayout.rowHeight) rowHeight; @State(state => state.editor.gridLayout.rowHeight) rowHeight;
@Mutation('DEL_ELEMENTS') delPageInfo;
isLayoutComReady = false; isLayoutComReady = false;
...@@ -45,7 +46,7 @@ export default class Activity extends Vue { ...@@ -45,7 +46,7 @@ export default class Activity extends Vue {
createStyle({h}) { createStyle({h}) {
return EASY_ENV_IS_NODE ? { return EASY_ENV_IS_NODE ? {
height: `${h * 50 + Math.max(0, h - 1) * 10}px`, height: `${h * this.rowHeight}px`,
} : {}; } : {};
} }
} }
\ No newline at end of file
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
:h="item.point.h" :h="item.point.h"
:i="item.point.i" :i="item.point.i"
:key="item.point.i"> :key="item.point.i">
<component class="Dcmc-panel-com" :data-index="index" :containerIndex="index" :childItem="item" :is="item.name" :key="index" v-bind="item.props"></component> <component class="Dcmc-panel-com" :data-index="index" :containerIndex="index" :childItem="item" :is="item.name" :key="index" @delete="delPageInfo(index)" v-bind="item.props"></component>
</grid-item> </grid-item>
</grid-layout> </grid-layout>
</div> </div>
......
...@@ -4,6 +4,7 @@ import { reduce, ceil, subtract, divide } from 'lodash'; ...@@ -4,6 +4,7 @@ import { reduce, ceil, subtract, divide } from 'lodash';
import { ContextMenu } from '@editor/mixins/contextMenu.mixin'; import { ContextMenu } from '@editor/mixins/contextMenu.mixin';
import Upload from './component/Upload/index.vue'; import Upload from './component/Upload/index.vue';
import ColorSelector from './component/ColorSelector/index.vue'; import ColorSelector from './component/ColorSelector/index.vue';
import { resizeDiv } from '@/service/utils.service';
@Component({ components: { Upload, ColorSelector }, name: 'DynamicForm' }) @Component({ components: { Upload, ColorSelector }, name: 'DynamicForm' })
export default class DynamicForm extends Mixins(ContextMenu) { export default class DynamicForm extends Mixins(ContextMenu) {
...@@ -17,7 +18,6 @@ export default class DynamicForm extends Mixins(ContextMenu) { ...@@ -17,7 +18,6 @@ export default class DynamicForm extends Mixins(ContextMenu) {
@Watch('curElement', { immediate: true, deep: true }) @Watch('curElement', { immediate: true, deep: true })
onElementChange(newVal) { onElementChange(newVal) {
console.log('newVal', newVal);
newVal?.schame?.forEach(schame => { newVal?.schame?.forEach(schame => {
this.$set(this.form, schame.key, newVal.props[schame.key]); this.$set(this.form, schame.key, newVal.props[schame.key]);
}); });
...@@ -36,10 +36,16 @@ export default class DynamicForm extends Mixins(ContextMenu) { ...@@ -36,10 +36,16 @@ export default class DynamicForm extends Mixins(ContextMenu) {
return result; return result;
} }
resizedEvent(h, w) { resizedEvent(h, w, responsive) {
const elements = this.pageData.elements[this.curEleIndex]; const elements = this.pageData.elements[this.curEleIndex];
if (responsive) {
resizeDiv(this.form.backgroundImage, 667, 375, (imgHeight) => {
this.updatePageInfo({ containerIndex: this.curEleIndex, data: { ...elements, point: { ...elements.point, w: w ?? elements.point.w, h: imgHeight ?? elements.point.h, responsive: true } } });
});
} else {
this.updatePageInfo({ containerIndex: this.curEleIndex, data: { ...elements, point: { ...elements.point, w: w ?? elements.point.w, h: h ?? elements.point.h } } }); this.updatePageInfo({ containerIndex: this.curEleIndex, data: { ...elements, point: { ...elements.point, w: w ?? elements.point.w, h: h ?? elements.point.h } } });
} }
}
getStyle(oElement, sName) { getStyle(oElement, sName) {
const result = oElement.currentStyle ? oElement.currentStyle[sName] : getComputedStyle(oElement, null)[sName]; const result = oElement.currentStyle ? oElement.currentStyle[sName] : getComputedStyle(oElement, null)[sName];
......
...@@ -15,8 +15,11 @@ ...@@ -15,8 +15,11 @@
<Tooltip placement="top" content="全屏"> <Tooltip placement="top" content="全屏">
<Button type="ghost" icon="arrow-resize" @click="resizedEvent(667, 667)"></Button> <Button type="ghost" icon="arrow-resize" @click="resizedEvent(667, 667)"></Button>
</Tooltip> </Tooltip>
<Tooltip placement="top" content="根据背景图片自动调整宽高">
<Button type="ghost" icon="image" @click="resizedEvent(667, 375, true)" ></Button>
</Tooltip>
<Tooltip placement="top" content="宽100%"> <Tooltip placement="top" content="宽100%">
<Button type="ghost" icon="arrow-swap" @click="resizedEvent(null, 667)"></Button> <Button type="ghost" icon="arrow-swap" @click="resizedEvent(null, 375)"></Button>
</Tooltip> </Tooltip>
<Tooltip placement="top" content="高100%"> <Tooltip placement="top" content="高100%">
<Button type="ghost" icon="arrow-swap" @click="resizedEvent(667, null)" ></Button> <Button type="ghost" icon="arrow-swap" @click="resizedEvent(667, null)" ></Button>
......
...@@ -41,7 +41,11 @@ export default class FreedomContainer extends Mixins(ContextMenu) { ...@@ -41,7 +41,11 @@ export default class FreedomContainer extends Mixins(ContextMenu) {
transformStyle(styleObj) { transformStyle(styleObj) {
const style = {}; const style = {};
for (const key of Object.keys(styleObj)) { for (const key of Object.keys(styleObj)) {
style[key] = typeof styleObj[key] !== 'string' ? `${styleObj[key]}px` : styleObj[key]; if ( typeof styleObj[key] === 'number') {
style[key] = `${(styleObj[key] / 37.5).toFixed(2)}rem`;
} else {
style[key] = styleObj[key].includes('px') ? `${(+(styleObj[key].slice(0, -2)) / 37.5).toFixed(2)}rem` : styleObj[key];
}
if (key === 'backgroundImage') { if (key === 'backgroundImage') {
style.backgroundImage = `url(${style.backgroundImage})`; style.backgroundImage = `url(${style.backgroundImage})`;
} }
......
...@@ -6,7 +6,8 @@ import { ...@@ -6,7 +6,8 @@ import {
SET_DRAGABLE, SET_DRAGABLE,
COPY_OR_DELETE_PAGE_INFO, COPY_OR_DELETE_PAGE_INFO,
UPDATE_PAGE_INFO, UPDATE_PAGE_INFO,
ADD_ELEMENTS ADD_ELEMENTS,
DEL_ELEMENTS,
SET_CUR_ELE_INDEX, SET_CUR_ELE_INDEX,
SET_CUR_CHILD_INDEX, SET_CUR_CHILD_INDEX,
RESET_PAGE_DATA, RESET_PAGE_DATA,
...@@ -126,6 +127,10 @@ export default class EditorModule implements Module<EditorState, RootState> { ...@@ -126,6 +127,10 @@ export default class EditorModule implements Module<EditorState, RootState> {
page.push(data); page.push(data);
} }
}, },
[DEL_ELEMENTS](state, { containerIndex }) {
const page = (state.pageInfo.page as Page).elements;
page.splice(containerIndex, 1);
},
}; };
constructor(initState: EditorState = cloneDeep(defaultState)) { constructor(initState: EditorState = cloneDeep(defaultState)) {
......
...@@ -8,9 +8,9 @@ interface Point { ...@@ -8,9 +8,9 @@ interface Point {
x: number; x: number;
y: number; y: number;
w: number; w: number;
moved: boolean;
h: number; h: number;
i: number | string; i: number | string;
responsive: boolean; // 自定义属性, 表示是否根据背景图片调整宽高
} }
interface CommonStyle { interface CommonStyle {
......
...@@ -4,6 +4,7 @@ export const SET_DRAGABLE = 'SET_DRAGABLE'; ...@@ -4,6 +4,7 @@ export const SET_DRAGABLE = 'SET_DRAGABLE';
export const COPY_OR_DELETE_PAGE_INFO = 'COPY_OR_DELETE_PAGE_INFO'; export const COPY_OR_DELETE_PAGE_INFO = 'COPY_OR_DELETE_PAGE_INFO';
export const UPDATE_PAGE_INFO = 'UPDATE_PAGE_INFO'; export const UPDATE_PAGE_INFO = 'UPDATE_PAGE_INFO';
export const ADD_ELEMENTS = 'ADD_ELEMENTS'; export const ADD_ELEMENTS = 'ADD_ELEMENTS';
export const DEL_ELEMENTS = 'DEL_ELEMENTS';
export const SET_CUR_ELE_INDEX = 'SET_CUR_ELE_INDEX'; export const SET_CUR_ELE_INDEX = 'SET_CUR_ELE_INDEX';
export const SET_CUR_CHILD_INDEX = 'SET_CUR_CHILD_INDEX'; export const SET_CUR_CHILD_INDEX = 'SET_CUR_CHILD_INDEX';
export const RESET_PAGE_DATA = 'RESET_PAGE_DATA'; export const RESET_PAGE_DATA = 'RESET_PAGE_DATA';
......
...@@ -38,3 +38,17 @@ export function isPhone(str) { ...@@ -38,3 +38,17 @@ export function isPhone(str) {
} }
return false; return false;
} }
export function resizeDiv(imgUrl, clientHeight = 0, clientWidth = 0, callback) {
const img = new Image();
clientHeight = clientHeight || document.body.clientHeight;
clientWidth = clientWidth || document.body.clientWidth;
img.onload = function() {
const imgRealHeight = clientWidth * img.height / img.width;
console.log(img.width, img.height, imgRealHeight, clientHeight);
if (imgRealHeight > clientWidth) {
callback(imgRealHeight);
}
};
img.src = imgUrl;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment