Browse Source

Start refactoring rich text editor module

pull/36/head
Artur Arseniev 10 years ago
parent
commit
9629a3a942
  1. 1
      src/device_manager/main.js
  2. 5
      src/editor/main.js
  3. 30
      src/rich_text_editor/config/config.js
  4. 70
      src/rich_text_editor/main.js
  5. 20
      src/rich_text_editor/model/CommandButton.js
  6. 9
      src/rich_text_editor/model/CommandButtons.js
  7. 11
      src/rich_text_editor/view/CommandButtonView.js
  8. 22
      src/rich_text_editor/view/CommandButtonsView.js

1
src/device_manager/main.js

@ -1,5 +1,4 @@
/** /**
* * [init](#init)
* * [add](#add) * * [add](#add)
* * [get](#get) * * [get](#get)
* * [getAll](#getall) * * [getAll](#getall)

5
src/editor/main.js

@ -153,6 +153,11 @@ define(function (require){
*/ */
DeviceManager: em.get('DeviceManager'), DeviceManager: em.get('DeviceManager'),
/**
* @property {RichTextEditor}
*/
RichTextEditor: em.get('rte'),
/** /**
* @property {Utils} * @property {Utils}
*/ */

30
src/rich_text_editor/config/config.js

@ -4,20 +4,20 @@ define(function () {
toolbarId : 'toolbar', toolbarId : 'toolbar',
containerId : 'wrapper', containerId : 'wrapper',
commands : [{ commands : [{
command: 'bold', command: 'bold',
title: 'Bold', title: 'Bold',
class: 'fa fa-bold', class: 'fa fa-bold',
group: 'format' group: 'format'
},{ },{
command: 'italic', command: 'italic',
title: 'Italic', title: 'Italic',
class: 'fa fa-italic', class: 'fa fa-italic',
group: 'format' group: 'format'
},{ },{
command: 'underline', command: 'underline',
title: 'Underline', title: 'Underline',
class: 'fa fa-underline', class: 'fa fa-underline',
group: 'format' group: 'format'
},], },],
}; };
}); });

70
src/rich_text_editor/main.js

@ -1,3 +1,19 @@
/**
* * [add](#add)
* * [get](#get)
* * [getAll](#getall)
* * [remove](#remove)
*
* This module allows to customize the toolbar of the Rich Text Editor
* Before using methods you should get first the module from the editor instance, in this way:
*
* ```js
* var rte = editor.RichTextEditor;
* ```
* Complete list of commands
* http://www.quirksmode.org/dom/execCommand.html
* @module RichTextEditor
*/
define(function(require) { define(function(require) {
return function() { return function() {
@ -6,7 +22,7 @@ define(function(require) {
rte = require('./view/TextEditorView'), rte = require('./view/TextEditorView'),
CommandButtons = require('./model/CommandButtons'), CommandButtons = require('./model/CommandButtons'),
CommandButtonsView = require('./view/CommandButtonsView'); CommandButtonsView = require('./view/CommandButtonsView');
var tlbPfx, toolbar; var tlbPfx, toolbar, commands;
return { return {
@ -20,6 +36,7 @@ define(function(require) {
/** /**
* 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} config Configurations
* @private
*/ */
init: function(config) { init: function(config) {
c = config || {}; c = config || {};
@ -33,13 +50,39 @@ define(function(require) {
c.stylePrefix = ppfx + c.stylePrefix; c.stylePrefix = ppfx + c.stylePrefix;
tlbPfx = c.stylePrefix; tlbPfx = c.stylePrefix;
commands = new CommandButtons(c.commands);
toolbar = new CommandButtonsView({ toolbar = new CommandButtonsView({
collection: new CommandButtons(c.commands), collection: commands,
config: c, config: c,
}); });
return this; return this;
}, },
/**
* Add new command to the toolbar
* @param {string} command Command name
* @param {string} command Command name
* @example
* var cm = rte.add('bold', {
* title: 'Make bold',
* class: 'fa fa-bold',
* });
* // With arguments
* var cm = rte.add('fontSize', {
* title: 'Font size',
* arguments: [
* {name: 'Big', value: 5},
* {name: 'Normal', value: 3},
* {name: 'Small', value: 1}
* ]
* });
*/
add: function(command, opts) {
var obj = opts || {};
obj.command = command;
return commands.add(obj);
},
/** /**
* Triggered when the offset of the editro is changed * Triggered when the offset of the editro is changed
* @private * @private
@ -58,8 +101,9 @@ define(function(require) {
}, },
/** /**
* Bind rich text editor to element * Bind rich text editor to the element
* @param {View} view * @param {View} view
* @private
* */ * */
attach: function(view){ attach: function(view){
view.$el.wysiwyg({}).focus(); view.$el.wysiwyg({}).focus();
@ -76,9 +120,9 @@ define(function(require) {
}, },
/** /**
* Unbind rich text editor from element * Unbind rich text editor from the element
* @param {Object} view * @param {View} view
* * @private
* */ * */
detach: function(view){ detach: function(view){
view.$el.wysiwyg('destroy'); view.$el.wysiwyg('destroy');
@ -87,24 +131,24 @@ define(function(require) {
}, },
/** /**
* Show toolbar * Show the toolbar
* * @private
* */ * */
show: function(){ show: function(){
toolbar.el.style.display = 'block'; toolbar.el.style.display = 'block';
}, },
/** /**
* Hide toolbar * Hide the toolbar
* * @private
* */ * */
hide: function(){ hide: function(){
toolbar.el.style.display = 'none'; toolbar.el.style.display = 'none';
}, },
/** /**
* Isolate disable propagation method * Isolate the disable propagation method
* * @private
* */ * */
disableProp: function(e){ disableProp: function(e){
e.stopPropagation(); e.stopPropagation();
@ -113,6 +157,7 @@ define(function(require) {
/** /**
* Return toolbar element * Return toolbar element
* @return {HTMLElement} * @return {HTMLElement}
* @private
*/ */
getToolbarEl: function() { getToolbarEl: function() {
return toolbar.el; return toolbar.el;
@ -121,6 +166,7 @@ define(function(require) {
/** /**
* Render toolbar * Render toolbar
* @return {HTMLElement} * @return {HTMLElement}
* @private
*/ */
render: function(){ render: function(){
return toolbar.render().el; return toolbar.render().el;

20
src/rich_text_editor/model/CommandButton.js

@ -1,16 +1,14 @@
define(['backbone'], define(['backbone'],
function (Backbone) { function (Backbone) {
/** return Backbone.Model.extend({
* @class CommandButton
* */
return Backbone.Model.extend({
defaults: { defaults: {
command : '', command: '',
title : '', title: '',
class : '', class: '',
group : '', group: '',
arguments: [],
}, },
}); });
}); });

9
src/rich_text_editor/model/CommandButtons.js

@ -1,11 +1,8 @@
define([ 'backbone','./CommandButton'], define([ 'backbone','./CommandButton'],
function (Backbone, CommandButton) { function (Backbone, CommandButton) {
/**
* @class CommandButtons
* */
return Backbone.Collection.extend({ return Backbone.Collection.extend({
model: CommandButton, model: CommandButton,
}); });
}); });

11
src/rich_text_editor/view/CommandButtonView.js

@ -1,17 +1,14 @@
define(['backbone'], define(['backbone'],
function (Backbone) { function (Backbone) {
/**
* @class CommandButtonView
* */
return Backbone.View.extend({ return Backbone.View.extend({
tagName: 'a', tagName: 'a',
initialize: function(o){ initialize: function(o){
this.config = o.config || {}; this.config = o.config || {};
this.className = this.config.stylePrefix + 'btn ' + this.model.get('class'); this.className = this.config.stylePrefix + 'btn ' + this.model.get('class');
}, },
render: function() { render: function() {
this.$el.attr('class', _.result( this, 'className' ) ); this.$el.attr('class', _.result( this, 'className' ) );
return this; return this;

22
src/rich_text_editor/view/CommandButtonsView.js

@ -1,18 +1,15 @@
define(['backbone','./CommandButtonView'], define(['backbone','./CommandButtonView'],
function (Backbone, CommandButtonView) { function (Backbone, CommandButtonView) {
/**
* @class CommandButtonsView
* */
return Backbone.View.extend({ return Backbone.View.extend({
attributes : { attributes : {
'data-role' : 'editor-toolbar', 'data-role': 'editor-toolbar',
}, },
initialize: function(o){ initialize: function(o){
this.config = o.config || {}; this.config = o.config || {};
this.id = this.config.stylePrefix + this.config.toolbarId; this.id = this.config.stylePrefix + this.config.toolbarId;
this.$el.data('helper',1); this.$el.data('helper', 1);
}, },
render: function() { render: function() {
@ -21,11 +18,11 @@ define(['backbone','./CommandButtonView'],
this.collection.each(function(item){ this.collection.each(function(item){
var view = new CommandButtonView({ var view = new CommandButtonView({
model : item, model: item,
config : this.config, config: this.config,
attributes : { attributes: {
'title' : item.get('title'), 'title': item.get('title'),
'data-edit' : item.get('command'), 'data-edit': item.get('command'),
}, },
}); });
fragment.appendChild(view.render().el); fragment.appendChild(view.render().el);
@ -34,5 +31,6 @@ define(['backbone','./CommandButtonView'],
this.$el.attr('id', _.result( this, 'id' ) ); this.$el.attr('id', _.result( this, 'id' ) );
return this; return this;
} }
}); });
}); });

Loading…
Cancel
Save