setContentsToPaste

Release 2.10.0+

Sets the HTML content to paste. Typically called inside a beforePaste event handler to transform the clipboard data before it lands in the editor.

Parameters

NameTypeDescription
htmlstringHTML to paste into the editor

Example

editor.setEventListener('beforePaste', function (data) {
  var html = data.clipboardData.html;
  var text = data.clipboardData.text;
  var contents = html ? html : getHTMLString(text);

  // Prepend a heading and paste
  editor.setContentsToPaste('<h1>Synap Editor</h1>' + contents);
});

/**
 * Convert plain text to an HTML string.
 * @param {string} text
 * @returns {string}
 */
function getHTMLString(text) {
  return (text || '')
    .split(/\r\n|\r|\n/)
    .map(function (txt) { return '<p style="margin: 0px;">' + txt + '</p>'; })
    .join('');
}