Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 165417c

Browse files
authored
Merge pull request #60 from ckeditor/i/6192
Other: Implemented lazy loading for the font color and background color dropdowns. This will reduce editor initialization time. Closes ckeditor/ckeditor5#6192.
2 parents c3e7673 + c73c882 commit 165417c

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

src/ui/colortableview.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,21 @@ export default class ColorTableView extends View {
118118
* Preserves the reference to {@link module:ui/colorgrid/colorgrid~ColorGridView} used to create
119119
* the default (static) color set.
120120
*
121+
* The property is loaded once the the parent dropdown is opened the first time.
122+
*
121123
* @readonly
122-
* @member {module:ui/colorgrid/colorgrid~ColorGridView}
124+
* @member {module:ui/colorgrid/colorgrid~ColorGridView|undefined} staticColorsGrid
123125
*/
124-
this.staticColorsGrid = this._createStaticColorsGrid();
125126

126127
/**
127128
* Preserves the reference to {@link module:ui/colorgrid/colorgrid~ColorGridView} used to create
128129
* the document colors. It remains undefined if the document colors feature is disabled.
129130
*
131+
* The property is loaded once the the parent dropdown is opened the first time.
132+
*
130133
* @readonly
131-
* @member {module:ui/colorgrid/colorgrid~ColorGridView}
134+
* @member {module:ui/colorgrid/colorgrid~ColorGridView|undefined} documentColorsGrid
132135
*/
133-
this.documentColorsGrid;
134136

135137
/**
136138
* Helps cycling over focusable {@link #items} in the list.
@@ -152,6 +154,15 @@ export default class ColorTableView extends View {
152154
}
153155
} );
154156

157+
/**
158+
* Document color section's label.
159+
*
160+
* @private
161+
* @readonly
162+
* @type {String}
163+
*/
164+
this._documentColorsLabel = documentColorsLabel;
165+
155166
this.setTemplate( {
156167
tag: 'div',
157168
attributes: {
@@ -164,29 +175,6 @@ export default class ColorTableView extends View {
164175
} );
165176

166177
this.items.add( this._removeColorButton() );
167-
this.items.add( this.staticColorsGrid );
168-
169-
if ( documentColorsCount ) {
170-
// Create a label for document colors.
171-
const bind = Template.bind( this.documentColors, this.documentColors );
172-
const label = new LabelView( this.locale );
173-
174-
label.text = documentColorsLabel;
175-
label.extendTemplate( {
176-
attributes: {
177-
class: [
178-
'ck',
179-
'ck-color-grid__label',
180-
bind.if( 'isEmpty', 'ck-hidden' )
181-
]
182-
}
183-
} );
184-
185-
this.items.add( label );
186-
187-
this.documentColorsGrid = this._createDocumentColorsGrid();
188-
this.items.add( this.documentColorsGrid );
189-
}
190178
}
191179

192180
/**
@@ -252,6 +240,38 @@ export default class ColorTableView extends View {
252240
this.keystrokes.listenTo( this.element );
253241
}
254242

243+
/**
244+
* Appends {@link #staticColorsGrid} and {@link #documentColorsGrid} views.
245+
*/
246+
appendGrids() {
247+
if ( this.staticColorsGrid ) {
248+
return;
249+
}
250+
251+
this.staticColorsGrid = this._createStaticColorsGrid();
252+
253+
this.items.add( this.staticColorsGrid );
254+
255+
if ( this.documentColorsCount ) {
256+
// Create a label for document colors.
257+
const bind = Template.bind( this.documentColors, this.documentColors );
258+
const label = new LabelView( this.locale );
259+
label.text = this._documentColorsLabel;
260+
label.extendTemplate( {
261+
attributes: {
262+
class: [
263+
'ck',
264+
'ck-color-grid__label',
265+
bind.if( 'isEmpty', 'ck-hidden' )
266+
]
267+
}
268+
} );
269+
this.items.add( label );
270+
this.documentColorsGrid = this._createDocumentColorsGrid();
271+
this.items.add( this.documentColorsGrid );
272+
}
273+
}
274+
255275
/**
256276
* Focuses the first focusable element in {@link #items}.
257277
*/

src/ui/colorui.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export default class ColorUI extends Plugin {
130130
} );
131131

132132
dropdownView.on( 'change:isOpen', ( evt, name, isVisible ) => {
133+
// Grids rendering is deferred (#6192).
134+
dropdownView.colorTableView.appendGrids();
135+
133136
if ( isVisible ) {
134137
if ( documentColorsCount !== 0 ) {
135138
this.colorTableView.updateDocumentColors( editor.model, this.componentName );

tests/ui/colortableview.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe( 'ColorTableView', () => {
7979
documentColorsLabel: 'Document colors',
8080
documentColorsCount: 4
8181
} );
82+
colorTableView.appendGrids();
8283
colorTableView.render();
8384
} );
8485

@@ -363,6 +364,7 @@ describe( 'ColorTableView', () => {
363364
removeButtonLabel: 'Remove color',
364365
documentColorsCount: 0
365366
} );
367+
colorTableView.appendGrids();
366368
colorTableView.render();
367369
} );
368370

@@ -418,6 +420,8 @@ describe( 'ColorTableView', () => {
418420
model = editor.model;
419421

420422
dropdown = editor.ui.componentFactory.create( 'testColor' );
423+
dropdown.isOpen = true;
424+
dropdown.isOpen = false;
421425
dropdown.render();
422426

423427
staticColorsGrid = dropdown.colorTableView.staticColorsGrid;

tests/ui/colorui.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import TestColorPlugin from '../_utils/testcolorplugin';
99
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
10+
import ColorGridView from '@ckeditor/ckeditor5-ui/src/colorgrid/colorgridview';
1011
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
1112
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
1213
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
@@ -116,6 +117,7 @@ describe( 'ColorUI', () => {
116117
beforeEach( () => {
117118
command = editor.commands.get( 'testColorCommand' );
118119
dropdown = editor.ui.componentFactory.create( 'testColor' );
120+
dropdown.isOpen = true;
119121

120122
dropdown.render();
121123
} );
@@ -152,6 +154,15 @@ describe( 'ColorUI', () => {
152154
expect( colorTableView.documentColorsCount ).to.equal( 3 );
153155
} );
154156

157+
it( 'does not initialize grids when not open', () => {
158+
const localDropdown = editor.ui.componentFactory.create( 'testColor' );
159+
localDropdown.render();
160+
161+
for ( const item of localDropdown.colorTableView.items ) {
162+
expect( item ).not.to.be.instanceOf( ColorGridView );
163+
}
164+
} );
165+
155166
describe( 'model to command binding', () => {
156167
it( 'isEnabled', () => {
157168
command.isEnabled = false;
@@ -308,6 +319,7 @@ describe( 'ColorUI', () => {
308319

309320
colors.forEach( test => {
310321
it( `tested color "${ test.color }" translated to "${ test.label }".`, () => {
322+
dropdown.isOpen = true;
311323
const colorGrid = dropdown.colorTableView.items.get( 1 );
312324
const tile = colorGrid.items.find( colorTile => test.color === colorTile.color );
313325

0 commit comments

Comments
 (0)