Commit bc07ea99 authored by campaign's avatar campaign

添加 editor模块

parent 9be1c436
//模拟光标
Minder.Cursor = kity.createClass('Cursor',{
base: kity.Line,
constructor: function(height, color, width) {
this.callBase();
this.height = height || 20;
this.stroke(color || 'blue', width || 1);
this.setHide();
this.timer = null;
},
setPosition: function(offset) {
try{
this.setPoint1(offset.x,offset.y);
this.setPoint2(offset.x,offset.y + this.height);
}catch(e){
debugger
}
return this;
},
setHeight:function(height){
this.height = height;
},
setHide:function(){
clearInterval(this.timer);
this.setStyle('display','none');
return this;
},
setShowHold : function(){
clearInterval(this.timer);
this.setStyle('display','');
return this;
},
setShow:function(){
clearInterval(this.timer);
var me = this,
state = '';
this.timer = setInterval(function(){
me.setStyle('display',state);
state = state ? '' : 'none';
},300);
return this;
},
setTextShape:function(text){
if(!text){
this.text = new kity.Text();
}else{
this.text = text;
}
return this
},
getTextShape:function(){
return this.text
},
setTxtContent : function(text){
this.text.setContent(text)
},
updatePosition:function(index){
}
});
\ No newline at end of file
KityMinder.registerModule( "TextEditModule", function () {
var cursor = new Minder.Cursor();
var receiver = new Minder.Receiver();
var range = new Minder.Range();
this.getPaper().addShape(cursor);
return {
"events": {
'mousedown':function(e){
var originEvent = e.originEvent;
var targetShape = e.kityEvent.targetShape;
var position = e.getPosition();
if(targetShape.getType() != 'Text'){
// var offset = e.getPosition();
// cursor.setShow().setPosition(offset);
// receiver.clear()
// .setTextShape()
// .setTextShapeSize(cursor.height)
// .appendTextShapeToPaper(this.getPaper())
// .setPosition(position)
// .setRange(range,0)
// .setCursor(cursor)
}else{
receiver.setCursor(cursor)
.setTextShape(targetShape)
.setCursorHeight()
.setCurrentIndex(position)
.updateCursor()
.setRange(range)
}
},
'kbcreateandedit':function(){
receiver.clear()
}
}
};
} );
\ No newline at end of file
Minder.Range = kity.createClass('Range',{
constructor : function(){
this.nativeRange = document.createRange();
this.nativeSel = window.getSelection();
},
select:function(){
var start = this.nativeRange.startContainer;
if(start.nodeType == 1 && start.childNodes.length == 0){
var char = document.createTextNode('\u200b');
start.appendChild(char);
this.nativeRange.setStart(char,1);
this.nativeRange.collapse(true);
}
this.nativeSel.removeAllRanges();
this.nativeSel.addRange(this.nativeRange);
return this;
},
setStart:function(node,index){
this.nativeRange.setStart(node,index);
return this;
},
setEnd:function(node,index){
this.nativeRange.setEnd(node,index);
return this;
},
collapse:function(toStart){
this.nativeRange.collapse(toStart === true);
return this;
},
insertNode:function(node){
this.nativeRange.insertNode(node);
return this;
}
});
\ No newline at end of file
//接收者
Minder.Receiver = kity.createClass('Receiver',{
clear : function(){
this.container.innerHTML = '';
this.index = 0;
return this;
},
constructor : function(){
var _div = document.createElement('div');
_div.setAttribute('contenteditable',true);
_div.className = 'km_receiver';
this.container = document.body.insertBefore(_div,document.body.firstChild);
utils.addCssRule('km_receiver_css',' .km_receiver{position:absolute;padding:0;margin:0;word-wrap:break-word;clip:rect(1em 1em 1em 1em);}');
utils.listen(_div,'keydown keypress keyup', utils.proxy(this.keyboardEvents,this));
this.timer = null;
this.index = 0;
},
setPosition : function(textShapeOffset){
this.container.style.top = textShapeOffset.x + 'px';
this.container.style.left = textShapeOffset.y + 'px';
this.textShape.setPosition(textShapeOffset.x,textShapeOffset.y + this.textShape.getSize());
return this;
},
setRange : function(range,index){
this.index = index || this.index;
var text = this.container.firstChild;
range.setStart(text || this.container, this.index).collapse(true);
setTimeout(function(){
range.select()
});
return this;
},
setTextShape:function(textShape){
if(!textShape){
textShape = new kity.Text();
}
this.textShape = textShape;
this.container.innerHTML = textShape.getContent();
return this;
},
setTextShapeSize:function(size){
this.textShape.setSize(size);
return this;
},
getTextShapeHeight:function(){
return this.textShape.getRenderBox().height;
},
appendTextShapeToPaper:function(paper){
paper.addShape(this.textShape);
return this;
},
keyboardEvents : function(e){
clearTimeout(this.timer);
var me = this;
switch(e.type){
case 'keyup':
this.textShape.setContent((this.container.textContent || this.container.innerText).replace(/\u200b/g,''));
this.updateTextData();
this.updateCursor();
this.timer = setTimeout(function(){
me.cursor.setShow()
},500);
break;
case 'keypress':
case 'keyup':
}
},
updateTextData : function(){
this.textShape.textData = this.getTextOffsetData();
this.index = this.index + 1;
},
setCursor : function(cursor){
this.cursor = cursor;
return this;
},
updateCursor : function(){
this.cursor.setShowHold();
if(this.index == this.textData.length){
this.cursor.setPosition({
x : this.textData[this.index-1].x + this.textData[this.index-1].width,
y : this.textData[this.index-1].y
})
}else if(this.index == 0){
this.cursor.setPosition({
x : this.textShape.getX(),
y : this.textShape.getY()
})
}else{
if(this.index + 1 == this.textData.length){
var lastChar = this.textData[this.index];
this.cursor.setPosition({
x : lastChar.x + lastChar.width,
y : lastChar.y
})
}else{
this.cursor.setPosition(this.textData[this.index])
}
}
return this;
},
getTextOffsetData:function(){
var text = this.textShape.getContent();
this.textData = [];
// this.textShape.clearContent();
var containerOffset = this.textShape.container.getRenderBox();
for(var i= 0,l = text.length;i<l;i++){
var box = this.textShape.getExtentOfChar(i);
this.textData.push({
x:box.x + containerOffset.x,
y:box.y + containerOffset.y,
width:box.width,
height:box.height
})
}
return this;
},
setCurrentIndex:function(offset){
var me = this;
this.getTextOffsetData();
var hadChanged = false;
utils.each(this.textData,function(i,v){
if(offset.x >= v.x && offset.x <= v.x + v.width){
if(offset.x - v.x > v.width/2){
me.index = i + 1;
}else {
me.index = i
}
hadChanged = true;
return false;
}
});
return this;
},
setCursorHeight:function(){
this.cursor.setHeight(this.getTextShapeHeight())
return this;
}
});
\ No newline at end of file
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