javascript - Formatting buttons for contentEditable reflect selection formatting -
we have contenteditable="true"
div use wysiwyg text editor. added basic formatting toolbar similar 1 uses medium.com
using execcommand()
, we're able make selected text bold if, instance, user presses bold
button. works great.
however, haven't managed yet make bold
button highlighted if user selects part of text in bold, illustrated in picture above. done passing is-active
class on button of course, how know selected text is, in case, bold?
we think perhaps done using selection
api use case seems undocumented.
since execcommand() use html tag wrappers b tag bold . when selecting word sure using sort of js show formating tollbar add more code in function this.
if(selectedelement.nodename == "b"){ toolbarboldbutton.classlist.add("is-active"); }
note: using js function "nodename" tag name wrapped around execcommand() in case bold/b matching if matches tag , if true adding is-active class toolbars bold style element.
Comments
Post a Comment