Skip to content

Commit 8abbc10

Browse files
committed
Merge branch 't/10136' into major
2 parents 75f88e4 + f23c001 commit 8abbc10

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

core/editor.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@
399399
// be called asynchronously.
400400
// In both cases - styles will be preload before plugins initialization.
401401
function preloadStylesSet( editor ) {
402-
editor.getStylesSet( function() {
402+
editor.getStylesSet( function( styles ) {
403+
// Wait for editor#loaded, so plugins could add their listeners.
404+
// But listen with high priority to fire editor#stylesSet before editor#uiReady and editor#setData.
405+
editor.once( 'loaded', function() {
406+
// Note: we can't use fireOnce because this event may canceled and fired again.
407+
editor.fire( 'stylesSet', { styles: styles } );
408+
}, null, null, 1 );
409+
403410
loadPlugins( editor );
404411
} );
405412
}
@@ -1147,6 +1154,19 @@ CKEDITOR.ELEMENT_MODE_INLINE = 3;
11471154
* @param {CKEDITOR.editor} editor This editor instance.
11481155
*/
11491156

1157+
/**
1158+
* Fired when styles set is loaded. During editor initialization
1159+
* phase the {@link #getStylesSet} method returns only styles that
1160+
* are already loaded, which may not include e.g. styles parsed
1161+
* by `stylesheetparser` plugin. Thus, to be notified when all
1162+
* styles are ready you can listen on this event.
1163+
*
1164+
* @since 4.1
1165+
* @event stylesSet
1166+
* @param {CKEDITOR.editor} editor This editor instance.
1167+
* @param {Array} styles Array of styles definitions.
1168+
*/
1169+
11501170
/**
11511171
* Fired before the command execution when {@link #execCommand} is called.
11521172
*

plugins/stylescombo/plugin.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
combo,
1919
allowedContent = [];
2020

21-
editor.getStylesSet( function( stylesDefinitions ) {
21+
editor.on( 'stylesSet', function( evt ) {
22+
var stylesDefinitions = evt.data.styles;
23+
2224
if ( !stylesDefinitions )
2325
return;
2426

@@ -50,13 +52,7 @@
5052

5153
// Sorts the Array, so the styles get grouped by type in proper order (#9029).
5254
stylesList.sort( function( styleA, styleB ) { return styleA._.weight - styleB._.weight; } );
53-
});
54-
55-
// Hide entire combo when all styles are rejected.
56-
// Although it looks like editor.getStylesSet is asynchronous,
57-
// at this point it should behave synchronously.
58-
if ( !stylesList.length )
59-
return;
55+
} );
6056

6157
editor.ui.addRichCombo( 'Styles', {
6258
label: lang.label,

plugins/stylesheetparser/plugin.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,36 @@
8787

8888
// Register a plugin named "stylesheetparser".
8989
CKEDITOR.plugins.add( 'stylesheetparser', {
90-
onLoad: function() {
91-
var obj = CKEDITOR.editor.prototype;
92-
obj.getStylesSet = CKEDITOR.tools.override( obj.getStylesSet, function( org ) {
93-
return function( callback ) {
94-
var self = this;
95-
org.call( this, function( definitions ) {
90+
init: function( editor ) {
91+
// Stylesheet parser is incompatible with filter (#10136).
92+
editor.filter.disable();
93+
94+
var cachedDefinitions;
95+
96+
editor.once( 'stylesSet', function( evt ) {
97+
// Cancel event and fire it again when styles are ready.
98+
evt.cancel();
99+
100+
// Overwrite editor#getStylesSet asap (contentDom is the first moment
101+
// when editor.document is ready), but before stylescombo reads styles set (priority 5).
102+
editor.once( 'contentDom', function() {
103+
editor.getStylesSet( function( definitions ) {
96104
// Rules that must be skipped
97-
var skipSelectors = self.config.stylesheetParser_skipSelectors || ( /(^body\.|^\.)/i ),
105+
var skipSelectors = editor.config.stylesheetParser_skipSelectors || ( /(^body\.|^\.)/i ),
98106
// Rules that are valid
99-
validSelectors = self.config.stylesheetParser_validSelectors || ( /\w+\.\w+/ );
107+
validSelectors = editor.config.stylesheetParser_validSelectors || ( /\w+\.\w+/ );
100108

101-
callback( ( self._.stylesDefinitions = definitions.concat( LoadStylesCSS( self.document.$, skipSelectors, validSelectors ) ) ) );
102-
});
103-
};
104-
});
109+
cachedDefinitions = definitions.concat( LoadStylesCSS( editor.document.$, skipSelectors, validSelectors ) );
110+
111+
editor.getStylesSet = function( callback ) {
112+
if ( cachedDefinitions )
113+
return callback( cachedDefinitions );
114+
};
105115

116+
editor.fire( 'stylesSet', { styles: cachedDefinitions } );
117+
} );
118+
} );
119+
}, null, null, 1 );
106120
}
107121
});
108122
})();

0 commit comments

Comments
 (0)