From 75cd582a8d1a91096276bc4dccc7475a269ad45c Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 8 Feb 2022 08:48:20 +0100 Subject: [PATCH] Use insertLineBreak for the default RTE --- src/rich_text_editor/model/RichTextEditor.js | 42 +++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/rich_text_editor/model/RichTextEditor.js b/src/rich_text_editor/model/RichTextEditor.js index 5cf6234d6..bcea64cde 100644 --- a/src/rich_text_editor/model/RichTextEditor.js +++ b/src/rich_text_editor/model/RichTextEditor.js @@ -77,6 +77,7 @@ export default class RichTextEditor { el[RTE_KEY] = this; this.setEl(el); this.updateActiveActions = this.updateActiveActions.bind(this); + this.__onKeydown = this.__onKeydown.bind(this); const acts = (settings.actions || []).map(action => { let result = action; @@ -173,26 +174,37 @@ export default class RichTextEditor { } enable() { - if (this.enabled) { - return this; + if (this.enabled) return this; + return this.__toggleEffects(true); + } + + disable() { + return this.__toggleEffects(false); + } + + __toggleEffects(enable = false) { + const method = enable ? on : off; + const { el, doc } = this; + this.actionbarEl().style.display = enable ? '' : 'none'; + el.contentEditable = !!enable; + method(el, 'mouseup keyup', this.updateActiveActions); + method(doc, 'keydown', this.__onKeydown); + this.enabled = enable; + + if (enable) { + this.syncActions(); + this.updateActiveActions(); + el.focus(); } - this.actionbarEl().style.display = ''; - this.el.contentEditable = true; - on(this.el, 'mouseup keyup', this.updateActiveActions); - this.syncActions(); - this.updateActiveActions(); - this.el.focus(); - this.enabled = 1; return this; } - disable() { - this.actionbarEl().style.display = 'none'; - this.el.contentEditable = false; - off(this.el, 'mouseup keyup', this.updateActiveActions); - this.enabled = 0; - return this; + __onKeydown(event) { + if (event.key === 'Enter') { + this.doc.execCommand('insertLineBreak'); + event.preventDefault(); + } } /**