在UEditor编辑器的工具栏上加一行文字或按钮
 微wx笑
微wx笑 2022-01-23【网页网站】
 2022-01-23【网页网站】 260
260 3
 3 0关键字:
UEditor  编辑器
 0关键字:
UEditor  编辑器  
有时候我们可能需要在UEditor编辑器的工具栏上加一行文字或按钮,用来提示一些信息,当前一些耗时操作的状态,比如远程图片的抓取。
之前遇到一个问题:百度UEditor粘贴内容包含大量远程图片抓取失败的问题
问题虽然解决了,但是并不完美,很多状态提示信息是通过控制台输出的,不打开开发者工具根本看不到,使用体验比较差,于是就想到在工具栏上添加一个输出提示信息的功能。
完成后的效果:


示例代码:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <!DOCTYPE HTML><html><head>    <meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>    <title></title>    <script type="text/javascript"charset="utf-8"src="../ueditor.config.js"></script>    <script type="text/javascript"charset="utf-8"src="editor_api.js"></script>    <script type="text/javascript"charset="utf-8"src="../../js/jquery-1.9.1.min.js"></script></head><body>    <h1>UEditor简单功能</h1>    <!--style给定宽度可以影响编辑器的最终宽度-->    <script type="text/plain"id="myEditor">        <p>这里我可以写一些输入提示</p>    </script>    <script type="text/javascript">        varue = UE.getEditor('myEditor',{            //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个            toolbars:[['insertimage']],            //focus时自动清空初始化时的内容            autoClearinitialContent:true,            //关闭字数统计            wordCount:false,            //关闭elementPath            elementPathEnabled:false,            //默认的编辑区域高度            initialFrameHeight:300            //更多其他参数,请参考ueditor.config.js中的配置项        });        ue.ready(function() {            $(".edui-toolbar").append('<div style="float:left;">请点击按钮上传图片</div>');        });    </script></body></html> | 
有时候可能不只是需要添加文字的提示信息,还需要添加按钮操作,以下代码是写在UEditor代码内部的:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |         functioncatchremoteimageGetTip(){          varupload_tip = $("#edui_upload_tip");          if(upload_tip.length == 0){            $(".edui-toolbar").append('<div id="edui_upload_tip" class="edui-box edui-default" style="padding: 5px;"></div>');            upload_tip = $("#edui_upload_tip");          }          returnupload_tip;        }                functioncatchremoteimageRetry(){          varupload_tip = catchremoteimageGetTip();          if(upload_tip.length > 0){             varretry = $("<a>重试</a>");             $(retry).click(function(){ me.fireEvent("catchRemoteImage"); });             upload_tip.append(" ");             upload_tip.append(retry);          }else{            console.log("catchremoteimageTip,无法获取或创建edui_upload_tip引用");           }        } | 
代码的功能是在工具栏的后面添加一个id="edui_upload_tip"的div用于显示提示信息,
如果调用“catchremoteimageRetry()”,还可以在后面添加一个重试的按钮,功能是重新调用远程图片的抓取。
下面的代码是提示信息的处理:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |         functioncatchremoteimageTip(tipStr, callbackInfo) {          //本方法用于在工具栏上显示抓取远程图片的提示信息          if(typeoftipStr == 'undefined'){ return; }                    varupload_tip = catchremoteimageGetTip();          if(upload_tip.length > 0){            if(typeofcallbackInfo == 'undefined'){              upload_tip.text(tipStr);             }else{                if(tipStr.length > 0){                  tipStr += ",";                 }                if(callbackInfo.state && callbackInfo.state == 'SUCCESS'){                   varlistCount = 0;                   for(vari = 0; i < callbackInfo.list.length; i++){                     if(callbackInfo.list[i].state == "SUCCESS"){                        listCount++;                     }                   }                   remoteImagesLength -= listCount;                   if(listCount == callbackInfo.list.length){                     if(remoteImagesLength < 1){                       upload_tip.text(tipStr+ "本次成功"+ listCount + "张,全部抓取完成");                     }else{                       upload_tip.text(tipStr+ "本次成功"+ listCount + "张,剩余"+ remoteImagesLength + "张");                     }                   }else{                     upload_tip.text(tipStr+ "本次成功"+ listCount + "张,失败"+ (callbackInfo.list.length - listCount) + "张");                   }                }else{                   upload_tip.text(tipStr+ "服务器返回失败,详情请查看控制台输出");                }            }          }else{            console.log("catchremoteimageTip,无法获取或创建edui_upload_tip引用");           }        } | 
抓取事件处理函数
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |     me.addListener("catchRemoteImage", function() {        varcatcherLocalDomain = me.getOpt('catcherLocalDomain'),            catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),            catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),            catcherFieldName = me.getOpt('catcherFieldName');        varremoteImages = [],            imgs = domUtils.getElementsByTagName(me.document, "img"),            test = function(src, urls) {                if(src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {                    returntrue;                }                if(urls) {                    for(varj = 0, url; url = urls[j++];) {                        if(src.indexOf(url) !== -1) {                            returntrue;                        }                    }                }                returnfalse;            };        for(vari = 0, ci; ci = imgs[i++];) {            if(ci.getAttribute("word_img")) {                continue;            }            varsrc = ci.getAttribute("_src") || ci.src || "";            if(/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {                remoteImages.push(src);            }        }                catchremoteimageTip(remoteImages.length + " 张图片待抓取");                if(remoteImages.length) {            console.log("remoteImages.length",remoteImages.length);            console.log("开始分段抓取远程图片......");            if(remoteImagesLength == 0){              catchremoteimageTip("开始分段抓取,总计"+ remoteImages.length + "张");              remoteImagesLength = remoteImages.length;            }else{              catchremoteimageTip("分段抓取中,剩余"+ remoteImages.length + "张");            }                        varpk = 0;            //while (pk < remoteImages.length){              varimgPart = [],pkBegin = pk;              for(vark = 0; k < 5; k++){                if(pk < remoteImages.length){                  imgPart.push(remoteImages[pk]);                  pk++;                }              }              console.log("imgPartBegin", pkBegin, "End", pk);              if(imgPart.length) {                console.log("imgPart", imgPart);                catchremoteimage(imgPart, {                  //成功抓取                  success: function(r) {                      try{                          varinfo = r.state !== undefined ? r:eval("("+ r.responseText + ")");                          console.log("info", info);                          catchremoteimageTip("", info);                      } catch(e) {                          catchremoteimageTip("r.responseText转换json对象失败");                          catchremoteimageRetry();                          console.log(e);                          return;                      }                      /* 获取源路径和新路径 */                      vari, j, ci, cj, oldSrc, newSrc, list = info.list,succecsCount = 0;                      for(i = 0; ci = imgs[i++];) {                          oldSrc = ci.getAttribute("_src") || ci.src || "";                          for(j = 0; cj = list[j++];) {                              if(oldSrc == cj.source.replace(/&/ig, "&") && cj.state == "SUCCESS") {  //抓取失败时不做替换处理                                  succecsCount++;                                  newSrc = catcherUrlPrefix + cj.url;                                  domUtils.setAttributes(ci, {                                      "src": newSrc,                                      "_src": newSrc                                  });                                  break;                              }                          }                      }                      me.fireEvent('catchremotesuccess');                      if(succecsCount == 0){                        catchremoteimageTip("本次抓取全部失败");                        catchremoteimageRetry();                      }else{                        setTimeout(function(){                          me.fireEvent("catchRemoteImage");                        },2000);                      }                  },                  //回调失败,本次请求超时                  error: function() {                      me.fireEvent("catchremoteerror");                      catchremoteimageTip("图片抓取失败,请求超时");                      catchremoteimageRetry();                  }                });              }            //}        }else{          remoteImagesLength = 0;        } | 
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0  许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/web/2022-01-23/966.html








