Browse Source

Update RichTextEditor, set element to globalRte

pull/415/head
Artur Arseniev 9 years ago
parent
commit
f94a055ae6
  1. 2
      src/rich_text_editor/index.js
  2. 27
      src/rich_text_editor/model/RichTextEditor.js

2
src/rich_text_editor/index.js

@ -104,6 +104,7 @@ module.exports = () => {
actionbar, actionbar,
actionbarContainer, actionbarContainer,
}); });
globalRte && globalRte.setEl(el);
if (rte.actionbar) { if (rte.actionbar) {
this.actionbar = rte.actionbar; this.actionbar = rte.actionbar;
@ -135,7 +136,6 @@ module.exports = () => {
*/ */
add(name, action = {}) { add(name, action = {}) {
action.name = name; action.name = name;
globalRte.getActions().push(action);
globalRte.addAction(action, {sync: 1}); globalRte.addAction(action, {sync: 1});
}, },

27
src/rich_text_editor/model/RichTextEditor.js

@ -32,18 +32,13 @@ const defActions = {
}, },
link: { link: {
icon: `<span style="transform:rotate(45deg)">&supdsub;</span>`, icon: `<span style="transform:rotate(45deg)">&supdsub;</span>`,
name: 'link',
attributes: { attributes: {
style: 'font-size:1.4rem;padding:0 4px 2px;', style: 'font-size:1.4rem;padding:0 4px 2px;',
title: 'Link', title: 'Link',
}, },
result: (rte) => rte.insertHTML(`<a class="link" href="">${rte.selection()}</a>`) result: (rte) => rte.insertHTML(`<a class="link" href="">${rte.selection()}</a>`)
}, }
avoid: {
name: 'avoid',
icon: '<strike>avoid</strike>',
attributes: {title: 'avoid'},
result: (rte) => rte.insertHTML(`<b>Avoid</b>`)
},
} }
export default class RichTextEditor { export default class RichTextEditor {
@ -56,9 +51,7 @@ export default class RichTextEditor {
} }
el[RTE_KEY] = this; el[RTE_KEY] = this;
this.el = el; this.setEl(el);
this.doc = el.ownerDocument;
this.updateActiveActions = this.updateActiveActions.bind(this); this.updateActiveActions = this.updateActiveActions.bind(this);
const settAct = settings.actions || []; const settAct = settings.actions || [];
@ -101,6 +94,11 @@ export default class RichTextEditor {
return this; return this;
} }
setEl(el) {
this.el = el;
this.doc = el.ownerDocument;
}
updateActiveActions() { updateActiveActions() {
this.getActions().forEach(action => { this.getActions().forEach(action => {
const btn = action.btn; const btn = action.btn;
@ -114,19 +112,25 @@ export default class RichTextEditor {
} }
enable() { enable() {
if (this.enabled) {
return this;
}
this.actionbarEl().style.display = ''; this.actionbarEl().style.display = '';
this.el.contentEditable = true; this.el.contentEditable = true;
on(this.el, 'mouseup keyup', this.updateActiveActions) on(this.el, 'mouseup keyup', this.updateActiveActions)
this.syncActions(); this.syncActions();
this.updateActiveActions(); this.updateActiveActions();
this.el.focus(); this.el.focus();
this.enabled = 1;
return this; return this;
} }
disable() { disable() {
this.actionbarEl().style.display = 'none'; this.actionbarEl().style.display = 'none';
this.el.contentEditable = false; this.el.contentEditable = false;
off(this.el, 'mouseup keyup', this.updateActiveActions) off(this.el, 'mouseup keyup', this.updateActiveActions);
this.enabled = 0;
return this; return this;
} }
@ -169,6 +173,7 @@ export default class RichTextEditor {
this.actionbarEl().appendChild(btn); this.actionbarEl().appendChild(btn);
if (sync) { if (sync) {
this.actions.push(action);
this.syncActions(); this.syncActions();
} }
} }

Loading…
Cancel
Save