');
this.indents += this.indentChar;
}
if (!dtd.$inline[tag]) {
this.newline();
}
},
indent: function (){
this.buff.push(this.indents);
this.indenting = 0;
},
newline: function (){
this.buff.push(this.breakChar);
this.indenting = 1;
},
visitEndTag: function (tag){
this.indents = this.indents.slice(0, -this.indentChar.length);
if (this.indenting) {
this.indent();
} else if (!dtd.$inline[tag]) {
this.newline();
this.indent();
}
this.buff.push('', tag, '>');
},
visitText: function (text,notTrans){
if (this.indenting) {
this.indent();
}
if(!notTrans){
text = text.replace(/ /g, ' ').replace(/[ ][ ]+/g, function (m){
return new Array(m.length + 1).join(' ');
}).replace(/(?:^ )|(?: $)/g, ' ');
}
this.buff.push(text);
},
visitComment: function (text){
if (this.indenting) {
this.indent();
}
this.buff.push('');
}
};
var sourceEditors = {
textarea: function (editor, holder){
var textarea = holder.ownerDocument.createElement('textarea');
textarea.style.cssText = 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;';
// todo: IE下只有onresize属性可用... 很纠结
if (baidu.editor.browser.ie && baidu.editor.browser.version < 8) {
textarea.style.width = holder.offsetWidth + 'px';
textarea.style.height = holder.offsetHeight + 'px';
holder.onresize = function (){
textarea.style.width = holder.offsetWidth + 'px';
textarea.style.height = holder.offsetHeight + 'px';
};
}
holder.appendChild(textarea);
return {
setContent: function (content){
textarea.value = content;
},
getContent: function (){
return textarea.value;
},
select: function (){
var range;
if (baidu.editor.browser.ie) {
range = textarea.createTextRange();
range.collapse(true);
range.select();
} else {
//todo: chrome下无法设置焦点
textarea.setSelectionRange(0, 0);
textarea.focus();
}
},
dispose: function (){
holder.removeChild(textarea);
// todo
holder.onresize = null;
textarea = null;
holder = null;
}
};
},
codemirror: function (editor, holder){
var options = {
mode: "text/html",
tabMode: "indent",
lineNumbers: true,
lineWrapping:true
};
var codeEditor = window.CodeMirror(holder, options);
var dom = codeEditor.getWrapperElement();
dom.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
codeEditor.getScrollerElement().style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;';
codeEditor.refresh();
return {
setContent: function (content){
codeEditor.setValue(content);
},
getContent: function (){
return codeEditor.getValue();
},
select: function (){
codeEditor.focus();
},
dispose: function (){
holder.removeChild(dom);
dom = null;
codeEditor = null;
}
};
}
};
baidu.editor.plugins['source'] = function (){
var me = this,utils = baidu.editor.utils;
me.initPlugins(['serialize']);
var opt = this.options;
var formatter = new SourceFormater(opt.source);
var sourceMode = false;
var sourceEditor;
function createSourceEditor(holder){
var useCodeMirror = opt.sourceEditor == 'codemirror' && window.CodeMirror;
return sourceEditors[useCodeMirror ? 'codemirror' : 'textarea'](me, holder);
}
var bakCssText;
me.commands['source'] = {
execCommand: function (){
sourceMode = !sourceMode;
if (sourceMode) {
me.undoManger && me.undoManger.save();
this.currentSelectedArr && domUtils.clearSelectedArr(this.currentSelectedArr);
if(browser.gecko)
me.body.contentEditable = false;
bakCssText = me.iframe.style.cssText;
me.iframe.style.cssText += 'position:absolute;left:-32768px;top:-32768px;';
var content = formatter.format(me.hasContents() ? me.getContent() : '');
sourceEditor = createSourceEditor(me.iframe.parentNode);
sourceEditor.setContent(content);
setTimeout(function (){
sourceEditor.select();
});
} else {
me.iframe.style.cssText = bakCssText;
me.setContent(sourceEditor.getContent().replace(new RegExp(formatter.breakChar + '?(' + formatter.indentChar + '){0,}<','g'),'<') || '' + (browser.ie ? '' : '
')+'
');
sourceEditor.dispose();
sourceEditor = null;
setTimeout(function(){
var first = me.body.firstChild;
//trace:1106 都删除空了,下边会报错,所以补充一个p占位
if(!first){
me.body.innerHTML = ''+(browser.ie?'':'
')+'
';
first = me.body.firstChild;
}
//要在ifm为显示时ff才能取到selection,否则报错
me.undoManger && me.undoManger.save();
while(first && first.firstChild){
first = first.firstChild;
}
var range = me.selection.getRange();
if(first.nodeType == 3 || baidu.editor.dom.dtd.$empty[first.tagName]){
range.setStartBefore(first)
}else{
range.setStart(first,0);
}
if(baidu.editor.browser.gecko){
var input = document.createElement('input');
input.style.cssText = 'position:absolute;left:0;top:-32768px';
document.body.appendChild(input);
me.body.contentEditable = false;
setTimeout(function(){
domUtils.setViewportOffset(input, { left: -32768, top: 0 });
input.focus();
setTimeout(function(){
me.body.contentEditable = true;
range.setCursor(false,true);
baidu.editor.dom.domUtils.remove(input)
})
})
}else{
range.setCursor(false,true);
}
})
}
this.fireEvent('sourcemodechanged', sourceMode);
},
queryCommandState: function (){
return sourceMode|0;
}
};
var oldQueryCommandState = me.queryCommandState;
me.queryCommandState = function (cmdName){
cmdName = cmdName.toLowerCase();
if (sourceMode) {
return cmdName == 'source' ? 1 : -1;
}
return oldQueryCommandState.apply(this, arguments);
};
//解决在源码模式下getContent不能得到最新的内容问题
var oldGetContent = me.getContent;
me.getContent = function (){
if(sourceMode && sourceEditor ){
var html = sourceEditor.getContent();
if (this.serialize) {
var node = this.serialize.parseHTML(html);
node = this.serialize.filter(node);
html = this.serialize.toHTML(node);
}
return html;
}else{
return oldGetContent.apply(this, arguments)
}
};
me.addListener("ready",function(){
if(opt.sourceEditor == "codemirror"){
utils.loadFile(document,{
src : opt.UEDITOR_HOME_URL+"third-party/codemirror2.15/codemirror.js",
tag : "script",
type : "text/javascript",
defer : "defer"
});
utils.loadFile(document,{
tag : "link",
rel : "stylesheet",
type : "text/css",
href : opt.UEDITOR_HOME_URL+"third-party/codemirror2.15/codemirror.css"
});
}
});
};
})();
///import core
///commands 快捷键
///commandsName ShortCutKeys
///commandsTitle 快捷键
//配置快捷键
baidu.editor.plugins['shortcutkeys'] = function(){
var editor = this,
shortcutkeys = baidu.editor.utils.extend({
"ctrl+66" : "Bold" //^B
,"ctrl+90" : "Undo" //undo
,"ctrl+89" : "Redo" //redo
,"ctrl+73" : "Italic" //^I
,"ctrl+85" : "Underline:Underline" //^U
,"ctrl+shift+67" : "removeformat" //清除格式
,"ctrl+shift+76" : "justify:left" //居左
,"ctrl+shift+82" : "justify:right" //居右
,"ctrl+65" : "selectAll"
// ,"9" : "indent" //tab
},editor.options.shortcutkeys);
editor.addListener('keydown',function(type,e){
var keyCode = e.keyCode || e.which,value;
for ( var i in shortcutkeys ) {
if ( /^(ctrl)(\+shift)?\+(\d+)$/.test( i.toLowerCase() ) || /^(\d+)$/.test( i ) ) {
if ( ( (RegExp.$1 == 'ctrl' ? (e.ctrlKey||e.metaKey) : 0)
&& (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1)
&& keyCode == RegExp.$3
) ||
keyCode == RegExp.$1
){
value = shortcutkeys[i].split(':');
editor.execCommand( value[0],value[1]);
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
}
}
}
});
};
///import core
///import plugins/undo/undo.js
///commands 设置回车标签p或br
///commandsName EnterKey
///commandsTitle 设置回车标签p或br
/**
* @description 处理回车
* @author zhanyi
*/
(function() {
var browser = baidu.editor.browser,
domUtils = baidu.editor.dom.domUtils,
hTag;
baidu.editor.plugins['enterkey'] = function() {
var me = this,
tag = me.options.enterTag;
me.addListener('keyup', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
if (keyCode == 13) {
var range = me.selection.getRange(),
start = range.startContainer,
doSave;
//修正在h1-h6里边回车后不能嵌套p的问题
if (!browser.ie) {
if (/h\d/i.test(hTag)) {
if (browser.gecko) {
var h = domUtils.findParentByTagName(start, [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote'], true);
if (!h) {
me.document.execCommand('formatBlock', false, '');
doSave = 1;
}
} else {
//chrome remove div
if (start.nodeType == 1) {
var tmp = me.document.createTextNode(''),div;
range.insertNode(tmp);
div = domUtils.findParentByTagName(tmp, 'div', true);
if (div) {
var p = me.document.createElement('p');
while (div.firstChild) {
p.appendChild(div.firstChild);
}
div.parentNode.insertBefore(p, div);
domUtils.remove(div);
range.setStartBefore(tmp).setCursor();
doSave = 1;
}
domUtils.remove(tmp);
}
}
if (me.undoManger && doSave) {
me.undoManger.save()
}
}
}
range = me.selection.getRange();
setTimeout(function() {
range.scrollToView(me.autoHeightEnabled, me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0);
}, 50)
}
});
me.addListener('keydown', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
if (keyCode == 13) {//回车
if (me.undoManger) {
me.undoManger.save()
}
hTag = '';
var range = me.selection.getRange();
if (!range.collapsed) {
//跨td不能删
var start = range.startContainer,
end = range.endContainer,
startTd = domUtils.findParentByTagName(start, 'td', true),
endTd = domUtils.findParentByTagName(end, 'td', true);
if (startTd && endTd && startTd !== endTd || !startTd && endTd || startTd && !endTd) {
evt.preventDefault ? evt.preventDefault() : ( evt.returnValue = false);
return;
}
}
me.currentSelectedArr && domUtils.clearSelectedArr(me.currentSelectedArr);
if (tag == 'p') {
if (!browser.ie) {
start = domUtils.findParentByTagName(range.startContainer, ['ol','ul','p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','blockquote'], true);
if (!start) {
me.document.execCommand('formatBlock', false, '
');
if (browser.gecko) {
range = me.selection.getRange();
start = domUtils.findParentByTagName(range.startContainer, 'p', true);
start && domUtils.removeDirtyAttr(start);
}
} else {
hTag = start.tagName;
start.tagName.toLowerCase() == 'p' && browser.gecko && domUtils.removeDirtyAttr(start);
}
}
} else {
evt.preventDefault ? evt.preventDefault() : ( evt.returnValue = false);
if (!range.collapsed) {
range.deleteContents();
start = range.startContainer;
if (start.nodeType == 1 && (start = start.childNodes[range.startOffset])) {
while (start.nodeType == 1) {
if (baidu.editor.dom.dtd.$empty[start.tagName]) {
range.setStartBefore(start).setCursor();
if (me.undoManger) {
me.undoManger.save()
}
return false;
}
if (!start.firstChild) {
var br = range.document.createElement('br');
start.appendChild(br);
range.setStart(start, 0).setCursor();
if (me.undoManger) {
me.undoManger.save()
}
return false;
}
start = start.firstChild
}
if (start === range.startContainer.childNodes[range.startOffset]) {
br = range.document.createElement('br');
range.insertNode(br).setCursor();
} else {
range.setStart(start, 0).setCursor();
}
} else {
br = range.document.createElement('br');
range.insertNode(br).setStartAfter(br).setCursor();
}
} else {
br = range.document.createElement('br');
range.insertNode(br);
var parent = br.parentNode;
if (parent.lastChild === br) {
br.parentNode.insertBefore(br.cloneNode(true), br);
range.setStartBefore(br)
} else {
range.setStartAfter(br)
}
range.setCursor();
}
}
}
});
}
})();
/*
* 处理特殊键的兼容性问题
*/
(function() {
var domUtils = baidu.editor.dom.domUtils,
browser = baidu.editor.browser,
dtd = baidu.editor.dom.dtd,
utils = baidu.editor.utils,
flag = 0,
keys = domUtils.keys,
trans = {
'B' : 'strong',
'I' : 'em',
'FONT' : 'span'
},
sizeMap = [0, 10, 12, 16, 18, 24, 32, 48],
listStyle = {
'OL':['decimal','lower-alpha','lower-roman','upper-alpha','upper-roman'],
'UL':[ 'circle','disc','square']
};
baidu.editor.plugins['keystrokes'] = function() {
var me = this;
me.addListener('keydown', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
//处理backspace/del
if (keyCode == 8 ) {//|| keyCode == 46
var range = me.selection.getRange(),
tmpRange,
start,end;
//当删除到body最开始的位置时,会删除到body,阻止这个动作
if(range.collapsed){
start = range.startContainer;
//有可能是展位符号
if(domUtils.isWhitespace(start)){
start = start.parentNode;
}
if(domUtils.isEmptyNode(start) && start === me.body.firstChild){
if(start.tagName != 'P'){
p = me.document.createElement('p');
me.body.insertBefore(p,start);
domUtils.fillNode(me.document,p);
range.setStart(p,0).setCursor(false,true);
}
domUtils.preventDefault(evt);
return;
}
}
if (range.collapsed && range.startContainer.nodeType == 3 && range.startContainer.nodeValue.replace(new RegExp(domUtils.fillChar, 'g'), '').length == 0) {
range.setStartBefore(range.startContainer).collapse(true)
}
//解决选中control元素不能删除的问题
if (start = range.getClosedNode()) {
me.undoManger && me.undoManger.save();
range.setStartBefore(start);
domUtils.remove(start);
range.setCursor();
me.undoManger && me.undoManger.save();
domUtils.preventDefault(evt);
return;
}
//阻止在table上的删除
if (!browser.ie) {
start = domUtils.findParentByTagName(range.startContainer, 'table', true);
end = domUtils.findParentByTagName(range.endContainer, 'table', true);
if (start && !end || !start && end || start !== end) {
evt.preventDefault();
return;
}
if (browser.webkit && range.collapsed && start) {
tmpRange = range.cloneRange().txtToElmBoundary();
start = tmpRange.startContainer;
if (domUtils.isBlockElm(start) && start.nodeType == 1 && !dtd.$tableContent[start.tagName] && !domUtils.getChildCount(start, function(node) {
return node.nodeType == 1 ? node.tagName !== 'BR' : 1;
})) {
tmpRange.setStartBefore(start).setCursor();
domUtils.remove(start, true);
evt.preventDefault();
return;
}
}
}
if (me.undoManger) {
if (!range.collapsed) {
me.undoManger.save();
flag = 1;
}
}
}
//处理tab键的逻辑
if (keyCode == 9) {
range = me.selection.getRange();
me.undoManger && me.undoManger.save();
for (var i = 0,txt = ''; i < me.options.tabSize; i++) {
txt += me.options.tabNode;
}
var span = me.document.createElement('span');
span.innerHTML = txt;
if (range.collapsed) {
li = domUtils.findParentByTagName(range.startContainer, 'li', true);
if (li && domUtils.isStartInblock(range)) {
bk = range.createBookmark();
var parentLi = li.parentNode,
list = me.document.createElement(parentLi.tagName);
var index = utils.indexOf(listStyle[list.tagName], domUtils.getComputedStyle(parentLi, 'list-style-type'));
index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
domUtils.setStyle(list, 'list-style-type', listStyle[list.tagName][index]);
parentLi.insertBefore(list, li);
list.appendChild(li);
range.moveToBookmark(bk).select()
} else
range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
} else {
//处理table
start = domUtils.findParentByTagName(range.startContainer, 'table', true);
end = domUtils.findParentByTagName(range.endContainer, 'table', true);
if (start || end) {
evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
return
}
//处理列表 再一个list里处理
start = domUtils.findParentByTagName(range.startContainer, ['ol','ul'], true);
end = domUtils.findParentByTagName(range.endContainer, ['ol','ul'], true);
if (start && end && start === end) {
var bk = range.createBookmark();
start = domUtils.findParentByTagName(range.startContainer, 'li', true);
end = domUtils.findParentByTagName(range.endContainer, 'li', true);
//在开始单独处理
if (start === start.parentNode.firstChild) {
var parentList = me.document.createElement(start.parentNode.tagName);
start.parentNode.parentNode.insertBefore(parentList, start.parentNode);
parentList.appendChild(start.parentNode);
} else {
parentLi = start.parentNode,
list = me.document.createElement(parentLi.tagName);
index = utils.indexOf(listStyle[list.tagName], domUtils.getComputedStyle(parentLi, 'list-style-type'));
index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
domUtils.setStyle(list, 'list-style-type', listStyle[list.tagName][index]);
start.parentNode.insertBefore(list, start);
var nextLi;
while (start !== end) {
nextLi = start.nextSibling;
list.appendChild(start);
start = nextLi;
}
list.appendChild(end);
}
range.moveToBookmark(bk).select();
} else {
if (start || end) {
evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
return
}
//普通的情况
start = domUtils.findParent(range.startContainer, filterFn);
end = domUtils.findParent(range.endContainer, filterFn);
if (start && end && start === end) {
range.deleteContents();
range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
} else {
var bookmark = range.createBookmark(),
filterFn = function(node) {
return domUtils.isBlockElm(node);
};
range.enlarge(true);
var bookmark2 = range.createBookmark(),
current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
current.insertBefore(span.cloneNode(true).firstChild, current.firstChild);
current = domUtils.getNextDomNode(current, false, filterFn);
}
range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
}
}
}
me.undoManger && me.undoManger.save();
evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
}
//trace:1634
//ff的del键在容器空的时候,也会删除
if(browser.gecko && keyCode == 46){
range = me.selection.getRange();
if(range.collapsed){
start = range.startContainer;
if(domUtils.isEmptyBlock(start)){
var parent = start.parentNode;
while(domUtils.getChildCount(parent) == 1 && !domUtils.isBody(parent)){
start = parent;
parent = parent.parentNode;
}
if(start === parent.lastChild)
evt.preventDefault();
return;
}
}
}
});
me.addListener('keyup', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
//修复ie/chrome x| 当点退格后在输入文字后会出现 x
if (!browser.gecko && !keys[keyCode] && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
range = me.selection.getRange();
if (range.collapsed) {
var start = range.startContainer,
isFixed = 0;
while (!domUtils.isBlockElm(start)) {
if (start.nodeType == 1 && utils.indexOf(['FONT','B','I'], start.tagName) != -1) {
var tmpNode = me.document.createElement(trans[start.tagName]);
if (start.tagName == 'FONT') {
//chrome only remember color property
tmpNode.style.cssText = (start.getAttribute('size') ? 'font-size:' + (sizeMap[start.getAttribute('size')] || 12) + 'px' : '')
+ ';' + (start.getAttribute('color') ? 'color:' + start.getAttribute('color') : '')
+ ';' + (start.getAttribute('face') ? 'font-family:' + start.getAttribute('face') : '')
+ ';' + start.style.cssText;
}
while (start.firstChild) {
tmpNode.appendChild(start.firstChild)
}
start.parentNode.insertBefore(tmpNode, start);
domUtils.remove(start);
if (!isFixed) {
range.setEnd(tmpNode, tmpNode.childNodes.length).collapse(true)
}
start = tmpNode;
isFixed = 1;
}
start = start.parentNode;
}
isFixed && range.select()
}
}
if (keyCode == 8 ) {//|| keyCode == 46
//针对ff下在列表首行退格,不能删除空格行的问题
if(browser.gecko){
for(var i=0,li,lis = domUtils.getElementsByTagName(this.body,'li');li=lis[i++];){
if(domUtils.isEmptyNode(li) && !li.previousSibling){
var liOfPn = li.parentNode;
domUtils.remove(li);
if(domUtils.isEmptyNode(liOfPn)){
domUtils.remove(liOfPn)
}
}
}
}
var range,start,parent,
tds = this.currentSelectedArr;
if (tds && tds.length > 0) {
for (var i = 0,ti; ti = tds[i++];) {
ti.innerHTML = browser.ie ? ( browser.version < 9 ? '' : '' ) : '
';
}
range = new baidu.editor.dom.Range(this.document);
range.setStart(tds[0], 0).setCursor();
if (flag) {
me.undoManger.save();
flag = 0;
}
//阻止chrome执行默认的动作
if (browser.webkit) {
evt.preventDefault();
}
return;
}
range = me.selection.getRange();
//ctrl+a 后全部删除做处理
if (domUtils.isEmptyBlock(me.body) && !range.startOffset) {
//trace:1633
me.body.innerHTML = '
'+(browser.ie ? ' ' : '
')+'
';
range.setStart(me.body.firstChild,0).setCursor(false,true);
me.undoManger && me.undoManger.save();
//todo 对性能会有影响
browser.ie && me._selectionChange();
return;
}
//处理删除不干净的问题
start = range.startContainer;
if(domUtils.isWhitespace(start)){
start = start.parentNode
}
//标志位防止空的p无法删除
var removeFlag = 0;
while (start.nodeType == 1 && domUtils.isEmptyNode(start) && dtd.$removeEmpty[start.tagName]) {
removeFlag = 1;
parent = start.parentNode;
domUtils.remove(start);
start = parent;
}
if ( removeFlag && start.nodeType == 1 && domUtils.isEmptyNode(start)) {
//ie下的问题,虽然没有了相应的节点但一旦你输入文字还是会自动把删除的节点加上,
if (browser.ie) {
var span = range.document.createElement('span');
start.appendChild(span);
range.setStart(start,0).setCursor();
//for ie
li = domUtils.findParentByTagName(start,'li',true);
if(li){
var next = li.nextSibling;
while(next){
if(domUtils.isEmptyBlock(next)){
li = next;
next = next.nextSibling;
domUtils.remove(li);
continue;
}
break;
}
}
} else {
start.innerHTML = '
';
range.setStart(start, 0).setCursor(false,true);
}
setTimeout(function() {
if (browser.ie) {
domUtils.remove(span);
}
if (flag) {
me.undoManger.save();
flag = 0;
}
}, 0)
} else {
if (flag) {
me.undoManger.save();
flag = 0;
}
}
}
})
}
})();
///import core
///commands 修复chrome下图片不能点击的问题
///commandsName FixImgClick
///commandsTitle 修复chrome下图片不能点击的问题
//修复chrome下图片不能点击的问题
//todo 可以改大小
baidu.editor.plugins['fiximgclick'] = function() {
var me = this,
browser = baidu.editor.browser;
if ( browser.webkit ) {
me.addListener( 'click', function( type, e ) {
if ( e.target.tagName == 'IMG' ) {
var range = new baidu.editor.dom.Range( me.document );
range.selectNode( e.target ).select();
}
} )
}
};
///import core
///commands 为非ie浏览器自动添加a标签
///commandsName AutoLink
///commandsTitle 自动增加链接
/**
* @description 为非ie浏览器自动添加a标签
* @author zhanyi
*/
(function() {
var editor = baidu.editor,
browser = editor.browser,
domUtils = editor.dom.domUtils;
baidu.editor.plugins['autolink'] = function() {
var cont = 0;
if (browser.ie) {
return;
}
var me = this;
me.addListener('reset',function(){
cont = 0;
});
me.addListener('keydown', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
if (keyCode == 32 || keyCode == 13) {
var sel = me.selection.getNative(),
range = sel.getRangeAt(0).cloneRange(),
offset,
charCode;
var start = range.startContainer;
while (start.nodeType == 1 && range.startOffset > 0) {
start = range.startContainer.childNodes[range.startOffset - 1];
if (!start)
break;
range.setStart(start, start.nodeType == 1 ? start.childNodes.length : start.nodeValue.length);
range.collapse(true);
start = range.startContainer;
}
do{
if (range.startOffset == 0) {
start = range.startContainer.previousSibling;
while (start && start.nodeType == 1) {
start = start.lastChild;
}
if (!start || domUtils.isFillChar(start))
break;
offset = start.nodeValue.length;
} else {
start = range.startContainer;
offset = range.startOffset;
}
range.setStart(start, offset - 1);
charCode = range.toString().charCodeAt(0);
} while (charCode != 160 && charCode != 32);
if (range.toString().replace(new RegExp(domUtils.fillChar, 'g'), '').match(/^(\s*)(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)) {
var a = me.document.createElement('a'),text = me.document.createTextNode(' '),href;
//去掉开头的空格
if (RegExp.$1.length) {
range.setStart(range.startContainer, range.startOffset + RegExp.$1.length);
}
a.appendChild(range.extractContents());
a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g,'');
href = a.getAttribute("href").replace(new RegExp(domUtils.fillChar,'g'),'');
a.href = /^(?:https?:\/\/)/ig.test(href) ? href : "http://"+href;
range.insertNode(a);
a.parentNode.insertBefore(text, a.nextSibling);
range.setStart(text, 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range)
}
}
})
}
})();
///import core
///commands 当输入内容超过编辑器高度时,编辑器自动增高
///commandsName AutoHeight
///commandsTitle 自动增高
/**
* @description 自动伸展
* @author zhanyi
*/
(function() {
var domUtils = baidu.editor.dom.domUtils;
baidu.editor.plugins['autoheight'] = function() {
var me = this;
//提供开关,就算加载也可以关闭
me.autoHeightEnabled = me.options.autoHeightEnabled;
if(!me.autoHeightEnabled)return;
var timer;
var bakScroll;
var bakOverflow,
div,tmpNode;
me.enableAutoHeight = function (){
var iframe = me.iframe,
doc = me.document;
me.autoHeightEnabled = true;
bakScroll = iframe.scrolling;
iframe.scrolling = 'no';
bakOverflow = doc.body.style.overflowY;
doc.body.style.overflowY = 'hidden';
var lastHeight = 0,currentHeight;
timer = setInterval(function(){
if (me.queryCommandState('source') != 1) {
if(!div){
div = me.document.createElement('div');
div.style.cssText = 'height:0;overflow:hidden;margin:0;padding:0;border:0;clear:both;';
div.innerHTML ='.';
}
tmpNode = div.cloneNode(true);
me.body.appendChild(tmpNode);
currentHeight = Math.max(domUtils.getXY(tmpNode).y + tmpNode.offsetHeight,me.options.minFrameHeight);
if(!baidu.editor.browser.gecko || currentHeight - lastHeight != tmpNode.offsetHeight){
me.setHeight(currentHeight);
lastHeight = currentHeight;
}
domUtils.remove(tmpNode)
}
},50);
me.addListener('destroy',function(){
clearInterval(timer)
});
me.fireEvent('autoheightchanged', me.autoHeightEnabled);
};
me.disableAutoHeight = function (){
var iframe = me.iframe,
doc = me.document;
iframe.scrolling = bakScroll;
doc.body.style.overflowY = bakOverflow;
clearInterval(timer);
me.autoHeightEnabled = false;
me.fireEvent('autoheightchanged', me.autoHeightEnabled);
};
me.addListener( 'ready', function() {
me.enableAutoHeight();
});
}
})();
///import core
///commands 悬浮工具栏
///commandsName AutoFloat
///commandsTitle 悬浮工具栏
/*
* modified by chengchao01
*
* 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
*/
(function(){
var browser = baidu.editor.browser,
domUtils = baidu.editor.dom.domUtils,
uiUtils,
utils = baidu.editor.utils,
LteIE6 = browser.ie && browser.version <= 6;
baidu.editor.plugins['autofloat'] = function() {
var optsAutoFloatEnabled = this.options.autoFloatEnabled;
//如果不固定toolbar的位置,则直接退出
if(!optsAutoFloatEnabled){
return;
}
var editor = this,
floating = false,
MIN_HEIGHT = 0,
bakCssText,
placeHolder = document.createElement('div');
function setFloating(delta){
var toolbarBox = editor.ui.getDom('toolbarbox'),
toobarBoxPos = domUtils.getXY(toolbarBox),
origalFloat = window.getComputedStyle? document.defaultView.getComputedStyle(toolbarBox, null).position : toolbarBox.currentStyle.position,
origalLeft = window.getComputedStyle? document.defaultView.getComputedStyle(toolbarBox, null).left : toolbarBox.currentStyle.left;
placeHolder.style.height = toolbarBox.offsetHeight + 'px';
bakCssText = toolbarBox.style.cssText;
if (browser.ie7Compat) {
var left = (toolbarBox.getBoundingClientRect().left -
document.documentElement.getBoundingClientRect().left) + 'px';
}
toolbarBox.style.width = toolbarBox.offsetWidth + 'px';
toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
if (LteIE6) {
toolbarBox.style.position = 'absolute';
toolbarBox.style.setExpression('top', 'eval("((document.documentElement||document.body).scrollTop-'+ delta +')+\'px\'")');
toolbarBox.style.zIndex = '1';
} else {
toolbarBox.style.position = 'fixed';
toolbarBox.style.zIndex = '1';
toolbarBox.style.top = '0';
if (browser.ie7Compat) {
toolbarBox.style.left = left;
}
((origalFloat == 'absolute' || origalFloat == 'relative') && parseFloat(origalLeft)) && (toolbarBox.style.left = toobarBoxPos.x + 'px');
}
floating = true;
}
function unsetFloating(){
var toolbarBox = editor.ui.getDom('toolbarbox');
placeHolder.parentNode.removeChild(placeHolder);
if (LteIE6) {
toolbarBox.style.removeExpression('top');
}
toolbarBox.style.cssText = bakCssText;
floating = false;
}
var updateFloating = utils.defer(function(){
var rect = uiUtils.getClientRect(
editor.ui.getDom('toolbarbox'));
var rect2 = uiUtils.getClientRect(
editor.ui.getDom('iframeholder'));
if (!floating) {
if (rect.top < 0 && rect2.bottom > rect.height + MIN_HEIGHT) {
var delta = (document.documentElement.scrollTop || document.body.scrollTop) + rect.top;
setFloating(delta);
}
} else {
var rect1 = uiUtils.getClientRect(placeHolder);
if (rect.top < rect1.top || rect.bottom + MIN_HEIGHT > rect2.bottom) {
unsetFloating();
}
}
},100,true);
editor.addListener('destroy',function(){
domUtils.un(window, ['scroll','resize'], updateFloating);
editor.removeListener('keydown', updateFloating);
});
editor.addListener('ready', function(){
if(checkHasUI()){
if(LteIE6){
fixIE6FixedPos();
}
editor.addListener('autoheightchanged', function (t, enabled){
if (enabled) {
domUtils.on(window, ['scroll','resize'], updateFloating);
editor.addListener('keydown', updateFloating);
} else {
domUtils.un(window, ['scroll','resize'], updateFloating);
editor.removeListener('keydown', updateFloating);
}
});
editor.addListener('beforefullscreenchange', function (t, enabled){
if (enabled) {
if (floating) {
unsetFloating();
}
}
});
editor.addListener('fullscreenchanged', function (t, enabled){
if (!enabled) {
updateFloating();
}
});
editor.addListener('sourcemodechanged', function (t, enabled){
setTimeout(function (){
updateFloating();
});
});
}
})
};
function checkHasUI(){
try{
uiUtils = baidu.editor.ui.uiUtils;
}catch( ex ){
alert('autofloat插件功能依赖于UEditor UI\nautofloat定义位置: _src/plugins/autofloat/autofloat.js');
throw({
name: '未包含UI文件',
message: 'autofloat功能依赖于UEditor UI。autofloat定义位置: _src/plugins/autofloat/autofloat.js'
});
}
return 1;
}
function fixIE6FixedPos(){
var docStyle = document.body.style;
docStyle.backgroundImage = 'url("about:blank")';
docStyle.backgroundAttachment = 'fixed';
}
})();
///import core
///import commands/inserthtml.js
///commands 插入代码
///commandsName HighlightCode
///commandsTitle 插入代码
///commandsDialog dialogs\code\code.html
baidu.editor.plugins['highlight'] = function() {
var me = this,domUtils = baidu.editor.dom.domUtils,utils = baidu.editor.utils,browser = baidu.editor.browser;
me.commands['highlightcode'] = {
execCommand: function (cmdName, code, syntax) {
if(code && syntax){
var pre = document.createElement("pre");
pre.className = "brush: "+syntax+";toolbar:false;";
pre.style.display = "";
pre.appendChild(document.createTextNode(code));
document.body.appendChild(pre);
if(me.queryCommandState("highlightcode")){
me.execCommand("highlightcode");
}
me.execCommand('inserthtml', SyntaxHighlighter.highlight(pre,null,true));
var div = me.document.getElementById(SyntaxHighlighter.getHighlighterDivId());
div.setAttribute('highlighter',pre.className);
domUtils.remove(pre);
adjustHeight()
}else{
var range = this.selection.getRange(),
start = domUtils.findParentByTagName(range.startContainer, 'table', true),
end = domUtils.findParentByTagName(range.endContainer, 'table', true),
codediv;
if(start && end && start === end && start.parentNode.className.indexOf("syntaxhighlighter")>-1){
codediv = start.parentNode;
if(domUtils.isBody(codediv.parentNode)){
var p = me.document.createElement('p');
p.innerHTML = baidu.editor.browser.ie ? '' : '
';
me.body.insertBefore(p,codediv);
range.setStart(p,0)
}else{
range.setStartBefore(codediv)
}
range.setCursor();
domUtils.remove(codediv);
}
}
},
queryCommandState: function(){
var range = this.selection.getRange(),start,end;
range.adjustmentBoundary();
start = domUtils.findParent(range.startContainer,function(node){
return node.nodeType == 1 && node.tagName == 'DIV' && domUtils.hasClass(node,'syntaxhighlighter')
},true);
end = domUtils.findParent(range.endContainer,function(node){
return node.nodeType == 1 && node.tagName == 'DIV' && domUtils.hasClass(node,'syntaxhighlighter')
},true);
return start && end && start == end ? 1 : 0;
}
};
me.addListener('beforeselectionchange',function(){
me.highlight = me.queryCommandState('highlightcode') == 1 ? 1 : 0;
});
me.addListener('afterselectionchange',function(){
me.highlight = 0;
});
me.addListener("ready",function(){
//避免重复加载高亮文件
if(typeof XRegExp == "undefined"){
var obj = {
id : "syntaxhighlighter_js",
src : me.options.highlightJsUrl,
tag : "script",
type : "text/javascript",
defer : "defer"
};
utils.loadFile(document,obj,function(){
changePre();
});
}
if(!me.document.getElementById("syntaxhighlighter_css")){
var obj = {
id : "syntaxhighlighter_css",
tag : "link",
rel : "stylesheet",
type : "text/css",
href : me.options.highlightCssUrl
};
utils.loadFile(me.document,obj);
}
});
me.addListener("beforegetcontent",function(type,cmd){
for(var i=0,di,divs=domUtils.getElementsByTagName(me.body,'div');di=divs[i++];){
if(di.className == 'container'){
var pN = di.parentNode;
while(pN){
if(pN.tagName == 'DIV' && /highlighter/.test(pN.id)){
break;
}
pN = pN.parentNode;
}
if(!pN)return;
var pre = me.document.createElement('pre');
for(var str=[],c=0,ci;ci=di.childNodes[c++];){
str.push(ci[browser.ie?'innerText':'textContent']);
}
pre.appendChild(me.document.createTextNode(str.join('\n')));
pre.className = pN.getAttribute('highlighter');
pN.parentNode.insertBefore(pre,pN);
domUtils.remove(pN);
}
}
});
me.addListener("aftergetcontent",function(type,cmd){
changePre();
});
function adjustHeight(){
var div = me.document.getElementById(SyntaxHighlighter.getHighlighterDivId());
if(div){
var tds = div.getElementsByTagName('td');
for(var i=0,li,ri;li=tds[0].childNodes[i];i++){
ri = tds[1].firstChild.childNodes[i];
ri.style.height = li.style.height = ri.offsetHeight + 'px';
}
}
}
function changePre(){
for(var i=0,pr,pres = domUtils.getElementsByTagName(me.document,"pre");pr=pres[i++];){
if(pr.className.indexOf("brush")>-1){
var pre = document.createElement("pre"),txt,div;
pre.className = pr.className;
pre.style.display = "none";
pre.appendChild(document.createTextNode(pr[browser.ie?'innerText':'textContent']));
document.body.appendChild(pre);
try{
txt = SyntaxHighlighter.highlight(pre,null,true);
}catch(e){
domUtils.remove(pre);
return ;
}
div = me.document.createElement("div");
div.innerHTML = txt;
div.firstChild.setAttribute('highlighter',pre.className);
pr.parentNode.insertBefore(div.firstChild,pr);
domUtils.remove(pre);
domUtils.remove(pr);
adjustHeight()
}
}
}
me.addListener("aftersetcontent",function(){
changePre();
})
};
///import core
///commands 定制过滤规则
///commandsName Serialize
///commandsTitle 定制过滤规则
baidu.editor.plugins['serialize'] = function () {
var dtd = baidu.editor.dom.dtd,
utils = baidu.editor.utils,
domUtils = baidu.editor.dom.domUtils,
browser = baidu.editor.browser,
ie = browser.ie,
version = browser.version;
var me = this,
EMPTY_TAG = baidu.editor.dom.dtd.$empty,
parseHTML = function () {
var RE_PART = /<(?:(?:\/([^>]+)>[ \t\r\n]*)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>[ \t\r\n]*))/g,
RE_ATTR = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g,
EMPTY_ATTR = {checked:1,compact:1,declare:1,defer:1,disabled:1,ismap:1,multiple:1,nohref:1,noresize:1,noshade:1,nowrap:1,readonly:1,selected:1},
CDATA_TAG = {script:1,style: 1},
NEED_PARENT_TAG = {
"li": { "$": 'ul', "ul": 1, "ol": 1 },
"dd": { "$": "dl", "dl": 1 },
"dt": { "$": "dl", "dl": 1 },
"option": { "$": "select", "select": 1 },
"td": { "$": "tr", "tr": 1 },
"th": { "$": "tr", "tr": 1 },
"tr": { "$": "tbody", "tbody": 1, "thead": 1, "tfoot": 1, "table": 1 },
"tbody": { "$": "table", 'table':1,"colgroup": 1 },
"thead": { "$": "table", "table": 1 },
"tfoot": { "$": "table", "table": 1 },
"col": { "$": "colgroup","colgroup":1 }
};
var NEED_CHILD_TAG = {
"table": "td", "tbody": "td", "thead": "td", "tfoot": "td", "tr": "td",
"colgroup": "col",
"ul": "li", "ol": "li",
"dl": "dd",
"select": "option"
};
function parse( html, callbacks ) {
var match,
nextIndex = 0,
tagName,
cdata;
RE_PART.exec( "" );
while ( (match = RE_PART.exec( html )) ) {
var tagIndex = match.index;
if ( tagIndex > nextIndex ) {
var text = html.slice( nextIndex, tagIndex );
if ( cdata ) {
cdata.push( text );
} else {
callbacks.onText( text );
}
}
nextIndex = RE_PART.lastIndex;
if ( (tagName = match[1]) ) {
tagName = tagName.toLowerCase();
if ( cdata && tagName == cdata._tag_name ) {
callbacks.onCDATA( cdata.join( '' ) );
cdata = null;
}
if ( !cdata ) {
callbacks.onTagClose( tagName );
continue;
}
}
if ( cdata ) {
cdata.push( match[0] );
continue;
}
if ( (tagName = match[3]) ) {
if ( /="/.test( tagName ) ) {
continue;
}
tagName = tagName.toLowerCase();
var attrPart = match[4],
attrMatch,
attrMap = {},
selfClosing = attrPart && attrPart.slice( -1 ) == '/';
if ( attrPart ) {
RE_ATTR.exec( "" );
while ( (attrMatch = RE_ATTR.exec( attrPart )) ) {
var attrName = attrMatch[1].toLowerCase(),
attrValue = attrMatch[2] || attrMatch[3] || attrMatch[4] || '';
if ( !attrValue && EMPTY_ATTR[attrName] ) {
attrValue = attrName;
}
if ( attrName == 'style' ) {
if ( ie && version <= 6 ) {
attrValue = attrValue.replace( /(?!;)\s*([\w-]+):/g, function ( m, p1 ) {
return p1.toLowerCase() + ':';
} );
}
}
//没有值的属性不添加
if ( attrValue ) {
attrMap[attrName] = attrValue.replace( /:\s*/g, ':' )
}
}
}
callbacks.onTagOpen( tagName, attrMap, selfClosing );
if ( !cdata && CDATA_TAG[tagName] ) {
cdata = [];
cdata._tag_name = tagName;
}
continue;
}
if ( (tagName = match[2]) ) {
callbacks.onComment( tagName );
}
}
if ( html.length > nextIndex ) {
callbacks.onText( html.slice( nextIndex, html.length ) );
}
}
return function ( html, forceDtd ) {
var fragment = {
type: 'fragment',
parent: null,
children: []
};
var currentNode = fragment;
function addChild( node ) {
node.parent = currentNode;
currentNode.children.push( node );
}
function addElement( element, open ) {
var node = element;
// 遇到结构化标签的时候
if ( NEED_PARENT_TAG[node.tag] ) {
// 考虑这种情况的时候, 结束之前的标签
// e.g. 12312` |
`4566
while ( NEED_PARENT_TAG[currentNode.tag] && NEED_PARENT_TAG[currentNode.tag][node.tag] ) {
currentNode = currentNode.parent;
}
// 如果前一个标签和这个标签是同一级, 结束之前的标签
// e.g. - 123
-
if ( currentNode.tag == node.tag ) {
currentNode = currentNode.parent;
}
// 向上补齐父标签
while ( NEED_PARENT_TAG[node.tag] ) {
if ( NEED_PARENT_TAG[node.tag][currentNode.tag] ) break;
node = node.parent = {
type: 'element',
tag: NEED_PARENT_TAG[node.tag]['$'],
attributes: {},
children: [node]
};
}
}
if ( forceDtd ) {
// 如果遇到这个标签不能放在前一个标签内部,则结束前一个标签,span单独处理
while ( dtd[node.tag] && !(currentNode.tag == 'span' ? utils.extend( dtd['strong'], {'a':1,'A':1} ) : (dtd[currentNode.tag] || dtd['div']))[node.tag] ) {
if ( tagEnd( currentNode ) ) continue;
if ( !currentNode.parent ) break;
currentNode = currentNode.parent;
}
}
node.parent = currentNode;
currentNode.children.push( node );
if ( open ) {
currentNode = element;
}
if ( element.attributes.style ) {
element.attributes.style = element.attributes.style.toLowerCase();
}
return element;
}
// 结束一个标签的时候,需要判断一下它是否缺少子标签
// e.g.
function tagEnd( node ) {
var needTag;
if ( !node.children.length && (needTag = NEED_CHILD_TAG[node.tag]) ) {
addElement( {
type: 'element',
tag: needTag,
attributes: {},
children: []
}, true );
return true;
}
return false;
}
parse( html, {
onText: function ( text ) {
while ( !(dtd[currentNode.tag] || dtd['div'])['#'] ) {
if ( tagEnd( currentNode ) ) continue;
currentNode = currentNode.parent;
}
// TODO: 注意这里会去掉空白节点
if ( /[^ \t\r\n]/.test( text ) ) {
addChild( {
type: 'text',
data: text
} );
}
},
onComment: function ( text ) {
addChild( {
type: 'comment',
data: text
} );
},
onCDATA: function ( text ) {
while ( !(dtd[currentNode.tag] || dtd['div'])['#'] ) {
if ( tagEnd( currentNode ) ) continue;
currentNode = currentNode.parent;
}
addChild( {
type: 'cdata',
data: text
} );
},
onTagOpen: function ( tag, attrs, closed ) {
closed = closed || EMPTY_TAG[tag];
addElement( {
type: 'element',
tag: tag,
attributes: attrs,
closed: closed,
children: []
}, !closed );
},
onTagClose: function ( tag ) {
var node = currentNode;
// 向上找匹配的标签, 这里不考虑dtd的情况是因为tagOpen的时候已经处理过了, 这里不会遇到
while ( node && tag != node.tag ) {
node = node.parent;
}
if ( node ) {
// 关闭中间的标签
for ( var tnode = currentNode; tnode !== node.parent; tnode = tnode.parent ) {
tagEnd( tnode );
}
//去掉空白的inline节点
//分页,锚点保留
//|| dtd.$removeEmptyBlock[node.tag])
if ( !node.children.length && dtd.$removeEmpty[node.tag] && !node.attributes.anchorname && node.attributes['class'] != 'pagebreak' ) {
node.parent.children.pop();
}
currentNode = node.parent;
} else {
// 如果没有找到开始标签, 则创建新标签
// eg. =>
if ( !(dtd.$removeEmpty[tag] || dtd.$removeEmptyBlock[tag]) ) {
node = {
type: 'element',
tag: tag,
attributes: {},
children: []
};
addElement( node, true );
tagEnd( node );
currentNode = node.parent;
}
}
}
} );
// 处理这种情况, 只有开始标签没有结束标签的情况, 需要关闭开始标签
// eg.
while ( currentNode !== fragment ) {
tagEnd( currentNode );
currentNode = currentNode.parent;
}
return fragment;
};
}();
var unhtml1 = function () {
var map = { '<': '<', '>': '>', '"': '"', "'": ''' };
function rep( m ) {
return map[m];
}
return function ( str ) {
str = str + '';
return str ? str.replace( /[<>"']/g, rep ) : '';
};
}();
var toHTML = function () {
function printChildren( node, pasteplain ) {
var children = node.children;
var buff = [];
for ( var i = 0,ci; ci = children[i]; i++ ) {
buff.push( toHTML( ci, pasteplain ) );
}
return buff.join( '' );
}
function printAttrs( attrs ) {
var buff = [];
for ( var k in attrs ) {
var value = attrs[k];
if(k == 'style'){
//pt==>px
if ( /pt/.test(value) ) {
value = value.replace( /([\d.]+)pt/g, function( str ) {
return Math.round(parseInt(str, 10) * 96 / 72) + "px";
} )
}
//color rgb ==> hex
if(/rgba?\s*\([^)]*\)/.test(value)){
value = value.replace( /rgba?\s*\(([^)]*)\)/g, function( str ) {
return utils.fixColor('color',str);
} )
}
attrs[k] = value.replace(/windowtext/g,'#000');
}
buff.push( k + '="' + unhtml1( attrs[k] ) + '"' );
}
return buff.join( ' ' ).replace(/\;+/g,';')
}
function printData( node, notTrans ) {
//trace:1399 输入html代码时空格转换成为
//node.data.replace(/ /g,' ') 针对pre中的空格和出现的 把他们在得到的html代码中都转换成为空格,为了在源码模式下显示为空格而不是
return notTrans ? node.data.replace(/ /g,' ') : unhtml1( node.data ).replace(/ /g,' ');
}
//纯文本模式下标签转换
var transHtml = {
'div':'p',
'li':'p',
'tr':'p',
'br':'br',
'p':'p'//trace:1398 碰到p标签自己要加上p,否则transHtml[tag]是undefined
};
function printElement( node, pasteplain ) {
var tag = node.tag;
if ( pasteplain && tag == 'td' ) {
if ( !html ) html = '';
html += printChildren( node, pasteplain ) + ' ';
} else {
var attrs = printAttrs( node.attributes );
var html = '<' + (pasteplain && transHtml[tag] ? transHtml[tag] : tag) + (attrs ? ' ' + attrs : '') + (EMPTY_TAG[tag] ? ' />' : '>');
if ( !EMPTY_TAG[tag] ) {
//trace:1627
//p标签在ie下为空,将不占位这里占位符不起作用,用
if(browser.ie && tag == 'p' && !node.children.length){
html += ' ';
}
html += printChildren( node, pasteplain );
html += '' + (pasteplain && transHtml[tag] ? transHtml[tag] : tag) + '>';
}
}
return html;
}
return function ( node, pasteplain ) {
if ( node.type == 'fragment' ) {
return printChildren( node, pasteplain );
} else if ( node.type == 'element' ) {
return printElement( node, pasteplain );
} else if ( node.type == 'text' || node.type == 'cdata' ) {
return printData( node, dtd.$notTransContent[node.parent.tag] );
} else if ( node.type == 'comment' ) {
return '';
}
return '';
};
}();
//过滤word
var transformWordHtml = function () {
function isWordDocument( strValue ) {
var re = new RegExp( /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|[\s\S]*$/, '' );
//remove link break
str = str.replace( /^(\r\n|\n|\r)|(\r\n|\n|\r)$/ig, "" );
//remove entities at the start of contents
str = str.replace( /^\s*( )+/ig, "" );
//remove entities at the end of contents
str = str.replace( /( |
]*>)+\s*$/ig, "" );
// Word comments like conditional comments etc
str = str.replace( //ig, "" );
//转换img
str = str.replace( /v:imagedata/g, 'img' ).replace( /<\/img>/g, '' );
//去掉多余的属性
str = str.replace( /v:\w+=["']?[^'"]+["']?/g, '' );
// Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags
str = str.replace( /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "" );
//convert word headers to strong
str = str.replace( /]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "
$1
" );
//remove lang attribute
str = str.replace( /(lang)\s*=\s*([\'\"]?)[\w-]+\2/ig, "" );
//清除多余的font不能匹配 有可能是空格
str = str.replace( /]*>\s*<\/font>/gi, '' );
//清除多余的class
str = str.replace( /class\s*=\s*["']?(?:(?:MsoTableGrid)|(?:MsoNormal(Table)?))\s*["']?/gi, '' );
// Examine all styles: delete junk, transform some, and keep the rest
//修复了原有的问题, 比如style='fontsize:"宋体"'原来的匹配失效了
str = str.replace( /(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function( str, tag, tmp, style ) {
var n = [],
i = 0,
s = style.replace( /^\s+|\s+$/, '' ).replace( /"/gi, "'" ).split( /;\s*/g );
// Examine each style definition within the tag's style attribute
for ( var i = 0; i < s.length; i++ ) {
var v = s[i];
var name, value,
parts = v.split( ":" );
if ( parts.length == 2 ) {
name = parts[0].toLowerCase();
value = parts[1].toLowerCase();
// Translate certain MS Office styles into their CSS equivalents
switch ( name ) {
case "mso-padding-alt":
case "mso-padding-top-alt":
case "mso-padding-right-alt":
case "mso-padding-bottom-alt":
case "mso-padding-left-alt":
case "mso-margin-alt":
case "mso-margin-top-alt":
case "mso-margin-right-alt":
case "mso-margin-bottom-alt":
case "mso-margin-left-alt":
case "mso-table-layout-alt":
case "mso-height":
case "mso-width":
case "mso-vertical-align-alt":
n[i++] = name.replace( /^mso-|-alt$/g, "" ) + ":" + ensureUnits( value );
continue;
case "horiz-align":
n[i++] = "text-align:" + value;
continue;
case "vert-align":
n[i++] = "vertical-align:" + value;
continue;
case "font-color":
case "mso-foreground":
n[i++] = "color:" + value;
continue;
case "mso-background":
case "mso-highlight":
n[i++] = "background:" + value;
continue;
case "mso-default-height":
n[i++] = "min-height:" + ensureUnits( value );
continue;
case "mso-default-width":
n[i++] = "min-width:" + ensureUnits( value );
continue;
case "mso-padding-between-alt":
n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits( value );
continue;
case "text-line-through":
if ( (value == "single") || (value == "double") ) {
n[i++] = "text-decoration:line-through";
}
continue;
//word里边的字体统一干掉
case 'font-family':
case 'border-width':
case 'border-style':
continue;
//word进来的默认都是1px solid #000
// case 'border-color':
// case 'border':
// n[i++] = 'border:1px solid #000';
// continue;
case "mso-zero-height":
if ( value == "yes" ) {
n[i++] = "display:none";
}
continue;
case 'margin':
if ( !/[1-9]/.test( parts[1] ) ) {
continue;
}
}
if ( /^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?:decor|trans)|top-bar|version|vnd|word-break)/.test( name ) ) {
if ( !/mso\-list/.test( name ) )
continue;
}
//pt 转换成为px
// if ( /pt$/.test( parts[1] ) ) {
// parts[1] = parts[1].replace( /([\d.]+)pt/g, function( str ) {
// return Math.round(parseInt(str, 10) * 96 / 72) + "px";
// } )
//
// }
n[i] = name + ":" + parts[1]; // Lower-case name, but keep value case
}
}
// If style attribute contained any valid styles the re-write it; otherwise delete style attribute.
if ( i > 0 ) {
return tag + ' style="' + n.join( ';' ) + '"';
} else {
return tag;
}
} );
str = str.replace( /([ ]+)<\/span>/ig, function ( m, p ) {
return new Array( p.length + 1 ).join( ' ' ) + '';
} );
return str;
}
return function ( html ) {
//过了word,才能转p->li
first = null;
parentTag = '',liStyle = '',firstTag = '';
if ( isWordDocument( html ) ) {
html = filterPasteWord( html );
}
return html.replace( />[ \t\r\n]*<' );
};
}();
var NODE_NAME_MAP = {
'text': '#text',
'comment': '#comment',
'cdata': '#cdata-section',
'fragment': '#document-fragment'
};
function _likeLi( node ) {
var a;
if ( node && node.tag == 'p' ) {
//office 2011下有效
if ( node.attributes['class'] == 'MsoListParagraph' || /mso-list/.test( node.attributes.style ) ) {
a = 1;
} else {
var firstChild = node.children[0];
if ( firstChild && firstChild.tag == 'span' && /Wingdings/i.test( firstChild.attributes.style ) ) {
a = 1;
}
}
}
return a;
}
//为p==>li 做个标志
var first,
orderStyle = {
'decimal' : /\d+/,
'lower-roman': /^m{0,4}(cm|cd|d?c{0,3})(xc|xl|l?x{0,3})(ix|iv|v?i{0,3})$/,
'upper-roman': /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/,
'lower-alpha' : /^\(?[a-z]+\)?$/,
'upper-alpha': /^\(?[A-Z]+\)?$/
},
unorderStyle = { 'disc' : /^[l\u00B7\u2002]/, 'circle' : /^[\u006F\u00D8]/,'square' : /^[\u006E\u25C6]/},
parentTag = '',liStyle = '',firstTag;
//写入编辑器时,调用,进行转换操作
function transNode( node, word_img_flag ) {
//dtd.$removeEmptyBlock[node.tag]
if ( node.type == 'element' && !node.children.length && (dtd.$removeEmpty[node.tag]) && node.tag != 'a' ) {// 锚点保留
return {
type : 'fragment',
children:[]
}
}
var sizeMap = [0, 10, 12, 16, 18, 24, 32, 48],
attr,
indexOf = utils.indexOf;
switch ( node.tag ) {
case 'img':
//todo base64暂时去掉,后边做远程图片上传后,干掉这个
if(node.attributes.src && /^data:/.test(node.attributes.src)){
return {
type : 'fragment',
children:[]
}
}
if ( node.attributes.src && /^(?:file)/.test( node.attributes.src ) ) {
if ( !/(gif|bmp|png|jpg|jpeg)$/.test( node.attributes.src ) ) {
return {
type : 'fragment',
children:[]
}
}
node.attributes.word_img = node.attributes.src;
node.attributes.src = me.options.UEDITOR_HOME_URL + 'themes/default/images/localimage.jpg';
node.attributes.style = 'width:395px;height:173px;';
word_img_flag && (word_img_flag.flag = 1);
}
if(browser.ie && browser.version < 7 && me.options.relativePath)
node.attributes.orgSrc = node.attributes.src;
node.attributes.data_ue_src = node.attributes.data_ue_src || node.attributes.src;
break;
case 'li':
var child = node.children[0];
if ( !child || child.type != 'element' || child.tag != 'p' && dtd.p[child.tag] ) {
node.children = [
{
type: 'element',
tag: 'p',
attributes: {},
children: child ? node.children : [
{
type : 'element',
tag : 'br',
attributes:{},
closed: true,
children: []
}
],
parent : node
}
];
}
break;
case 'table':
case 'td':
optStyle( node );
break;
case 'a'://锚点,a==>img
if ( node.attributes['anchorname'] ) {
node.tag = 'img';
node.attributes = {
'class' : 'anchorclass',
'anchorname':node.attributes['name']
};
node.closed = 1;
}
node.attributes.href && (node.attributes.data_ue_src = node.attributes.href);
break;
case 'b':
node.tag = node.name = 'strong';
break;
case 'i':
node.tag = node.name = 'em';
break;
case 'u':
node.tag = node.name = 'span';
node.attributes.style = (node.attributes.style || '') + ';text-decoration:underline;';
break;
case 's':
case 'del':
node.tag = node.name = 'span';
node.attributes.style = (node.attributes.style || '') + ';text-decoration:line-through;';
if ( node.children.length == 1 ) {
child = node.children[0];
if ( child.tag == node.tag ) {
node.attributes.style += ";" + child.attributes.style;
node.children = child.children;
}
}
break;
case 'span':
if ( /mso-list/.test( node.attributes.style ) ) {
//判断了两次就不在判断了
if ( firstTag != 'end' ) {
var ci = node.children[0],p;
while ( ci.type == 'element' ) {
ci = ci.children[0];
}
for ( p in unorderStyle ) {
if ( unorderStyle[p].test( ci.data ) ) {
// ci.data = ci.data.replace(unorderStyle[p],'');
parentTag = 'ul';
liStyle = p;
break;
}
}
if ( !parentTag ) {
for ( p in orderStyle ) {
if ( orderStyle[p].test( ci.data.replace( /\.$/, '' ) ) ) {
// ci.data = ci.data.replace(orderStyle[p],'');
parentTag = 'ol';
liStyle = p;
break;
}
}
}
if ( firstTag ) {
if ( ci.data == firstTag ) {
if ( parentTag != 'ul' ) {
liStyle = '';
}
parentTag = 'ul'
} else {
if ( parentTag != 'ol' ) {
liStyle = '';
}
parentTag = 'ol'
}
firstTag = 'end'
} else {
firstTag = ci.data
}
if ( parentTag ) {
var tmpNode = node;
while ( tmpNode && tmpNode.tag != 'ul' && tmpNode.tag != 'ol' ) {
tmpNode = tmpNode.parent;
}
if(tmpNode ){
tmpNode.tag = parentTag;
tmpNode.attributes.style = 'list-style-type:' + liStyle;
}
}
}
node = {
type : 'fragment',
children : []
};
break;
}
var style = node.attributes.style;
if ( style ) {
//trace:1493
//ff3.6出来的是background: none repeat scroll %0 %0 颜色
style = style.match( /(?:\b(?:color|font-size|background(-color)?|font-family|text-decoration)\b\s*:\s*(&[^;]+;|[^;])+(?=;)?)/gi );
if ( style ) {
node.attributes.style = style.join( ';' );
if ( !node.attributes.style ) {
delete node.attributes.style;
}
}
}
//针对ff3.6span的样式不能正确继承的修复
if(browser.gecko && browser.version <= 10902 && node.parent){
var parent = node.parent;
if(parent.tag == 'span' && parent.attributes && parent.attributes.style){
node.attributes.style = parent.attributes.style + ';' + node.attributes.style;
}
}
if ( utils.isEmptyObject( node.attributes ) ) {
node.type = 'fragment'
}
break;
case 'font':
node.tag = node.name = 'span';
attr = node.attributes;
node.attributes = {
'style': (attr.size ? 'font-size:' + (sizeMap[attr.size] || 12) + 'px' : '')
+ ';' + (attr.color ? 'color:'+ attr.color : '')
+ ';' + (attr.face ? 'font-family:'+ attr.face : '')
+ ';' + (attr.style||'')
};
while(node.parent.tag == node.tag && node.parent.children.length == 1){
node.attributes.style && (node.parent.attributes.style ? (node.parent.attributes.style += ";" + node.attributes.style) : (node.parent.attributes.style = node.attributes.style));
node.parent.children = node.children;
node = node.parent;
}
break;
case 'p':
if ( node.attributes.align ) {
node.attributes.style = (node.attributes.style || '') + ';text-align:' +
node.attributes.align + ';';
delete node.attributes.align;
}
if ( _likeLi( node ) ) {
if ( !first ) {
var ulNode = {
type: 'element',
tag: 'ul',
attributes: {},
children: []
},
index = indexOf( node.parent.children, node );
node.parent.children[index] = ulNode;
ulNode.parent = node.parent;
ulNode.children[0] = node;
node.parent = ulNode;
while ( 1 ) {
node = ulNode.parent.children[index + 1];
if ( _likeLi( node ) ) {
ulNode.children[ulNode.children.length] = node;
node.parent = ulNode;
ulNode.parent.children.splice( index + 1, 1 );
} else {
break;
}
}
return ulNode;
}
node.tag = node.name = 'li';
//为chrome能找到标号做的处理
if ( browser.webkit ) {
var span = node.children[0];
while ( span && span.type == 'element' ) {
span = span.children[0]
}
span && (span.parent.attributes.style = (span.parent.attributes.style || '') + ';mso-list:10');
}
delete node.attributes['class'];
delete node.attributes.style;
}
}
return node;
}
function optStyle( node ) {
if ( ie && node.attributes.style ) {
var style = node.attributes.style;
node.attributes.style = style.replace(/;\s*/g,';');
// var border = node.attributes.style.match( /border[^:]*:([^;]*)/i );
// if ( border ) {
// border = border[1];
// if ( border ) {
// node.attributes.style = node.attributes.style.replace( /border[^;]*?(;|$)/ig, '' ).replace( /^\s*|\s*$/, '' );
//
//// if ( !/^\s*#\w+\s*$/.test( border ) ) {
//// node.attributes.style = (/;$/.test( node.attributes.style ) || node.attributes.style.length == 0 ? '' : ';') + 'border:' + border;
//// }
//
// }
// }
node.attributes.style = node.attributes.style.replace( /^\s*|\s*$/, '' )
}
}
function transOutNode( node ) {
if ( node.type == 'text' ) {
//trace:1269 先注释了,引起ie预览会折行的问题
//node.data = node.data.replace(/ /g,' ')
}
switch ( node.tag ) {
case 'table':
!node.attributes.style && delete node.attributes.style;
if ( ie && node.attributes.style ) {
optStyle( node );
}
break;
case 'td':
case 'th':
if ( /display\s*:\s*none/i.test( node.attributes.style ) ) {
return {
type: 'fragment',
children: []
};
}
if ( ie && !node.children.length ) {
var txtNode = {
type: 'text',
data:domUtils.fillChar,
parent : node
};
node.children[0] = txtNode;
}
if ( ie && node.attributes.style ) {
optStyle( node );
}
break;
case 'img'://锚点,img==>a
if ( node.attributes.anchorname ) {
node.tag = 'a';
node.attributes = {
name : node.attributes.anchorname,
anchorname : 1
};
node.closed = null;
}else{
if(node.attributes.data_ue_src){
node.attributes.src = node.attributes.data_ue_src;
delete node.attributes.data_ue_src;
}
}
break;
case 'a':
if(node.attributes.data_ue_src){
node.attributes.href = node.attributes.data_ue_src;
delete node.attributes.data_ue_src;
}
}
return node;
}
function childrenAccept( node, visit, ctx ) {
if ( !node.children || !node.children.length ) {
return node;
}
var children = node.children;
for ( var i = 0; i < children.length; i++ ) {
var newNode = visit( children[i], ctx );
if ( newNode.type == 'fragment' ) {
var args = [i, 1];
args.push.apply( args, newNode.children );
children.splice.apply( children, args );
//节点为空的就干掉,不然后边的补全操作会添加多余的节点
if ( !children.length ) {
node = {
type: 'fragment',
children: []
}
}
i --;
} else {
children[i] = newNode;
}
}
return node;
}
function Serialize( rules ) {
this.rules = rules;
}
Serialize.prototype = {
// NOTE: selector目前只支持tagName
rules: null,
// NOTE: node必须是fragment
filter: function ( node, rules, modify ) {
rules = rules || this.rules;
var whiteList = rules && rules.whiteList;
var blackList = rules && rules.blackList;
function visitNode( node, parent ) {
node.name = node.type == 'element' ?
node.tag : NODE_NAME_MAP[node.type];
if ( parent == null ) {
return childrenAccept( node, visitNode, node );
}
if ( blackList && blackList[node.name] ) {
modify && (modify.flag = 1);
return {
type: 'fragment',
children: []
};
}
if ( whiteList ) {
if ( node.type == 'element' ) {
if ( parent.type == 'fragment' ? whiteList[node.name] : whiteList[node.name] && whiteList[parent.name][node.name] ) {
var props;
if ( (props = whiteList[node.name].$) ) {
var oldAttrs = node.attributes;
var newAttrs = {};
for ( var k in props ) {
if ( oldAttrs[k] ) {
newAttrs[k] = oldAttrs[k];
}
}
node.attributes = newAttrs;
}
} else {
modify && (modify.flag = 1);
node.type = 'fragment';
// NOTE: 这里算是一个hack
node.name = parent.name;
}
} else {
// NOTE: 文本默认允许
}
}
if ( blackList || whiteList ) {
childrenAccept( node, visitNode, node );
}
return node;
}
return visitNode( node, null );
},
transformInput: function ( node, word_img_flag ) {
function visitNode( node ) {
node = transNode( node, word_img_flag );
if ( node.tag == 'ol' || node.tag == 'ul' ) {
first = 1;
}
node = childrenAccept( node, visitNode, node );
if ( node.tag == 'ol' || node.tag == 'ul' ) {
first = 0;
parentTag = '',liStyle = '',firstTag = '';
}
if ( node.type == 'text' && node.data.replace( /\s/g, '' ) == me.options.pageBreakTag ) {
node.type = 'element';
node.name = node.tag = 'div';
delete node.data;
node.attributes = {
'class' : 'pagebreak',
'unselectable' : 'on',
'style' : 'moz-user-select:none;-khtml-user-select: none;'
};
node.children = [];
}
//去掉多余的空格和换行
if(node.type == 'text' && !dtd.$notTransContent[node.parent.tag]){
node.data = node.data.replace(/[\r\t\n]*/g,'').replace(/[ ]*$/g,'')
}
return node;
}
return visitNode( node );
},
transformOutput: function ( node ) {
function visitNode( node ) {
if ( node.tag == 'div' && node.attributes['class'] == 'pagebreak' ) {
delete node.tag;
node.type = 'text';
node.data = me.options.pageBreakTag;
delete node.children;
}
node = transOutNode( node );
if ( node.tag == 'ol' || node.tag == 'ul' ) {
first = 1;
}
node = childrenAccept( node, visitNode, node );
if ( node.tag == 'ol' || node.tag == 'ul' ) {
first = 0;
}
return node;
}
return visitNode( node );
},
toHTML: toHTML,
parseHTML: parseHTML,
word: transformWordHtml
};
me.serialize = new Serialize( me.options.serialize );
baidu.editor.serialize = new Serialize( {} );
};
///import core
///import commands/inserthtml.js
///commands 视频
///commandsName InsertVideo
///commandsTitle 插入视频
///commandsDialog dialogs\video\video.html
(function (){
baidu.editor.plugins['video'] = function (){
var me = this;
var fakedMap = {};
var fakedPairs = [];
var lastFakedId = 0;
function fake(url, width, height,style){
var fakedId = 'edui_faked_video_' + (lastFakedId ++);
var fakedHtml = '';
fakedMap[fakedId] = '';
return fakedHtml;
}
me.commands['insertvideo'] = {
execCommand: function (cmd, options){
var url = options.url;
var width = options.width || 320;
var height = options.height || 240;
var style = options.style ? options.style : "";
me.execCommand('inserthtml', fake(url, width, height,style));
},
queryCommandState : function(){
return this.highlight ? -1 :0;
}
};
//获得style里的某个样式对应的值
function getPars(str,par){
var reg = new RegExp(par+":\\s*((\\w)*)","ig");
var arr = reg.exec(str);
return arr ? arr[1] : "";
}
me.addListener('beforegetcontent', function (){
var tempDiv = me.document.createElement('div');
var newFakedMap = {};
for (var fakedId in fakedMap) {
var fakedImg;
while ((fakedImg = me.document.getElementById(fakedId))) {
tempDiv.innerHTML = fakedMap[fakedId];
var temp = tempDiv.firstChild;
temp.width = fakedImg.width;
temp.height = fakedImg.height;
var strcss = fakedImg.style.cssText;
if(/float/ig.test(strcss)){
if(!!window.ActiveXObject){
temp.style.styleFloat = getPars(strcss,"float");
}else{
temp.style.cssFloat = getPars(strcss,"float");
}
}else if(/display/ig.test(strcss)){
temp.style.display = getPars(strcss,"display");
}
fakedImg.parentNode.replaceChild(temp, fakedImg);
fakedPairs.push([fakedImg, temp]);
newFakedMap[fakedId] = fakedMap[fakedId];
}
}
fakedMap = newFakedMap;
});
me.addListener('aftersetcontent', function (){
var tempDiv = me.document.createElement('div');
fakedMap = {};
var embedNodeList = me.document.getElementsByTagName('embed');
var embeds = [];
var k = embedNodeList.length;
while (k --) {
embeds[k] = embedNodeList[k];
}
k = embeds.length;
while (k --) {
var url = embeds[k].getAttribute('src');
var width = embeds[k].width || 320;
var height = embeds[k].height || 240;
var strcss = embeds[k].style.cssText;
var style = getPars(strcss,"display") ? "display:"+getPars(strcss,"display") : "float:"+getPars(strcss,"float");
tempDiv.innerHTML = fake(url, width, height,style);
embeds[k].parentNode.replaceChild(tempDiv.firstChild, embeds[k]);
}
});
me.addListener('aftergetcontent', function (){
for (var i=0; i 0 ? -1 : 0;
},
execCommand: function (cmdName, tableobj) {
tableOpt = tableobj;
var arr = [];
arr.push('cellpadding="' + (tableobj.cellpadding || 0) + '"');
if(tableobj.cellspacing > 0){
arr.push('cellspacing="'+tableobj.cellspacing+'" style="border-collapse:separate;"')
}else{
arr.push('cellspacing="' + (tableobj.cellspacing || 0) + '"');
}
tableobj.width ? arr.push('width="' + tableobj.width + '"') : arr.push('width="500"');
arr.push('borderColor="' + (tableobj.bordercolor || '#000000') + '"');
arr.push('border="' + (tableobj.border || 1) + '"');
var html,rows = [],j = tableobj.numRows;
if (j) while (j --) {
var cols = [];
var k = tableobj.numCols;
while (k --) {
var cssStyle = 'style="';
if(tableobj.cellpadding){
cssStyle += 'padding:'+ tableobj.cellpadding + 'px; '
}
if(tableobj.border || tableobj.bordercolor){
cssStyle += 'border:' + (tableobj.border||1) +'px solid '+ (tableobj.bordercolor||'#000000') + ';';
}
cssStyle += '" ';
cols[k] = '' +
//trace: IE6下占位符
(browser.ie ? domUtils.fillChar : ' ') + ' | ';
}
rows.push('' + cols.join('') + '
');
}
html = '';
html += tableobj.tablealign ? "" : "";
this.execCommand('insertHtml', html);
reset();
}
};
function insertClearNode(node) {
var clearnode = node.nextSibling,p;
if (!(clearnode && clearnode.nodeType == 1 && domUtils.hasClass(clearnode, "tableclear"))) {
p = me.document.createElement("p");
p.className = "tableclear";
domUtils.insertAfter(node, p);
}
}
me.commands['edittable'] = {
queryCommandState: function () {
var range = this.selection.getRange();
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
return domUtils.findParentByTagName(range.startContainer, 'table', true)
|| me.currentSelectedArr.length > 0 ? 0 : -1;
},
execCommand: function (cmdName, tableobj) {
var start = this.selection.getStart();
var table = domUtils.findParentByTagName(start, 'table', true);
if (table) {
var tmp = table.getAttribute("cellPadding");
if(tmp != tableobj.cellpadding){
table.setAttribute("cellPadding", tableobj.cellpadding||0);
var tds = table.getElementsByTagName("td");
for(var i=0,ci;ci=tds[i++];){
ci.style.padding = (tableobj.cellpadding||0) + "px";
}
}
tmp = table.getAttribute("cellSpacing");
if(tmp != tableobj.cellspacing){
tableobj.cellspacing == 0 ? table.style.borderCollapse = "collapse": table.style.borderCollapse = "separate";
table.setAttribute("cellSpacing", tableobj.cellspacing||0);
}else{
tableobj.cellspacing ==0 ? table.style.borderCollapse = "collapse":"";
}
table.setAttribute("width", tableobj.width||0);
table.setAttribute("height", tableobj.height||0);
table.setAttribute("border", tableobj.border||0);
table.style.border = (tableobj.border||0)+"px solid #ffffff";
table.setAttribute("borderColor", tableobj.bordercolor);
table.style.borderColor = tableobj.bordercolor;
var tdss = table.getElementsByTagName("td");
for(var ii=0,tii;tii = tdss[ii++];){
tii.style.border = (tableobj.border||0)+"px solid #ffffff";
tii.style.borderColor = tableobj.bordercolor;
}
table.setAttribute("align", tableobj.tablealign);
//fixed 1368
table.setAttribute("bgColor", tableobj.backgroundcolor);
if(me.currentSelectedArr.length > 0){
for(var t=0,ti;ti=me.currentSelectedArr[t++];){
domUtils.setStyle(ti,'text-align',tableobj.align);
}
}else{
var td = domUtils.findParentByTagName(start,'td',true);
if(td){
domUtils.setStyle(td,'text-align',tableobj.align);
}
}
if (tableobj.tablealign) {
insertClearNode(table);
}
}
}
};
/**
* 删除表格
*/
me.commands['deletetable'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange();
return (domUtils.findParentByTagName(range.startContainer, 'table', true)
&& domUtils.findParentByTagName(range.endContainer, 'table', true)) || me.currentSelectedArr.length > 0 ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
table = domUtils.findParentByTagName(me.currentSelectedArr.length > 0 ? me.currentSelectedArr[0] : range.startContainer, 'table', true);
var p = table.ownerDocument.createElement('p'),clearnode;
p.innerHTML = browser.ie ? ' ' : '
';
table.parentNode.insertBefore(p, table);
clearnode = domUtils.getNextDomNode(table);
if (clearnode && domUtils.hasClass(clearnode, "tableclear")) {
domUtils.remove(clearnode);
}
domUtils.remove(table);
range.setStart(p, 0).setCursor();
reset();
}
};
/**
* 添加表格标题
*/
me.commands['addcaption'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange();
return (domUtils.findParentByTagName(range.startContainer, 'table', true)
&& domUtils.findParentByTagName(range.endContainer, 'table', true)) || me.currentSelectedArr.length > 0 ? 0 : -1;
},
execCommand:function(cmdName, opt) {
var range = this.selection.getRange(),
table = domUtils.findParentByTagName(me.currentSelectedArr.length > 0 ? me.currentSelectedArr[0] : range.startContainer, 'table', true);
if (opt == "on") {
var c = table.createCaption();
c.innerHTML = "请在此输入表格标题";
} else {
table.removeChild(table.caption);
}
}
};
/**
* 向右合并单元格
*/
me.commands['mergeright'] = {
queryCommandState : function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true);
if (!td || this.currentSelectedArr.length > 1)return -1;
var tr = td.parentNode;
//最右边行不能向右合并
var rightCellIndex = getIndex(td) + td.colSpan;
if (rightCellIndex >= tr.cells.length) {
return -1;
}
//单元格不在同一行不能向右合并
var rightCell = tr.cells[rightCellIndex];
if (_isHide(rightCell)) {
return -1;
}
return td.rowSpan == rightCell.rowSpan ? 0 : -1;
},
execCommand : function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true) || me.currentSelectedArr[0],
tr = td.parentNode,
rows = tr.parentNode.parentNode.rows;
//找到当前单元格右边的未隐藏单元格
var rightCellRowIndex = tr.rowIndex,
rightCellCellIndex = getIndex(td) + td.colSpan,
rightCell = rows[rightCellRowIndex].cells[rightCellCellIndex];
//在隐藏的原生td对象上增加两个属性,分别表示当前td对应的真实td坐标
for (var i = rightCellRowIndex; i < rightCellRowIndex + rightCell.rowSpan; i++) {
for (var j = rightCellCellIndex; j < rightCellCellIndex + rightCell.colSpan; j++) {
var tmpCell = rows[i].cells[j];
tmpCell.setAttribute('rootRowIndex', tr.rowIndex);
tmpCell.setAttribute('rootCellIndex', getIndex(td));
}
}
//合并单元格
td.colSpan += rightCell.colSpan || 1;
//合并内容
_moveContent(td, rightCell);
//删除被合并的单元格,此处用隐藏方式实现来提升性能
rightCell.style.display = "none";
//重新让单元格获取焦点
//trace:1565
if(domUtils.isEmptyBlock(td)){
range.setStart(td,0).setCursor();
}else{
range.selectNodeContents(td).setCursor(true,true);
}
//处理有寛高,导致ie的文字不能输入占满
browser.ie && domUtils.removeAttributes(td,['width','height']);
}
};
/**
* 向下合并单元格
*/
me.commands['mergedown'] = {
queryCommandState : function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, 'td', true);
if (!td || getCount(me.currentSelectedArr) > 1)return -1;
var tr = td.parentNode,
table = tr.parentNode.parentNode,
rows = table.rows;
//已经是最底行,不能向下合并
var downCellRowIndex = tr.rowIndex + td.rowSpan;
if (downCellRowIndex >= rows.length) {
return -1;
}
//如果下一个单元格是隐藏的,表明他是由左边span过来的,不能向下合并
var downCell = rows[downCellRowIndex].cells[getIndex(td)];
if (_isHide(downCell)) {
return -1;
}
//只有列span都相等时才能合并
return td.colSpan == downCell.colSpan ? 0 : -1;
},
execCommand : function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true) || me.currentSelectedArr[0];
var tr = td.parentNode,
rows = tr.parentNode.parentNode.rows;
var downCellRowIndex = tr.rowIndex + td.rowSpan,
downCellCellIndex = getIndex(td),
downCell = rows[downCellRowIndex].cells[downCellCellIndex];
//找到当前列的下一个未被隐藏的单元格
for (var i = downCellRowIndex; i < downCellRowIndex + downCell.rowSpan; i++) {
for (var j = downCellCellIndex; j < downCellCellIndex + downCell.colSpan; j++) {
var tmpCell = rows[i].cells[j];
tmpCell.setAttribute('rootRowIndex', tr.rowIndex);
tmpCell.setAttribute('rootCellIndex', getIndex(td));
}
}
//合并单元格
td.rowSpan += downCell.rowSpan || 1;
//合并内容
_moveContent(td, downCell);
//删除被合并的单元格,此处用隐藏方式实现来提升性能
downCell.style.display = "none";
//重新让单元格获取焦点
if(domUtils.isEmptyBlock(td)){
range.setStart(td,0).setCursor();
}else{
range.selectNodeContents(td).setCursor(true,true);
}
//处理有寛高,导致ie的文字不能输入占满
browser.ie && domUtils.removeAttributes(td,['width','height']);
}
};
/**
* 删除行
*/
me.commands['deleterow'] = {
queryCommandState : function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true);
if (!td && me.currentSelectedArr.length == 0)return -1;
},
execCommand : function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true),
tr,
table,
cells,
rows ,
rowIndex ,
cellIndex;
if (td && me.currentSelectedArr.length == 0) {
var count = (td.rowSpan || 1) - 1;
me.currentSelectedArr.push(td);
tr = td.parentNode,
table = tr.parentNode.parentNode;
rows = table.rows,
rowIndex = tr.rowIndex + 1,
cellIndex = getIndex(td);
while (count) {
me.currentSelectedArr.push(rows[rowIndex].cells[cellIndex]);
count--;
rowIndex++
}
}
while (td = me.currentSelectedArr.pop()) {
if (!domUtils.findParentByTagName(td, 'table')) {//|| _isHide(td)
continue;
}
tr = td.parentNode,
table = tr.parentNode.parentNode;
cells = tr.cells,
rows = table.rows,
rowIndex = tr.rowIndex,
cellIndex = getIndex(td);
/*
* 从最左边开始扫描并隐藏当前行的所有单元格
* 若当前单元格的display为none,往上找到它所在的真正单元格,获取colSpan和rowSpan,
* 将rowspan减一,并跳转到cellIndex+colSpan列继续处理
* 若当前单元格的display不为none,分两种情况:
* 1、rowspan == 1 ,直接设置display为none,跳转到cellIndex+colSpan列继续处理
* 2、rowspan > 1 , 修改当前单元格的下一个单元格的display为"",
* 并将当前单元格的rowspan-1赋给下一个单元格的rowspan,当前单元格的colspan赋给下一个单元格的colspan,
* 然后隐藏当前单元格,跳转到cellIndex+colSpan列继续处理
*/
for (var currentCellIndex = 0; currentCellIndex < cells.length;) {
var currentNode = cells[currentCellIndex];
if (_isHide(currentNode)) {
var topNode = rows[currentNode.getAttribute('rootRowIndex')].cells[currentNode.getAttribute('rootCellIndex')];
topNode.rowSpan--;
currentCellIndex += topNode.colSpan;
} else {
if (currentNode.rowSpan == 1) {
currentCellIndex += currentNode.colSpan;
} else {
var downNode = rows[rowIndex + 1].cells[currentCellIndex];
downNode.style.display = "";
downNode.rowSpan = currentNode.rowSpan - 1;
downNode.colSpan = currentNode.colSpan;
currentCellIndex += currentNode.colSpan;
}
}
}
//完成更新后再删除外层包裹的tr
domUtils.remove(tr);
//重新定位焦点
var topRowTd, focusTd, downRowTd;
if (rowIndex == rows.length) { //如果被删除的行是最后一行,这里之所以没有-1是因为已经删除了一行
//如果删除的行也是第一行,那么表格总共只有一行,删除整个表格
if (rowIndex == 0) {
var p = table.ownerDocument.createElement('p');
p.innerHTML = browser.ie ? ' ' : '
';
table.parentNode.insertBefore(p, table);
domUtils.remove(table);
range.setStart(p, 0).setCursor();
return;
}
//如果上一单元格未隐藏,则直接定位,否则定位到最近的上一个非隐藏单元格
var preRowIndex = rowIndex - 1;
topRowTd = rows[preRowIndex].cells[ cellIndex];
focusTd = _isHide(topRowTd) ? rows[topRowTd.getAttribute('rootRowIndex')].cells[topRowTd.getAttribute('rootCellIndex')] : topRowTd;
} else { //如果被删除的不是最后一行,则光标定位到下一行,此处未加1是因为已经删除了一行
downRowTd = rows[rowIndex].cells[cellIndex];
focusTd = _isHide(downRowTd) ? rows[downRowTd.getAttribute('rootRowIndex')].cells[downRowTd.getAttribute('rootCellIndex')] : downRowTd;
}
}
range.setStart(focusTd, 0).setCursor();
update(table)
}
};
/**
* 删除列
*/
me.commands['deletecol'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true);
if (!td && me.currentSelectedArr.length == 0)return -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true);
if (td && me.currentSelectedArr.length == 0) {
var count = (td.colSpan || 1) - 1;
me.currentSelectedArr.push(td);
while (count) {
do{
td = td.nextSibling
} while (td.nodeType == 3);
me.currentSelectedArr.push(td);
count--;
}
}
while (td = me.currentSelectedArr.pop()) {
if (!domUtils.findParentByTagName(td, 'table')) { //|| _isHide(td)
continue;
}
var tr = td.parentNode,
table = tr.parentNode.parentNode,
cellIndex = getIndex(td),
rows = table.rows,
cells = tr.cells,
rowIndex = tr.rowIndex;
/*
* 从第一行开始扫描并隐藏当前列的所有单元格
* 若当前单元格的display为none,表明它是由左边Span过来的,
* 将左边第一个非none单元格的colSpan减去1并删去对应的单元格后跳转到rowIndex + rowspan行继续处理;
* 若当前单元格的display不为none,分两种情况,
* 1、当前单元格的colspan == 1 , 则直接删除该节点,跳转到rowIndex + rowspan行继续处理
* 2、当前单元格的colsapn > 1, 修改当前单元格右边单元格的display为"",
* 并将当前单元格的colspan-1赋给它的colspan,当前单元格的rolspan赋给它的rolspan,
* 然后删除当前单元格,跳转到rowIndex+rowSpan行继续处理
*/
var rowSpan;
for (var currentRowIndex = 0; currentRowIndex < rows.length;) {
var currentNode = rows[currentRowIndex].cells[cellIndex];
if (_isHide(currentNode)) {
var leftNode = rows[currentNode.getAttribute('rootRowIndex')].cells[currentNode.getAttribute('rootCellIndex')];
//依次删除对应的单元格
rowSpan = leftNode.rowSpan;
for (var i = 0; i < leftNode.rowSpan; i++) {
var delNode = rows[currentRowIndex + i].cells[cellIndex];
domUtils.remove(delNode);
}
//修正被删后的单元格信息
leftNode.colSpan--;
currentRowIndex += rowSpan;
} else {
if (currentNode.colSpan == 1) {
rowSpan = currentNode.rowSpan;
for (var i = currentRowIndex,l = currentRowIndex + currentNode.rowSpan; i < l; i++) {
domUtils.remove(rows[i].cells[cellIndex]);
}
currentRowIndex += rowSpan;
} else {
var rightNode = rows[currentRowIndex].cells[cellIndex + 1];
rightNode.style.display = "";
rightNode.rowSpan = currentNode.rowSpan;
rightNode.colSpan = currentNode.colSpan - 1;
currentRowIndex += currentNode.rowSpan;
domUtils.remove(currentNode);
}
}
}
//重新定位焦点
var preColTd, focusTd, nextColTd;
if (cellIndex == cells.length) { //如果当前列是最后一列,光标定位到当前列的前一列,同样,这里没有减去1是因为已经被删除了一列
//如果当前列也是第一列,则删除整个表格
if (cellIndex == 0) {
var p = table.ownerDocument.createElement('p');
p.innerHTML = browser.ie ? ' ' : '
';
table.parentNode.insertBefore(p, table);
domUtils.remove(table);
range.setStart(p, 0).setCursor();
return;
}
//找到当前单元格前一列中和本单元格最近的一个未隐藏单元格
var preCellIndex = cellIndex - 1;
preColTd = rows[rowIndex].cells[preCellIndex];
focusTd = _isHide(preColTd) ? rows[preColTd.getAttribute('rootRowIndex')].cells[preColTd.getAttribute('rootCellIndex')] : preColTd;
} else { //如果当前列不是最后一列,则光标定位到当前列的后一列
nextColTd = rows[rowIndex].cells[cellIndex];
focusTd = _isHide(nextColTd) ? rows[nextColTd.getAttribute('rootRowIndex')].cells[nextColTd.getAttribute('rootCellIndex')] : nextColTd;
}
}
range.setStart(focusTd, 0).setCursor();
update(table)
}
};
/**
* 完全拆分单元格
*/
me.commands['splittocells'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true);
return td && ( td.rowSpan > 1 || td.colSpan > 1 ) && (!me.currentSelectedArr.length || getCount(me.currentSelectedArr) == 1) ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true),
tr = td.parentNode,
table = tr.parentNode.parentNode;
var rowIndex = tr.rowIndex,
cellIndex = getIndex(td),
rowSpan = td.rowSpan,
colSpan = td.colSpan;
for (var i = 0; i < rowSpan; i++) {
for (var j = 0; j < colSpan; j++) {
var cell = table.rows[rowIndex + i].cells[cellIndex + j];
cell.rowSpan = 1;
cell.colSpan = 1;
if (_isHide(cell)) {
cell.style.display = "";
cell.innerHTML = browser.ie ? '' : "
";
}
}
}
}
};
/**
* 将单元格拆分成行
*/
me.commands['splittorows'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, 'td', true) || me.currentSelectedArr[0];
return td && ( td.rowSpan > 1) && (!me.currentSelectedArr.length || getCount(me.currentSelectedArr) == 1) ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, 'td', true) || me.currentSelectedArr[0],
tr = td.parentNode,
rows = tr.parentNode.parentNode.rows;
var rowIndex = tr.rowIndex,
cellIndex = getIndex(td),
rowSpan = td.rowSpan,
colSpan = td.colSpan;
for (var i = 0; i < rowSpan; i++) {
var cells = rows[rowIndex + i],
cell = cells.cells[cellIndex];
cell.rowSpan = 1;
cell.colSpan = colSpan;
if (_isHide(cell)) {
cell.style.display = "";
//原有的内容要清除掉
cell.innerHTML = browser.ie ? '' : '
'
}
//修正被隐藏单元格中存储的rootRowIndex和rootCellIndex信息
for (var j = cellIndex + 1; j < cellIndex + colSpan; j++) {
cell = cells.cells[j];
cell.setAttribute('rootRowIndex', rowIndex + i)
}
}
clearSelectedTd(me.currentSelectedArr);
this.selection.getRange().setStart(td, 0).setCursor();
}
};
/**
* 在表格前插入行
*/
me.commands['insertparagraphbeforetable'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, 'td', true) || me.currentSelectedArr[0];
return td && domUtils.findParentByTagName(td, 'table') ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
table = domUtils.findParentByTagName(start, 'table', true);
start = me.document.createElement(me.options.enterTag);
table.parentNode.insertBefore(start, table);
clearSelectedTd(me.currentSelectedArr);
if (start.tagName == 'P') {
//trace:868
start.innerHTML = browser.ie ? '' : '
';
range.setStart(start, 0)
} else {
range.setStartBefore(start)
}
range.setCursor();
}
};
/**
* 将单元格拆分成列
*/
me.commands['splittocols'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true) || me.currentSelectedArr[0];
return td && ( td.colSpan > 1) && (!me.currentSelectedArr.length || getCount(me.currentSelectedArr) == 1) ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true) || me.currentSelectedArr[0],
tr = td.parentNode,
rows = tr.parentNode.parentNode.rows;
var rowIndex = tr.rowIndex,
cellIndex = getIndex(td),
rowSpan = td.rowSpan,
colSpan = td.colSpan;
for (var i = 0; i < colSpan; i++) {
var cell = rows[rowIndex].cells[cellIndex + i];
cell.rowSpan = rowSpan;
cell.colSpan = 1;
if (_isHide(cell)) {
cell.style.display = "";
cell.innerHTML = browser.ie ? '' : '
'
}
for (var j = rowIndex + 1; j < rowIndex + rowSpan; j++) {
var tmpCell = rows[j].cells[cellIndex + i];
tmpCell.setAttribute('rootCellIndex', cellIndex + i);
}
}
clearSelectedTd(me.currentSelectedArr);
this.selection.getRange().setStart(td, 0).setCursor();
}
};
/**
* 插入行
*/
me.commands['insertrow'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange();
return domUtils.findParentByTagName(range.startContainer, 'table', true)
|| domUtils.findParentByTagName(range.endContainer, 'table', true) || me.currentSelectedArr.length != 0 ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
tr = domUtils.findParentByTagName(start, 'tr', true) || me.currentSelectedArr[0].parentNode,
table = tr.parentNode.parentNode,
rows = table.rows;
//记录插入位置原来所有的单元格
var rowIndex = tr.rowIndex,
cells = rows[rowIndex].cells;
//插入新的一行
var newRow = table.insertRow(rowIndex);
var newCell;
//遍历表格中待插入位置中的所有单元格,检查其状态,并据此修正新插入行的单元格状态
for (var cellIndex = 0; cellIndex < cells.length;) {
var tmpCell = cells[cellIndex];
if (_isHide(tmpCell)) { //如果当前单元格是隐藏的,表明当前单元格由其上部span过来,找到其上部单元格
//找到被隐藏单元格真正所属的单元格
var topCell = rows[tmpCell.getAttribute('rootRowIndex')].cells[tmpCell.getAttribute('rootCellIndex')];
//增加一行,并将所有新插入的单元格隐藏起来
topCell.rowSpan++;
for (var i = 0; i < topCell.colSpan; i++) {
newCell = tmpCell.cloneNode(false);
newCell.rowSpan = newCell.colSpan = 1;
newCell.innerHTML = browser.ie ? '' : "
";
newCell.className = '';
if (newRow.children[cellIndex + i]) {
newRow.insertBefore(newCell, newRow.children[cellIndex + i]);
} else {
newRow.appendChild(newCell)
}
newCell.style.display = "none";
}
cellIndex += topCell.colSpan;
} else {//若当前单元格未隐藏,则在其上行插入colspan个单元格
for (var j = 0; j < tmpCell.colSpan; j++) {
newCell = tmpCell.cloneNode(false);
newCell.rowSpan = newCell.colSpan = 1;
newCell.innerHTML = browser.ie ? '' : "
";
newCell.className = '';
if (newRow.children[cellIndex + j]) {
newRow.insertBefore(newCell, newRow.children[cellIndex + j]);
} else {
newRow.appendChild(newCell)
}
}
cellIndex += tmpCell.colSpan;
}
}
update(table);
range.setStart(newRow.cells[0], 0).setCursor();
clearSelectedTd(me.currentSelectedArr);
}
};
/**
* 插入列
*/
me.commands['insertcol'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var range = this.selection.getRange();
return domUtils.findParentByTagName(range.startContainer, 'table', true)
|| domUtils.findParentByTagName(range.endContainer, 'table', true) || me.currentSelectedArr.length != 0 ? 0 : -1;
},
execCommand:function() {
var range = this.selection.getRange(),
start = range.startContainer,
td = domUtils.findParentByTagName(start, ['td','th'], true) || me.currentSelectedArr[0],
table = domUtils.findParentByTagName(td, 'table'),
rows = table.rows;
var cellIndex = getIndex(td),
newCell;
//遍历当前列中的所有单元格,检查其状态,并据此修正新插入列的单元格状态
for (var rowIndex = 0; rowIndex < rows.length;) {
var tmpCell = rows[rowIndex].cells[cellIndex],tr;
if (_isHide(tmpCell)) {//如果当前单元格是隐藏的,表明当前单元格由其左边span过来,找到其左边单元格
var leftCell = rows[tmpCell.getAttribute('rootRowIndex')].cells[tmpCell.getAttribute('rootCellIndex')];
leftCell.colSpan++;
for (var i = 0; i < leftCell.rowSpan; i++) {
newCell = td.cloneNode(false);
newCell.rowSpan = newCell.colSpan = 1;
newCell.innerHTML = browser.ie ? '' : "
";
newCell.className = '';
tr = rows[rowIndex + i];
if (tr.children[cellIndex]) {
tr.insertBefore(newCell, tr.children[cellIndex]);
} else {
tr.appendChild(newCell)
}
newCell.style.display = "none";
}
rowIndex += leftCell.rowSpan;
} else { //若当前单元格未隐藏,则在其左边插入rowspan个单元格
for (var j = 0; j < tmpCell.rowSpan; j++) {
newCell = td.cloneNode(false);
newCell.rowSpan = newCell.colSpan = 1;
newCell.innerHTML = browser.ie ? '' : "
";
newCell.className = '';
tr = rows[rowIndex + j];
if (tr.children[cellIndex]) {
tr.insertBefore(newCell, tr.children[cellIndex]);
} else {
tr.appendChild(newCell)
}
newCell.innerHTML = browser.ie ? '' : "
";
}
rowIndex += tmpCell.rowSpan;
}
}
update(table);
range.setStart(rows[0].cells[cellIndex], 0).setCursor();
clearSelectedTd(me.currentSelectedArr);
}
};
/**
* 合并多个单元格,通过两个cell将当前包含的所有横纵单元格进行合并
*/
me.commands['mergecells'] = {
queryCommandState:function() {
if(this.highlight || this.queryCommandState('highlightcode')){
return -1;
}
var count = 0;
for (var i = 0,ti; ti = this.currentSelectedArr[i++];) {
if (!_isHide(ti))
count++;
}
return count > 1 ? 0 : -1;
},
execCommand:function() {
var start = me.currentSelectedArr[0],
end = me.currentSelectedArr[me.currentSelectedArr.length - 1],
table = domUtils.findParentByTagName(start, 'table'),
rows = table.rows,
cellsRange = {
beginRowIndex:start.parentNode.rowIndex,
beginCellIndex:getIndex(start),
endRowIndex:end.parentNode.rowIndex,
endCellIndex:getIndex(end)
},
beginRowIndex = cellsRange.beginRowIndex,
beginCellIndex = cellsRange.beginCellIndex,
rowsLength = cellsRange.endRowIndex - cellsRange.beginRowIndex + 1,
cellLength = cellsRange.endCellIndex - cellsRange.beginCellIndex + 1,
tmp = rows[beginRowIndex].cells[beginCellIndex];
for (var i = 0, ri; (ri = rows[beginRowIndex + i++]) && i <= rowsLength;) {
for (var j = 0, ci; (ci = ri.cells[beginCellIndex + j++]) && j <= cellLength;) {
if (i == 1 && j == 1) {
ci.style.display = "";
ci.rowSpan = rowsLength;
ci.colSpan = cellLength;
} else {
ci.style.display = "none";
ci.rowSpan = 1;
ci.colSpan = 1;
ci.setAttribute('rootRowIndex', beginRowIndex);
ci.setAttribute('rootCellIndex', beginCellIndex);
//传递内容
_moveContent(tmp, ci);
}
}
}
this.selection.getRange().setStart(tmp, 0).setCursor();
//处理有寛高,导致ie的文字不能输入占满
browser.ie && domUtils.removeAttributes(tmp,['width','height']);
clearSelectedTd(me.currentSelectedArr);
}
};
/**
* 将cellFrom单元格中的内容移动到cellTo中
* @param cellTo 目标单元格
* @param cellFrom 源单元格
*/
function _moveContent(cellTo, cellFrom) {
if (_isEmpty(cellFrom)) return;
if (_isEmpty(cellTo)) {
cellTo.innerHTML = cellFrom.innerHTML;
return;
}
var child = cellTo.lastChild;
if (child.nodeType != 1 || child.tagName != 'BR') {
cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
}
//依次移动内容
while (child = cellFrom.firstChild) {
cellTo.appendChild(child);
}
}
/**
* 根据两个单元格来获取中间包含的所有单元格集合选区
* @param cellA
* @param cellB
* @return {Object} 选区的左上和右下坐标
*/
function _getCellsRange(cellA, cellB) {
var trA = cellA.parentNode,
trB = cellB.parentNode,
aRowIndex = trA.rowIndex,
bRowIndex = trB.rowIndex,
rows = trA.parentNode.parentNode.rows,
rowsNum = rows.length,
cellsNum = rows[0].cells.length,
cellAIndex = getIndex(cellA),
cellBIndex = getIndex(cellB);
if (cellA == cellB) {
return {
beginRowIndex: aRowIndex,
beginCellIndex: cellAIndex,
endRowIndex: aRowIndex + cellA.rowSpan - 1,
endCellIndex: cellBIndex + cellA.colSpan - 1
}
}
var
beginRowIndex = Math.min(aRowIndex, bRowIndex),
beginCellIndex = Math.min(cellAIndex, cellBIndex),
endRowIndex = Math.max(aRowIndex + cellA.rowSpan - 1, bRowIndex + cellB.rowSpan - 1),
endCellIndex = Math.max(cellAIndex + cellA.colSpan - 1, cellBIndex + cellB.colSpan - 1);
while (1) {
var tmpBeginRowIndex = beginRowIndex,
tmpBeginCellIndex = beginCellIndex,
tmpEndRowIndex = endRowIndex,
tmpEndCellIndex = endCellIndex;
// 检查是否有超出TableRange上边界的情况
if (beginRowIndex > 0) {
for (cellIndex = beginCellIndex; cellIndex <= endCellIndex;) {
var currentTopTd = rows[beginRowIndex].cells[cellIndex];
if (_isHide(currentTopTd)) {
//overflowRowIndex = beginRowIndex == currentTopTd.rootRowIndex ? 1:0;
beginRowIndex = currentTopTd.getAttribute('rootRowIndex');
currentTopTd = rows[currentTopTd.getAttribute('rootRowIndex')].cells[currentTopTd.getAttribute('rootCellIndex')];
}
cellIndex = getIndex(currentTopTd) + (currentTopTd.colSpan || 1);
}
}
//检查是否有超出左边界的情况
if (beginCellIndex > 0) {
for (var rowIndex = beginRowIndex; rowIndex <= endRowIndex;) {
var currentLeftTd = rows[rowIndex].cells[beginCellIndex];
if (_isHide(currentLeftTd)) {
// overflowCellIndex = beginCellIndex== currentLeftTd.rootCellIndex ? 1:0;
beginCellIndex = currentLeftTd.getAttribute('rootCellIndex');
currentLeftTd = rows[currentLeftTd.getAttribute('rootRowIndex')].cells[currentLeftTd.getAttribute('rootCellIndex')];
}
rowIndex = currentLeftTd.parentNode.rowIndex + (currentLeftTd.rowSpan || 1);
}
}
// 检查是否有超出TableRange下边界的情况
if (endRowIndex < rowsNum) {
for (var cellIndex = beginCellIndex; cellIndex <= endCellIndex;) {
var currentDownTd = rows[endRowIndex].cells[cellIndex];
if (_isHide(currentDownTd)) {
currentDownTd = rows[currentDownTd.getAttribute('rootRowIndex')].cells[currentDownTd.getAttribute('rootCellIndex')];
}
endRowIndex = currentDownTd.parentNode.rowIndex + currentDownTd.rowSpan - 1;
cellIndex = getIndex(currentDownTd) + (currentDownTd.colSpan || 1);
}
}
//检查是否有超出右边界的情况
if (endCellIndex < cellsNum) {
for (rowIndex = beginRowIndex; rowIndex <= endRowIndex;) {
var currentRightTd = rows[rowIndex].cells[endCellIndex];
if (_isHide(currentRightTd)) {
currentRightTd = rows[currentRightTd.getAttribute('rootRowIndex')].cells[currentRightTd.getAttribute('rootCellIndex')];
}
endCellIndex = getIndex(currentRightTd) + currentRightTd.colSpan - 1;
rowIndex = currentRightTd.parentNode.rowIndex + (currentRightTd.rowSpan || 1);
}
}
if (tmpBeginCellIndex == beginCellIndex && tmpEndCellIndex == endCellIndex && tmpEndRowIndex == endRowIndex && tmpBeginRowIndex == beginRowIndex) {
break;
}
}
//返回选区的起始和结束坐标
return {
beginRowIndex: beginRowIndex,
beginCellIndex: beginCellIndex,
endRowIndex: endRowIndex,
endCellIndex: endCellIndex
}
}
/**
* 鼠标按下事件
* @param type
* @param evt
*/
function _mouseDownEvent(type, evt) {
anchorTd = evt.target || evt.srcElement;
if(me.queryCommandState('highlightcode')||domUtils.findParent(anchorTd,function(node){
return node.tagName == "DIV"&&/highlighter/.test(node.id);
})){
return;
}
if (evt.button == 2)return;
me.document.body.style.webkitUserSelect = '';
clearSelectedTd(me.currentSelectedArr);
domUtils.clearSelectedArr(me.currentSelectedArr);
//在td里边点击,anchorTd不是td
if (anchorTd.tagName !== 'TD') {
anchorTd = domUtils.findParentByTagName(anchorTd, 'td') || anchorTd;
}
if (anchorTd.tagName == 'TD') {
me.addListener('mouseover', function(type, evt) {
var tmpTd = evt.target || evt.srcElement;
_mouseOverEvent.call(me, tmpTd);
evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
});
} else {
reset();
}
}
/**
* 鼠标移动事件
* @param tmpTd
*/
function _mouseOverEvent(tmpTd) {
if (anchorTd && tmpTd.tagName == "TD") {
me.document.body.style.webkitUserSelect = 'none';
var table = tmpTd.parentNode.parentNode.parentNode;
me.selection.getNative()[browser.ie ? 'empty' : 'removeAllRanges']();
var range = _getCellsRange(anchorTd, tmpTd);
_toggleSelect(table, range);
}
}
/**
* 切换选区状态
* @param table
* @param cellsRange
*/
function _toggleSelect(table, cellsRange) {
var rows = table.rows;
clearSelectedTd(me.currentSelectedArr);
for (var i = cellsRange.beginRowIndex; i <= cellsRange.endRowIndex; i++) {
for (var j = cellsRange.beginCellIndex; j <= cellsRange.endCellIndex; j++) {
var td = rows[i].cells[j];
td.className = me.options.selectedTdClass;
me.currentSelectedArr.push(td);
}
}
}
//更新rootRowIndxe,rootCellIndex
function update(table) {
var tds = table.getElementsByTagName('td'),
rowIndex,cellIndex,rows = table.rows;
for (var j = 0,tj; tj = tds[j++];) {
if (!_isHide(tj)) {
rowIndex = tj.parentNode.rowIndex;
cellIndex = getIndex(tj);
for (var r = 0; r < tj.rowSpan; r++) {
var c = r == 0 ? 1 : 0;
for (; c < tj.colSpan; c++) {
var tmp = rows[rowIndex + r].children[cellIndex + c];
tmp.setAttribute('rootRowIndex', rowIndex);
tmp.setAttribute('rootCellIndex', cellIndex);
}
}
}
}
}
me.adjustTable = function(cont) {
var table = cont.getElementsByTagName('table');
for (var i = 0,ti; ti = table[i++];) {
if (ti.getAttribute('align')) {
insertClearNode(ti)
}
if (!ti.getAttribute('border')) {
ti.setAttribute('border', 1);
}
if (domUtils.getComputedStyle(ti, 'border-color') == '#ffffff') {
ti.setAttribute('borderColor', '#000000');
}
var tds = domUtils.getElementsByTagName(ti, 'td'),
td,tmpTd;
for (var j = 0,tj; tj = tds[j++];) {
if (domUtils.isEmptyNode(tj)) {
tj.innerHTML = browser.ie ? domUtils.fillChar : '
';
}
var index = getIndex(tj),
rowIndex = tj.parentNode.rowIndex,
rows = domUtils.findParentByTagName(tj, 'table').rows;
for (var r = 0; r < tj.rowSpan; r++) {
var c = r == 0 ? 1 : 0;
for (; c < tj.colSpan; c++) {
if (!td) {
td = tj.cloneNode(false);
td.rowSpan = td.colSpan = 1;
td.style.display = 'none';
td.innerHTML = browser.ie ? '' : '
';
} else {
td = td.cloneNode(true)
}
td.setAttribute('rootRowIndex', tj.parentNode.rowIndex);
td.setAttribute('rootCellIndex', index);
if (r == 0) {
if (tj.nextSibling) {
tj.parentNode.insertBefore(td, tj.nextSibling);
} else {
tj.parentNode.appendChild(td)
}
} else {
tmpTd = rows[rowIndex + r].children[index];
if (tmpTd) {
tmpTd.parentNode.insertBefore(td, tmpTd)
} else {
//trace:1032
rows[rowIndex + r].appendChild(td)
}
}
}
}
}
}
}
};
})();
///import core
///commands 右键菜单
///commandsName ContextMenu
///commandsTitle 右键菜单
/**
* 右键菜单
* @function
* @name baidu.editor.plugins.contextmenu
* @author zhanyi
*/
(function () {
baidu.editor.plugins['contextmenu'] = function () {
var me = this,
menu,
items = me.options.contextMenu;
if(!items || items.length==0) return;
var editor = baidu.editor,
domUtils = editor.dom.domUtils,
browser = editor.browser;
var uiUtils = baidu.editor.ui.uiUtils;
me.addListener('contextmenu',function(type,evt){
var offset = uiUtils.getViewportOffsetByEvent(evt);
if (menu)
menu.destroy();
for (var i = 0,ti,contextItems = []; ti = items[i]; i++) {
var last;
(function(item) {
if (item == '-') {
if ((last = contextItems[contextItems.length - 1 ] ) && last !== '-')
contextItems.push('-');
} else if (item.group) {
for (var j = 0,cj,subMenu = []; cj = item.subMenu[j]; j++) {
(function(subItem) {
if (subItem == '-') {
if ((last = subMenu[subMenu.length - 1 ] ) && last !== '-')
subMenu.push('-');
} else {
if (me.queryCommandState(subItem.cmdName) != -1) {
subMenu.push({
'label':subItem.label,
className: 'edui-for-' + subItem.cmdName + (subItem.value || ''),
onclick : subItem.exec ? function() {
subItem.exec.call(me)
} : function() {
me.execCommand(subItem.cmdName, subItem.value)
}
})
}
}
})(cj)
}
if (subMenu.length) {
contextItems.push({
'label' : item.group,
className: 'edui-for-' + item.icon,
'subMenu' : {
items: subMenu,
editor:me
}
})
}
} else {
if (me.queryCommandState(item.cmdName) != -1) {
//highlight todo
if(item.cmdName == 'highlightcode' && me.queryCommandState(item.cmdName) == 0)
return;
contextItems.push({
'label':item.label,
className: 'edui-for-' + (item.icon ? item.icon : item.cmdName + (item.value || '')),
onclick : item.exec ? function() {
item.exec.call(me)
} : function() {
me.execCommand(item.cmdName, item.value)
}
})
}
}
})(ti)
}
if (contextItems[contextItems.length - 1] == '-')
contextItems.pop();
menu = new baidu.editor.ui.Menu({
items: contextItems,
editor:me
});
menu.render();
menu.showAt(offset);
domUtils.preventDefault(evt);
if(browser.ie){
var ieRange;
try{
ieRange = me.selection.getNative().createRange();
}catch(e){
return;
}
if(ieRange.item){
var range = new editor.dom.Range(me.document);
range.selectNode(ieRange.item(0)).select(true,true);
}
}
})
};
})();
///import core
///commands 添加分页功能
///commandsName PageBreak
///commandsTitle 分页
/**
* @description 添加分页功能
* @author zhanyi
*/
(function() {
var editor = baidu.editor,
domUtils = editor.dom.domUtils,
notBreakTags = ['td'];
baidu.editor.plugins['pagebreak'] = function() {
var me = this;
//重写了Editor.hasContents
var hasContentsOrg = me.hasContents;
me.hasContents = function(tags){
for(var i=0,di,divs = me.document.getElementsByTagName('div');di=divs[i++];){
if(domUtils.hasClass(di,'pagebreak')){
return true;
}
}
return hasContentsOrg.call(me,tags);
};
me.commands['pagebreak'] = {
execCommand:function(){
var range = me.selection.getRange();
var div = me.document.createElement('div');
div.className = 'pagebreak';
domUtils.unselectable(div);
//table单独处理
var node = domUtils.findParentByTagName(range.startContainer,notBreakTags,true),
parents = [],pN;
if(node){
switch (node.tagName){
case 'TD':
pN = node.parentNode;
if(!pN.previousSibling){
var table = domUtils.findParentByTagName(pN,'table');
table.parentNode.insertBefore(div,table);
parents = domUtils.findParents(div,true);
}else{
pN.parentNode.insertBefore(div,pN);
parents = domUtils.findParents(div);
}
pN = parents[1];
if(div!==pN){
domUtils.breakParent(div,pN);
}
domUtils.clearSelectedArr(me.currentSelectedArr);
}
}else{
if(!range.collapsed){
range.deleteContents();
var start = range.startContainer;
while(domUtils.isBlockElm(start) && domUtils.isEmptyNode(start)){
range.setStartBefore(start).collapse(true);
domUtils.remove(start);
start = range.startContainer;
}
}
parents = domUtils.findParents(range.startContainer,true);
pN = parents[1];
range.insertNode(div);
pN && domUtils.breakParent(div,pN);
range.setEndAfter(div).setCursor(true,true)
}
},
queryCommandState : function(){
return this.highlight ? -1 :0;
}
}
}
})();
///import core
///commands 加粗,斜体,上标,下标
///commandsName Bold,Italic,Subscript,Superscript
///commandsTitle 加粗,加斜,下标,上标
/**
* b u i等基础功能实现
* @function
* @name baidu.editor.execCommands
* @param {String} cmdName bold加粗。italic斜体。subscript上标。superscript下标。
*/
baidu.editor.plugins['basestyle'] = function(){
var basestyles = {
'bold':['strong','b'],
'italic':['em','i'],
//'underline':['u'],
//'strikethrough':['strike'],
'subscript':['sub'],
'superscript':['sup']
},
domUtils = baidu.editor.dom.domUtils,
getObj = function(editor,tagNames){
var start = editor.selection.getStart();
return domUtils.findParentByTagName( start, tagNames, true )
},
flag = 0,
me = this;
for ( var style in basestyles ) {
(function( cmd, tagNames ) {
me.commands[cmd] = {
execCommand : function( cmdName ) {
var range = new baidu.editor.dom.Range(this.document),obj = '',me = this;
//执行了上述代码可能产生冗余的html代码,所以要注册 beforecontent去掉这些冗余的代码
if(!flag){
this.addListener('beforegetcontent',function(){
domUtils.clearReduent(me.document,['strong','u','em','sup','sub','strike'])
});
flag = 1;
}
//table的处理
if(me.currentSelectedArr && me.currentSelectedArr.length > 0){
for(var i=0,ci;ci=me.currentSelectedArr[i++];){
if(ci.style.display != 'none'){
range.selectNodeContents(ci).select();
//trace:943
!obj && (obj = getObj(this,tagNames));
if(cmdName == 'superscript' || cmdName == 'subscript'){
if(!obj || obj.tagName.toLowerCase() != cmdName)
range.removeInlineStyle(['sub','sup'])
}
obj ? range.removeInlineStyle( tagNames ) : range.applyInlineStyle( tagNames[0] )
}
}
range.selectNodeContents(me.currentSelectedArr[0]).select();
}else{
range = me.selection.getRange();
obj = getObj(this,tagNames);
if ( range.collapsed ) {
if ( obj ) {
var tmpText = me.document.createTextNode('');
range.insertNode( tmpText ).removeInlineStyle( tagNames );
range.setStartBefore(tmpText);
domUtils.remove(tmpText);
} else {
var tmpNode = range.document.createElement( tagNames[0] );
if(cmdName == 'superscript' || cmdName == 'subscript'){
tmpText = me.document.createTextNode('');
range.insertNode(tmpText)
.removeInlineStyle(['sub','sup'])
.setStartBefore(tmpText)
.collapse(true);
}
range.insertNode( tmpNode ).setStart( tmpNode, 0 );
}
range.collapse( true )
} else {
if(cmdName == 'superscript' || cmdName == 'subscript'){
if(!obj || obj.tagName.toLowerCase() != cmdName)
range.removeInlineStyle(['sub','sup'])
}
obj ? range.removeInlineStyle( tagNames ) : range.applyInlineStyle( tagNames[0] )
}
range.select();
}
return true;
},
queryCommandState : function() {
if(this.highlight){
return -1;
}
return getObj(this,tagNames) ? 1 : 0;
}
}
})( style, basestyles[style] );
}
};
///import core
///commands 选区路径
///commandsName ElementPath
///commandsTitle 选区路径
/**
* 选区路径
* @function
* @name baidu.editor.execCommand
* @param {String} cmdName elementpath选区路径
*/
baidu.editor.plugins['elementpath'] = function(){
var domUtils = baidu.editor.dom.domUtils,
currentLevel,
tagNames,
dtd = baidu.editor.dom.dtd,
me = this;
me.commands['elementpath'] = {
execCommand : function( cmdName, level ) {
var me = this,
start = tagNames[level],
range = me.selection.getRange();
me.currentSelectedArr && domUtils.clearSelectedArr(me.currentSelectedArr);
currentLevel = level*1;
if(dtd.$tableContent[start.tagName]){
switch (start.tagName){
case 'TD':me.currentSelectedArr = [start];
start.className = me.options.selectedTdClass;
break;
case 'TR':
var cells = start.cells;
for(var i=0,ti;ti=cells[i++];){
me.currentSelectedArr.push(ti);
ti.className = me.options.selectedTdClass;
}
break;
case 'TABLE':
case 'TBODY':
var rows = start.rows;
for(var i=0,ri;ri=rows[i++];){
cells = ri.cells;
for(var j=0,tj;tj=cells[j++];){
me.currentSelectedArr.push(tj);
tj.className = me.options.selectedTdClass;
}
}
}
start = me.currentSelectedArr[0];
if(domUtils.isEmptyNode(start)){
range.setStart(start,0).setCursor()
}else{
range.selectNodeContents(start).select()
}
}else{
range.selectNode(start).select()
}
},
queryCommandValue : function() {
var start = this.selection.getStart(),
parents = domUtils.findParents(start, true),
names = [];
tagNames = parents;
for(var i=0,ci;ci=parents[i];i++){
if(ci.nodeType == 3) continue;
var name = ci.tagName.toLowerCase();
if(name == 'img' && ci.getAttribute('anchorname')){
name = 'anchor'
}
names[i] = name;
if(currentLevel == i){
currentLevel = -1;
break;
}
}
return names;
}
}
};
///import core
///import commands\removeformat.js
///commands 格式刷
///commandsName FormatMatch
///commandsTitle 格式刷
/**
* 格式刷,只格式inline的
* @function
* @name baidu.editor.execCommand
* @param {String} cmdName formatmatch执行格式刷
*/
baidu.editor.plugins['formatmatch'] = function(){
var me = this,
domUtils = baidu.editor.dom.domUtils,
list = [],img,
flag = 0,
browser = baidu.editor.browser;
this.addListener('reset',function(){
list = [];
flag = 0;
});
function addList(type,evt){
if(browser.webkit){
var target = evt.target.tagName == 'IMG' ? evt.target : null;
}
function addFormat(range){
if(text && (!me.currentSelectedArr || !me.currentSelectedArr.length)){
range.selectNode(text);
}
return range.applyInlineStyle(list[list.length-1].tagName,null,list);
}
me.undoManger && me.undoManger.save();
var range = me.selection.getRange(),
imgT = target || range.getClosedNode();
if(img && imgT && imgT.tagName == 'IMG'){
//trace:964
imgT.style.cssText += ';float:' + (img.style.cssFloat || img.style.styleFloat ||'none') + ';display:' + (img.style.display||'inline');
img = null;
}else{
if(!img){
var collapsed = range.collapsed;
if(collapsed){
var text = me.document.createTextNode('match');
range.insertNode(text).select();
}
me.__hasEnterExecCommand = true;
//不能把block上的属性干掉
//trace:1553
var removeFormatAttributes = me.options.removeFormatAttributes;
me.options.removeFormatAttributes = '';
me.execCommand('removeformat');
me.options.removeFormatAttributes = removeFormatAttributes;
me.__hasEnterExecCommand = false;
//trace:969
range = me.selection.getRange();
if(list.length == 0){
if(me.currentSelectedArr && me.currentSelectedArr.length > 0){
range.selectNodeContents(me.currentSelectedArr[0]).select();
}
}else{
if(me.currentSelectedArr && me.currentSelectedArr.length > 0){
for(var i=0,ci;ci=me.currentSelectedArr[i++];){
range.selectNodeContents(ci);
addFormat(range);
}
range.selectNodeContents(me.currentSelectedArr[0]).select();
}else{
addFormat(range)
}
}
if(!me.currentSelectedArr || !me.currentSelectedArr.length){
if(text){
range.setStartBefore(text).collapse(true);
}
range.select()
}
text && domUtils.remove(text);
}
}
me.undoManger && me.undoManger.save();
me.removeListener('mouseup',addList);
flag = 0;
}
me.commands['formatmatch'] = {
execCommand : function( cmdName ) {
if(flag){
flag = 0;
list = [];
me.removeListener('mouseup',addList);
return;
}
var range = me.selection.getRange();
img = range.getClosedNode();
if(!img || img.tagName != 'IMG'){
range.collapse(true).shrinkBoundary();
var start = range.startContainer;
list = domUtils.findParents(start,true,function(node){
return !domUtils.isBlockElm(node) && node.nodeType == 1
});
//a不能加入格式刷, 并且克隆节点
for(var i=0,ci;ci=list[i];i++){
if(ci.tagName == 'A'){
list.splice(i,1);
break;
}
}
}
me.addListener('mouseup',addList);
flag = 1;
},
queryCommandState : function() {
if(this.highlight){
return -1;
}
return flag;
},
notNeedUndo : 1
}
};
///import core
///commands 查找替换
///commandsName SearchReplace
///commandsTitle 查询替换
///commandsDialog dialogs\searchreplace\searchreplace.html
/**
* @description 查找替换
* @author zhanyi
*/
baidu.editor.plugins['searchreplace'] = function(){
var currentRange,
first,
me = this;
this.addListener('reset',function(){
currentRange = null;
first = null;
});
me.commands['searchreplace'] = {
execCommand : function(cmdName,opt){
var editor = this,
sel = editor.selection,
range,
nativeRange,
num = 0,
opt = baidu.editor.utils.extend(opt,{
all : false,
casesensitive : false,
dir : 1
},true);
if(baidu.editor.browser.ie){
while(1){
var tmpRange;
nativeRange = editor.document.selection.createRange();
tmpRange = nativeRange.duplicate();
tmpRange.moveToElementText(editor.document.body);
if(opt.all){
first = 0;
opt.dir = 1;
if(currentRange){
tmpRange.setEndPoint(opt.dir == -1 ? 'EndToStart' : 'StartToEnd',currentRange)
}
}else{
tmpRange.setEndPoint(opt.dir == -1 ? 'EndToStart' : 'StartToEnd',nativeRange);
if(opt.hasOwnProperty("replaceStr")){
tmpRange.setEndPoint(opt.dir == -1 ? 'StartToEnd' : 'EndToStart',nativeRange);
}
}
nativeRange = tmpRange.duplicate();
if(!tmpRange.findText(opt.searchStr,opt.dir,opt.casesensitive ? 4 : 0)){
currentRange = null;
tmpRange = editor.document.selection.createRange();
tmpRange.scrollIntoView();
return num;
}
tmpRange.select();
//替换
if(opt.hasOwnProperty("replaceStr")){
range = sel.getRange();
range.deleteContents().insertNode(range.document.createTextNode(opt.replaceStr)).select();
currentRange = sel.getNative().createRange();
}
num++;
if(!opt.all)break;
}
}else{
var w = editor.window,nativeSel = sel.getNative(),tmpRange;
while(1){
if(opt.all){
if(currentRange){
currentRange.collapse(false);
nativeRange = currentRange;
}else{
nativeRange = editor.document.createRange();
nativeRange.setStart(editor.document.body,0);
}
nativeSel.removeAllRanges();
nativeSel.addRange( nativeRange );
first = 0;
opt.dir = 1;
}else{
nativeRange = w.getSelection().getRangeAt(0);
if(opt.hasOwnProperty("replaceStr")){
nativeRange.collapse(opt.dir == 1 ? true : false);
}
}
//如果是第一次并且海选中了内容那就要清除,为find做准备
if(!first){
nativeRange.collapse( opt.dir <0 ? true : false);
nativeSel.removeAllRanges();
nativeSel.addRange( nativeRange );
}else{
nativeSel.removeAllRanges();
}
if(!w.find(opt.searchStr,opt.casesensitive,opt.dir < 0 ? true : false) ) {
currentRange = null;
nativeSel.removeAllRanges();
return num;
}
first = 0;
range = w.getSelection().getRangeAt(0);
if(!range.collapsed){
if(opt.hasOwnProperty("replaceStr")){
range.deleteContents();
var text = w.document.createTextNode(opt.replaceStr);
range.insertNode(text);
range.selectNode(text);
nativeSel.addRange(range);
currentRange = range.cloneRange();
}
}
num++;
if(!opt.all)break;
}
}
return true;
}
}
};
///import core
///commands 自定义样式
///commandsName CustomStyle
///commandsTitle 自定义样式
(function() {
var domUtils = baidu.editor.dom.domUtils,
dtd = baidu.editor.dom.dtd;
baidu.editor.plugins['customstyle'] = function() {
var me = this;
me.commands['customstyle'] = {
execCommand : function(cmdName, obj) {
var me = this,
tagName = obj.tag,
node = domUtils.findParent(me.selection.getStart(), function(node) {
return node.getAttribute('label') == obj.label
}, true),
range,bk,tmpObj = {};
for (var p in obj) {
tmpObj[p] = obj[p]
}
delete tmpObj.tag;
if (node && node.getAttribute('label') == obj.label) {
range = this.selection.getRange();
bk = range.createBookmark();
if (range.collapsed) {
domUtils.remove(node, true);
} else {
var common = domUtils.getCommonAncestor(bk.start, bk.end),
nodes = domUtils.getElementsByTagName(common, tagName);
for (var i = 0,ni; ni = nodes[i++];) {
if (ni.getAttribute('label') == obj.label) {
var ps = domUtils.getPosition(ni, bk.start),pe = domUtils.getPosition(ni, bk.end);
if ((ps & domUtils.POSITION_FOLLOWING || ps & domUtils.POSITION_CONTAINS)
&&
(pe & domUtils.POSITION_PRECEDING || pe & domUtils.POSITION_CONTAINS)
)
if (dtd.$block[tagName]) {
var fillNode = me.document.createElement('p');
domUtils.moveChild(ni, fillNode);
ni.parentNode.insertBefore(fillNode, ni);
}
domUtils.remove(ni, true)
}
}
node = domUtils.findParent(common, function(node) {
return node.getAttribute('label') == obj.label
}, true);
if (node) {
domUtils.remove(node, true)
}
}
range.moveToBookmark(bk).select();
} else {
if (dtd.$block[tagName]) {
this.execCommand('paragraph', tagName, tmpObj);
range = me.selection.getRange();
if (!range.collapsed) {
range.collapse();
node = domUtils.findParent(me.selection.getStart(), function(node) {
return node.getAttribute('label') == obj.label
}, true);
var pNode = me.document.createElement('p');
domUtils.insertAfter(node, pNode);
domUtils.fillNode(me.document, pNode);
range.setStart(pNode, 0).setCursor()
}
} else {
range = me.selection.getRange();
if (range.collapsed) {
node = me.document.createElement(tagName);
domUtils.setAttributes(node, tmpObj);
range.insertNode(node).setStart(node, 0).setCursor();
return;
}
bk = range.createBookmark();
range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select()
}
}
},
queryCommandValue : function() {
var startNode = this.selection.getStart(),
parent = domUtils.findParent(startNode, function(node) {
return node.getAttribute('label')
}, true);
return parent ? parent.getAttribute('label') : '';
},
queryCommandState : function() {
return this.highlight ? -1 : 0;
}
};
me.addListener('keyup', function(type, evt) {
var keyCode = evt.keyCode || evt.which;
if (keyCode == 32 || keyCode == 13) {
var range = me.selection.getRange();
if (range.collapsed) {
var node = domUtils.findParent(me.selection.getStart(), function(node) {
return node.getAttribute('label') != ""
}, true);
if (node) {
if (baidu.editor.dom.dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
var p = me.document.createElement('p');
domUtils.insertAfter(node, p);
domUtils.fillNode(me.document, p);
domUtils.remove(node);
range.setStart(p, 0).setCursor();
}
}
}
}
})
}
})();
var baidu = baidu || {};
baidu.editor = baidu.editor || {};
baidu.editor.ui = {};
(function (){
var browser = baidu.editor.browser,
domUtils = baidu.editor.dom.domUtils;
var magic = '$EDITORUI';
var root = window[magic] = {};
var uidMagic = 'ID' + magic;
var uidCount = 0;
var uiUtils = baidu.editor.ui.uiUtils = {
uid: function (obj){
return (obj ? obj[uidMagic] || (obj[uidMagic] = ++ uidCount) : ++ uidCount);
},
hook: function ( fn, callback ) {
var dg;
if (fn && fn._callbacks) {
dg = fn;
} else {
dg = function (){
var q;
if (fn) {
q = fn.apply(this, arguments);
}
var callbacks = dg._callbacks;
var k = callbacks.length;
while (k --) {
var r = callbacks[k].apply(this, arguments);
if (q === undefined) {
q = r;
}
}
return q;
};
dg._callbacks = [];
}
dg._callbacks.push(callback);
return dg;
},
createElementByHtml: function (html){
var el = document.createElement('div');
el.innerHTML = html;
el = el.firstChild;
el.parentNode.removeChild(el);
return el;
},
getViewportElement: function (){
return (browser.ie && browser.quirks) ?
document.body : document.documentElement;
},
getClientRect: function (element){
var bcr = element.getBoundingClientRect();
var rect = {
left: Math.round(bcr.left),
top: Math.round(bcr.top),
height: Math.round(bcr.bottom - bcr.top),
width: Math.round(bcr.right - bcr.left)
};
var doc;
while ((doc = element.ownerDocument) !== document &&
(element = domUtils.getWindow(doc).frameElement)) {
bcr = element.getBoundingClientRect();
rect.left += bcr.left;
rect.top += bcr.top;
}
rect.bottom = rect.top + rect.height;
rect.right = rect.left + rect.width;
return rect;
},
getViewportRect: function (){
var viewportEl = uiUtils.getViewportElement();
var width = (window.innerWidth || viewportEl.clientWidth) | 0;
var height = (window.innerHeight ||viewportEl.clientHeight) | 0;
return {
left: 0,
top: 0,
height: height,
width: width,
bottom: height,
right: width
};
},
setViewportOffset: function (element, offset){
var rect;
var fixedLayer = uiUtils.getFixedLayer();
if (element.parentNode === fixedLayer) {
element.style.left = offset.left + 'px';
element.style.top = offset.top + 'px';
} else {
domUtils.setViewportOffset(element, offset);
}
},
getEventOffset: function (evt){
var el = evt.target || evt.srcElement;
var rect = uiUtils.getClientRect(el);
var offset = uiUtils.getViewportOffsetByEvent(evt);
return {
left: offset.left - rect.left,
top: offset.top - rect.top
};
},
getViewportOffsetByEvent: function (evt){
var el = evt.target || evt.srcElement;
var frameEl = domUtils.getWindow(el).frameElement;
var offset = {
left: evt.clientX,
top: evt.clientY
};
if (frameEl && el.ownerDocument !== document) {
var rect = uiUtils.getClientRect(frameEl);
offset.left += rect.left;
offset.top += rect.top;
}
return offset;
},
setGlobal: function (id, obj){
root[id] = obj;
return magic + '["' + id + '"]';
},
unsetGlobal: function (id){
delete root[id];
},
copyAttributes: function (tgt, src){
var attributes = src.attributes;
var k = attributes.length;
while (k --) {
var attrNode = attributes[k];
if ( attrNode.nodeName != 'style' && attrNode.nodeName != 'class' && (!browser.ie || attrNode.specified) ) {
tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
}
}
if (src.className) {
tgt.className += ' ' + src.className;
}
if (src.style.cssText) {
tgt.style.cssText += ';' + src.style.cssText;
}
},
removeStyle: function (el, styleName){
if (el.style.removeProperty) {
el.style.removeProperty(styleName);
} else if (el.style.removeAttribute) {
el.style.removeAttribute(styleName);
} else throw '';
},
contains: function (elA, elB){
return elA && elB && (elA === elB ? false : (
elA.contains ? elA.contains(elB) :
elA.compareDocumentPosition(elB) & 16
));
},
startDrag: function (evt, callbacks){
var doc = document;
var startX = evt.clientX;
var startY = evt.clientY;
function handleMouseMove(evt){
var x = evt.clientX - startX;
var y = evt.clientY - startY;
callbacks.ondragmove(x, y);
if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.cancelBubble = true;
}
}
if (doc.addEventListener) {
function handleMouseUp(evt){
doc.removeEventListener('mousemove', handleMouseMove, true);
doc.removeEventListener('mouseup', handleMouseMove, true);
callbacks.ondragstop();
}
doc.addEventListener('mousemove', handleMouseMove, true);
doc.addEventListener('mouseup', handleMouseUp, true);
evt.preventDefault();
} else {
var elm = evt.srcElement;
elm.setCapture();
function releaseCaptrue(){
elm.releaseCapture();
elm.detachEvent('onmousemove', handleMouseMove);
elm.detachEvent('onmouseup', releaseCaptrue);
elm.detachEvent('onlosecaptrue', releaseCaptrue);
callbacks.ondragstop();
}
elm.attachEvent('onmousemove', handleMouseMove);
elm.attachEvent('onmouseup', releaseCaptrue);
elm.attachEvent('onlosecaptrue', releaseCaptrue);
evt.returnValue = false;
}
callbacks.ondragstart();
},
getFixedLayer: function (){
var layer = document.getElementById('edui_fixedlayer');
if (layer == null) {
layer = document.createElement('div');
layer.id = 'edui_fixedlayer';
document.body.appendChild(layer);
if (browser.ie && browser.version <= 8) {
layer.style.position = 'absolute';
bindFixedLayer();
setTimeout(updateFixedOffset);
} else {
layer.style.position = 'fixed';
}
layer.style.left = '0';
layer.style.top = '0';
layer.style.width = '0';
layer.style.height = '0';
}
return layer;
},
makeUnselectable: function (element){
if (browser.opera || (browser.ie && browser.version < 9)) {
element.unselectable = 'on';
if (element.hasChildNodes()) {
for (var i=0; i';
}
};
utils.inherits(Separator, UIBase);
})();
///import core
///import uicore
(function (){
var utils = baidu.editor.utils,
domUtils = baidu.editor.dom.domUtils,
UIBase = baidu.editor.ui.UIBase,
uiUtils = baidu.editor.ui.uiUtils;
var Mask = baidu.editor.ui.Mask = function (options){
this.initOptions(options);
this.initUIBase();
};
Mask.prototype = {
getHtmlTpl: function (){
return '';
},
postRender: function (){
var me = this;
domUtils.on(window, 'resize', function (){
setTimeout(function (){
if (!me.isHidden()) {
me._fill();
}
});
});
},
show: function (zIndex){
this._fill();
this.getDom().style.display = '';
this.getDom().style.zIndex = zIndex;
},
hide: function (){
this.getDom().style.display = 'none';
this.getDom().style.zIndex = '';
},
isHidden: function (){
return this.getDom().style.display == 'none';
},
_onMouseDown: function (){
return false;
},
_fill: function (){
var el = this.getDom();
var vpRect = uiUtils.getViewportRect();
el.style.width = vpRect.width + 'px';
el.style.height = vpRect.height + 'px';
}
};
utils.inherits(Mask, UIBase);
})();
///import core
///import uicore
(function () {
var utils = baidu.editor.utils,
uiUtils = baidu.editor.ui.uiUtils,
domUtils = baidu.editor.dom.domUtils,
UIBase = baidu.editor.ui.UIBase,
Popup = baidu.editor.ui.Popup = function (options){
this.initOptions(options);
this.initPopup();
};
var allPopups = [];
function closeAllPopup( el ){
var newAll = [];
for ( var i = 0; i < allPopups.length; i++ ) {
var pop = allPopups[i];
if (!pop.isHidden()) {
if (pop.queryAutoHide(el) !== false) {
pop.hide();
}
}
}
}
Popup.postHide = closeAllPopup;
var ANCHOR_CLASSES = ['edui-anchor-topleft','edui-anchor-topright',
'edui-anchor-bottomleft','edui-anchor-bottomright'];
Popup.prototype = {
SHADOW_RADIUS: 5,
content: null,
_hidden: false,
autoRender: true,
canSideLeft: true,
canSideUp: true,
initPopup: function (){
this.initUIBase();
allPopups.push( this );
},
getHtmlTpl: function (){
return '';
},
getContentHtmlTpl: function (){
if (typeof this.content == 'string') {
return this.content;
}
return this.content.renderHtml();
},
_UIBase_postRender: UIBase.prototype.postRender,
postRender: function (){
if (this.content instanceof UIBase) {
this.content.postRender();
}
this.hide(true);
this._UIBase_postRender();
},
_doAutoRender: function (){
if (!this.getDom() && this.autoRender) {
this.render();
}
},
mesureSize: function (){
var box = this.getDom('content');
return uiUtils.getClientRect(box);
},
fitSize: function (){
var popBodyEl = this.getDom('body');
popBodyEl.style.width = '';
popBodyEl.style.height = '';
var size = this.mesureSize();
popBodyEl.style.width = size.width + 'px';
popBodyEl.style.height = size.height + 'px';
return size;
},
showAnchor: function ( element, hoz ){
this.showAnchorRect( uiUtils.getClientRect( element ), hoz );
},
showAnchorRect: function ( rect, hoz, adj ){
this._doAutoRender();
var vpRect = uiUtils.getViewportRect();
this._show();
var popSize = this.fitSize();
var sideLeft, sideUp, left, top;
if (hoz) {
sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
left = (sideLeft ? rect.left - popSize.width : rect.right);
top = (sideUp ? rect.bottom - popSize.height : rect.top);
} else {
sideLeft = this.canSideLeft && (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
sideUp = this.canSideUp && (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
left = (sideLeft ? rect.right - popSize.width : rect.left);
top = (sideUp ? rect.top - popSize.height : rect.bottom);
}
var popEl = this.getDom();
uiUtils.setViewportOffset(popEl, {
left: left,
top: top
});
domUtils.removeClasses(popEl, ANCHOR_CLASSES);
popEl.className += ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
if(this.editor){
popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex = popEl.style.zIndex - 1;
}
},
showAt: function (offset) {
var left = offset.left;
var top = offset.top;
var rect = {
left: left,
top: top,
right: left,
bottom: top,
height: 0,
width: 0
};
this.showAnchorRect(rect, false, true);
},
_show: function (){
if (this._hidden) {
var box = this.getDom();
box.style.display = '';
this._hidden = false;
// if (box.setActive) {
// box.setActive();
// }
this.fireEvent('show');
}
},
isHidden: function (){
return this._hidden;
},
show: function (){
this._doAutoRender();
this._show();
},
hide: function (notNofity){
if (!this._hidden && this.getDom()) {
// this.getDom().style.visibility = 'hidden';
this.getDom().style.display = 'none';
this._hidden = true;
if (!notNofity) {
this.fireEvent('hide');
}
}
},
queryAutoHide: function (el){
return !el || !uiUtils.contains(this.getDom(), el);
}
};
utils.inherits(Popup, UIBase);
domUtils.on( document, 'mousedown', function ( evt ) {
var el = evt.target || evt.srcElement;
closeAllPopup( el );
} );
domUtils.on( window, 'scroll', function () {
closeAllPopup();
} );
// var lastVpRect = uiUtils.getViewportRect();
// domUtils.on( window, 'resize', function () {
// var vpRect = uiUtils.getViewportRect();
// if (vpRect.width != lastVpRect.width || vpRect.height != lastVpRect.height) {
// closeAllPopup();
// }
// } );
})();
///import core
///import uicore
(function (){
var utils = baidu.editor.utils,
UIBase = baidu.editor.ui.UIBase,
ColorPicker = baidu.editor.ui.ColorPicker = function (options){
this.initOptions(options);
this.noColorText = this.noColorText || '不设置颜色';
this.initUIBase();
};
ColorPicker.prototype = {
getHtmlTpl: function (){
return genColorPicker(
this.noColorText
);
},
_onTableClick: function (evt){
var tgt = evt.target || evt.srcElement;
var color = tgt.getAttribute('data-color');
if (color) {
this.fireEvent('pickcolor', color);
}
},
_onTableOver: function (evt){
var tgt = evt.target || evt.srcElement;
var color = tgt.getAttribute('data-color');
if (color) {
this.getDom('preview').style.backgroundColor = color;
}
},
_onTableOut: function (){
this.getDom('preview').style.backgroundColor = '';
},
_onPickNoColor: function (){
this.fireEvent('picknocolor');
}
};
utils.inherits(ColorPicker, UIBase);
var COLORS = (
'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
'7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,').split(',');
function genColorPicker(noColorText){
var html = '' +
'
' +
'
' +
'
'+ noColorText +'
' +
'
' +
'
' +
'主题颜色 |
'+
'';
for (var i=0; i'+(i==60?'标准颜色 |
':'')+'';
}
html += i<70 ? ' | ':'';
}
html += '
';
return html;
}
})();
///import core
///import uicore
(function (){
var utils = baidu.editor.utils,
uiUtils = baidu.editor.ui.uiUtils,
UIBase = baidu.editor.ui.UIBase;
var TablePicker = baidu.editor.ui.TablePicker = function (options){
this.initOptions(options);
this.initTablePicker();
};
TablePicker.prototype = {
defaultNumRows: 10,
defaultNumCols: 10,
maxNumRows: 20,
maxNumCols: 20,
numRows: 10,
numCols: 10,
lengthOfCellSide: 22,
initTablePicker: function (){
this.initUIBase();
},
getHtmlTpl: function (){
return '' +
'
' +
'
' +
'' +
'更多' +
'
' +
'
' +
'
' +
'
';
},
_UIBase_render: UIBase.prototype.render,
render: function (holder){
this._UIBase_render(holder);
this.getDom('label').innerHTML = '0列 x 0行';
},
_track: function (numCols, numRows){
var style = this.getDom('overlay').style;
var sideLen = this.lengthOfCellSide;
style.width = numCols * sideLen + 'px';
style.height = numRows * sideLen + 'px';
var label = this.getDom('label');
label.innerHTML = numCols + '列 x ' + numRows + '行';
this.numCols = numCols;
this.numRows = numRows;
},
_onMouseOver: function (evt, el){
var rel = evt.relatedTarget || evt.fromElement;
if (!uiUtils.contains(el, rel) && el !== rel) {
this.getDom('label').innerHTML = '0列 x 0行';
this.getDom('overlay').style.visibility = '';
}
},
_onMouseOut: function (evt, el){
var rel = evt.relatedTarget || evt.toElement;
if (!uiUtils.contains(el, rel) && el !== rel) {
this.getDom('label').innerHTML = '0列 x 0行';
this.getDom('overlay').style.visibility = 'hidden';
}
},
_onMouseMove: function (evt, el){
var style = this.getDom('overlay').style;
var offset = uiUtils.getEventOffset(evt);
var sideLen = this.lengthOfCellSide;
var numCols = Math.ceil(offset.left / sideLen);
var numRows = Math.ceil(offset.top / sideLen);
this._track(numCols, numRows);
},
_onClick: function (){
this.fireEvent('picktable', this.numCols, this.numRows);
},
_onMore: function (){
this.fireEvent('more');
}
};
utils.inherits(TablePicker, UIBase);
})();
(function (){
var browser = baidu.editor.browser,
domUtils = baidu.editor.dom.domUtils,
uiUtils = baidu.editor.ui.uiUtils;
var TPL_STATEFUL = 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
( browser.ie ? (
' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
' onmouseleave="$$.Stateful_onMouseLeave(event, this);"' )
: (
' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
' onmouseout="$$.Stateful_onMouseOut(event, this);"' ));
baidu.editor.ui.Stateful = {
alwalysHoverable: false,
Stateful_init: function (){
this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
this.getHtmlTpl = this.Stateful_getHtmlTpl;
},
Stateful_getHtmlTpl: function (){
var tpl = this._Stateful_dGetHtmlTpl();
// 使用function避免$转义
return tpl.replace(/stateful/g, function (){ return TPL_STATEFUL; });
},
Stateful_onMouseEnter: function (evt, el){
if (!this.isDisabled() || this.alwalysHoverable) {
this.addState('hover');
this.fireEvent('over');
}
},
Stateful_onMouseLeave: function (evt, el){
if (!this.isDisabled() || this.alwalysHoverable) {
this.removeState('hover');
this.removeState('active');
this.fireEvent('out');
}
},
Stateful_onMouseOver: function (evt, el){
var rel = evt.relatedTarget;
if (!uiUtils.contains(el, rel) && el !== rel) {
this.Stateful_onMouseEnter(evt, el);
}
},
Stateful_onMouseOut: function (evt, el){
var rel = evt.relatedTarget;
if (!uiUtils.contains(el, rel) && el !== rel) {
this.Stateful_onMouseLeave(evt, el);
}
},
Stateful_onMouseDown: function (evt, el){
if (!this.isDisabled()) {
this.addState('active');
}
},
Stateful_onMouseUp: function (evt, el){
if (!this.isDisabled()) {
this.removeState('active');
}
},
Stateful_postRender: function (){
if (this.disabled && !this.hasState('disabled')) {
this.addState('disabled');
}
},
hasState: function (state){
return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state);
},
addState: function (state){
if (!this.hasState(state)) {
this.getStateDom().className += ' edui-state-' + state;
}
},
removeState: function (state){
if (this.hasState(state)) {
domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state]);
}
},
getStateDom: function (){
return this.getDom('state');
},
isChecked: function (){
return this.hasState('checked');
},
setChecked: function (checked){
if (!this.isDisabled() && checked) {
this.addState('checked');
} else {
this.removeState('checked');
}
},
isDisabled: function (){
return this.hasState('disabled');
},
setDisabled: function (disabled){
if (disabled) {
this.removeState('hover');
this.removeState('checked');
this.removeState('active');
this.addState('disabled');
} else {
this.removeState('disabled');
}
}
};
})();
///import core
///import uicore
///import ui/stateful.js
(function (){
var utils = baidu.editor.utils,
UIBase = baidu.editor.ui.UIBase,
Stateful = baidu.editor.ui.Stateful,
Button = baidu.editor.ui.Button = function (options){
this.initOptions(options);
this.initButton();
};
Button.prototype = {
uiName: 'button',
label: '',
title: '',
showIcon: true,
showText: true,
initButton: function (){
this.initUIBase();
this.Stateful_init();
},
getHtmlTpl: function (){
return '' +
'
' +
'
' +
(this.showIcon ? '
' : '') +
(this.showText ? '
' + this.label + '
' : '') +
'
' +
'
' +
'
';
},
postRender: function (){
this.Stateful_postRender();
},
_onClick: function (){
if (!this.isDisabled()) {
this.fireEvent('click');
}
}
};
utils.inherits(Button, UIBase);
utils.extend(Button.prototype, Stateful);
})();
///import core
///import uicore
///import ui/stateful.js
(function (){
var utils = baidu.editor.utils,
uiUtils = baidu.editor.ui.uiUtils,
domUtils = baidu.editor.dom.domUtils,
UIBase = baidu.editor.ui.UIBase,
Stateful = baidu.editor.ui.Stateful,
SplitButton = baidu.editor.ui.SplitButton = function (options){
this.initOptions(options);
this.initSplitButton();
};
SplitButton.prototype = {
popup: null,
uiName: 'splitbutton',
title: '',
initSplitButton: function (){
this.initUIBase();
this.Stateful_init();
var me = this;
if (this.popup != null) {
var popup = this.popup;
this.popup = null;
this.setPopup(popup);
}
},
_UIBase_postRender: UIBase.prototype.postRender,
postRender: function (){
this.Stateful_postRender();
this._UIBase_postRender();
},
setPopup: function (popup){
if (this.popup === popup) return;
if (this.popup != null) {
this.popup.dispose();
}
popup.addListener('show', utils.bind(this._onPopupShow, this));
popup.addListener('hide', utils.bind(this._onPopupHide, this));
popup.addListener('postrender', utils.bind(function (){
popup.getDom('body').appendChild(
uiUtils.createElementByHtml('')
);
popup.getDom().className += ' ' + this.className;
}, this));
this.popup = popup;
},
_onPopupShow: function (){
this.addState('opened');
},
_onPopupHide: function (){
this.removeState('opened');
},
getHtmlTpl: function (){
return '';
},
showPopup: function (){
// 当popup往上弹出的时候,做特殊处理
var rect = uiUtils.getClientRect(this.getDom());
rect.top -= this.popup.SHADOW_RADIUS;
rect.height += this.popup.SHADOW_RADIUS;
this.popup.showAnchorRect(rect);
},
_onArrowClick: function (event, el){
if (!this.isDisabled()) {
this.showPopup();
}
},
_onButtonClick: function (){
if (!this.isDisabled()) {
this.fireEvent('buttonclick');
}
}
};
utils.inherits(SplitButton, UIBase);
utils.extend(SplitButton.prototype, Stateful, true);
})();
///import core
///import uicore
///import ui/colorpicker.js
///import ui/popup.js
///import ui/splitbutton.js
(function (){
var utils = baidu.editor.utils,
uiUtils = baidu.editor.ui.uiUtils,
ColorPicker = baidu.editor.ui.ColorPicker,
Popup = baidu.editor.ui.Popup,
SplitButton = baidu.editor.ui.SplitButton,
ColorButton = baidu.editor.ui.ColorButton = function (options){
this.initOptions(options);
this.initColorButton();
};
ColorButton.prototype = {
initColorButton: function (){
var me = this;
this.popup = new Popup({
content: new ColorPicker({
noColorText: '清除颜色',
onpickcolor: function (t, color){
me._onPickColor(color);
},
onpicknocolor: function (t, color){
me._onPickNoColor(color);
}
}),
editor:me.editor
});
this.initSplitButton();
},
_SplitButton_postRender: SplitButton.prototype.postRender,
postRender: function (){
this._SplitButton_postRender();
this.getDom('button_body').appendChild(
uiUtils.createElementByHtml('')
);
this.getDom().className += ' edui-colorbutton';
},
setColor: function (color){
this.getDom('colorlump').style.backgroundColor = color;
this.color = color;
},
_onPickColor: function (color){
if (this.fireEvent('pickcolor', color) !== false) {
this.setColor(color);
this.popup.hide();
}
},
_onPickNoColor: function (color){
if (this.fireEvent('picknocolor') !== false) {
this.popup.hide();
}
}
};
utils.inherits(ColorButton, SplitButton);
})();
///import core
///import uicore
///import ui/popup.js
///import ui/tablepicker.js
///import ui/splitbutton.js
(function (){
var utils = baidu.editor.utils,
Popup = baidu.editor.ui.Popup,
TablePicker = baidu.editor.ui.TablePicker,
SplitButton = baidu.editor.ui.SplitButton,
TableButton = baidu.editor.ui.TableButton = function (options){
this.initOptions(options);
this.initTableButton();
};
TableButton.prototype = {
initTableButton: function (){
var me = this;
this.popup = new Popup({
content: new TablePicker({
onpicktable: function (t, numCols, numRows){
me._onPickTable(numCols, numRows);
},
onmore: function (){
me.popup.hide();
me.fireEvent('more');
}
}),
'editor':me.editor
});
this.initSplitButton();
},
_onPickTable: function (numCols, numRows){
if (this.fireEvent('picktable', numCols, numRows) !== false) {
this.popup.hide();
}
}
};
utils.inherits(TableButton, SplitButton);
})();
(function (){
var utils = baidu.editor.utils,
uiUtils = baidu.editor.ui.uiUtils,
UIBase = baidu.editor.ui.UIBase,
Toolbar = baidu.editor.ui.Toolbar = function (options){
this.initOptions(options);
this.initToolbar();
};
Toolbar.prototype = {
items: null,
initToolbar: function (){
this.items = this.items || [];
this.initUIBase();
},
add: function (item){
this.items.push(item);
},
getHtmlTpl: function (){
var buff = [];
for (var i=0; i' +
buff.join('') +
''
},
postRender: function (){
var box = this.getDom();
for (var i=0; i