Fire an event before `cut` event in ace editor

501 views Asked by At

I want to fire an event before cut, so that I can get what text is being cut i.e. what has been already selected. I am currently using the following code, which doesn't seem to work as desired.

    $("#editor").bind({
    cut:function(){
        console.log('Cut Detected');            
       alert(editor.selection.getRange());

    }
});

editor is the id of the "div" tag which is editable. editor.selection.getRange() returns the start and end of selection. edit I am woring with content editable div and want to apply the functionality on it.

 <!DOCTYPE html>
<html lang="en">
<head>
<title>Editor</title>
</head>
<body>

<div id='myTa' contenteditable>hello world where are you</div>
<script type='text/javascript' src='jquery-2.1.4.min.js'></script>
<script type='text/javascript'>
$("#myTa").on("cut", function(){
alert(this.selectionStart+ " to " + this.selectionEnd);
})
</script>


</body>
</html>
3

There are 3 answers

2
fuyushimoya On

I think at first that clipboardEvent will have the clipped text, but it seems not, so I try to find the selection related properties of input and found it.

And the reference is HTMLInputElement.

This codes work in presumption that your $("#editor") is either an input or textarea.

$("#editor").bind({
    cut:function(e){
        console.log('Cut Detected');
        var $this = $(this);
        var selectStart = this.selectionStart;
        var selectionEnd = this.selectionEnd;
       var clippedValue = $this.val().slice(selectStart, selectionEnd);
       // Now you have the clipped value, do whatever you want with the
       // value.
       alert(clippedValue);
    }
});

For works on contentediable, you can do this, which I just found info from Return HTML from a user-selected text and MDN

$("#myTa").on("cut", function(e){
    // Seems diff bro
    var selections = window.getSelection();
    var currentSelection = selections.getRangeAt(0);
    var start = currentSelection.startOffset;
    var end  = currentSelection.endOffset;
    var selectedContents = currentSelection.toString();
    // Do whatever you want.
    console.log(start, end);
    console.log(selectedContents);
    alert(selectedContents);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id='myTa' contenteditable>ask;ndjkasn asdbasj aujs d sdib askjbnsaab asbh mjn a</div>

2
jcuenod On

You are correct that you need to use the cut event. The ClipboardEvent API is apparently unstable but yes, I would have thought it would include the text being moved onto the clipboard.

The following works for me:

$("textarea").on("cut", function(){
    alert(this.value.substring(this.selectionStart, this.selectionEnd));
})

It's worth noting that bind is deprecated in jQuery, you should use on instead. Try out the snippet:

$("textarea").on("cut", function() {
      alert(this.value.substring(this.selectionStart, this.selectionEnd));
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea></textarea>

0
Rohit Katariya On

I have got the solution to my answer. apperently there is an editor.on('cut',function(e)) in ace editor I use

editor.on("cut", function(e){
        console.log('Cut Detected');
        console.log(editor.selection.getRange());
    });