1.定義js/ueditor/myCustomize/myCustomizeLine.js
UE.registerUI('customizeline',function(editor,uiName){
//建立dialog
var dialog = new UE.ui.Dialog({
//指定彈出層中頁面的路徑
iframeUrl:'js/ueditor/myCustomize/myCustomizeLinePage.html',
//需要指定當前的編輯器執行個體
editor:editor,
//指定dialog的名字
name:uiName,
//dialog的標題
title:"插入自訂XXX效果",
//指定dialog的外圍樣式
cssRules:"width:300px;height:170px;",
//如果給出了buttons就代表dialog有確定和取消
buttons:[
{
className:'edui-okbutton',
label:'確定',
onclick:function () {
dialog.close(true);
}
},
{
className:'edui-cancelbutton',
label:'取消',
onclick:function () {
dialog.close(false);
}
}
]});
//參考myCustomizeLine.js
var btn = new UE.ui.Button({
name:'customizelinebutton',
title: uiName,
//需要添加的額外樣式,指定icon表徵圖,這裡預設使用一個重複的icon
cssRules :'background-position: -500px 0;',
onclick:function () {
//渲染dialog
dialog.render();
dialog.open();
}
});
return btn;
}/*index 指定添加到工具列上的那個位置,預設時追加到最後,editorId 指定這個UI是那個編輯器執行個體上的,預設是頁面上所有的編輯器都會添加這個按鈕*/);
2.定義myCustomizeLinePage.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<div class="content">
<p>
名稱:<input type="text" id="txtName" /><br/>
</p>
</div>
<!--頁面中一定要引入internal.js為了能直接使用當前開啟dialog的執行個體變數-->
<!--internal.js預設是放到dialogs目錄下的-->
<script type="text/javascript" src="../dialogs/internal.js"></script>
<script>
//可以直接使用以下全域變數
//當前開啟dialog的執行個體變數
//console.log('editor: '+editor);
//一些常用工具
//console.log('domUtils: '+domUtils);
//console.log('utils: '+utils);
//console.log('browser: '+browser);
dialog.onok = function (){
console.log("我點擊了確定");
var txtName= document.getElementById("txtName").value;
var insertHtmlStr = '', txtNameAttr='';
if(txtName!= null && txtName!= ""){
txtNameAttr = 'name="'+txtName+'" ';
}
insertHtmlStr = '<input type="text" ' + txtNameAttr+ '/>';
console.log(insertHtmlStr);
editor.execCommand('inserthtml', insertHtmlStr);
};
dialog.oncancel = function () {
//console.log("我點擊了取消");
};
</script>
</body>
</html>
3.在ueditor.config.js中的whitList處添加,input: ['type', 'id', 'name', 'width', 'height', 'title','class', 'style']
4.index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>完整demo</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" charset="utf-8" src="js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="js/ueditor/ueditor.all.min.js"> </script>
<!--建議手動加在語言,避免在ie下有時因為載入語言失敗導致編輯器載入失敗-->
<!--這裡載入的語言檔案會覆蓋你在設定項目裡添加的語言類型,比如你在設定項目裡配置的是英文,這裡載入的中文,那最後就是中文-->
<script type="text/javascript" charset="utf-8" src="js/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="js/ueditor/myCustomize/myCustomizeLine.js"></script>
<style type="text/css">
div{
width:100%;
}
</style>
</head>
<body>
<div>
<h1>完整demo</h1>
<script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
</div>
<div id="btns">
<div>
<button onclick="getAllHtml()">獲得整個html的內容</button>
<button onclick="getContent()">獲得內容</button>
<button onclick="setContent()">寫入內容</button>
<button onclick="setContent(true)">追加內容</button>
<button onclick="getContentTxt()">獲得純文字</button>
<button onclick="getPlainTxt()">獲得帶格式的純文字</button>
<button onclick="hasContent()">判斷是否有內容</button>
<button onclick="setFocus()">使編輯器獲得焦點</button>
<button onmousedown="isFocus(event)">編輯器是否獲得焦點</button>
<button onmousedown="setblur(event)" >編輯器失去焦點</button>
</div>
<div>
<button onclick="getText()">獲得當前選中的文本</button>
<button onclick="insertHtml()">插入給定的內容</button>
<button id="enable" onclick="setEnabled()">可以編輯</button>
<button onclick="setDisabled()">不可編輯</button>
<button onclick=" UE.getEditor('editor').setHide()">隱藏編輯器</button>
<button onclick=" UE.getEditor('editor').setShow()">顯示編輯器</button>
<button onclick=" UE.getEditor('editor').setHeight(300)">設定高度為300預設關閉了自動長高</button>
</div>
<div>
<button onclick="getLocalData()" >擷取草稿箱內容</button>
<button onclick="clearLocalData()" >清空草稿箱</button>
</div>
</div>
<div>
<button onclick="createEditor()">
建立編輯器</button>
<button onclick="deleteEditor()">
刪除編輯器</button>
</div>
<script type="text/javascript">
///////////////////////////////////////////////////////////////////
//建立一個在選中的圖片單擊時添加邊框的外掛程式,其實質就是在baidu.editor.plugins塞進一個閉包
/* UE.plugins["testMyPlugin"] = function () {
var me = this;
//建立一個改變圖片邊框的命令
me.commands["myPluginCmd"] = {
execCommand:function () {
alert(1);
}
};
//註冊一個觸發命令的事件,同學們可以在任意地放綁定觸發此命令的事件
me.addListener( 'click', function () {
setTimeout(function(){
me.execCommand( "myPluginCmd" );
})
} );
}; */
///////////////////////////////////////////////////////////////////
//執行個體化編輯器
//建議使用Factory 方法getEditor建立和引用編輯器執行個體,如果在某個閉包下引用該編輯器,直接調用UE.getEditor('editor')就能拿到相關的執行個體
var ue = UE.getEditor('editor' , {'enterTag':'br'});
function isFocus(e){
alert(UE.getEditor('editor').isFocus());
UE.dom.domUtils.preventDefault(e)
}
function setblur(e){
UE.getEditor('editor').blur();
UE.dom.domUtils.preventDefault(e)
}
function insertHtml() {
var value = prompt('插入html代碼', '');
UE.getEditor('editor').execCommand('insertHtml', value)
}
function createEditor() {
enableBtn();
UE.getEditor('editor');
}
function getAllHtml() {
alert(UE.getEditor('editor').getAllHtml())
}
function getContent() {
var arr = [];
arr.push("使用editor.getContent()方法可以獲得編輯器的內容");
arr.push("內容為:");
arr.push(UE.getEditor('editor').getContent());
alert(arr.join("\n"));
}
function getPlainTxt() {
var arr = [];
arr.push("使用editor.getPlainTxt()方法可以獲得編輯器的帶格式的純文字內容");
arr.push("內容為:");
arr.push(UE.getEditor('editor').getPlainTxt());
alert(arr.join('\n'))
}
function setContent(isAppendTo) {
var arr = [];
arr.push("使用editor.setContent('歡迎使用ueditor')方法可以設定編輯器的內容");
UE.getEditor('editor').setContent('歡迎使用ueditor', isAppendTo);
alert(arr.join("\n"));
}
function setDisabled() {
UE.getEditor('editor').setDisabled('fullscreen');
disableBtn("enable");
}
function setEnabled() {
UE.getEditor('editor').setEnabled();
enableBtn();
}
function getText() {
//當你點擊按鈕時編輯地區已經失去了焦點,如果直接用getText將不會得到內容,所以要在選回來,然後取得內容
var range = UE.getEditor('editor').selection.getRange();
range.select();
var txt = UE.getEditor('editor').selection.getText();
alert(txt)
}
function getContentTxt() {
var arr = [];
arr.push("使用editor.getContentTxt()方法可以獲得編輯器的純文字內容");
arr.push("編輯器的純文字內容為:");
arr.push(UE.getEditor('editor').getContentTxt());
alert(arr.join("\n"));
}
function hasContent() {
var arr = [];
arr.push("使用editor.hasContents()方法判斷編輯器裡是否有內容");
arr.push("判斷結果為:");
arr.push(UE.getEditor('editor').hasContents());
alert(arr.join("\n"));
}
function setFocus() {
UE.getEditor('editor').focus();
}
function deleteEditor() {
disableBtn();
UE.getEditor('editor').destroy();
}
function disableBtn(str) {
var div = document.getElementById('btns');
var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
for (var i = 0, btn; btn = btns[i++];) {
if (btn.id == str) {
UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
} else {
btn.setAttribute("disabled", "true");
}
}
}
function enableBtn() {
var div = document.getElementById('btns');
var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
for (var i = 0, btn; btn = btns[i++];) {
UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
}
}
function getLocalData () {
alert(UE.getEditor('editor').execCommand( "getlocaldata" ));
}
function clearLocalData () {
UE.getEditor('editor').execCommand( "clearlocaldata" );
alert("已清空草稿箱")
}
</script>
</body>
</html>
幾個注意:iframeUrl的設定(在web工程裡要設定路徑,否則可能報404錯誤)
internal.js的設定
index中要引用自訂JS
參考: http://formdesign.leipi.org/doc.html
http://blog.csdn.net/u014224349/article/details/39037141