1 /* © 2014 Benoît Pin, MINES ParisTech */
4 var PlinnStylesCombo = function(editor
) {
7 this.title
= 'CSS Styles';
8 this.toolbar
= 'styles,10';
10 css
: [CKEDITOR
.skin
.getPath( 'editor' )].concat(editor
.config
.contentsCss
),
12 attributes
: {'aria-label': this.title
}
17 PlinnStylesCombo
.prototype.onRender = function() {
19 this.editor
.on('selectionChange', function(evt
){self
.checkSelection(evt
);});
22 PlinnStylesCombo
.prototype.checkSelection = function(evt
) {
23 if (evt
.data
.selection
.getRanges().length
> 1) {
31 PlinnStylesCombo
.prototype.loadStyle = function(definition
) {
32 this.styles
.push(definition
);
33 this.styles
[definition
.name
] = definition
;
36 PlinnStylesCombo
.prototype.init = function() {
38 for (i
=0 ; i
< this.styles
.length
; i
++) {
39 style
= this.styles
[i
];
41 '<div class="' + style
.className
+ '">' +
49 PlinnStylesCombo
.prototype.onClick = function(value
) {
51 this.editor
.fire('saveSnapshot');
52 var style
= this.styles
[value
]
53 var className
= style
.className
;
54 var ranges
= this.editor
.getSelection().getRanges();
55 var element
= this.editor
.elementPath().lastElement
;
56 if(ranges
.length
=== 1) {
57 var start
= ranges
[0].startContainer
;
58 var end
= ranges
[0].endContainer
;
59 if(start
.$ !== end
.$) {
60 // selection is a fragment that need to be wrapped in container to apply style
61 element
= new CKEDITOR
.dom
.element('div');
62 element
.append(ranges
[0].cloneContents());
63 this.editor
.insertElement(element
);
66 if (element
.hasClass(className
)) {
67 element
.removeClass(className
);
70 element
.addClass(className
);
72 this.editor
.fire('saveSnapshot');
75 var PlinnStylePlugin = function() {
76 this.requires
= 'richcombo';
77 this.combo
= undefined;
80 PlinnStylePlugin
.prototype.init = function(editor
) {
81 this.combo
= new PlinnStylesCombo(editor
);
82 editor
.ui
.addRichCombo('PlinnStyles', this.combo
);
84 editor
.on('stylesSet', function(evt
){self
.onStylesSet(evt
);});
87 PlinnStylePlugin
.prototype.onStylesSet = function(evt
) {
88 var stylesDefinitions
= evt
.data
.styles
;
89 if (!stylesDefinitions
) { return; }
91 for(i
=0 ; i
< stylesDefinitions
.length
; i
++) {
92 this.combo
.loadStyle(stylesDefinitions
[i
]);
99 CKEDITOR
.plugins
.add( 'plinn_styles', new PlinnStylePlugin());