问题
I intended to use Document.execCommand()
method along with contenteditable
attribute to build my custom WYSIWYG editor. But when I checked the documentation for Document.execCommand()
, I found that it's now obsolete. What's the modern (or extant) alternative for it?
回答1:
I created the Rich Editor for my platform's XML (HTML5 + XHTML) editing purposes. I would not say that document.execCommand()
is completely dead because some parts of it still work fine. Unfortunately the primary issue for me was that browsers use a lot of different code to generate those styles which are not recognized by screen readers used by those who are blind or nearly so.
Additionally the most expensive time bug I ever had to conquer was a Gecko/Presto bug where the visual and technical selections (why they aren't the same thing, don't ask me) would result in part of the DOM being changed that the user did not intend and this would come down to the fact that the pixel count per character is low so if the Rich Editor did not honor visual selections a user would very quickly walk away. That required four months to conquer and there are other bugs too.
Ultimately it's a harsh though achievable endeavor though if you intend to build an HTML/XML editor like I did you should plan for at least six months if you plan to not only do it properly though test it to the point of hating cake to then only have someone come along and point out yet another bug.
Your primary focus JavaScript wise should be on the following:
document.createRange()
window.getSelection()
appendChild
insertBefore
insertBefore
+nextSibling
replaceChild
I actually have been intending to revise my Rich Editor (it's been getting patched though not yet properly rewritten) though you're welcomed to look at the source code when it loads on a blog page in the site linked in my profile. The original project took me 11 months though with my experience now I think it would take me about three to four. If you're serious I highly recommend staying the hell away from frameworks and libraries. "But ... but, they make life easier!" ... until you want to use a new version and have to rewrite the entire project. Use pure JavaScript the first time and negate pointless maintenance. Good luck!
来源:https://stackoverflow.com/questions/60581285/execcommand-is-now-obsolete-whats-the-alternative