Browse Source

Prevent divs on RTE paste and plain text. Fixes #4195

pull/4214/head
Artur Arseniev 4 years ago
parent
commit
4279b22920
  1. 14
      src/rich_text_editor/model/RichTextEditor.js

14
src/rich_text_editor/model/RichTextEditor.js

@ -92,6 +92,7 @@ export default class RichTextEditor {
this.setEl(el);
this.updateActiveActions = this.updateActiveActions.bind(this);
this.__onKeydown = this.__onKeydown.bind(this);
this.__onPaste = this.__onPaste.bind(this);
const acts = (settings.actions || []).map(action => {
let result = action;
@ -201,6 +202,7 @@ export default class RichTextEditor {
el.contentEditable = !!enable;
method(el, 'mouseup keyup', this.updateActiveActions);
method(doc, 'keydown', this.__onKeydown);
method(doc, 'paste', this.__onPaste);
this.enabled = enable;
if (enable) {
@ -237,6 +239,18 @@ export default class RichTextEditor {
}
}
__onPaste(ev) {
const clipboardData = ev.clipboardData || window.clipboardData;
const text = clipboardData.getData('text');
const textHtml = clipboardData.getData('text/html');
// Replace \n with <br> in case of plain text
if (text && !textHtml) {
ev.preventDefault();
const html = text.replace(/(?:\r\n|\r|\n)/g, '<br/>');
this.doc.execCommand('insertHTML', false, html);
}
}
/**
* Sync actions with the current RTE
*/

Loading…
Cancel
Save