1. 程式人生 > >在Ueditor 1.4.3中自定義外掛

在Ueditor 1.4.3中自定義外掛

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" );
            })

        } );
    }; */
      ///////////////////////////////////////////////////////////////////
       
    //例項化編輯器
    //建議使用工廠方法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

相關推薦

Ueditor 1.4.3定義外掛

1.定義js/ueditor/myCustomize/myCustomizeLine.js UE.registerUI('customizeline',function(editor,uiName){     //建立dialog     var dialog = new

網頁文字編輯器UEditor.1.4.3 jsp使用配置

網頁文字編輯器UEditor.1.4.3 jsp使用配置 由於專案需要文字編輯的功能,研究了百度ueditor編輯器,實現線上富文字的編輯和圖片檔案的上傳。 首先需要配置好jsp開發的基本環境,即jee-eclipse(IDE)、JRE、Tomcat(JSP伺服器),網上資

vue-cli 3.x 定義外掛併發布到 npm

乾貨轉載——https://www.cnblogs.com/wisewrong/archive/2018/12/28/10186611.html 全是知識點吶 趕緊記下來啊 一、調整專案結構 首先用 vue-cli 建立一個 default 專案 // 順便安利一篇文章《Vue 爬坑之路(十二)——

Ueditor 1.4.3 單獨呼叫上傳圖片,或檔案功能

第一步, 引入檔案 <script src="ueditor/ueditor.config.js" type="text/javascript" charset="utf-8"></script> <script src="u

最新ueditor1.4.3.3)版本修改定義上傳路徑

專案需求:例如tomcat在c盤,檔案上傳到d盤,縱觀ueditor的文件說明沒有該功能,預設都上傳的專案的路徑下,要想修改此功能則必須修改ueditor的java原始碼和js原始碼。先上修改後的效果圖,要實現這樣的功能,修改的地方比較多,個人建議最好還是別改了(如果不想研

第一行程式碼 3.4.2 建立定義控制元件 章節初上手出項的下載完成後閃退問題和定義控制元件無反應問題

關於出項下載後閃退並且開啟app時也閃退的問題,主要是xml檔案出錯,一般情況按照書中的流程title.xml檔案是沒有錯誤的,主要原因在於 activity_main.xm了檔案中,直接說程式碼 <RelativeLayout xmlns:android="http://schema

UEditor (JSP 1.4.3版本) 整合到ssh框架

     通過以上配置,ueditor基本上就可以使用了。 二:圖片顯示問題 1,如果ueditor圖片上傳失敗,提示“未找到上傳資料”    這時要檢查專案的jar包,刪除common-io-1.4.jar 即可上傳成功 2,如果ueditor圖片上傳成功,但是顯示不出來    這個問題就是讀取圖片

第一行程式碼 3.4.2 建立定義控制元件 章節初上手出項的下載完成後閃退問題和定義控制元件無反應問題

關於出項下載後閃退並且開啟app時也閃退的問題,主要是xml檔案出錯,一般情況按照書中的流程title.xml檔案是沒有錯誤的,主要原因在於 activity_main.xm了檔案中,直接說程式碼 <RelativeLayout xmlns:android="http

Laravel5.4定義404等錯誤頁面

dtd app top exc auto get 1.0 存在 href 1.在resources/views/下簡歷文件夾error,在error文件中建立"404.blade.php文件"。    1 <!DOCTYPE html PUBLIC "-//W3C

1 .net定義事件的步驟

sender soc size etc utf nbsp 並且 ram void 1 申明一個自定義的類並且繼承事件的基類 public class ClientSocketModelConnectedEvent:EventArgs { priv

6.1 如何在spring定義xml標簽

關聯 tex and 啟動流程 調用 mls ram 如果 .com dubbo自定義了很多xml標簽,例如<dubbo:application>,那麽這些自定義標簽是怎麽與spring結合起來的呢?我們先看一個簡單的例子。 一 編寫模型類 1 packa

laravel5.4定義ajax請求響應類

accep 是把 post pos 放置 source 接收 尋找 但是 本人小白在it界混跡一年之久暫時沒有什麽成績只是把所以到的和大家分享一下,在工作和學習的過程中一直追求美觀,但是對於我來說畢竟實力有限,每當遇到一些返回或者是網站或者項目中的返回給用戶的友好提示的時

Zabbix 3.4.11版本 定義監控項

linux con 進行 獲取信息 腳本 server linu 監控 conf 一、實驗思路過程 創建項目、觸發器、圖形,驗證監控效果; Template OS Linux 模板基本涵蓋了所有系統層面的監控,包括了我們最關註的 幾項:ping、load、cpu 使用率、m

Cordova與現有框架的結合,Cordova外掛使用教程,Cordova定義外掛,框架整合Cordova,將Cordova整合到現有框架

 一、框架整合cordova 將cordova整合到現有框架中 一般cordova工程是通過CMD命令來建立一個工程並新增Android、ios等平臺,這樣的建立方式可以完整的下載開發過程中所需要的的外掛。也是最方便和快捷一種方式。因此我們需要用這種方式將我們現有的框架放入到已建好的cordov

Asp.net MVC Preview 4 定義Jquery的HtmlHelper擴充套件

using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; us

Andrid 7.1 啟動init.rc定義service

平臺: RK3288 + Android7.12 問題: 無法啟動init.rc中新增的服務. 步驟: 新增服務 |–system/extras/info-service/Android.mk LOCAL_PATH:= $(call my-dir) includ

vue定義元件(外掛

在vue專案中,可以自定義元件像vue-resource一樣使用Vue.use()方法來使用,具體實現方法: 1、首先建一個自定義元件的資料夾,比如叫loading,裡面有一個index.js,還有一個自定義元件loading.vue,在這個loading.v

ueditor1.4.3)的使用

1.新增相關檔案 2.匯入jar包以及相關Java類 3.圖片上傳配置 4.多圖片線上管理controller.jsp修改成如下: <%@ page language="java" contentType="text/html; charset=UTF-8"

Ueditor圖片上傳設定(1.4.3 JSP版本)

最近研究了一下Ueditor,發現圖片上傳功能不能直接使用,需要修改一些東西,記下來以備參考。 1. 修改com.baidu.ueditor.hunter.FileManager類下的一個方法,修改如下: 原始碼: private String getPath ( File

百度ueditor富文字--定義外掛按鈕

我們在在之前的文章中講了 百度ueditor富文字 的 配置和初始化的方法。我們可以給它配置更多的外掛,全部外掛可參考官網:如果官網提供的外掛仍不能滿足我們的需要時,則可以自定義外掛按鈕。比如 我們這裡