8
0
Mateusz Samsel 6 жил өмнө
parent
commit
a542de6e09

+ 9 - 0
packages/ckeditor5-font/src/documentcolorscollection.js

@@ -63,6 +63,15 @@ export default class DocumentColorsCollection extends Collection {
 
 
 		return ret;
 		return ret;
 	}
 	}
+
+	/**
+	 * Checks if object with given colors is present in collection.
+	 *
+	 * @param {String} color
+	 */
+	hasColor( color ) {
+		return !!this.find( item => item.color === color );
+	}
 }
 }
 
 
 mix( DocumentColorsCollection, ObservableMixin );
 mix( DocumentColorsCollection, ObservableMixin );

+ 4 - 7
packages/ckeditor5-font/src/ui/colortableview.js

@@ -204,14 +204,15 @@ export default class ColorTableView extends View {
 		const maxCount = this.documentColorsCount;
 		const maxCount = this.documentColorsCount;
 
 
 		this.documentColors.clear();
 		this.documentColors.clear();
-		this.documentColors.selectedColor = null;
 
 
 		for ( const rootName of document.getRootNames() ) {
 		for ( const rootName of document.getRootNames() ) {
 			const root = document.getRoot( rootName );
 			const root = document.getRoot( rootName );
 			const range = model.createRangeIn( root );
 			const range = model.createRangeIn( root );
+
 			for ( const node of range.getItems() ) {
 			for ( const node of range.getItems() ) {
 				if ( node.is( 'textProxy' ) && node.hasAttribute( componentName ) ) {
 				if ( node.is( 'textProxy' ) && node.hasAttribute( componentName ) ) {
 					this._addColorToDocumentColors( node.getAttribute( componentName ) );
 					this._addColorToDocumentColors( node.getAttribute( componentName ) );
+
 					if ( this.documentColors.length >= maxCount ) {
 					if ( this.documentColors.length >= maxCount ) {
 						return;
 						return;
 					}
 					}
@@ -229,8 +230,8 @@ export default class ColorTableView extends View {
 		const staticColorsGrid = this.staticColorsGrid;
 		const staticColorsGrid = this.staticColorsGrid;
 		const selectedColor = this.selectedColor;
 		const selectedColor = this.selectedColor;
 
 
-		if ( documentColorsGrid ) {
-			if ( isInDocumentColors( documentColorsGrid.items, selectedColor ) ) {
+		if ( !this.documentColors.isEmpty ) {
+			if ( this.documentColors.hasColor( selectedColor ) ) {
 				staticColorsGrid.selectedColor = null;
 				staticColorsGrid.selectedColor = null;
 				documentColorsGrid.selectedColor = selectedColor;
 				documentColorsGrid.selectedColor = selectedColor;
 			} else {
 			} else {
@@ -395,7 +396,3 @@ export default class ColorTableView extends View {
 		}
 		}
 	}
 	}
 }
 }
-
-function isInDocumentColors( collection, color ) {
-	return !!collection.find( item => item.color === color );
-}

+ 5 - 0
packages/ckeditor5-font/tests/documentcolorscollection.js

@@ -53,4 +53,9 @@ describe( 'DocumentColorsCollection', () => {
 		documentColorsCollection.add( { color: '111' } );
 		documentColorsCollection.add( { color: '111' } );
 		expect( documentColorsCollection.length ).to.equal( 4 );
 		expect( documentColorsCollection.length ).to.equal( 4 );
 	} );
 	} );
+
+	it( 'hasColor()', () => {
+		expect( documentColorsCollection.hasColor( '111' ) ).to.be.true;
+		expect( documentColorsCollection.hasColor( '555' ) ).to.be.false;
+	} );
 } );
 } );

+ 2 - 3
packages/ckeditor5-font/tests/ui/colortableview.js

@@ -246,6 +246,7 @@ describe( 'ColorTableView', () => {
 				hasBorder: true
 				hasBorder: true
 			}
 			}
 		};
 		};
+
 		describe( 'default checks', () => {
 		describe( 'default checks', () => {
 			let documentColorsGridView, documentColors;
 			let documentColorsGridView, documentColors;
 
 
@@ -329,13 +330,12 @@ describe( 'ColorTableView', () => {
 			describe( 'binding', () => {
 			describe( 'binding', () => {
 				it( 'add tile item when document colors model is updated', () => {
 				it( 'add tile item when document colors model is updated', () => {
 					let itm;
 					let itm;
+
 					expect( documentColors.length ).to.equal( 0 );
 					expect( documentColors.length ).to.equal( 0 );
 					expect( documentColorsGridView.items.length ).to.equal( 0 );
 					expect( documentColorsGridView.items.length ).to.equal( 0 );
 
 
 					documentColors.add( Object.assign( {}, colorBlack ) );
 					documentColors.add( Object.assign( {}, colorBlack ) );
-
 					expect( documentColors.length ).to.equal( 1 );
 					expect( documentColors.length ).to.equal( 1 );
-
 					expect( documentColorsGridView.items.length ).to.equal( 1 );
 					expect( documentColorsGridView.items.length ).to.equal( 1 );
 
 
 					itm = documentColorsGridView.items.first;
 					itm = documentColorsGridView.items.first;
@@ -496,7 +496,6 @@ describe( 'ColorTableView', () => {
 				.then( newEditor => {
 				.then( newEditor => {
 					editor = newEditor;
 					editor = newEditor;
 					model = editor.model;
 					model = editor.model;
-					// testColorPlugin = newEditor.plugins.get( 'TestColorPlugin' );
 					dropdown = editor.ui.componentFactory.create( 'testColor' );
 					dropdown = editor.ui.componentFactory.create( 'testColor' );
 
 
 					dropdown.render();
 					dropdown.render();