Désactivation de la combo des style lorsque la sélection est discontinue.
[ckeditor.git] / skins / ckeditor / plugins / plinn_styles / plugin.js
index 245a0e1..742a40a 100644 (file)
@@ -1,3 +1,4 @@
+/* © 2014 Benoît Pin, MINES ParisTech */
 ( function() {
 
 var PlinnStylesCombo = function(editor) {
 ( function() {
 
 var PlinnStylesCombo = function(editor) {
@@ -13,6 +14,20 @@ var PlinnStylesCombo = function(editor) {
        this.styles = [];
 };
 
        this.styles = [];
 };
 
+PlinnStylesCombo.prototype.onRender = function() {
+       var self = this;
+       this.editor.on('selectionChange', function(evt){self.checkSelection(evt);});
+};
+
+PlinnStylesCombo.prototype.checkSelection = function(evt) {
+       if (evt.data.selection.getRanges().length > 1) {
+               this.disable();
+       }
+       else {
+               this.enable();
+       }
+};
+
 PlinnStylesCombo.prototype.loadStyle = function(definition) {
        this.styles.push(definition);
        this.styles[definition.name] = definition;
 PlinnStylesCombo.prototype.loadStyle = function(definition) {
        this.styles.push(definition);
        this.styles[definition.name] = definition;
@@ -33,17 +48,28 @@ PlinnStylesCombo.prototype.init = function() {
 
 PlinnStylesCombo.prototype.onClick = function(value) {
        this.editor.focus();
 
 PlinnStylesCombo.prototype.onClick = function(value) {
        this.editor.focus();
-       this.editor.fire( 'saveSnapshot' );
+       this.editor.fire('saveSnapshot');
        var style = this.styles[value]
        var className = style.className;
        var style = this.styles[value]
        var className = style.className;
+       var ranges = this.editor.getSelection().getRanges();
        var element = this.editor.elementPath().lastElement;
        var element = this.editor.elementPath().lastElement;
+       if(ranges.length === 1) {
+               var start = ranges[0].startContainer;
+               var end = ranges[0].endContainer;
+               if(start.$ !== end.$) {
+                       // selection is a fragment that need to be wrapped in container to apply style
+                       element = new CKEDITOR.dom.element('div');
+                       element.append(ranges[0].cloneContents());
+                       this.editor.insertElement(element);
+               }
+       }
        if (element.hasClass(className)) {
                element.removeClass(className);
        }
        else {
                element.addClass(className);
        }
        if (element.hasClass(className)) {
                element.removeClass(className);
        }
        else {
                element.addClass(className);
        }
-       this.editor.fire( 'saveSnapshot' );
+       this.editor.fire('saveSnapshot');
 };
 
 var PlinnStylePlugin = function() {
 };
 
 var PlinnStylePlugin = function() {
@@ -55,7 +81,7 @@ PlinnStylePlugin.prototype.init = function(editor) {
        this.combo = new PlinnStylesCombo(editor);
        editor.ui.addRichCombo('PlinnStyles', this.combo);
        var self = this;
        this.combo = new PlinnStylesCombo(editor);
        editor.ui.addRichCombo('PlinnStyles', this.combo);
        var self = this;
-       editor.on('stylesSet', function(evt){self.onStylesSet(evt)});
+       editor.on('stylesSet', function(evt){self.onStylesSet(evt);});
 };
 
 PlinnStylePlugin.prototype.onStylesSet = function(evt) {
 };
 
 PlinnStylePlugin.prototype.onStylesSet = function(evt) {