0
votes

I am using tinyMCE text editor with some pre populated content. The content is a number of paragraphs and I want one of the paragraphs (a div) to be uneditable and undeletable. I've used the tinyMCE nonEditable plugin but if I select all the text or put the cursor below the paragraph and hit the delete button, it will delete. I figure there must be some JS that knows when it is selected and on keypress - backspace, it prevents the deletion of that paragraph (div).

1

1 Answers

0
votes

The issue you will run into (and why this is not something someone has already solved) is there are a great many ways something can get "deleted". You could...

  • Press the DEL or BKSPACE keys
  • Use any TinyMCE APIs to insert/set content that would overwrite a selection that includes a non-editable section
  • Click a TinyMCE toolbar button or menu option that would insert/overwrite content when your selection includes a non-editable section
  • Copy/paste content into the editor when your selection includes a non-editable section

So in reality to truly make something non-deletable you would need to address any possible situation where an action would overwrite a non-editable section along with any situation where a key press would delete a non-editable section.

TinyMCE does offer a variety of events like keypress, BeforeSetContent and change that could help with this but catching all the edge cases is the devil in the details that makes this really hard.

If all you want is to try to stop the use of BSPACE and DEL you can capture key presses and look at what is about to be deleted and stop the key press if you don't want that to happen.