Commit 7e559721 authored by campaign's avatar campaign

by zhanyi

parent 02af05d8
......@@ -29,7 +29,7 @@ $dependency = Array(
,'src/module/editor.js'
,'src/module/editor.range.js'
,'src/module/editor.receiver.js'
,'src/module/editor.cursor.js'
,'src/module/editor.selection.js'
,'src/module/basestyle.js'
,'src/module/font.js'
,'src/module/zoom.js'
......
......@@ -31,5 +31,4 @@ $.wordCountAdaptive = function( word, hasSuffix ) {
return $.wordCountAdaptive( word + '...', true );
}
};
\ No newline at end of file
KityMinder.registerModule( "TextEditModule", function () {
var cursor = new Minder.Cursor();
var sel = new Minder.Selection();
var receiver = new Minder.Receiver(this);
var range = new Minder.Range();
this.receiver = receiver;
var mouseDownStatus = false;
var oneTime = 0;
var lastEvtPosition,dir = 1;
return {
//插入光标
"init":function(){
this.getPaper().addShape(cursor);
this.getPaper().addShape(sel);
},
"events": {
'beforemousedown':function(e){
cursor.setHide();
sel.setHide();
var node = e.getTargetNode();
if(node){
var textShape = node.getTextShape();
textShape.setStyle('cursor','default');
if(node.isSelected()){
sel.collapse();
node.getTextShape().setStyle('cursor','text');
receiver.setTextEditStatus(true)
.setCursor(cursor)
.setSelection(sel)
.setKityMinder(this)
.setMinderNode(node)
.setTextShape(textShape)
.setBaseOffset()
.setContainerStyle()
.setCursorHeight()
.setSelectionHeight()
.setCurrentIndex(e.getPosition())
.updateCursor()
.updateSelection()
.setRange(range);
mouseDownStatus = true;
lastEvtPosition = e.getPosition();
}
}
},
'mouseup':function(e){
mouseDownStatus = false;
oneTime = 0;
},
'mousemove':function(e){
if(mouseDownStatus){
var offset = e.getPosition();
dir = offset.x > lastEvtPosition.x ? 1 : (offset.x < lastEvtPosition.x ? -1 : dir);
receiver.updateSelectionByMousePosition(offset,dir)
.updateSelectionShow(dir);
sel.stroke('none',0);
lastEvtPosition = e.getPosition();
}
},
'restoreScene':function(){
cursor.setHide();
sel.setHide();
},
'stopTextEdit':function(){
cursor.setHide();
sel.setHide();
}
}
......
......@@ -2,7 +2,7 @@
Minder.Receiver = kity.createClass('Receiver',{
clear : function(){
this.container.innerHTML = '';
this.cursor.setHide();
this.selection.setHide();
this.index = 0;
return this;
},
......@@ -99,10 +99,10 @@ Minder.Receiver = kity.createClass('Receiver',{
this.setBaseOffset();
this.updateTextData();
this.updateIndex();
this.updateCursor();
this.updateSelection();
this.timer = setTimeout(function(){
me.cursor.setShow()
me.selection.setShow()
},500);
return true;
}
......@@ -115,21 +115,23 @@ Minder.Receiver = kity.createClass('Receiver',{
updateTextData : function(){
this.textShape.textData = this.getTextOffsetData();
},
setCursor : function(cursor){
this.cursor = cursor;
setSelection : function(selection){
this.selection = selection;
return this;
},
updateCursor : function(){
this.cursor.setShowHold();
this.cursor.bringTop();
updateSelection : function(){
this.selection.setShowHold();
this.selection.bringTop();
//更新模拟选区的范围
this.selection.setStartOffset(this.index).collapse(true);
if(this.index == this.textData.length){
this.cursor.setPosition({
this.selection.setPosition({
x : this.textData[this.index-1].x + this.textData[this.index-1].width,
y : this.textData[this.index-1].y
})
}else{
this.cursor.setPosition(this.textData[this.index])
this.selection.setPosition(this.textData[this.index])
}
return this;
},
......@@ -199,8 +201,73 @@ Minder.Receiver = kity.createClass('Receiver',{
return this;
},
setCursorHeight:function(){
this.cursor.setHeight(this.getTextShapeHeight());
setSelectionHeight:function(){
this.selection.setHeight(this.getTextShapeHeight());
return this;
},
getIndexByMousePosition:function(offset,dir){
var me = this;
var currentIndex;
var hadChanged = false;
utils.each(this.textData,function(i,v){
//点击开始之前
if(i == 0 && offset.x <= v.x){
currentIndex = 0;
return false;
}
if(i == me.textData.length -1 && offset.x >= v.x){
currentIndex = me.textData.length;
return false;
}
if(offset.x >= v.x && offset.x <= v.x + v.width){
currentIndex = i + (dir == -1 ? 0 : 1);
return false;
}
});
return currentIndex;
},
updateSelectionByMousePosition:function(offset,dir){
var currentIndex = this.getIndexByMousePosition(offset,dir);
if(currentIndex == 0 || currentIndex == this.textData.length ){
this.selection.setEndOffset(currentIndex);
}else{
if(dir == -1 && currentIndex < this.selection.startOffset){
this.selection.setStartOffset(currentIndex)
}else{
this.selection.setEndOffset(currentIndex);
}
console.log(this.selection.startOffset + ':' + this.selection.endOffset)
console.log(this.selection.isCollapsed)
}
return this;
},
updateSelectionShow:function(dir){
var startOffset = this.textData[this.selection.startOffset],
endOffset = this.textData[this.selection.endOffset],
width = 0 ;
if(this.selection.isCollapsed){
this.selection.updateShow(startOffset,0);
return this;
}
if(!endOffset){
var lastOffset = this.textData[this.textData.length -1];
width = lastOffset.x - startOffset.x + lastOffset.width;
}else{
width = endOffset.x - startOffset.x;
}
this.selection.updateShow(startOffset,width);
return this;
},
updateNativeRange:function(){
}
});
\ No newline at end of file
//模拟光标
Minder.Cursor = kity.createClass( 'Cursor', {
base: kity.Line,
Minder.Selection = kity.createClass( 'Selection', {
base: kity.Rect,
constructor: function ( height, color, width ) {
this.callBase();
this.height = height || 20;
this.stroke( color || 'blue', width || 1 );
this.width = 1;
this.fill('#99C8FF');
this.setHide();
this.timer = null;
this.isCollapsed = true;
this.startOffset = this.endOffset = 0;
this.setOpacity(0.5)
},
collapse : function(toEnd){
this.stroke( 'blue', 1 );
this.width = 1;
this.isCollapsed = true;
if(toEnd){
this.startOffset = this.endOffset
}else{
this.endOffset = this.startOffset;
}
return this;
},
setStartOffset:function(offset){
this.startOffset = offset;
var tmpOffset = this.startOffset;
if(this.startOffset > this.endOffset){
this.startOffset = this.endOffset;
this.endOffset = tmpOffset;
}else if(this.startOffset == this.endOffset){
this.collapse();
return this;
}
this.isCollapsed = false;
this.stroke('none');
return this;
},
setEndOffset:function(offset){
this.endOffset = offset;
var tmpOffset = this.endOffset;
if(this.endOffset < this.startOffset){
this.endOffset = this.startOffset;
this.startOffset = tmpOffset;
}else if(this.startOffset == this.endOffset){
this.collapse();
return this;
}
this.isCollapsed = false;
this.stroke('none');
return this;
},
updateShow : function(offset,width){
this.setPosition(offset).setWidth(width);
return this;
},
setPosition: function ( offset ) {
try {
this.setPoint1( offset.x, offset.y );
this.setPoint2( offset.x, offset.y + this.height );
this.x = offset.x;
this.y = offset.y;
} catch ( e ) {
debugger
console.log(e)
}
return this;
return this.update();
},
setHeight: function ( height ) {
this.height = height;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment