Medium Editor copy & paste functionality

Just an oddball issue I encountered using Medium-Editor.

This issue had been bugging me forever. It seemed that copying and pasting into a medium-editor instance would cause my application to save and unfocus from that medium-editor instance. I’d forget about it for weeks at a time until I needed to paste something.

So I finally sat down to fix it. Apparently Medium-Editor creates a ‘pastebin’ element for a paste, presumably so it can do whatever preprocessing it needs to do to HTML. The problem is if you’re dispatching some kind of action on a blur (such as saving changes) it’ll be triggered by this ephemeral pastebin element stealing focus for a moment.

The fix is simple, in my case just putting this code before dispatching a save:

if (document.activeElement.id.startsWith('medium-editor-pastebin')) {