Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent backspace if carret is in the beginning of paragraph in tinyMCE

Tags:

tinymce

I need to prevent backspace if my cursor position is in the beginning of the first paragraph. The whole thing is that I want to prevent the deletion of my first paragraph in the editor.

like image 454
cyrat Avatar asked Sep 18 '25 02:09

cyrat


1 Answers

You can do (using Tinymce3) something like

tinyMCE.init({
    mode : "textareas",
    ...
    setup : function(ed) {

        ed.onKeyDown.add(function(ed, event) {

            var range = ed.selection.getRng();

            // case: first editor node is the node with the caret in it
            if (range.startOffset == 0 && ed.getBody().getNode() == ed.getBody().firstChild)
            {
               event.preventDefault;
               return false;

            }
        });
    }
});

Since TinyMCE 4x keydown, keypress and keyup all became events of editor.on, the same result can be achieved doing the following:

ed.on('keydown', function( args ) {

  var range = ed.selection.getRng();

     // First editor node is the node with the caret in it
     if ( range.startOffset === 0 ) {
       event.preventDefault();
       return false;
     }

 }); 
like image 130
Thariama Avatar answered Sep 21 '25 08:09

Thariama