HTML 注释(选择、突出显示、删除格式)

发布于 2024-08-14 20:37:15 字数 2361 浏览 9 评论 0原文

我正在开发一个基于跨浏览器网络的注释工具集,它允许用户选择网页的任何部分

  • HIGHLIGHT,如果您选择:

约翰是

  • 转储
  • 结果

    约翰是

  • dump
    • 删除格式:如果您选择:

    约翰

    来自

    约翰是

  • dump
  • 结果

    ;约翰

  • dump
    • 撤消/重做:很高兴有功能

    为了能够撤消和重做所执行的操作,

    我有一个用于突出显示的部分解决方案

    function highlight(colour) {
    var range, sel;
    if (document.selection && (!window.getSelection)) {
     // IE case
        range = document.selection.createRange();  
        range.execCommand("BackColor", false, colour);  
    } else if (window.getSelection) {    
    // Non-IE case 
        sel = window.getSelection();
        if (sel.getRangeAt) {
            range = sel.getRangeAt(0);
        }
    //without designmode=on, you can't highlight the selected html (chrome)
        document.designMode = "on";
        if (range) {
            sel.removeAllRanges();
            sel.addRange(range);
        }
        // HiliteColor avoids FF3.5 from painting the background of the whole block
        if (!document.execCommand("HiliteColor", false, colour) ) {
            document.execCommand("BackColor", false, colour);
        }
        document.designMode = "off";
    } 
    

    }

    由于我的要求与富文本编辑器有很多相似之处,所以我研究了 ckeditor 和(广泛)谷歌闭包编辑器的代码。我对它们都放弃了希望,因为它们只能在可编辑的 iframe 中工作。我的要求之一是不允许用户更改原始文本,而只能添加自己的注释(突出显示、内联注释等)。

    我很乐意在这里提出您的所有意见,如果可能的话,请为我指明正确的方向。

    ——初尚

    I am working on a cross browser web based annotation toolset, which allows the users to select any part of a web page

    • HIGHLIGHT, if you select:

    john is <li>big</li> <li>dump</li>

    Result

    <span style="background-color: rgb(106, 168, 79)">john is</span>
    <li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>

    • REMOVE FORMAT: if you select:

    john

    from

    <span style="background-color: rgb(106, 168, 79)">john is</span>
    <li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>

    Result

    <span style="background-color: rgb(106, 168, 79)"></span> john <span style="background-color: rgb(106, 168, 79)">is</span>
    <li><span style="background-color: rgb(106, 168, 79)">big</span></li> <li><span style="background-color: rgb(106, 168, 79)">dump</span></li>

    • UNDO/REDO: nice to have feature

    To be able to undo and redo the actions performed

    I have a partial solution for highlighting

    function highlight(colour) {
    var range, sel;
    if (document.selection && (!window.getSelection)) {
     // IE case
        range = document.selection.createRange();  
        range.execCommand("BackColor", false, colour);  
    } else if (window.getSelection) {    
    // Non-IE case 
        sel = window.getSelection();
        if (sel.getRangeAt) {
            range = sel.getRangeAt(0);
        }
    //without designmode=on, you can't highlight the selected html (chrome)
        document.designMode = "on";
        if (range) {
            sel.removeAllRanges();
            sel.addRange(range);
        }
        // HiliteColor avoids FF3.5 from painting the background of the whole block
        if (!document.execCommand("HiliteColor", false, colour) ) {
            document.execCommand("BackColor", false, colour);
        }
        document.designMode = "off";
    } 
    

    }

    Since my requirements had much similarity with a richtext editor, I looked into the codes of ckeditor and (extensively) in google closures editor. I have given up hope in both of them because, they work in only an editable iframe. One of my requirement is that users are not allowed to change the original text but only to add there own annotations(highlight, inline notes,etc).

    I would love to here all of your opinions and if possible to point me to the right direction.

    --Choesang

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    挽梦忆笙歌 2024-08-21 20:37:15

    我认为你仍然可以使用“富文本编辑器”方式(iframe),但然后尝试捕获“onkeypress”、“onkeydown”和其他交互事件来停止默认行为(编辑文档)。

    I think you can still use "rich-text-editor" way (iframe), but then try to catch "onkeypress","onkeydown" and other interactive events to stop default behavior (editing the document).

    风月客 2024-08-21 20:37:15

    这是一个很好的资源,可以帮助我完成类似的事情: http://www.quirksmode.org/dom /execCommand.html

    从第一页链接作为示例:

    http://www. quirksmode.org/dom/execCommand/

    Here's a good resource that helped me with something similiar: http://www.quirksmode.org/dom/execCommand.html

    linked from the first page as an example:

    http://www.quirksmode.org/dom/execCommand/

    煮茶煮酒煮时光 2024-08-21 20:37:15

    您可以在论坛中找到解决方案: http://cksource.com /forums/viewtopic.php?f=11&t=15659

    为了清楚起见,我插入下面的代码:

    // Temporary workaround for providing editor 'read-only' toggling functionality.  
       ( function()
      {
       var cancelEvent = function( evt )
      {
         evt.cancel();
      };
    
     CKEDITOR.editor.prototype.readOnly = function( isReadOnly )
     {
      // Turn off contentEditable.
      this.document.$.body.disabled = isReadOnly;
      CKEDITOR.env.ie ? this.document.$.body.contentEditable = !isReadOnly
      : this.document.$.designMode = isReadOnly ? "off" : "on";
    
      // Prevent key handling.
      this[ isReadOnly ? 'on' : 'removeListener' ]( 'key', cancelEvent, null, null, 0 );
      this[ isReadOnly ? 'on' : 'removeListener' ]( 'selectionChange', cancelEvent, null, null, 0 );
    
      // Disable all commands in wysiwyg mode.
      var command,
         commands = this._.commands,
         mode = this.mode;
    
      for ( var name in commands )
      {
         command = commands[ name ];
         isReadOnly ? command.disable() : command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
         this[ isReadOnly ? 'on' : 'removeListener' ]( 'state', cancelEvent, null, null, 0 );
      }
     }
    } )();
    

    并在您的 javascript 中,按如下方式调用

    // Turn CKEditor into 'ready-only' mode or vice versa.
    CKEDITOR.instances.editor1.readOnly( true );
    CKEDITOR.instances.editor1.readOnly( false );
    

    上面基本上提供了一个区域(iframe),即可编辑,同时只读(不能从键盘输入)。它完全满足了我想要的所有功能。我不必关心如何实现:突出显示、删除格式、撤消和重做。一切都由 ckeditor 处理:)

    You will find the solution in the forum: http://cksource.com/forums/viewtopic.php?f=11&t=15659

    For the sake of clarity, i am inserting the code below:

    // Temporary workaround for providing editor 'read-only' toggling functionality.  
       ( function()
      {
       var cancelEvent = function( evt )
      {
         evt.cancel();
      };
    
     CKEDITOR.editor.prototype.readOnly = function( isReadOnly )
     {
      // Turn off contentEditable.
      this.document.$.body.disabled = isReadOnly;
      CKEDITOR.env.ie ? this.document.$.body.contentEditable = !isReadOnly
      : this.document.$.designMode = isReadOnly ? "off" : "on";
    
      // Prevent key handling.
      this[ isReadOnly ? 'on' : 'removeListener' ]( 'key', cancelEvent, null, null, 0 );
      this[ isReadOnly ? 'on' : 'removeListener' ]( 'selectionChange', cancelEvent, null, null, 0 );
    
      // Disable all commands in wysiwyg mode.
      var command,
         commands = this._.commands,
         mode = this.mode;
    
      for ( var name in commands )
      {
         command = commands[ name ];
         isReadOnly ? command.disable() : command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
         this[ isReadOnly ? 'on' : 'removeListener' ]( 'state', cancelEvent, null, null, 0 );
      }
     }
    } )();
    

    and in your javascript, call as following

    // Turn CKEditor into 'ready-only' mode or vice versa.
    CKEDITOR.instances.editor1.readOnly( true );
    CKEDITOR.instances.editor1.readOnly( false );
    

    The above basically provides an area (iframe) that is editable and it the same time read-only (you can not input from keyboard). It completely fulfills all my desired functionalities. I do not have to care about how to implement: highlighting, remove format, undo and redo. Everything is taken cared of ckeditor :)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文