提交 491e4a04 编写于 作者: C campaign

by zhanyi

上级 d898466d
......@@ -17,8 +17,10 @@ UE.plugins['font'] = function() {
'fontsize':'font-size',
'fontfamily':'font-family',
'underline':'text-decoration',
'strikethrough':'text-decoration'
};
'strikethrough':'text-decoration',
'fontborder':'border'
},
needCmd = {'underline':1,'strikethrough':1,'fontborder':1};
me.setOpt({
'fontfamily':[
{ name:'songti',val:'宋体,SimSun'},
......@@ -35,6 +37,110 @@ UE.plugins['font'] = function() {
],
'fontsize':[10, 11, 12, 14, 16, 18, 20, 24, 36]
});
function mergesibling(range){
var collapsed = range.collapsed,
bk = range.createBookmark(),common;
if(collapsed){
common = bk.start.parentNode;
while(dtd.$inline[common.tagName]){
common = common.parentNode;
}
}else{
common = domUtils.getCommonAncestor(bk.start,bk.end);
}
utils.each(domUtils.getElementsByTagName(common,'span'),function(node){
if(!node.parentNode){
return;
}
function isBorder(n){
if(!n)
return false;
if(n.tagName != 'SPAN')
return false;
var val;
if(val = domUtils.getComputedStyle( n, 'border' )){
if(/1px/.test(val) && /solid/.test(val)){
return true;
}
}
return false;
}
function nextSibling(node){
var next = node.nextSibling;
if(next && domUtils.isBookmarkNode(next)){
next = next.nextSibling;
}
return next;
}
var span = me.document.createElement('span');
span.style.cssText = 'border:1px solid #000';
node.parentNode.insertBefore(span,node);
while(isBorder(node) && isBorder(nextSibling(node))){
var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
node.style.cssText = val;
var tmp = node.nextSibling;
if(!val){
while(node.firstChild){
if(isBorder(node.firstChild)){
node.firstChild.style.cssText = node.firstChild.style.cssText.replace(/border[^:]+:[^;]+;?/gi,'');
}
span.appendChild(node.firstChild)
}
domUtils.remove(node)
}else{
span.appendChild(node);
}
if(domUtils.isBookmarkNode(tmp)){
node = tmp.nextSibling;
span.appendChild(tmp)
}else{
node = tmp;
}
}
if(!span.firstChild){
domUtils.remove(span)
}else{
if(isBorder(span) && isBorder(nextSibling(span))){
node = span.nextSibling;
var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
node.style.cssText = val;
if(!val){
while(node.firstChild){
if(isBorder(node.firstChild)){
node.firstChild.style.cssText = node.firstChild.style.cssText.replace(/border[^:]+:[^;];?/,'');
}
span.appendChild(node.firstChild)
}
domUtils.remove(node)
}else{
span.appendChild(node);
}
}
}
if(isBorder(node)){
var tmpParent = node.parentNode;
while(dtd.$inline[tmpParent.tagName]){
if(isBorder(tmpParent)){
var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
node.style.cssText = val;
if(!val){
domUtils.remove(node,true)
}
break;
}
tmpParent = tmpParent.parentNode;
}
}
});
range.moveToBookmark(bk);
}
me.addInputRule(function(root){
utils.each(root.getNodesByTagName('u s del font'),function(node){
if(node.tagName == 'font'){
......@@ -70,7 +176,9 @@ UE.plugins['font'] = function() {
(function( cmd, style ) {
UE.commands[cmd] = {
execCommand : function( cmdName, value ) {
value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' : 'line-through');
value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' :
cmdName == 'fontborder' ? '1px solid #000' :
'line-through');
var me = this,
range = this.selection.getRange(),
text;
......@@ -90,14 +198,15 @@ UE.plugins['font'] = function() {
} else {
if ( !range.collapsed ) {
if((cmd == 'underline'||cmd=='strikethrough') && me.queryCommandValue(cmd)){
if(needCmd[cmd] && me.queryCommandValue(cmd)){
me.execCommand( 'removeFormat', 'span,a', style );
}
range = me.selection.getRange();
range.applyInlineStyle( 'span', {'style':style + ':' + value} ).select();
range.applyInlineStyle( 'span', {'style':style + ':' + value} );
mergesibling(range);
range.select();
} else {
var span = domUtils.findParentByTagName(range.startContainer,'span',true);
......@@ -105,7 +214,7 @@ UE.plugins['font'] = function() {
if(span && !span.children.length && !span[browser.ie ? 'innerText':'textContent'].replace(fillCharReg,'').length){
//for ie hack when enter
range.insertNode(text);
if(cmd == 'underline'||cmd=='strikethrough'){
if(needCmd[cmd]){
range.selectNode(text).select();
me.execCommand( 'removeFormat','span,a', style, null );
......@@ -122,7 +231,7 @@ UE.plugins['font'] = function() {
range.selectNode(text).select();
span = range.document.createElement( 'span' );
if(cmd == 'underline'||cmd=='strikethrough'){
if(needCmd[cmd]){
//a标签内的不处理跳过
if(domUtils.findParentByTagName(text,'a',true)){
range.setStartBefore(text).setCursor();
......@@ -149,18 +258,21 @@ UE.plugins['font'] = function() {
}
if(opera){
setTimeout(function(){
range.setStart(span,0).setCursor();
range.setStart(span,0).collapse(true);
mergesibling(range);
range.select();
});
}else{
range.setStart(span,0).setCursor();
range.setStart(span,0).collapse(true);
mergesibling(range);
range.select();
}
//trace:981
//domUtils.mergeToParent(span)
}
domUtils.remove(text);
}
......@@ -188,17 +300,33 @@ UE.plugins['font'] = function() {
}
return 'none';
}
if(cmdName == 'fontborder'){
var tmp = startNode,val;
while(tmp && dtd.$inline[tmp.tagName]){
if(val = domUtils.getComputedStyle( tmp, 'border' )){
if( /1px/.test(val) && /solid/.test(val)){
return val;
}
}
tmp = tmp.parentNode;
}
return ''
}
return domUtils.getComputedStyle( startNode, style );
},
queryCommandState : function(cmdName){
if(!(cmdName == 'underline'||cmdName=='strikethrough')){
if(!needCmd[cmdName])
return 0;
var val = this.queryCommandValue(cmdName);
if(cmdName == 'fontborder'){
return /1px/.test(val) && /solid/.test(val)
}else{
return val == (cmdName == 'underline' ?
'underline' : 'line-through');
}
return this.queryCommandValue(cmdName) == (cmdName == 'underline' ? 'underline' : 'line-through');
}
};
})( p, fonts[p] );
}
};
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册