I’m wondering what would be the best way to print the entire (formatted) content of the editor when hitting Cmd+P in the browser.
My first approach was to register an event listener with
window.beforeprint which passes the contents of
state.doc.toString() to an external highlighter library (e.g., marked) and updates the normally hidden DOM element for print content with its resulting HTML.
However, this does not produce a correct print preview. Since events are asynchronous, the print preview shows an empty document. I cannot force the print dialog to wait until my DOM is up-to-date.
So the second thing I tried, was to update den print DOM on every doc change. But this is very costly in terms of CPU, since this happens on every key press. Especially with large documents. So I tried to get rid of the external syntax highlighter and just use CM’s markup by writing
editor.contentDOM.innerHTML into my desired printable DOM element. However, this only prints what’s currently inside CM’s buffer and not the whole document.
I’m really out of ideas right now. Maybe anyone had a similar challenge?