add copy paste

This commit is contained in:
mkwiser 2014-09-20 22:57:12 +08:00
parent 4b32480358
commit dfaee05a79
6 changed files with 130 additions and 10 deletions

View File

@ -131,6 +131,28 @@ define(
return true;
};
/**
* 添加到剪切板
*
* @param {Array} 形状集合
* @return {this}
*/
Editor.prototype.setClipBoard = function(shapes) {
Editor.ClipBoardData = null;
Editor.ClipBoardData = shapes;
return this;
};
/**
* 从剪切板中获取
*
* @param {Array} 形状集合
* @return {this}
*/
Editor.prototype.getClipBoard = function() {
return Editor.ClipBoardData ? lang.clone(Editor.ClipBoardData) : null;
};
/**
* 注销
*/

View File

@ -190,6 +190,40 @@ define(
axisLayer.removeShape(l);
});
axisLayer.refresh();
},
/**
* 剪切shapes
*
* @param {Array} shapes 形状集合
*/
cutshapes: function(shapes) {
var cutedShapes = this.getShapes(shapes);
this.setClipBoard(cutedShapes);
var fontLayer = this.fontLayer;
shapes.forEach(function(shape) {
fontLayer.removeShape(shape);
});
fontLayer.refresh();
},
/**
* 复制shapes
*
* @param {Array} shapes 形状集合
*/
copyshapes: function(shapes) {
shapes = this.getShapes(shapes);
this.setClipBoard(shapes);
},
/**
* 增加shapes
*
* @param {Array} shapes 形状集合
*/
addshapes: function(shapes) {
this.setShapes(shapes);
}
};

View File

@ -13,7 +13,8 @@ define(
var lang = require('common/lang');
var pathAdjust = require('graphics/pathAdjust');
var computeBoundingBox = require('graphics/computeBoundingBox');
var guid = require('render/util/guid');
/**
* 初始化字体
*
@ -80,13 +81,27 @@ define(
function setShapes(shapes) {
var origin = this.render.getLayer('axis').shapes[0];
var scale = this.render.camera.scale;
// 调整坐标系
var fontLayer = this.fontLayer;
// 建立id hash 防止重复
var shapeIdList = {};
fontLayer.shapes.forEach(function(shape) {
shapeIdList[shape.id] = true;
});
// 调整坐标系重置ID
shapes.forEach(function(shape) {
pathAdjust(shape.points, scale, -scale);
pathAdjust(shape.points, 1, 1, origin.x, origin.y);
if (shapeIdList[shape.id]) {
shape.id = guid('shape');
}
fontLayer.addShape(shape);
});
Array.prototype.splice.apply(this.fontLayer.shapes, [-1, 0].concat(shapes));
this.fontLayer.refresh();
fontLayer.refresh();
return this;
}
@ -100,6 +115,7 @@ define(
var origin = this.render.getLayer('axis').shapes[0];
shapes = shapes ? lang.clone(shapes) : lang.clone(this.fontLayer.shapes);
var scale = 1 / this.render.camera.scale;
// 调整坐标系
shapes.forEach(function(shape) {
pathAdjust(shape.points, scale, -scale, -origin.x, -origin.y);

View File

@ -23,6 +23,14 @@ define(
var pos = e.pos;
this.execCommand('addreferenceline', pos.x, pos.y);
}
else if (e.command == 'paste') {
var shapes = this.getClipBoard();
if(shapes) {
this.setShapes(shapes);
this.setMode('shapes', shapes);
this.fire('change');
}
}
else {
this.execCommand(e.command, e);
}
@ -155,6 +163,15 @@ define(
else if (e.keyCode == 89 && e.ctrlKey) {
me.execCommand('redo');
}
// 粘贴
else if (e.keyCode == 86 && e.ctrlKey) {
var shapes = me.getClipBoard();
if(shapes) {
me.setShapes(shapes);
me.setMode('shapes', shapes);
me.fire('change');
}
}
// esc键重置model
else if (e.key == 'esc' && !me.mode.keyup) {
me.setMode();

View File

@ -44,13 +44,13 @@ define(
title: '改变方向'
},
// cut: {
// title: '剪切'
// },
cut: {
title: '剪切'
},
// copy: {
// title: '复制'
// },
copy: {
title: '复制'
},
top: {
title: '置前'
@ -98,6 +98,15 @@ define(
remove: {
title: '删除轮廓'
},
cut: {
title: '剪切'
},
copy: {
title: '复制'
},
rotate_left: {
title: '向左旋转'
@ -143,6 +152,10 @@ define(
title: '恢复'
},
paste: {
title: '粘贴'
},
add_referenceline: {
title: '添加参考线'
},

View File

@ -95,6 +95,14 @@ define(
);
}
}
else if (command == 'cut') {
this.execCommand('cutshapes', shapes);
this.setMode();
this.fire('change');
}
else if (command == 'copy') {
this.execCommand('copyshapes', shapes);
}
this.fire('change');
}
@ -234,6 +242,16 @@ define(
this.currentGroup.setShapes(this.fontLayer.shapes.slice());
this.currentGroup.refresh();
}
// 剪切
else if (e.keyCode == 88 && e.ctrlKey && this.currentGroup) {
this.execCommand('cutshapes', this.currentGroup.shapes);
this.setMode();
this.fire('change');
}
// 复制
else if (e.keyCode == 67 && e.ctrlKey && this.currentGroup) {
this.execCommand('copyshapes', this.currentGroup.shapes);
}
// 移动
else if(stepMap[e.key] && this.currentGroup) {
this.fire('change');