【问题标题】:CLEditor, pasting content as text functionality in main windowCLEditor,在主窗口中将内容粘贴为文本功能
【发布时间】:2012-04-12 09:59:17
【问题描述】:

我正在为我的文本编辑器使用 CLEditor,http://premiumsoftware.net/cleditor/。有一个“粘贴为文本”的功能,当单击按钮时,会出现一个弹出窗口,其中包含一个文本区域,用于粘贴您的内容并删除样​​式。

我想做的是采用相同的功能,但只要有人粘贴到主文本区域,此操作就会自动执行。我将如何触发它?

我创建了一个 JS Fiddle,http://jsfiddle.net/helpinspireme/naubS/,但无法将所有 JS 粘贴到那里,以便链接到 CLEditor 的主 JS 文件,请访问他们的网站:http://premiumsoftware.net/cleditor/

感谢您的帮助。

【问题讨论】:

    标签: javascript cleditor


    【解决方案1】:

    我知道我没有回答您的实际问题,但如果您的 真正 问题是尝试从 Microsoft Office(例如 Word)粘贴文本的用户产生的垃圾,我会推荐考虑替代解决方案。

    CLEditor 本身可以在 iFrame(富文本模式)和 textarea(源模式)之间切换。 “粘贴为文本”功能使用了不支持富文本的 textarea,因此它首先不允许垃圾 html。

    但是,如果编辑器处于富文本模式,则很难阻止用户从 Word 粘贴(他可以使用常规粘贴按钮、按 CTRL-V 甚至使用鼠标右键上下文菜单,它们是所有不同的事件并且难以使用 javascript 拦截)。所以现在损害已经造成;您的编辑器中有凌乱的 html。因此,我没有尝试清理 Word 产生的垃圾,而是在保存捕获的输入时执行了以下检查(javascript):

    if(clEditorValue && (clEditorValue.indexOf('<!--') !== -1 || clEditorValue.indexOf('mso-ansi-language') !== -1 ) ) {
      alert('Unable to process text pasted from Word, please use "Paste as text" or modify your input');
      return;
    }
    

    我希望这个答案对其他试图实现相同目标的人有用。

    【讨论】:

      【解决方案2】:

      当粘贴到 cleditor 中时,编辑器会从源代码中提取所有 html。我最终编辑了 jquery.cleditor.js 并向 $doc.click(hidePopups) 函数添加了一个绑定函数。我使用 setTimeouts 允许文本填充输入并完成 updateTextArea 函数。

          .bind("paste", function() {
                  setTimeout(function() {
                      refreshButtons(editor);
                      updateTextArea(editor, true);
                      //remove any and all html from the paste action
                      setTimeout(function() {
                          //clean up the html with removeHtml function
                          $(editor.doc.body).html(removeHtml($(editor.doc.body).html()));
      
                          //change the input value with new clean string
                          $("#" + editor.$area[0].id).val($(editor.doc.body).html());
                      }, 100);
                  }, 100);
              })
      

      我使用下面的 removeHtml 函数从粘贴的内容中删除所有 HTML。

      //remove html altogether
      function removeHtml(str) {
          var regex = /(<([^>]+)>)/ig;
          var result = str.replace(regex, "");
          return result;
      }
      

      此解决方案现已投入生产。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        相关资源
        最近更新 更多