Commit 7e559721 authored by campaign's avatar campaign

by zhanyi

parent 02af05d8
...@@ -29,7 +29,7 @@ $dependency = Array( ...@@ -29,7 +29,7 @@ $dependency = Array(
,'src/module/editor.js' ,'src/module/editor.js'
,'src/module/editor.range.js' ,'src/module/editor.range.js'
,'src/module/editor.receiver.js' ,'src/module/editor.receiver.js'
,'src/module/editor.cursor.js' ,'src/module/editor.selection.js'
,'src/module/basestyle.js' ,'src/module/basestyle.js'
,'src/module/font.js' ,'src/module/font.js'
,'src/module/zoom.js' ,'src/module/zoom.js'
......
...@@ -31,5 +31,4 @@ $.wordCountAdaptive = function( word, hasSuffix ) { ...@@ -31,5 +31,4 @@ $.wordCountAdaptive = function( word, hasSuffix ) {
return $.wordCountAdaptive( word + '...', true ); return $.wordCountAdaptive( word + '...', true );
} }
}; };
\ No newline at end of file
KityMinder.registerModule( "TextEditModule", function () { KityMinder.registerModule( "TextEditModule", function () {
var cursor = new Minder.Cursor(); var sel = new Minder.Selection();
var receiver = new Minder.Receiver(this); var receiver = new Minder.Receiver(this);
var range = new Minder.Range(); var range = new Minder.Range();
this.receiver = receiver; this.receiver = receiver;
var mouseDownStatus = false;
var oneTime = 0;
var lastEvtPosition,dir = 1;
return { return {
//插入光标 //插入光标
"init":function(){ "init":function(){
this.getPaper().addShape(cursor); this.getPaper().addShape(sel);
}, },
"events": { "events": {
'beforemousedown':function(e){ 'beforemousedown':function(e){
cursor.setHide(); sel.setHide();
var node = e.getTargetNode(); var node = e.getTargetNode();
if(node){ if(node){
var textShape = node.getTextShape(); var textShape = node.getTextShape();
textShape.setStyle('cursor','default'); textShape.setStyle('cursor','default');
if(node.isSelected()){ if(node.isSelected()){
sel.collapse();
node.getTextShape().setStyle('cursor','text'); node.getTextShape().setStyle('cursor','text');
receiver.setTextEditStatus(true) receiver.setTextEditStatus(true)
.setCursor(cursor) .setSelection(sel)
.setKityMinder(this) .setKityMinder(this)
.setMinderNode(node) .setMinderNode(node)
.setTextShape(textShape) .setTextShape(textShape)
.setBaseOffset() .setBaseOffset()
.setContainerStyle() .setContainerStyle()
.setCursorHeight() .setSelectionHeight()
.setCurrentIndex(e.getPosition()) .setCurrentIndex(e.getPosition())
.updateCursor() .updateSelection()
.setRange(range); .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(){ 'restoreScene':function(){
cursor.setHide(); sel.setHide();
}, },
'stopTextEdit':function(){ 'stopTextEdit':function(){
cursor.setHide(); sel.setHide();
} }
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Minder.Receiver = kity.createClass('Receiver',{ Minder.Receiver = kity.createClass('Receiver',{
clear : function(){ clear : function(){
this.container.innerHTML = ''; this.container.innerHTML = '';
this.cursor.setHide(); this.selection.setHide();
this.index = 0; this.index = 0;
return this; return this;
}, },
...@@ -99,10 +99,10 @@ Minder.Receiver = kity.createClass('Receiver',{ ...@@ -99,10 +99,10 @@ Minder.Receiver = kity.createClass('Receiver',{
this.setBaseOffset(); this.setBaseOffset();
this.updateTextData(); this.updateTextData();
this.updateIndex(); this.updateIndex();
this.updateCursor(); this.updateSelection();
this.timer = setTimeout(function(){ this.timer = setTimeout(function(){
me.cursor.setShow() me.selection.setShow()
},500); },500);
return true; return true;
} }
...@@ -115,21 +115,23 @@ Minder.Receiver = kity.createClass('Receiver',{ ...@@ -115,21 +115,23 @@ Minder.Receiver = kity.createClass('Receiver',{
updateTextData : function(){ updateTextData : function(){
this.textShape.textData = this.getTextOffsetData(); this.textShape.textData = this.getTextOffsetData();
}, },
setCursor : function(cursor){ setSelection : function(selection){
this.cursor = cursor; this.selection = selection;
return this; return this;
}, },
updateCursor : function(){ updateSelection : function(){
this.cursor.setShowHold(); this.selection.setShowHold();
this.cursor.bringTop(); this.selection.bringTop();
//更新模拟选区的范围
this.selection.setStartOffset(this.index).collapse(true);
if(this.index == this.textData.length){ 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, x : this.textData[this.index-1].x + this.textData[this.index-1].width,
y : this.textData[this.index-1].y y : this.textData[this.index-1].y
}) })
}else{ }else{
this.cursor.setPosition(this.textData[this.index]) this.selection.setPosition(this.textData[this.index])
} }
return this; return this;
}, },
...@@ -199,8 +201,73 @@ Minder.Receiver = kity.createClass('Receiver',{ ...@@ -199,8 +201,73 @@ Minder.Receiver = kity.createClass('Receiver',{
return this; return this;
}, },
setCursorHeight:function(){ setSelectionHeight:function(){
this.cursor.setHeight(this.getTextShapeHeight()); this.selection.setHeight(this.getTextShapeHeight());
return this; 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', { Minder.Selection = kity.createClass( 'Selection', {
base: kity.Line, base: kity.Rect,
constructor: function ( height, color, width ) { constructor: function ( height, color, width ) {
this.callBase(); this.callBase();
this.height = height || 20; this.height = height || 20;
this.stroke( color || 'blue', width || 1 ); this.stroke( color || 'blue', width || 1 );
this.width = 1;
this.fill('#99C8FF');
this.setHide(); this.setHide();
this.timer = null; 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 ) { setPosition: function ( offset ) {
try { try {
this.setPoint1( offset.x, offset.y ); this.x = offset.x;
this.setPoint2( offset.x, offset.y + this.height ); this.y = offset.y;
} catch ( e ) { } catch ( e ) {
debugger console.log(e)
} }
return this;
return this.update();
}, },
setHeight: function ( height ) { setHeight: function ( height ) {
this.height = 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