Browse Source

Init only one default RTE when necessary

pull/4141/head
Artur Arseniev 4 years ago
parent
commit
e49b5e7ed5
  1. 41
      src/rich_text_editor/index.js
  2. 12
      src/rich_text_editor/model/RichTextEditor.js

41
src/rich_text_editor/index.js

@ -72,10 +72,7 @@ export default () => {
* @private * @private
*/ */
init(opts = {}) { init(opts = {}) {
config = { config = { ...defaults, ...opts };
...defaults,
...opts,
};
const ppfx = config.pStylePrefix; const ppfx = config.pStylePrefix;
if (ppfx) { if (ppfx) {
@ -88,7 +85,6 @@ export default () => {
if (!hasWin()) return this; if (!hasWin()) return this;
toolbar = document.createElement('div'); toolbar = document.createElement('div');
toolbar.className = `${ppfx}rte-toolbar ${ppfx}one-bg`; toolbar.className = `${ppfx}rte-toolbar ${ppfx}one-bg`;
globalRte = this.initRte(document.createElement('div'));
//Avoid closing on toolbar clicking //Avoid closing on toolbar clicking
on(toolbar, 'mousedown', e => e.stopPropagation()); on(toolbar, 'mousedown', e => e.stopPropagation());
@ -97,7 +93,7 @@ export default () => {
destroy() { destroy() {
const { customRte } = this; const { customRte } = this;
globalRte && globalRte.destroy(); globalRte?.destroy();
customRte && customRte.destroy && customRte.destroy(); customRte && customRte.destroy && customRte.destroy();
this.actionbar = 0; this.actionbar = 0;
this.actions = 0; this.actions = 0;
@ -134,25 +130,30 @@ export default () => {
inactive: `${pfx}inactive`, inactive: `${pfx}inactive`,
disabled: `${pfx}disabled`, disabled: `${pfx}disabled`,
}; };
const rte = new RichTextEditor({
em,
el,
classes,
actions,
actionbar,
actionbarContainer,
});
globalRte && globalRte.setEl(el);
if (rte.actionbar) { if (!globalRte) {
this.actionbar = rte.actionbar; globalRte = new RichTextEditor({
em,
el,
classes,
actions,
actionbar,
actionbarContainer,
});
} else {
globalRte.em = em;
globalRte.setEl(el);
}
if (globalRte.actionbar) {
this.actionbar = globalRte.actionbar;
} }
if (rte.actions) { if (globalRte.actions) {
this.actions = rte.actions; this.actions = globalRte.actions;
} }
return rte; return globalRte;
}, },
/** /**

12
src/rich_text_editor/model/RichTextEditor.js

@ -78,8 +78,8 @@ const defActions = {
export default class RichTextEditor { export default class RichTextEditor {
constructor(settings = {}) { constructor(settings = {}) {
const el = settings.el; const { el, em } = settings;
this.em = settings.em; this.em = em;
if (el[RTE_KEY]) { if (el[RTE_KEY]) {
return el[RTE_KEY]; return el[RTE_KEY];
@ -241,7 +241,7 @@ export default class RichTextEditor {
*/ */
syncActions() { syncActions() {
this.getActions().forEach(action => { this.getActions().forEach(action => {
if (this.settings.actionbar) { if (this.actionbar) {
if (!action.state || (action.state && action.state(this, this.doc) >= 0)) { if (!action.state || (action.state && action.state(this, this.doc) >= 0)) {
const event = action.event || 'click'; const event = action.event || 'click';
action.btn[`on${event}`] = e => { action.btn[`on${event}`] = e => {
@ -323,7 +323,7 @@ export default class RichTextEditor {
* @param {string} value HTML string * @param {string} value HTML string
*/ */
insertHTML(value, { select } = {}) { insertHTML(value, { select } = {}) {
const { em, doc } = this; const { em, doc, el } = this;
const sel = doc.getSelection(); const sel = doc.getSelection();
if (sel && sel.rangeCount) { if (sel && sel.rangeCount) {
@ -343,7 +343,7 @@ export default class RichTextEditor {
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(range); sel.addRange(range);
this.el.focus(); el.focus();
if (select) { if (select) {
const cmp = em.getSelected(); const cmp = em.getSelected();
@ -351,7 +351,7 @@ export default class RichTextEditor {
const toSel = cmp.find(`[${customElAttr}]`)[0]; const toSel = cmp.find(`[${customElAttr}]`)[0];
if (!toSel) return; if (!toSel) return;
toSel.removeAttributes(customElAttr); toSel.removeAttributes(customElAttr);
editor.select(toSel); em.setSelected(toSel);
}); });
cmp.trigger('disable'); cmp.trigger('disable');
} }

Loading…
Cancel
Save