cjwirth / RichEditorView

RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.
BSD 3-Clause "New" or "Revised" License
1.9k stars 445 forks source link

Toggle Heading #180

Open mrlaunch opened 5 years ago

mrlaunch commented 5 years ago

Is there any way to make <h1></h1> back to plain text?

nwainwri commented 5 years ago

Came to see if there was a fix for this, as that's rather important that you can't 'toggle' header(int) size 'off' after adding it to text, where as bold/italic can be toggle on/off with the same button.

"undo" does work to remove that, but only if that was the last edit done to the string... other wise you'd have to remove/redo that sentance/word/etc.

nwainwri commented 5 years ago

Other issue with this that I've just noticed. If you say have "Title" and tag it is H1 (for example), as you can't remove that, or maybe undo is just too far back. Let's say you manually remove that "title", and then re-add it as bold text instead. The "h1" tag is still there.. and if any text is backspaced into it... it gets formatted by that H1 "trap".

Kind of a big deal.

kaminwong commented 5 years ago

You can check ZSSRichTextEditor's implementation.

  1. In "RichEditorView.swift" replace header function with public func header(_ h: Int) { runJS("RE.setHeading('h\(h)');") }

  2. In "rich_editor.js" replace RE.setHeading with var current_selection = $(RE.getSelectedNode()); var t = current_selection.prop("tagName").toLowerCase(); var is_heading = (t == 'h1' || t == 'h2' || t == 'h3' || t == 'h4' || t == 'h5' || t == 'h6'); if (is_heading && heading == t) { var c = current_selection.html(); current_selection.replaceWith(c); } else { document.execCommand('formatBlock', false, '<'+heading+'>'); }

  3. In rich_editor.html, insert <script type="text/javascript" src="jQuery.js"></script> in between "<head /head>"

  4. Finally, copy the jQuery.js file from ZSSRichTextEditor to RichEditorView and put under "Resources"

I think this should do the trick... Note that you may need to use the legacy build system if your changes were not registered properly. I found that the js part for ZSS is written a little better, but the swift part of this library is structured very well.