[wp-trac] [WordPress Trac] #47306: comment reply event listener: need ability to run custom js function

WordPress Trac noreply at wordpress.org
Fri May 17 16:24:11 UTC 2019


#47306: comment reply event listener:  need ability to run custom js function
-------------------------+-----------------------------
 Reporter:  jnorell      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Comments     |    Version:  5.1
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 I wrote the comment-tweaks plugin, which adds the tinymce editor to the
 comments field.  When threaded comments are enabled, clicking on a Reply
 link moves #comment within the dom, and that breaks tinymce - I need to be
 able to run a bit of js when a Reply link is clicked to remove the editor,
 then move the element, then add the editor back.

 Prior to 5.1, the comment reply link had an onclick attribute calling
 moveForm(), and I simply overwrote the single onclick handler with a
 custom one.  This is/was a bit of a hack, but worked fine (as long as no
 other plugin was hoping to also take over onclick).

 Recent changes (#46260) to comment replying changed how event listeners
 are handled for the comment Reply and Cancel reply buttons, which now uses
 a MutationObserver to (re)add click/touchstart listeners every time the
 document body changes.  I can no longer overwrite the onclick handler, as
 it will be re-added in subsequent document changes.

 I could probably add my own MutationObserver, handle the race of which
 fires first, and continue to overwrite the core click/touchstart event
 handlers, but this is just another hack and I would rather fix it
 correctly, where other plugins could utilize event handlers on those links
 as well.

 Note that I need more than just the ability to add my own event listeners,
 as they fire in the order they were added, and I need to remove the editor
 prior to moving #comment (addComment.clickEvent() firing), and add it
 again afterwards.

 A javascript "action hook" that fires in clickEvent() ahead of moveForm()
 and another which fires afterwards may be a viable solution.  (And another
 pair of "action hooks" in cancelEvent().)  Probably could make the entire
 click/cancel event handlers just call the action hook and add the current
 clickEvent/cancelEvent code in functions called via those same hooks, so
 they could be overridden if needed?  (I don't need to overwrite them
 myself if I can order a call before and after them, but perhaps other
 plugins would.)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47306>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list