Browse Source

Update RichTextEditor module

pull/415/head
Artur Arseniev 9 years ago
parent
commit
b9f5c59ff3
  1. 10
      src/dom_components/view/ComponentTextView.js
  2. 94
      src/rich_text_editor/index.js

10
src/dom_components/view/ComponentTextView.js

@ -19,11 +19,10 @@ module.exports = ComponentView.extend({
}, },
/** /**
* Enable the component to be editable * Enable element content editing
* @param {Event} e
* @private * @private
* */ * */
enableEditing(e) { enableEditing() {
const editable = this.model.get('editable'); const editable = this.model.get('editable');
const rte = this.rte; const rte = this.rte;
@ -40,11 +39,10 @@ module.exports = ComponentView.extend({
}, },
/** /**
* Disable this component to be editable * Disable element content editing
* @param {Event}
* @private * @private
* */ * */
disableEditing(e) { disableEditing() {
const model = this.model; const model = this.model;
const editable = model.get('editable'); const editable = model.get('editable');
const rte = this.rte; const rte = this.rte;

94
src/rich_text_editor/index.js

@ -3,6 +3,7 @@
* * [get](#get) * * [get](#get)
* * [getAll](#getall) * * [getAll](#getall)
* * [remove](#remove) * * [remove](#remove)
* * [getToolbarEl](#gettoolbarel)
* *
* This module allows to customize the toolbar of the Rich Text Editor and use commands from the HTML Editing APIs. * This module allows to customize the toolbar of the Rich Text Editor and use commands from the HTML Editing APIs.
* For more info about HTML Editing APIs check here: * For more info about HTML Editing APIs check here:
@ -21,16 +22,17 @@
* http://www.quirksmode.org/dom/execCommand.html * http://www.quirksmode.org/dom/execCommand.html
* @module RichTextEditor * @module RichTextEditor
*/ */
const $ = Backbone.$; import RichTextEditor from './model/RichTextEditor';
import {on, off} from 'utils/mixins'
module.exports = () => { module.exports = () => {
var c = {}, let config = {},
defaults = require('./config/config'), defaults = require('./config/config'),
rte = require('./view/TextEditorView'), rte = require('./view/TextEditorView'),
CommandButtons = require('./model/CommandButtons'), CommandButtons = require('./model/CommandButtons'),
CommandButtonsView = require('./view/CommandButtonsView'); CommandButtonsView = require('./view/CommandButtonsView');
const $ = require('backbone').$; const $ = require('backbone').$;
let toolbar, commands; let toolbar, commands, lastEl;
return { return {
@ -45,27 +47,30 @@ module.exports = () => {
/** /**
* Initialize module. Automatically called with a new instance of the editor * Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations * @param {Object} opts Options
* @private * @private
*/ */
init(config) { init(opts) {
c = config || {}; config = opts || {};
for (let name in defaults) { for (let name in defaults) {
if (!(name in c)) { if (!(name in config)) {
c[name] = defaults[name]; config[name] = defaults[name];
} }
} }
const ppfx = c.pStylePrefix; const ppfx = config.pStylePrefix;
if (ppfx) { if (ppfx) {
c.stylePrefix = ppfx + c.stylePrefix; config.stylePrefix = ppfx + config.stylePrefix;
} }
toolbar = document.createElement('div'); toolbar = document.createElement('div');
toolbar.className = `${ppfx}rte-toolbar`; toolbar.className = `${ppfx}rte-toolbar`;
//Avoid closing on toolbar clicking
on(toolbar, 'mousedown', e => e.stopPropagation());
/* /*
commands = new CommandButtons(c.commands); commands = new CommandButtons(c.commands);
toolbar = new CommandButtonsView({ toolbar = new CommandButtonsView({
@ -133,12 +138,12 @@ module.exports = () => {
*/ */
udpatePosition() { udpatePosition() {
var u = 'px'; var u = 'px';
var canvas = c.em.get('Canvas'); var canvas = config.em.get('Canvas');
var pos = canvas.getTargetToElementDim(toolbar.el, this.lastEl, { var pos = canvas.getTargetToElementDim(toolbar.el, lastEl, {
event: 'rteToolbarPosUpdate', event: 'rteToolbarPosUpdate',
}); });
if (c.adjustToolbar) { if (config.adjustToolbar) {
// Move the toolbar down when the top canvas edge is reached // Move the toolbar down when the top canvas edge is reached
if (pos.top <= pos.canvasTop) { if (pos.top <= pos.canvasTop) {
pos.top = pos.elementTop + pos.elementHeight; pos.top = pos.elementTop + pos.elementHeight;
@ -151,26 +156,19 @@ module.exports = () => {
}, },
/** /**
* Bind rich text editor to the element * Enable rich text editor on the element
* @param {View} view * @param {View} view
* @param {Object} rte The instance of already defined RTE * @param {Object} rte The instance of already defined RTE
* @private * @private
* */ * */
attach(view, rte) { enable(view, rte) {
const em = c.em; lastEl = view.el;
// lastEl will be used to place the RTE toolbar const em = config.em;
this.lastEl = view.el; const el = view.getChildrenContainer();
var el = view.getChildrenContainer(); const customRte = this.customRte;
var customRte = this.customRte;
// If a custom RTE is defined
if (customRte) {
rte = customRte.enable(el, rte);
} else {
$(el).wysiwyg({}).focus();
}
this.show(); rte = customRte ? customRte.enable(el, rte) : new RichTextEditor({el});
toolbar.style.display = '';
if (em) { if (em) {
setTimeout(this.udpatePosition.bind(this), 0); setTimeout(this.udpatePosition.bind(this), 0);
@ -181,8 +179,6 @@ module.exports = () => {
em.on('canvasScroll', this.udpatePosition, this); em.on('canvasScroll', this.udpatePosition, this);
} }
//Avoid closing edit mode clicking on toolbar
toolbar.$el.on('mousedown', this.disableProp);
return rte; return rte;
}, },
@ -192,17 +188,17 @@ module.exports = () => {
* @param {Object} rte The instance of already defined RTE * @param {Object} rte The instance of already defined RTE
* @private * @private
* */ * */
detach(view, rte) { disable(view, rte) {
var customRte = this.customRte; const customRte = this.customRte;
var el = view.getChildrenContainer(); var el = view.getChildrenContainer();
if (customRte) { if (customRte) {
view.model.set('content', el.innerHTML);
customRte.disable(el, rte); customRte.disable(el, rte);
} else { } else {
$(el).wysiwyg('destroy'); rte.disable();
} }
this.hide();
toolbar.$el.off('mousedown', this.disableProp); toolbar.style.display = 'none';
}, },
/** /**
@ -223,31 +219,7 @@ module.exports = () => {
}, },
/** /**
* Show the toolbar * Return the toolbar element
* @private
* */
show() {
toolbar.style.display = '';
},
/**
* Hide the toolbar
* @private
* */
hide() {
toolbar.style.display = 'none';
},
/**
* Isolate the disable propagation method
* @private
* */
disableProp(e) {
e.stopPropagation();
},
/**
* Return toolbar element
* @return {HTMLElement} * @return {HTMLElement}
* @private * @private
*/ */

Loading…
Cancel
Save