87,907
社区成员
发帖
与我相关
我的任务
分享
var AceEditor = /^o/.test(typeof exports) ? exports : AceEditor || {};
void function(exports){
/**
* Ace Engine Editor
* 文本编辑器函数
* @see http://code.google.com/p/ace-engine/wiki/AceEditor
* @author 王集鹄(wangjihu,http://weibo.com/zswang)
* @version 1.0
*/
/**
* 设置选择范围
* @param{Element} editor 编辑器(<input>|<textarea>)
* @param{Array|[start,end]} range 选择范围
*/
function setSelectRange(editor, range){
if (!editor) return;
var start = Math.min(range[0], range[1]),
end = Math.max(range[0], range[1]);
editor.focus();
if (editor.setSelectionRange){
editor.setSelectionRange(start, end);
} else if (editor.createTextRange){
var textRange = editor.createTextRange();
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
}
}
/**
* 修改选中处文本
* @param{Element} editor 编辑器(<input>|<textarea>)
* @param{String} value 文本值
*/
function setSelectText(editor, value){
if (!editor) return;
editor.focus();
if (editor.document && editor.document.selection){
var textRange = editor.document.selection.createRange();
textRange.text = value;
textRange.select();
} else if (/^n/.test(typeof editor.selectionStart)){
var str = editor.value,
start = editor.selectionStart,
scroll = editor.scrollTop;
editor.value = str.substr(0, start) + value +
str.substring(editor.selectionEnd, str.length);
editor.selectionStart = start + value.length;
editor.selectionEnd = start + value.length;
editor.scrollTop = scroll;
}
}
function _calcBookmark(bookmark) {
return (bookmark.charCodeAt(0) - 1) + (bookmark.charCodeAt(3) - 1) * 65536 + (bookmark.charCodeAt(2) - 1);
}
function _getSelectPos(editor, isend) {
if (!editor) return;
if (/^n/.test(typeof editor.selectionStart))
return isend ? editor.selectionEnd : editor.selectionStart;
if (!editor.createTextRange || !editor.document) return;
editor.focus();
var doc = editor.document, range = doc.selection.createRange().duplicate();
if (!isend) range.collapse(true)
range.setEndPoint("StartToEnd", range);
var start = doc.body.createTextRange();
start.moveToElementText(editor);
return _calcBookmark(range.getBookmark()) - _calcBookmark(start.getBookmark());
}
function getSelectStart(editor){
return _getSelectPos(editor);
}
function getSelectEnd(editor){
return _getSelectPos(editor, true);
}
/**
* 获取选中范围
* @param{Element} editor 编辑器(<input>|<textarea>)
* @return{Array|[start,end]} 返回选中范围
*/
function getSelectRange(editor){
return [getSelectStart(editor), getSelectEnd(editor)];
}
/**
* 返回当前选中的文字
* @param{Element} editor 编辑器(<input>|<textarea>)
* @return{String} 返回当前选中文字
*/
function getSelectText(editor){
if (!editor) return;
editor.focus();
if (editor.document && editor.document.selection)
return editor.document.selection.createRange().text;
else if ("selectionStart" in editor)
return editor.value.substring(editor.selectionStart, editor.selectionEnd);
}
exports.setSelectRange = setSelectRange;
exports.getSelectRange = getSelectRange;
exports.setSelectText = setSelectText;
exports.getSelectText = getSelectText;
exports.getSelectStart = getSelectStart;
exports.getSelectEnd = getSelectEnd;
}(AceEditor);
//--DEMO--
function g(id){
return document.getElementById(id);
}
void function(){
var esperanto = [
['cx','\u0109'], ['gx','\u011D'],
['hx','\u0125'], ['jx','\u0135'],
['sx','\u015D'], ['ux','\u016D'],
['cX','\u0109'], ['gX','\u011D'],
['hX','\u0125'], ['jX','\u0135'],
['sX','\u015D'], ['uX','\u016D'],
['Cx','\u0108'], ['Gx','\u011C'],
['Hx','\u0124'], ['Jx','\u0134'],
['Sx','\u015C'], ['Ux','\u016C'],
['CX','\u0108'], ['GX','\u011C'],
['HX','\u0124'], ['JX','\u0134'],
['SX','\u015C'], ['UX','\u016C']
];
var dict = {};
for (var i = 0; i < esperanto.length; i++){
dict[esperanto[i][0]] = esperanto[i][1];
}
g('editor').onkeyup = function(e){
e = e || event;
switch(e.keyCode){
case 88: //x
var start = AceEditor.getSelectStart(this);
if (start < 2) break;
var key = this.value.replace(/\r\n?/g, '\n').substr(start - 2, 2),
value = dict[key];
if (value){
AceEditor.setSelectRange(this, [start - 2, start]);
AceEditor.setSelectText(this, value);
}
break;
}
};
}();