mercredi 28 janvier 2015

Using Redactor WYSIWYG with Ember

I'm using Redactor from http://ift.tt/Wa6JqJ with Ember. I created a Ember component:



App.RedactorEditorComponent = Ember.Component.extend({

/* --- Public API --- */
redactorText: null,
color: null,

/* --- Internal --- */
tagName: 'div',

init: function() {
this._super();
this.on("focusOut", this, this._elementValueDidChange);
this.on("change", this, this._elementValueDidChange);
this.on("paste", this, this._elementValueDidChange);
this.on("cut", this, this._elementValueDidChange);
this.on("input", this, this._elementValueDidChange);
},

_updateElementValue: function() {
var $el, comment;
comment = this.get('redactorText');
$el = this.$().context;
if ($el && comment !== $el.innerHTML) {
return $el.innerHTML = comment;
}
},

_elementValueDidChange: function() {
var $el;
$el = this.$().context;
var comment = Ember.set(this, "value", $el.innerHTML);
this.sendAction('saveRedactorText', comment);
return comment;
},

didInsertElement: function() {
this.$().redactor({
plugins: ['counter']
});
this._updateElementValue();
},

});


I create a plugin. The pluging code is strait from Redactor docs (http://ift.tt/1yOD0Gy). As per instructions, I placed the code into a counter.js file:



if (!RedactorPlugins) var RedactorPlugins = {};

(function($)
{
RedactorPlugins.counter = function()
{
return {
init: function()
{
if (!this.opts.counterCallback) return;

this.$editor.on('keyup.redactor-limiter', $.proxy(function(e)
{
var words = 0, characters = 0, spaces = 0;

var html = this.code.get();

var text = html.replace(/<\/(.*?)>/gi, ' ');
text = text.replace(/<(.*?)>/gi, '');
text = text.replace(/\t/gi, '');
text = text.replace(/\n/gi, '');
text = text.replace(/\r/gi, '');
text = $.trim(text);

if (text !== '')
{
var arrWords = text.split(/\s+/);
var arrSpaces = text.match(/\s/g);

if (arrWords) words = arrWords.length;
if (arrSpaces) spaces = arrSpaces.length;

characters = text.length;

}

this.core.setCallback('counter', { words: words, characters: characters, spaces: spaces });


}, this));
}
};
};
})(jQuery);


Questions: The plugin work but I understand that this plugin create a global variable. How can I make this code more compliant with Ember methodology? I did try to put the plugin code into the Ember redactor-editor component but I could not manage to make the plugin load on line plugins: ['counter'].





Aucun commentaire:

Enregistrer un commentaire