Commit 9cf27f58 by liuchuanming

海报逻辑修改&创建接口数据校正

1 parent cc2e0335
...@@ -19,6 +19,7 @@ const editorDefaultOptions = { ...@@ -19,6 +19,7 @@ const editorDefaultOptions = {
export default { export default {
name: 'PosterPage', name: 'PosterPage',
props: { props: {
btnContainer: null, // 自带按钮容器
includeUi: { includeUi: {
type: Boolean, type: Boolean,
default: true default: true
...@@ -126,9 +127,16 @@ export default { ...@@ -126,9 +127,16 @@ export default {
color: '#000000' color: '#000000'
}); });
this.textColorpicker.on('selectColor', function (event) { this.textColorpicker.on('selectColor', function (event) {
this.activeObjectId && this.editorInstance.changeTextStyle(this.activeObjectId, { if (this.activeObjectId) {
this.editorInstance.changeTextStyle(this.activeObjectId, {
fill: event.color, fill: event.color,
}); });
let vo = this.records[this.activeObjectId];
if (vo) {
vo.fill = event.color;
this.records[this.activeObjectId] = vo;
}
}
}.bind(this)); }.bind(this));
}, },
addEventListener() { addEventListener() {
...@@ -161,6 +169,37 @@ export default { ...@@ -161,6 +169,37 @@ export default {
objectActivated: this.objectActivated.bind(this), objectActivated: this.objectActivated.bind(this),
objectMoved: this.onObjectMoved.bind(this) objectMoved: this.onObjectMoved.bind(this)
}); });
this.btn_deleteAll.addEventListener('click', this.mouseClickHandler.bind(this))
this.btn_delete.addEventListener('click', this.mouseClickHandler.bind(this))
this.btn_reset.addEventListener('click', this.mouseClickHandler.bind(this))
this.btn_redo.addEventListener('click', this.mouseClickHandler.bind(this))
this.btn_undo.addEventListener('click', this.mouseClickHandler.bind(this))
this.btnText.addEventListener('click', this.mouseClickHandler.bind(this))
this.btnImage.addEventListener('click', this.mouseClickHandler.bind(this))
// text
this.btnClose.addEventListener('click', this.mouseClickHandler.bind(this))
// this.inputBrushWidthRange.addEventListener('onchange', this.mouseClickHandler.bind(this))
i = 0;
len = this.btnTextStyle.length;
while (i < len) {
this.btnTextStyle[i].name = 'textStyle'
this.btnTextStyle[i].addEventListener('click', this.mouseClickHandler.bind(this))
i++
}
},
redoHandler () {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
if (!this.btn_redo.matches(".disabled")) {
this.editorInstance.redo();
}
},
undoHandler () {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
if (!this.btn_undo.matches(".disabled")) {
this.editorInstance.undo();
}
}, },
onAddText(pos) { onAddText(pos) {
this.editorInstance this.editorInstance
...@@ -170,6 +209,9 @@ export default { ...@@ -170,6 +209,9 @@ export default {
.then(function (objectProps) { .then(function (objectProps) {
console.log(objectProps); console.log(objectProps);
this.records(objectProps) this.records(objectProps)
let id = this.GenNonDuplicateID() + '_' + objectProps.id;
let target = this.$parent.records[objectProps.id];
target && (target.randomId = id);
}.bind(this)); }.bind(this));
}, },
//移动 //移动
...@@ -252,7 +294,7 @@ export default { ...@@ -252,7 +294,7 @@ export default {
qrCodeHandler () { qrCodeHandler () {
// let codeUrl = 'https://images.gitee.com/uploads/images/2020/1231/234016_20fdd151_1480777.png'; // let codeUrl = 'https://images.gitee.com/uploads/images/2020/1231/234016_20fdd151_1480777.png';
this.activateImageMode(); this.activateImageMode();
this.addImage(qrCodeImage); this.addImage(qrCodeImage, 3);
}, },
clearObjects () { clearObjects () {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none'); this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
...@@ -262,18 +304,6 @@ export default { ...@@ -262,18 +304,6 @@ export default {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none'); this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
this.activeObjectId && this.editorInstance.removeObject(this.activeObjectId); this.activeObjectId && this.editorInstance.removeObject(this.activeObjectId);
}, },
redoHandler () {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
if (!this.btn_redo.matches(".disabled")) {
this.editorInstance.redo();
}
},
undoHandler () {
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
if (!this.btn_undo.matches(".disabled")) {
this.editorInstance.undo();
}
},
closeHandler () { closeHandler () {
this.editorInstance.stopDrawingMode(); this.editorInstance.stopDrawingMode();
this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none'); this.displayingSubMenu && (this.displayingSubMenu.style.display = 'none');
...@@ -286,21 +316,26 @@ export default { ...@@ -286,21 +316,26 @@ export default {
switch (styleType) { switch (styleType) {
case 'b': case 'b':
styleObj = { fontWeight: 'bold' }; styleObj = { fontWeight: 'bold' };
this.records[this.activeObjectId] && (this.records[this.activeObjectId].bold = true)
break; break;
case 'i': case 'i':
styleObj = { fontStyle: 'italic' }; styleObj = { fontStyle: 'italic' };
this.records[this.activeObjectId] && (this.records[this.activeObjectId].italic = true)
break; break;
case 'u': case 'u':
styleObj = { underline: true }; styleObj = { underline: true }; // 后端暂时不支持
break; break;
case 'l': case 'l':
styleObj = { textAlign: 'left' }; styleObj = { textAlign: 'left' };
this.records[this.activeObjectId] && (this.records[this.activeObjectId].textAlign = 'left')
break; break;
case 'c': case 'c':
styleObj = { textAlign: 'center' }; styleObj = { textAlign: 'center' };
this.records[this.activeObjectId] && (this.records[this.activeObjectId].textAlign = 'center')
break; break;
case 'r': case 'r':
styleObj = { textAlign: 'right' }; styleObj = { textAlign: 'right' };
this.records[this.activeObjectId] && (this.records[this.activeObjectId].textAlign = 'right')
break; break;
default: default:
styleObj = {}; styleObj = {};
...@@ -332,9 +367,13 @@ export default { ...@@ -332,9 +367,13 @@ export default {
}, },
inputFontSizeRangeChange () { inputFontSizeRangeChange () {
let value = this.inputFontSizeRange.getAttribute('value'); let value = this.inputFontSizeRange.getAttribute('value');
this.activeObjectId && this.editorInstance.changeTextStyle(this.activeObjectId, { if (this.activeObjectId) {
fontSize: parseInt(value, 10) let size = parseInt(value, 10);
this.editorInstance.changeTextStyle(this.activeObjectId, {
fontSize: size
}); });
this.records[this.activeObjectId] && (this.records[this.activeObjectId].fontSize = size);
}
}, },
activateTextMode() { activateTextMode() {
if (this.editorInstance.getDrawingMode() !== 'TEXT') { if (this.editorInstance.getDrawingMode() !== 'TEXT') {
...@@ -395,18 +434,23 @@ export default { ...@@ -395,18 +434,23 @@ export default {
}, },
getImages () { getImages () {
this.activateImageMode(); this.activateImageMode();
this.addImage('https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2980445260,41238050&fm=26&gp=0.jpg'); this.addImage('https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2980445260,41238050&fm=26&gp=0.jpg',2);
}, },
addImage (imgPath) { addImage (imgPath, type) {
if (!imgPath || !imgPath.length){ if (!imgPath || !imgPath.length){
throw Error("imgPath can't null") throw Error("imgPath can't null")
} }
// return new Promise(function(resolve, reject) {
this.editorInstance.addImageObject(imgPath).then(objectProps => { this.editorInstance.addImageObject(imgPath).then(objectProps => {
console.log(objectProps.id); console.log(objectProps.id);
// resolve(objectProps); let id = this.GenNonDuplicateID() + '_' + objectProps.id;
this.$parent.imgList[id] = imgPath; // FIXME
let target = this.$parent.records[objectProps.id];
if (target) {
target.url = imgPath;
target.randomId = id;
target.objType = type;
}
}); });
// }.bind(this));
}, },
addText (text, style = null, position = null) { addText (text, style = null, position = null) {
// return new Promise(function(resolve, reject) { // return new Promise(function(resolve, reject) {
...@@ -434,6 +478,9 @@ export default { ...@@ -434,6 +478,9 @@ export default {
}) })
.then(function (objectProps) { .then(function (objectProps) {
console.log(objectProps); console.log(objectProps);
let id = this.GenNonDuplicateID() + '_' + objectProps.id;
let target = this.$parent.records[objectProps.id];
target && (target.randomId = id);
}); });
}, },
activateImageMode () { activateImageMode () {
...@@ -447,28 +494,27 @@ export default { ...@@ -447,28 +494,27 @@ export default {
editor.style.height = window.getComputedStyle(container).maxHeight; editor.style.height = window.getComputedStyle(container).maxHeight;
}, },
getRecord(obj){ getRecord(obj){
// {
// "id":1,
// "type":"i-text",
// "left":172.90222908850143,
// "top":39.920166015625,
// "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"
// }
if (!this.records) this.records = {}; if (!this.records) this.records = {};
let vo = this.records[obj.id];
if (vo) {
vo.left = obj.left;
vo.top = obj.top;
vo.width = obj.width;
vo.height = obj.height;
vo.angle = obj.angle;
if (obj.type === 'text' || obj.type === 'i-text') {
vo.text = obj.text;
vo.fontSize = obj.fontSize;
}
this.records[obj.id] = vo;
} else {
this.records[obj.id] = obj; this.records[obj.id] = obj;
} }
},
GenNonDuplicateID(randomLength){
return parseFloat(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}
} }
}; };
</script> </script>
...@@ -29,9 +29,8 @@ export default { ...@@ -29,9 +29,8 @@ export default {
"tui-image-editor": PosterPage "tui-image-editor": PosterPage
}, },
data () { data () {
const bgPath = ""
return { return {
imgList: {},
dialog: { dialog: {
preview: false, // 预览弹出显示隐藏 preview: false, // 预览弹出显示隐藏
edit: false // 编辑弹出显示隐藏 edit: false // 编辑弹出显示隐藏
...@@ -162,7 +161,7 @@ export default { ...@@ -162,7 +161,7 @@ export default {
try { try {
const res = await getPosterInfo(item.id) const res = await getPosterInfo(item.id)
const data = res.data || {} const data = res.data || {}
console.log(data) console.log('getPosterInfo',data)
this.posterForm = { this.posterForm = {
id: data.id, id: data.id,
title: data.title, title: data.title,
...@@ -179,17 +178,57 @@ export default { ...@@ -179,17 +178,57 @@ export default {
ready () { ready () {
console.log('ready') console.log('ready')
}, },
checkState (obj) { onAddText(pos) {
switch (obj.type) { this.$refs.tuiImageEditor.editorInstance
case 'text': .addText('双击输入文字', {
this.showSubMenu('text'); position: pos.originPosition,
this.activateTextMode(); })
break; .then(function (objectProps) {
default: console.log(objectProps);
this.activateImageMode(); });
break; },
//移动
onObjectMoved(res) {
console.log('onObjectMoved')
this.getRecord(res)
},
//新增/选中
objectActivated(obj) {
console.log('objectActivated')
var imageEditor = this.$refs.tuiImageEditor;
imageEditor.activeObjectId = obj.id;
if (obj.type === 'text') {
imageEditor.showSubMenu('text');
imageEditor.setTextToolbar(obj);
imageEditor.activateTextMode();
}
},
//缩放
onObjectScaled(obj) {
console.log('onObjectScaled')
// this.getRecord(res);
// this.checkState();
if (obj.type === 'text') {
this.$refs.tuiImageEditor.inputFontSizeRange.setAttribute('value',obj.fontSize);
} }
}, },
//重做
onRedoStackChanged(length) {
if (length) {
this.$refs.tuiImageEditor.btn_redo.remove('disabled');
} else {
this.$refs.tuiImageEditor.btn_redo.add('disabled');
}
this.$refs.tuiImageEditor.resizeEditor();
},
onUndoStackChanged(length) {
if (length) {
this.$refs.tuiImageEditor.btn_undo.classList.remove('disabled')
} else {
this.$refs.tuiImageEditor.btn_undo.classList.add('disabled')
}
this.$refs.tuiImageEditor.resizeEditor();
},
showSubMenu (type) { showSubMenu (type) {
switch (type) { switch (type) {
case 'text': case 'text':
...@@ -206,6 +245,17 @@ export default { ...@@ -206,6 +245,17 @@ export default {
imageEditor.startDrawingMode('TEXT'); imageEditor.startDrawingMode('TEXT');
} }
}, },
checkState (obj) {
switch (obj.type) {
case 'text':
this.showSubMenu('text');
this.activateTextMode();
break;
default:
this.activateImageMode();
break;
}
},
activateImageMode () { activateImageMode () {
let imageEditor = this.$refs.tuiImageEditor.editorInstance; let imageEditor = this.$refs.tuiImageEditor.editorInstance;
imageEditor.stopDrawingMode(); imageEditor.stopDrawingMode();
...@@ -239,37 +289,79 @@ export default { ...@@ -239,37 +289,79 @@ export default {
this.msgSuccess('删除成功') this.msgSuccess('删除成功')
}) })
}, },
inputFontSizeRangeChange () { inputFontSizeRangeChange (e) {
this.$refs.tuiImageEditor.inputFontSizeRangeChange(); 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;
}
});
}, },
// //
async save() { async save() {
let list =[];
try { try {
// const posterForm = this.posterForm
// console.log('save', posterForm.id)
// let res = {}
// if (posterForm.id) {
// // 编辑海报
// res = await updatePoster(Object.assign({}, {
// backgroundImgPath: 'http://106.13.236.58:8080/profile/2020/12/31/5ca3fa0b-55d7-433c-ba02-61d4502e45e1.jpg',
// posterSubassemblyList: []
// }, this.posterForm))
// } else {
// // 新建海报
// res = await addPoster(Object.assign({}, {
// backgroundImgPath: 'http://106.13.236.58:8080/profile/2020/12/31/5ca3fa0b-55d7-433c-ba02-61d4502e45e1.jpg',
// posterSubassemblyList: []
// }, this.posterForm))
// }
// if (res.code === 200) {
// this.msgSuccess(res.msg)
// this.$refs.page.getList(1)
// this.beforeCloseDialog()
// }
let imageEditor = this.$refs.tuiImageEditor; let imageEditor = this.$refs.tuiImageEditor;
let res = {};
let list =[];
let deleteId = []; let deleteId = [];
imageEditor.editorInstance._invoker._undoStack.forEach(element => { imageEditor.editorInstance._invoker._undoStack.forEach(element => {
if(element.name == "removeObject"){ if(element.name == "removeObject"){
...@@ -286,10 +378,67 @@ export default { ...@@ -286,10 +378,67 @@ export default {
// const image = this.$refs.tuiImageEditor.editorInstance.toDataURL(); // const image = this.$refs.tuiImageEditor.editorInstance.toDataURL();
// res.url = image; // res.url = image;
res.records = list;
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
// TODO 塞新建海报的数据
let posterSubList = [];
let i = 0, len = list.length;
while (i < len) {
let vo = list[i];
let type = list[i].type;
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: 1, // 1 启动 0 删除 FIXME 暂时写死
fontColor: vo.fill || '#000000',
fontId: isText ? i : null, // 字体ID 与imgPath互斥
fontSize: vo.fontSize,
fontTextAlign: align, // 1 2 3 left center right
left: vo.left,
top: vo.top,
width: vo.width,
height: vo.height,
imgPath: vo.url || '',
posterId: null,
type: isText ? 1 : vo.objType, // 1 固定文本 2 固定图片 3 二维码图片
alpha: vo.opacity,
fontStyle: (vo.italic && vo.bold) ? 3 : vo.italic ? 2 : vo.bold ? 1 : 0, // 0 通常 1 粗体 2 斜体 3 粗 + 斜
rotate: vo.angle,
order: i, // 层级
categoryId: 0, // 分类ID
verticalType: 2 // 居中方式 写死2
};
posterSubList.push(posData);
i++;
}
const posterForm = this.posterForm
console.log('save', posterForm.id)
let res = {}
if (posterForm.id) {
// 编辑海报
res = await updatePoster(Object.assign({}, {
backgroundImgPath: 'http://106.13.236.58:8080/profile/2020/12/31/5ca3fa0b-55d7-433c-ba02-61d4502e45e1.jpg',
posterSubassemblyList: []
}, this.posterForm))
} else {
// 新建海报
res = await addPoster(Object.assign({}, {
backgroundImgPath: 'http://106.13.236.58:8080/profile/2020/12/31/5ca3fa0b-55d7-433c-ba02-61d4502e45e1.jpg',
posterSubassemblyList: posterSubList
}, this.posterForm))
}
if (res.code === 200) {
this.msgSuccess(res.msg)
this.$refs.page.getList(1)
this.beforeCloseDialog()
}
} }
}, },
} }
...@@ -466,7 +615,7 @@ export default { ...@@ -466,7 +615,7 @@ export default {
</li> </li>
<li class="menu-item"> <li class="menu-item">
<label class="no-pointer"> <label class="no-pointer">
<input id="input-font-size-range" @change="inputFontSizeRangeChange" type="range" min="10" max="100" value="10" /> <input id="input-font-size-range" @change="inputFontSizeRangeChange($event)" type="range" min="10" max="100" value="10" />
</label> </label>
</li> </li>
<li class="menu-item"> <li class="menu-item">
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!