How to track caret/cursor in contenteditable?

后端 未结 4 868
栀梦
栀梦 2021-02-07 04:40

I\'d like to track the movement of the caret/cursor in a contenteditable. I\'m not sure what\'s the best way to do this, though.

I\'m currently listening for click, key

相关标签:
4条回答
  • 2021-02-07 05:08

    It's nice to read that people are talking about CKEditor :). I'm one of its developers and I haven't been working much on selection, but I'll try to help.

    What I know is that we've got an internal selectionChange event. So when do we check if it changed? ... ... At least once per every 200ms :) See:

    http://dev.ckeditor.com/browser/CKEditor/trunk/_source/plugins/selection/plugin.js#L39

    We also check selection every time we know that it could have been changed (e.g. by the API or on keyup/mouseup or on native selectionchange - http://dev.ckeditor.com/browser/CKEditor/trunk/_source/plugins/selection/plugin.js#L554). So... pretty much all the time :) AFAIK we do some tricks, so it doesn't burn your CPU, but it's still heavy. However, if we did this, then it's the only possible way to have this working so nicely.

    Unfortunately, selection handling is by far the worst task in wysiwygs world. It's so broken in all - old and new browsers. More than 75% LOC in the file I linked above are hacks and tricks. That's complete madness.

    0 讨论(0)
  • 2021-02-07 05:10

    In Mozilla and Opera, the nasty business of handling key and mouse events is your only option. Not only is it fiddly, it also doesn't cover every case: it's possible to change the selection via the edit and context menus (via Select All, for example). To cover that, you'd also need to add some kind of polling of the selection object.

    However, in IE (all the way back to at least 5.5) and recent-ish WebKit, there is a selectionchange event that fires on the document whenever the selection changes.

    document.onselectionchange = function() {
        alert("Selection changed!");
    };
    

    There is a chance that Mozilla will support it in the future: https://bugzilla.mozilla.org/show_bug.cgi?id=571294

    0 讨论(0)
  • 2021-02-07 05:21

    It's not an easy task for the reasons you said. I came up with some kludge like this:

    var caretInterval, caretOffset;
    document.addEventListener("keydown", function(e) {
        if (!e.target.contentEditable || caretInterval) return;
        if (e.keyCode !== 37 && e.keyCode !== 39) // Left and right
            return;
        var sel = getSelection();
        caretInterval = setInterval(function() {
            if (sel.type === "Caret") caretOffset = sel.baseOffset;
        }, 50);
    });
    document.addEventListener("keyup", function(e) {
        if (e.keyCode !== 37 && e.keyCode !== 39) // Left and right
            return;
        clearInterval(caretInterval);
        caretInverval = null;
        var sel = getSelection();
        if (sel.type === "Caret") caretOffset = sel.baseOffset;
    });
    

    There could be a small problem if someone tries to press left and right at the same time. For ctrl-X and ctrl-V, you should catch the cut and paste event, and that's actually another pain in the bollocks.

    In the end, I decided it wasn't worth the effort for my purposes. Maybe you have different needs.

    0 讨论(0)
  • 2021-02-07 05:21

    WRT catching the event after the selection is updated: I simply wrap my handler functions in timeouts:

    editor.onkeydown = function() {
      window.setTimeout( function(){
        // Your handler code here
      }, 0 );
    };
    

    This registers your handler to be executed in the browser's event loop as soon as possible, but after the current (eg click) event is processed. But be aware of possible races if you have other scripts modifying the content; there is no guarantee that your timeout is the next in line to be run.

    0 讨论(0)
提交回复
热议问题