最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法詳解

 更新時(shí)間:2020年02月06日 11:07:05   作者:劉博的  
這篇文章主要介紹了CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了CKEditor擴(kuò)展插件實(shí)現(xiàn)自動排版功能的autoformat插件具體定義、配置與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

1.注冊插件

首先找到根目錄下的ckeditor/config.js文件,打開文件如下:

CKEDITOR.editorConfig = function (config) {
 // Define changes to default configuration here. For example:
 // config.language = 'fr';
 // config.uiColor = '#AADC6E';
}; 

我們需要將我們的插件注冊進(jìn)CKEDITOR中。

在方法內(nèi)部加入如下代碼:

config.extraPlugins = "autoformart";

如果值中有其他字符,則用","逗號分隔,增加.

2.創(chuàng)建Plugin.js文件

在Plugins文件下新建一個(gè)與插件名相同的文件夾:aotuformart 的文件夾,意為自動排版。

再在文件夾內(nèi)創(chuàng)建一個(gè)plugin.js文件,因?yàn)樵谧圆寮螅紫燃虞d和執(zhí)行的就是plugin.js這個(gè)文件。

首先我們構(gòu)建一個(gè)自執(zhí)行函數(shù),在自執(zhí)行函數(shù)中添加一個(gè)插件:

(function()
{
 CKEDITOR.plugins.add('autoformat',{
  init:function(editor){
  //初始化操作
  }
 });
})();

添加一個(gè)命令和按鈕在初始化函數(shù)中,如下:

(function()
{
 CKEDITOR.plugins.add('autoformat',{
  init:function(editor){
   editor.addCommand( 'autoformat', new CKEDITOR.autoformatCommand());
   editor.ui.addButton('Autoformat',{label:'自動排版',command:'autoformat',icon:CKEDITOR.getUrl( this.path + 'images/autoformat.png' )});
  }
 });
})();

addCommand方法有兩個(gè)參數(shù):插件命令名稱,第二個(gè)是命令執(zhí)行的方法。

addButton方法的第一個(gè)參數(shù)是:插件的按鈕名稱

    label:鼠標(biāo)懸浮時(shí)插件提示

    command:執(zhí)行插件命令的名稱

    icon:插件圖標(biāo)

所有代碼(上邊的兩塊代碼為演示注冊插件)

//一鍵排版
(function () {
 CKEDITOR.plugins.add('autoformat', {
  requires: ['styles', 'button'],
  init: function (a) {
   a.addCommand('autoformat', CKEDITOR.plugins.autoformat.commands.autoformat);
   a.ui.addButton('autoformat', {
    label: "一鍵排版",
    command: 'autoformat',
    icon: this.path + "images/autoformat.png"
   });
  }
 });
 CKEDITOR.plugins.autoformat = {
  commands: {
   autoformat: {
    exec: function (editor) {
     formatText(editor);
    }
   }
  }
 };
 //格式化
 function formatText(editor) {
  var myeditor = editor;
  if (myeditor.mode == "wysiwyg") {
   var tempimg = new Array();
   var temptable = new Array();
   var tempobject = new Array();
   var isPart = false; //暫時(shí)無法實(shí)現(xiàn)局部格式化
   if (!isPart) {
    var tmpDiv = document.createElement("DIV");
    var editorhtml = myeditor.getData();
    editorhtml = editorhtml.replace(/<div style="page-break-after: always;?">\s*<span style="display: none;?">&nbsp;<\/span>\s*<\/div>/gi, '<p>[ page]</p>'); //將div span標(biāo)簽替換為p 標(biāo)簽
    tmpDiv.innerHTML = editorhtml.replace(/&nbsp;/gi, '').replace(/<div/gi, '<p').replace(/<\/div/gi, '</p');  //移除空格標(biāo)簽,div替換為p標(biāo)簽。
    if (window.navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
     tmpDiv.innerHTML = tmpDiv.innerHTML.replace(/<\/p>/gi, '<br /><\/p>');  //每個(gè)段落相隔一行
    }
    var tables = tmpDiv.getElementsByTagName("TABLE");
    if (tables != null && tables.length > 0) {
     for (var j = 0; j < tables.length; j++) {
      temptable[temptable.length] = tables[j].outerHTML;
     }
     var formattableCount = 0;
     for (var j = 0; j < tables.length;) {
      tables[j].outerHTML = "#FormatTableID_" + formattableCount + "#";
      formattableCount++;
     }
    }
    var objects = tmpDiv.getElementsByTagName("OBJECT");
    if (objects != null && objects.length > 0) {
     for (var j = 0; j < objects.length; j++) {
      tempobject[tempobject.length] = objects[j].outerHTML;
     }
     var formatobjectCount = 0;
     for (var j = 0; j < objects.length;) {
      objects[j].outerHTML = "#FormatObjectID_" + formatobjectCount + "#";
      formatobjectCount++;
     }
    }
    var imgs = tmpDiv.getElementsByTagName("IMG");
    if (imgs != null && imgs.length > 0) {
     for (var j = 0; j < imgs.length; j++) {
      var t = document.createElement("IMG");
      t.alt = imgs[j].alt;
      t.src = imgs[j].src;
      t.width = imgs[j].width;
      t.height = imgs[j].height;
      t.align = imgs[j].align;
      tempimg[tempimg.length] = t;
     }
     var formatImgCount = 0;
     for (var j = 0; j < imgs.length;) {
      imgs[j].outerHTML = "#FormatImgID_" + formatImgCount + "#";
      formatImgCount++;
     }
    }
    var strongarray = new Array();
    var strongcount = 0;
    for (var i = 0; i < tmpDiv.getElementsByTagName('b').length; i++) {
     strongarray[strongcount] = tmpDiv.getElementsByTagName('b')[i].innerText.trim();
     tmpDiv.getElementsByTagName('b')[i].innerHTML = "#FormatStrongID_" + strongcount + "#";
     strongcount++;
    }
    for (var i = 0; i < tmpDiv.getElementsByTagName('strong').length; i++) {
     strongarray[strongcount] = tmpDiv.getElementsByTagName('strong')[i].innerText.trim();
     tmpDiv.getElementsByTagName('strong')[i].innerHTML = "#FormatStrongID_" + strongcount + "#";
     strongcount++;
    }
    var html = processFormatText(tmpDiv.innerText);
    html = html.replace(/<p>\[ page\]<\/p>/gi, '<div style="page-break-after: always;"><span style="display: none;">&nbsp;</span></div>'); //p標(biāo)簽替換回原來的div和span標(biāo)簽。
    if (temptable != null && temptable.length > 0) {
     for (var j = 0; j < temptable.length; j++) {
      var tablehtml = temptable[j];
      html = html.replace("#FormatTableID_" + j + "#", tablehtml);
     }
    }
    if (tempobject != null && tempobject.length > 0) {
     for (var j = 0; j < tempobject.length; j++) {
      var objecthtml = "<p align=\"center\">" + tempobject[j] + "</p>";
      html = html.replace("#FormatObjectID_" + j + "#", objecthtml);
     }
    }
    if (tempimg != null && tempimg.length > 0) {
     for (var j = 0; j < tempimg.length; j++) {
      var imgheight = "";
      var imgwidth = "";
      if (tempimg[j].height != 0)
       imgheight = " height=\"" + tempimg[j].height + "\"";
      if (tempimg[j].width != 0)
       imgwidth = " width=\"" + tempimg[j].width + "\"";
      var imgalign = "";
      if (tempimg[j].align != "")
       imgalign = " align=\"" + tempimg[j].align + "\"";
      var imghtml = "<p align=\"center\"><img src=\"" + tempimg[j].src + "\" alt=\"" + tempimg[j].alt + "\"" + imgwidth + " " + imgheight + " align=\"" + tempimg[j].align + "\" border=\"0\"></p>";
      html = html.replace("#FormatImgID_" + j + "#", imghtml);
     }
    }
    for (var i = 0; i < strongcount; i++) {
     html = html.replace("#FormatStrongID_" + i + "#", "<p><strong>" + strongarray[i] + "</strong></p>");
    }
    while (html.indexOf("</p></p>") != -1) html = html.replace("</p></p>", "</p>");
    while (html.indexOf('<p><p align="center">') != -1) html = html.replace('<p><p align="center">', '<p align="center">');
    editor.setData(html);
   } else {
   }
  } else {
   alert('必須在設(shè)計(jì)模式下操作!');
  }
 }
 function processFormatText(textContext) {
  var text = dbc2Sbc(textContext);
  var prefix = "";
  var tmps = text.split("\n");
  var html = "";
  for (var i = 0; i < tmps.length; i++) {
   var tmp = tmps[i].trim();
   if (tmp.length > 0) {
    var reg = /#Format[A-Za-z]+_\d+#/gi;
    var f = reg.exec(tmp);
    if (f != null) {
     tmp = tmp.replace(/#Format[A-Za-z]+_\d+#/gi, '');
     html += f;
     if (tmp != "")
      html += "<p align=\"center\">" + tmp + "</p>\n";
    } else {
     html += "<p style='text-indent:2em;'>" + tmp + "</p>\n";
    }
   }
  }
  return html;
 }
 function dbc2Sbc(str) {
  var result = '';
  for (var i = 0; i < str.length; i++) {
   var code = str.charCodeAt(i);
   // “65281”是“!”,“65373”是“}”,“65292”是“,”。不轉(zhuǎn)換","
   if (code >= 65281 && code < 65373 && code != 65292 && code != 65306) {
    // “65248”是轉(zhuǎn)換碼距
    result += String.fromCharCode(str.charCodeAt(i) - 65248);
   } else {
    result += str.charAt(i);
   }
  }
  return result;
 }
 String.prototype.trim = function () {
  return this.replace(/(^[\s ]*)|([\s ]*$)/g, "");
 };
 String.prototype.leftTrim = function () {
  return this.replace(/(^\s*)/g, "");
 };
 String.prototype.rightTrim = function () {
  return this.replace(/(\s*$)/g, "");
 };
})();

3、配置到菜單中

例basic模式:

['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],['Maximize']

改為

['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],['Maximize','autoformat']

4、圖標(biāo)

當(dāng)前占位已經(jīng)實(shí)現(xiàn),但由于沒有圖標(biāo),顯示上會有問題,此時(shí)自己找或制作一個(gè)圖標(biāo),放到autoformat/images/下命名為autoformat.png

借用某編輯器的:

如未生效,記得清除cookie或更換瀏覽器查看顯示效果。

5、效果對比

希望本文所述對大家CKEDitor富文本編輯器開發(fā)有所幫助。

相關(guān)文章

  • 頁面使用密碼保護(hù)代碼

    頁面使用密碼保護(hù)代碼

    這是一個(gè)由JS實(shí)現(xiàn)的網(wǎng)頁密碼保護(hù)代碼,在進(jìn)入網(wǎng)頁前需要在彈出框中輸入密碼才可以,不過現(xiàn)在不怎么用了,一般情況下,目前都在后臺處理這種功能,用戶輸入用戶名和密碼后交給服務(wù)器處理,然后再返回信息,若登錄無誤就可看到某些內(nèi)容
    2013-04-04
  • JavaScript中最常用的10種代碼簡寫技巧總結(jié)

    JavaScript中最常用的10種代碼簡寫技巧總結(jié)

    這篇文章主要總結(jié)了JavaScript中最常用的10種代碼簡寫技巧的相關(guān)資料,其中包括三元操作符、短路求值簡寫方式、聲明變量簡寫方法、if存在條件簡寫方法及JavaScript循環(huán)簡寫方法等等,分別給出了詳細(xì)的示例代碼供大家參考,需要的朋友們下面來一起看看吧。
    2017-06-06
  • uniapp踩坑實(shí)戰(zhàn)之文件查找失敗:'uview-ui'?at?main.js解決辦法

    uniapp踩坑實(shí)戰(zhàn)之文件查找失敗:'uview-ui'?at?main.js解決辦法

    這篇文章主要給大家介紹了關(guān)于uniapp踩坑實(shí)戰(zhàn)之文件查找失敗:'uview-ui'?at?main.js的解決辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • javascript SocialHistory 檢查訪問者是否訪問過某站點(diǎn)

    javascript SocialHistory 檢查訪問者是否訪問過某站點(diǎn)

    今天delicious上這個(gè)名為 SocialHistory 的腳本十分引人注目。源代碼可以在這里下載。這段js代碼的功能就是判斷你的用戶有沒有訪問過某個(gè)網(wǎng)站。使用方法很簡單,例如:
    2008-08-08
  • JS co 函數(shù)庫的含義和用法實(shí)例總結(jié)

    JS co 函數(shù)庫的含義和用法實(shí)例總結(jié)

    這篇文章主要介紹了JS co 函數(shù)庫的含義和用法,結(jié)合實(shí)例形式總結(jié)分析了JS co 函數(shù)庫的基本含義、功能、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • javascript實(shí)現(xiàn)點(diǎn)擊提交按鈕后顯示loading的方法

    javascript實(shí)現(xiàn)點(diǎn)擊提交按鈕后顯示loading的方法

    這篇文章主要介紹了javascript實(shí)現(xiàn)點(diǎn)擊提交按鈕后顯示loading的方法,涉及javascript動態(tài)設(shè)置頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • JS定時(shí)器使用,定時(shí)定點(diǎn),固定時(shí)刻,循環(huán)執(zhí)行詳解

    JS定時(shí)器使用,定時(shí)定點(diǎn),固定時(shí)刻,循環(huán)執(zhí)行詳解

    下面小編就為大家?guī)硪黄狫S定時(shí)器使用,定時(shí)定點(diǎn),固定時(shí)刻,循環(huán)執(zhí)行詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • JavaScript點(diǎn)擊按鈕后彈出透明浮動層的方法

    JavaScript點(diǎn)擊按鈕后彈出透明浮動層的方法

    這篇文章主要介紹了JavaScript點(diǎn)擊按鈕后彈出透明浮動層的方法,可實(shí)現(xiàn)點(diǎn)擊按鈕彈出居中的透明浮動層的效果,涉及javascript操作鼠標(biāo)事件及頁面樣式的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 精通Javascript系列之?dāng)?shù)值計(jì)算

    精通Javascript系列之?dāng)?shù)值計(jì)算

    在JS中如果希望某個(gè)變量包含一個(gè)數(shù)值,那么無需限定其必須是整數(shù)或者是浮點(diǎn)數(shù),下面來個(gè)例子
    2011-06-06
  • js使用文件流下載csv文件的實(shí)現(xiàn)方法

    js使用文件流下載csv文件的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于js使用文件流下載csv文件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評論

博罗县| 南京市| 岳西县| 冀州市| 平安县| 茌平县| 木兰县| 响水县| 中阳县| 永德县| 宝兴县| 玉林市| 沁水县| 会同县| 朔州市| 汝南县| 安康市| 汝州市| 石屏县| 全南县| 怀集县| 三穗县| 奉新县| 海盐县| 宁阳县| 中方县| 望谟县| 上栗县| 侯马市| 建阳市| 博罗县| 灵山县| 洛宁县| 东台市| 汾西县| 泽库县| 水城县| 昌乐县| 吉安县| 长春市| 陆丰市|