Is there something better than document.execCommand?

前端 未结 2 738
不知归路
不知归路 2020-12-31 01:23

When implementing a web-based rich-text editor, I read that document.execCommand is useful for performing operations on an HTML document (like making a selectio

2条回答
  •  隐瞒了意图╮
    2020-12-31 01:47

    There is one alternative to using execCommand - implementing the whole interaction of an editor including blinking of a cursor. And it has been done. Google does it in docs, but there's something free and open-source too. Cloud9 IDE http://c9.io has an implementation. AFAIK, github uses that editor for some time now. And you surely can do anything under that, because there's no native code involved - like in execCommand

    The repo is here: https://github.com/ajaxorg/cloud9 (it contains the whole IDE, you will need to find the code for the editor. )

    Also - dom mutation events are deprecated. If you can drop support for old browsers, try mutation observer. If not - try to avoid detecting DOM changes at all and intercept changes in the editor's implementation. It might be the way to go for the new browsers too.

提交回复
热议问题