Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to specific changes on contenteditable?

Warning: not duplicate with existing questions, read through

I know I can have an event listen on changes on an contenteditable element.

What I would like is to be able to know what the changes are.

For example:

  • inserted "This is a sentence." at position X.
  • deleted from position X to Y.
  • formatted from X to Y with <strong>

Is that possible? (other than by doing a diff I mean)


The reason for this is to make a WYSIWYG editor of other languages than HTML, for example Markdown.

So I'd like to apply the changes to the Markdown source (instead of having to go from HTML to Markdown).

like image 272
Matthieu Napoli Avatar asked Aug 04 '13 18:08

Matthieu Napoli


2 Answers

You may be able to do something with MutationObservers (falling back to DOM Mutation events in older browsers, although IE <= 8 supports neither) but I suspect it will still be hard work to achieve what you want.

Here's a simple example using MutationObservers:

http://jsfiddle.net/timdown/4n2Gz/

like image 143
Tim Down Avatar answered Oct 07 '22 01:10

Tim Down


Sorry, but there is no way to find out what the changes are without doing a diff between the original content and the modified one when changes occur.

like image 27
dcro Avatar answered Oct 06 '22 23:10

dcro