Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
feat(image): copy and paste
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 committed Oct 9, 2020
1 parent af7b939 commit 33d6c96
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/controllers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4853,6 +4853,9 @@ export default function luckysheetHandler() {
selection.pasteHandlerOfCopyPaste(Store.luckysheet_copy_save);
}
}
else if(txtdata.indexOf("luckysheet_copy_action_image") > - 1){
imageCtrl.pasteImgItem();
}
else {
if (txtdata.indexOf("table") > -1) {
$("#luckysheet-copy-content").html(txtdata);
Expand Down
65 changes: 64 additions & 1 deletion src/controllers/imageCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ const imageCtrl = {
cropChange: null,
cropChangeXY: null,
cropChangeObj: null,
copyImgItemObj: null,
inserImg: function(src){
let _this = this;

let rowIndex = Store.luckysheet_select_save[0].row_focus || 0;
let colIndex = Store.luckysheet_select_save[0].column_focus || 0;
let left = colIndex == 0 ? 0 : Store.visibledatacolumn[colIndex - 1];
Expand All @@ -67,7 +70,7 @@ const imageCtrl = {
originHeight: height
}

imageCtrl.addImgItem(img);
_this.addImgItem(img);
}
image.src = src;
},
Expand Down Expand Up @@ -615,6 +618,8 @@ const imageCtrl = {
"top": -imgItem.border.width,
"bottom": -imgItem.border.width,
})

_this.currentImgId = null;
},
addImgItem: function(img) {
let _this = this;
Expand Down Expand Up @@ -855,6 +860,64 @@ const imageCtrl = {

_this.ref();
},
copyImgItem: function(e) {
let _this = this;

_this.copyImgItemObj = $.extend(true, {}, _this.images[_this.currentImgId]);

let clipboardData = window.clipboardData; //for IE
if (!clipboardData) { // for chrome
clipboardData = e.originalEvent.clipboardData;
}

let cpdata = '<table data-type="luckysheet_copy_action_image"><tr><td><td></tr></table>';

if (!clipboardData) {
let textarea = $("#luckysheet-copy-content");
textarea.html(cpdata);
textarea.focus();
textarea.select();
document.execCommand("selectAll");
document.execCommand("Copy");
// 等50毫秒,keyPress事件发生了再去处理数据
setTimeout(function () {
$("#luckysheet-copy-content").blur();
}, 10);
}
else {
clipboardData.setData('Text', cpdata);
return false;//否则设不生效
}
},
pasteImgItem: function() {
let _this = this;

let rowIndex = Store.luckysheet_select_save[0].row_focus || 0;
let colIndex = Store.luckysheet_select_save[0].column_focus || 0;
let left = colIndex == 0 ? 0 : Store.visibledatacolumn[colIndex - 1];
let top = rowIndex == 0 ? 0 : Store.visibledatarow[rowIndex - 1];

let img = $.extend(true, {}, _this.copyImgItemObj);

img.default.left = left - img.crop.offsetLeft;
img.default.top = top - img.crop.offsetTop;

let scrollTop = $("#luckysheet-cell-main").scrollTop(),
scrollLeft = $("#luckysheet-cell-main").scrollLeft();

img.fixedLeft = img.default.left - scrollLeft + Store.rowHeaderWidth;
img.fixedTop = img.default.top - scrollTop + Store.infobarHeight + Store.toolbarHeight + Store.calculatebarHeight + Store.columeHeaderHeight;

let id = _this.generateRandomId();
let modelHtml = _this.modelHtml(id, img);

$("#luckysheet-image-showBoxs .img-list").append(modelHtml);

_this.images[id] = img;
_this.ref();

_this.init();
},
allImagesShow: function() {
let _this = this;

Expand Down
19 changes: 16 additions & 3 deletions src/controllers/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { selectHightlightShow, selectIsOverlap } from './select';
import selection from './selection';
import searchReplace from './searchReplace';
import controlHistory from './controlHistory';
import imageCtrl from './imageCtrl';

import {
getByteLen,
Expand Down Expand Up @@ -313,8 +314,6 @@ export function keyboardInitial(){

return;
}



let $inputbox = $("#luckysheet-input-box");

Expand Down Expand Up @@ -433,6 +432,11 @@ export function keyboardInitial(){
$("#luckysheet-icon-bold").click();
}
else if (kcode == 67) {//Ctrl + C 复制
if(imageCtrl.currentImgId != null){
imageCtrl.copyImgItem(event);
return;
}

//复制时存在格式刷状态,取消格式刷
if(menuButton.luckysheetPaintModelOn){
menuButton.cancelPaintModel();
Expand Down Expand Up @@ -749,10 +753,19 @@ export function keyboardInitial(){
selectHightlightShow();
}
else if (kcode == keycode.DELETE) {
$("#luckysheet-delete-text").click();
if(imageCtrl.currentImgId != null){
imageCtrl.removeImgItem();
}
else{
$("#luckysheet-delete-text").click();
}

event.preventDefault();
}
else if(kcode == 8 && imageCtrl.currentImgId != null){
imageCtrl.removeImgItem();
event.preventDefault();
}
else if (kcode == keycode.UP) {
if (parseInt($inputbox.css("top")) > 0 || $(event.target).hasClass("formulaInputFocus") || $("#luckysheet-singleRange-dialog").is(":visible") || $("#luckysheet-multiRange-dialog").is(":visible")) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/luckysheetConfigsetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const luckysheetConfigsetting = {

editMode: false,
beforeCreateDom: null,
workbookCreateBefore: null,
workbookCreateAfter: null,
fireMousedown: null,
plugins:[],
forceCalculation:false,//强制刷新公式,公式较多会有性能问题,慎用
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/sheetmanage.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,15 @@ const sheetmanage = {
}
}

//钩子函数 表格创建之前触发
if(typeof luckysheetConfigsetting.beforeCreateDom == "function" ){
luckysheetConfigsetting.beforeCreateDom(luckysheet);
}

if(typeof luckysheetConfigsetting.workbookCreateBefore == "function"){
luckysheetConfigsetting.workbookCreateBefore(luckysheet);
}

if(luckysheetConfigsetting.pointEdit){
setTimeout(function(){
$("#luckysheetloadingdata").remove();
Expand Down
2 changes: 2 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ luckysheet.create = function (setting) {

luckysheetConfigsetting.editMode = extendsetting.editMode;
luckysheetConfigsetting.beforeCreateDom = extendsetting.beforeCreateDom;
luckysheetConfigsetting.workbookCreateBefore = extendsetting.workbookCreateBefore;
luckysheetConfigsetting.workbookCreateAfter = extendsetting.workbookCreateAfter;

luckysheetConfigsetting.fireMousedown = extendsetting.fireMousedown;
luckysheetConfigsetting.forceCalculation = extendsetting.forceCalculation;
Expand Down
37 changes: 23 additions & 14 deletions src/global/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,26 @@ export function getCellValue(row, column, options = {}) {
* @param {Function} options.success 操作结束的回调函数
*/
export function setCellValue(row, column, value, options = {}) {
if (row == null && column == null) {
return tooltip.info('Arguments row or column cannot be null or undefined.', '')
if (getObjType(row) != "number" || getObjType(column) != "number") {
return tooltip.info('The row or column parameter is invalid.', '');
}
let curSheetOrder = getSheetIndex(Store.currentSheetIndex);

let {
index = Store.currentSheetIndex,
order = curSheetOrder,
order = getSheetIndex(Store.currentSheetIndex),
isRefresh = true,
success
} = {...options}

let file = Store.luckysheetfile[order];

if(file == null){
return tooltip.info("The order parameter is invalid.", "");
}

let luckysheetfile = getluckysheetfile();
let arrayIndex = getSheetIndex(index);
let data = luckysheetfile[arrayIndex].data;
let data = $.extend(true, [], file.data);
if(data.length == 0){
data = sheetmanage.buildGridData(file);
}

// luckysheetformula.updatecell(row, column, value);
let formatList = {
Expand All @@ -130,7 +136,12 @@ export function setCellValue(row, column, value, options = {}) {
//f: 1, //formula
qp:1 //quotePrefix, show number as string
}
if(value instanceof Object){

if(value == null || value.toString().length == 0){
formula.delFunctionGroup(row, column);
setcellvalue(row, column, data, value);
}
else if(value instanceof Object){
let curv = {};
if(value.f!=null && value.v==null){
curv.f = value.f;
Expand Down Expand Up @@ -163,22 +174,20 @@ export function setCellValue(row, column, value, options = {}) {
}
}
else{
if(value.substr(0,1)=="=" || value.substr(0,5)=="<span"){
if(value.toString().substr(0,1)=="=" || value.toString().substr(0,5)=="<span"){
data = luckysheetformula.updatecell(row, column, value, false).data;//update formula value or convert inline string html to object
}
else{
formula.delFunctionGroup(row, column);
setcellvalue(row, column, data, value);
}

}


if(isRefresh){
if(file.index == Store.currentSheetIndex && isRefresh){
jfrefreshgrid(data, [{ "row": [row, row], "column": [column, column] }]);//update data, meanwhile refresh canvas and store data to history
}
else{
luckysheetfile[arrayIndex] = data;//only update data
file.data = data;//only update data
}

if (success && typeof success === 'function') {
Expand Down

0 comments on commit 33d6c96

Please sign in to comment.