From f94a055ae6a4512b6368bd72025fa22cc7d1d960 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 12 Oct 2017 19:08:07 +0200 Subject: [PATCH] Update RichTextEditor, set element to globalRte --- src/rich_text_editor/index.js | 2 +- src/rich_text_editor/model/RichTextEditor.js | 27 ++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/rich_text_editor/index.js b/src/rich_text_editor/index.js index cbc461158..de8e750d7 100644 --- a/src/rich_text_editor/index.js +++ b/src/rich_text_editor/index.js @@ -104,6 +104,7 @@ module.exports = () => { actionbar, actionbarContainer, }); + globalRte && globalRte.setEl(el); if (rte.actionbar) { this.actionbar = rte.actionbar; @@ -135,7 +136,6 @@ module.exports = () => { */ add(name, action = {}) { action.name = name; - globalRte.getActions().push(action); globalRte.addAction(action, {sync: 1}); }, diff --git a/src/rich_text_editor/model/RichTextEditor.js b/src/rich_text_editor/model/RichTextEditor.js index e9107bbf5..565376b63 100644 --- a/src/rich_text_editor/model/RichTextEditor.js +++ b/src/rich_text_editor/model/RichTextEditor.js @@ -32,18 +32,13 @@ const defActions = { }, link: { icon: ``, + name: 'link', attributes: { style: 'font-size:1.4rem;padding:0 4px 2px;', title: 'Link', }, result: (rte) => rte.insertHTML(`${rte.selection()}`) - }, - avoid: { - name: 'avoid', - icon: 'avoid', - attributes: {title: 'avoid'}, - result: (rte) => rte.insertHTML(`Avoid`) - }, + } } export default class RichTextEditor { @@ -56,9 +51,7 @@ export default class RichTextEditor { } el[RTE_KEY] = this; - this.el = el; - this.doc = el.ownerDocument; - + this.setEl(el); this.updateActiveActions = this.updateActiveActions.bind(this); const settAct = settings.actions || []; @@ -101,6 +94,11 @@ export default class RichTextEditor { return this; } + setEl(el) { + this.el = el; + this.doc = el.ownerDocument; + } + updateActiveActions() { this.getActions().forEach(action => { const btn = action.btn; @@ -114,19 +112,25 @@ export default class RichTextEditor { } enable() { + if (this.enabled) { + return this; + } + 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) + off(this.el, 'mouseup keyup', this.updateActiveActions); + this.enabled = 0; return this; } @@ -169,6 +173,7 @@ export default class RichTextEditor { this.actionbarEl().appendChild(btn); if (sync) { + this.actions.push(action); this.syncActions(); } }