diff --git a/ureport2-js/src/i18n/designer.json b/ureport2-js/src/i18n/designer.json index c7b7b09..3577755 100644 --- a/ureport2-js/src/i18n/designer.json +++ b/ureport2-js/src/i18n/designer.json @@ -711,6 +711,8 @@ "repeatFooter":"重复表尾", "summary":"总结行", "cancel":"取消行类型", + "copy":"复制单元格样式", + "paste":"粘贴单元格样式", "clearContent":"清空内容", "clearStyle":"清空格式", "clearAll":"清空所有", diff --git a/ureport2-js/src/i18n/designer_en.json b/ureport2-js/src/i18n/designer_en.json index 593a269..83745b7 100644 --- a/ureport2-js/src/i18n/designer_en.json +++ b/ureport2-js/src/i18n/designer_en.json @@ -711,6 +711,8 @@ "repeatFooter":"Repeat Footer", "summary":"Summary Row", "cancel":"Cancel", + "copy":"Copy Style", + "paste":"Paste Style", "clearContent":"Empty Content", "clearStyle":"Empty Style", "clearAll":"Empty All", diff --git a/ureport2-js/src/table/ContextMenu.js b/ureport2-js/src/table/ContextMenu.js index 9206c45..8083370 100644 --- a/ureport2-js/src/table/ContextMenu.js +++ b/ureport2-js/src/table/ContextMenu.js @@ -61,7 +61,7 @@ export default function buildMenuConfigure(){ undo:function(){ undoCleanCells(_this.context,startRow,endRow,startCol,endCol,removeCellsMap,'all'); } - }) + }); }else if(key==='repeat_row_header'){ const selected=this.getSelected(); const startRow=selected[0],endRow=selected[2]; @@ -135,6 +135,31 @@ export default function buildMenuConfigure(){ }); },colWidth,true); setDirty(); + }else if(key==='copy_style'){ + const selected=this.getSelected(); + const startRow=selected[0],endRow=selected[2],startCol=selected[1],endCol=selected[3]; + let cell=_this.context.getCell(startRow,startCol); + if(!cell){ + alert("请先选中目标单元格!"); + return; + } + window.__copy_cell_style__=cell.cellStyle; + }else if(key==='paste_style'){ + if(!window.__copy_cell_style__){ + alert('请先复制目标单元格样式'); + return; + } + const selected=this.getSelected(); + const startRow=selected[0],endRow=selected[2],startCol=selected[1],endCol=selected[3]; + let oldCellsStyleMap=pasteStyle(_this.context,startRow,endRow,startCol,endCol); + undoManager.add({ + redo:function(){ + oldCellsStyleMap=pasteStyle(_this.context,startRow,endRow,startCol,endCol); + }, + undo:function(){ + undoPasteStyle(_this.context,startRow,endRow,startCol,endCol,oldCellsStyleMap); + } + }); } }, items: { @@ -186,6 +211,14 @@ export default function buildMenuConfigure(){ name: ` ${window.i18n.table.contextMenu.cancel}`, disabled:checkRowDeleteOperationDisabled }, + "copy_style": { + name: ` ${window.i18n.table.contextMenu.copy}`, + disabled:checkCopyOperationDisabled + }, + "paste_style": { + name: ` ${window.i18n.table.contextMenu.paste}`, + disabled:checkPasteOperationDisabled + }, "clean_content": { name: ` ${window.i18n.table.contextMenu.clearContent}`, disabled:checkCleanOperationDisabled @@ -253,6 +286,59 @@ export default function buildMenuConfigure(){ hot.render(); }; + + function undoPasteStyle(context,startRow,endRow,startCol,endCol,oldStyleMap){ + const style=window.__copy_cell_style__; + let cellsMap=new Map(),hot=context.hot; + for(let i=startRow;i<=endRow;i++){ + for(let j=startCol;j<=endCol;j++){ + let cell=context.getCell(i,j); + if(!cell){ + continue; + } + let key=cell.rowNumber+","+cell.columnNumber; + const oldStyle=oldStyleMap.get(key); + if(oldStyle){ + cell.cellStyle=oldStyle; + } + } + } + Handsontable.hooks.run(hot, 'afterSelectionEnd',startRow,startCol,endRow,endCol); + hot.render(); + return cellsMap; + }; + + function pasteStyle(context,startRow,endRow,startCol,endCol){ + const style=window.__copy_cell_style__; + let cellsMap=new Map(),hot=context.hot; + for(let i=startRow;i<=endRow;i++){ + for(let j=startCol;j<=endCol;j++){ + let cell=context.getCell(i,j); + if(!cell){ + continue; + } + let key=cell.rowNumber+","+cell.columnNumber; + if(!cell.cellStyle){ + cell.cellStyle={}; + } + const oldStyle=JSON.parse(JSON.stringify(cell.cellStyle)); + cellsMap.set(key,oldStyle); + cell.cellStyle.fontSize=style.fontSize; + cell.cellStyle.forecolor=style.forecolor; + cell.cellStyle.fontFamily=style.fontFamily; + cell.cellStyle.valign=style.valign; + cell.cellStyle.align=style.align; + cell.cellStyle.bgcolor=style.bgcolor; + cell.cellStyle.bold=style.bold; + cell.cellStyle.italic=style.italic; + cell.cellStyle.underline=style.underline; + } + } + Handsontable.hooks.run(hot, 'afterSelectionEnd',startRow,startCol,endRow,endCol); + hot.render(); + return cellsMap; + }; + function cleanCells(context,startRow,endRow,startCol,endCol,type){ let removeCellsMap=new Map(),hot=context.hot; for(let i=startRow;i<=endRow;i++){ @@ -298,6 +384,23 @@ export default function buildMenuConfigure(){ return removeCellsMap; }; + function checkCopyOperationDisabled(){ + const selected=this.getSelected(); + if(!selected){ + return true; + } + return false; + }; + function checkPasteOperationDisabled(){ + const selected=this.getSelected(); + if(!selected){ + return true; + } + if(window.__copy_cell_style__){ + return false; + } + return true; + }; function checkRowDeleteOperationDisabled(){ const selected=this.getSelected(); if(!selected){