Commit 8c944576 by xinla Committed by Gitee

!18 海报分支合并前端分支

Merge pull request !18 from xinla/dev_poster
2 parents edf8a52f 2d47c381
......@@ -23,6 +23,7 @@ export function updatePoster(data) {
* @param {*} data
*/
export function addPoster(data) {
console.log(JSON.stringify(data))
return request({ url: serviceCategory + '/insert', method: 'post', data })
}
......
......@@ -39,6 +39,7 @@ export default {
},
data() {
return {
qrCodeUrl: 'http://www.linkwechat.cn/static/img/qrCodeImage.a6d01316.png',
visible: false
}
},
......@@ -47,6 +48,9 @@ export default {
if (this.includeUi) {
options = Object.assign(includeUIOptions, this.options);
}
if (this.editorInstance) {
console.log('this.editorInstance')
}
this.editorInstance = new ImageEditor(this.$refs.tuiImageEditor, options);
document.getElementsByClassName('tui-image-editor-header')[0].innerHTML = '';
......@@ -67,6 +71,8 @@ export default {
this.editorInstance.ui._actions.text.modeChange = function () {}
document.getElementsByClassName('tui-image-editor-submenu')[0].style.display = 'none';
document.getElementsByClassName('tie-btn-text')[0].style.display = 'none';
document.getElementsByClassName('tui-image-editor-main')[0].style.top = '0';
document.getElementsByClassName('tui-image-editor-align-wrap')[0].style.verticalAlign = 'top';
this.initBtn();
this.addEventListener();
......@@ -75,16 +81,99 @@ export default {
// Object.keys(this.$listeners).forEach(eventName => {
// this.editorInstance.off(eventName);
// });
console.log('deatory poster')
this.editorInstance.destroy();
this.editorInstance = null;
},
methods: {
getBackgroundUrl(bacUrl) {
// 设置背景图片
getBackgroundUrl(bacUrl, posterSubassemblyList) {
this.editorInstance.loadImageFromURL(bacUrl, 'imagePoster').then(async result => {
console.log('old : ' + result.oldWidth + ', ' + result.oldHeight);
console.log('new : ' + result.newWidth + ', ' + result.newHeight);
// await this.addImage('https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2980445260,41238050&fm=26&gp=0.jpg')
// await this.addText('aloha');
// 由于篡改了tuiimage,第一次使用addText会报错,错误不影响流程,但是会导致promise中断,提前触发报错。
try {
this.editorInstance.
addText('', {
position: {
x: 0,
y: 0
},
})
.then(function (objectProps) {
console.log('dd');
}).catch(()=>{});
} catch (e) {
console.log('addText 内部状态错误')
}
// FIXME 暂时延迟回显
setTimeout(()=>{
posterSubassemblyList.length && this.reShowDisplay(posterSubassemblyList);
}, 500);
});
},
// 设置修改回显
async reShowDisplay (reShowDisplays) {
let i = 0,len = reShowDisplays.length;
let _data = null;
while (i < len) {
_data = reShowDisplays[i];
if (_data) {
await this.createReShowDisplay(_data);
}
i ++;
}
},
// 1 固定文本 2 固定图片 3 二维码图片
createReShowDisplay(data) {
return new Promise((resolve)=>{
switch (data.type) {
case 1:
this.activateTextMode();
this.editorInstance.
addText(data.content, {
position: {
x: data.left,
y: data.top
},
})
.then(function (objectProps) {
console.log(objectProps);
// 重置 TextStyle
this.editorInstance.changeTextStyle(objectProps.id, {
fontSize: data.fontSize,
fill: data.fontColor,
textAlign: data.fontTextAlign === 1 ? 'left' :
data.fontTextAlign === 2 ? 'center' : 'right'
});
resolve();
});
break;
case 2:
case 3:
this.activateImageMode()
this.editorInstance.addImageObject(
data.type === 2 ? data.imgPath :
data.imgPath === this.qrCodeUrl ? qrCodeImage : data.imgPath
).then(objectProps => {
console.log(objectProps.id);
let target = {}
target.url = data.imgPath;
target.randomId = objectProps.id;
target.objType = data.type;
this.editorInstance.setObjectProperties(objectProps.id, {
left: data.left,
top: data.top
})
// 将图片数据传给父组件保存
this.$emit('getImageData', target);console.log(objectProps);
resolve()
});
break;
}
});
},
initBtn () {
......@@ -178,6 +267,7 @@ export default {
redoStackChanged: this.onRedoStackChanged.bind(this),
objectScaled: this.onObjectScaled.bind(this),
addText: this.onAddText.bind(this),
textEditing: this.textEditing.bind(this),
objectActivated: this.objectActivated.bind(this),
objectMoved: this.onObjectMoved.bind(this)
});
......@@ -220,12 +310,13 @@ export default {
})
.then(function (objectProps) {
console.log(objectProps);
this.records(objectProps)
let id = this.GenNonDuplicateID() + '_' + objectProps.id;
let target = this.$parent.records[objectProps.id];
target && (target.randomId = id);
this.records(objectProps);
}.bind(this));
},
// 文本改变事件
textEditing (e) {
console.log('dsds', e)
},
//移动
onObjectMoved(obj) {
console.log('onObjectMoved');
......@@ -306,6 +397,7 @@ export default {
qrCodeHandler () {
// let codeUrl = 'https://images.gitee.com/uploads/images/2020/1231/234016_20fdd151_1480777.png';
this.activateImageMode();
// 暂时写死二维码
this.addImage(qrCodeImage, 3);
},
clearObjects () {
......@@ -446,12 +538,9 @@ export default {
},
getImages () {
this.visible = true; // 显示图片列表dialog
// this.activateImageMode();
// this.addImage('https://images.gitee.com/uploads/images/2020/1231/234016_20fdd151_1480777.png',2);
},
// 提交选择的图片
getMaterial(text, image, file) {
// console.log(image.materialUrl)
this.activateImageMode();
this.addImage(image.materialUrl, 2);
this.visible = false;
......@@ -461,50 +550,23 @@ export default {
throw Error("imgPath can't null")
}
this.editorInstance.addImageObject(imgPath).then(objectProps => {
let id = this.GenNonDuplicateID() + '_' + objectProps.id;
// this.$parent.imgList[id] = imgPath; // FIXME
// console.log(objectProps)
// console.log(this.$parent.records)
// let target = this.$parent.records[objectProps.id];
let target = {}
// if (target) {
target.url = imgPath;
target.randomId = objectProps.id;
target.objType = type;
// }
this.$emit('getImageData', target); // 将图片数据传给父组件保存
// 二维码是占位符,所以可以写死
target.url = type === 3 ? this.qrCodeUrl : imgPath;
target.randomId = objectProps.id;
target.objType = type;
// 将图片数据传给父组件保存
this.$emit('getImageData', target);
});
},
addText (text, style = null, position = null) {
// return new Promise(function(resolve, reject) {
// !style && (style = {
// fill: '#000',
// fontSize: 20,
// fontWeight: 'bold'
// })
// !position && (position = {
// x: 10,
// y: 10
// })
// this.activateTextMode();
// this.editorInstance.addText( text, {
// style: style,
// position: position
// }).then(objectProps => {
// console.log(objectProps.id);
// resolve(objectProps);
// });
// }.bind(this));
this.editorInstance
.addText(text || '双击输入文字', {
position: position,
})
.then(function (objectProps) {
console.log(objectProps);
let id = this.GenNonDuplicateID() + '_' + objectProps.id;
let target = this.$parent.records[objectProps.id];
target && (target.randomId = id);
});
},
activateImageMode () {
......@@ -535,10 +597,10 @@ export default {
this.records[obj.id] = obj;
}
},
GenNonDuplicateID(randomLength){
return parseFloat(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}
}
// GenNonDuplicateID(randomLength){
// return parseFloat(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
// }
}
};
</script>
<script>
import {
getList,
getPosterInfo,
addPoster,
updatePoster,
......@@ -32,7 +31,7 @@ export default {
data () {
return {
imgList: {},
imgData: [],
posterSubassemblyList: [],
dialogVisibleSelectMaterial: false,
dialog: {
preview: false, // 预览弹出显示隐藏
......@@ -178,6 +177,7 @@ export default {
type: data.type,
delFlag: data.delFlag,
}
this.posterSubassemblyList = data.posterSubassemblyList || [];
this.materialSelected = data.backgroundImgPath
this.posterEdit.step = 0
this.dialog.edit = true
......@@ -216,8 +216,6 @@ export default {
//缩放
onObjectScaled(obj) {
console.log('onObjectScaled')
// this.getRecord(res);
// this.checkState();
if (obj.type === 'text') {
this.$refs.tuiImageEditor.inputFontSizeRange.setAttribute('value',obj.fontSize);
}
......@@ -277,10 +275,16 @@ export default {
} else {
this.rangeErrorMsg = ''
}
if (this.$refs.tuiImageEditor && this.$refs.tuiImageEditor.editorInstance) {
this.$refs.tuiImageEditor.editorInstance._invoker._redoStack = [];
this.$refs.tuiImageEditor.editorInstance._invoker._undoStack = [];
this.$refs.tuiImageEditor.records = {}
this.imgList = {};
}
this.$refs.form.validate((valid) => {
if (valid) {
this.posterEdit.step = 1
this.$refs.tuiImageEditor.getBackgroundUrl(this.materialSelected)
this.$refs.tuiImageEditor.getBackgroundUrl(this.materialSelected, this.posterSubassemblyList);
} else {
return false;
}
......@@ -309,76 +313,38 @@ export default {
inputFontSizeRangeChange (e) {
this.$refs.tuiImageEditor.inputFontSizeRangeChange(e);
},
getRecord(res){
var flag = false;
if (this.records && this.records.length) {
for (let index = 0; index < this.records.length; index++) {
const element ={
fill: this.records[index].fill,
height: this.records[index].height,
id: this.records[index].id,
left: this.records[index].left,
opacity: this.records[index].opacity,
stroke: this.records[index].stroke,
strokeWidth: this.records[index].strokeWidth,
top: this.records[index].top,
type: this.records[index].type,
width: this.records[index].width,
};
if(element.id == res.id){
// console.log(element)
this.records[index] = res;
flag = true;
}
}
} else {
this.records = [];
}
if(!flag){
this.records.push(res)
}
},
// FIXME 没写完
async reShow (list) {
list = JSON.parse('[{"id":2,"type":"i-text","left":55.26947693665858,"top":180.32266601562497,"width":200,"height":45.199999999999996,"fill":"#000000","stroke":null,"strokeWidth":1,"opacity":1,"angle":0,"text":"请输入文字","fontFamily":"Times New Roman","fontSize":40,"fontStyle":"normal","textAlign":"left","fontWeight":"normal"},{"id":3,"type":"image","left":195.71660545226203,"top":506.3232314730081,"width":500,"height":500,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":0,"opacity":1,"angle":0},{"id":4,"type":"image","left":373.102002853498,"top":190.44302690999768,"width":792,"height":792,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":0,"opacity":1,"angle":0}]');
let i = 0, len = list && list.length || 0
for ( ; i<len; i++) {
await this.loadRes(list[i]);
}
},
async loadRes (obj) {
return new Promise((resolve) => {
let imageEditor = this.$refs.tuiImageEditor.editorInstance;
switch (obj.type) {
case 'i-text':
case 'text':
imageEditor.
addText(obj.text, {
position: {
x: obj.left,
y: obj.right
},
})
.then(function (objectProps) {
console.log(objectProps);
});
break;
case 'image':
this.editorInstance.addImageObject(imgPath).then(objectProps => {
console.log(objectProps.id);
}).then(function (objectProps) {
console.log(objectProps);
});
break;
}
});
},
// getRecord(res){
// var flag = false;
// if (this.records && this.records.length) {
// for (let index = 0; index < this.records.length; index++) {
// const element ={
// fill: this.records[index].fill,
// height: this.records[index].height,
// id: this.records[index].id,
// left: this.records[index].left,
// opacity: this.records[index].opacity,
// stroke: this.records[index].stroke,
// strokeWidth: this.records[index].strokeWidth,
// top: this.records[index].top,
// type: this.records[index].type,
// width: this.records[index].width,
// };
// if(element.id == res.id){
// // console.log(element)
// this.records[index] = res;
// flag = true;
// }
// }
// } else {
// this.records = [];
// }
// if(!flag){
// this.records.push(res)
// }
// },
// 获取子组件传来的数据
getImgData(data) {
this.imgList.id = data.imgUrl
// console.log(data)
this.imgData.push(data)
// console.log(this.imgData)
this.imgList[data.randomId] = data
},
// 选择素材确认按钮
submitSelectMaterial(text, image, file) {
......@@ -393,13 +359,21 @@ export default {
//
async save() {
let list =[];
let historys = [];
try {
let imageEditor = this.$refs.tuiImageEditor;
let deleteId = [];
let deleteId = [];
imageEditor.editorInstance._invoker._undoStack.forEach(element => {
if(element.name == "removeObject"){
deleteId.push(element.args[1])
deleteId.push(element.args[1])
}
else
{
if (element.args[1] instanceof Array) {
historys.push(element.args[1][0])
} else {
historys.push(element.args[1])
}
}
})
Object.values(imageEditor.records).forEach(item => {
......@@ -418,33 +392,41 @@ export default {
// TODO 塞新建海报的数据
let posterSubList = [];
let vo = null;
let i = 0, len = list.length;
while (i < len) {
let vo = list[i];
if (!this.imgData[i]) {
console.log('this.imgData[i] is null')
i++;
continue;
vo = list[i];
if (this.imgList[vo.id]) {
vo.objType = this.imgList[vo.id].objType;
vo.url = this.imgList[vo.id].url;
vo.randomId = this.imgList[vo.id].randomId
}
vo.objType = this.imgData[i].objType;
vo.url = this.imgData[i].url;
vo.randomId = this.imgData[i].randomId;
// console.log(vo)
let type = list[i].type;
let type = vo.type;
let isText = false;
if (type === 'i-text' || type === 'text') {
// 如果是文本需要对文字内容进行特殊处理
isText = true;
let targetData = this.getLastSelectData(vo.id, historys);
if (targetData) {
vo.text = targetData.text;
}
}
let align = vo.textAlign &&
(vo.textAlign === 'left' ? 1 : vo.textAlign === 'center' ? 2 : 3)
|| null;
let isText = type === 'i-text' || type === 'text';
let posData = {
id: vo.randomId || i,
content: vo.text || '', // 文本内容
delFlag: 0, // 1 启动 0 删除 FIXME 暂时写死
fontColor: vo.fill || '#000000', //
fontId: isText ? i : null, // 字体ID 与imgPath互斥
fontId: null,// TODO 后端让传NULL isText ? i : null, // 字体ID 与imgPath互斥
fontSize: vo.fontSize,
fontTextAlign: align, // 1 2 3 left center right
left: vo.left,
top: vo.top,
left: parseInt(vo.left),
top: parseInt(vo.top),
width: vo.width,
height: vo.height,
imgPath: vo.url || '',
......@@ -474,7 +456,6 @@ export default {
} else {
// 新建海报
res = await addPoster(Object.assign({}, {
// backgroundImgPath: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2980445260,41238050&fm=26&gp=0.jpg',
backgroundImgPath: this.materialSelected,
posterSubassemblyList: posterSubList
}, this.posterForm))
......@@ -484,8 +465,26 @@ export default {
this.$refs.page.getList(1)
this.beforeCloseDialog()
}
},
getLastSelectData (id, arr) {
let index = arr.length - 1, len = 0;
let selectData = null;
try {
while (index >= len) {
// 类型转换对比
if (arr[index].id == id) {
selectData = arr[index];
break;
}
index --;
}
}catch (e) {
console.log('getLastSelectData 循环出错')
}
return selectData;
}
},
}
}
</script>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!