8
0
Эх сурвалжийг харах

Improve code styling, extract part of the code to separate funciton.

Mateusz Samsel 6 жил өмнө
parent
commit
b3fcfd44a9

+ 30 - 22
packages/ckeditor5-font/src/ui/colorui.js

@@ -138,28 +138,7 @@ export default class ColorUI extends Plugin {
 			if ( documentColorsCount !== 0 ) {
 				dropdownView.on( 'change:isOpen', ( evt, name, val ) => {
 					if ( val ) {
-						const model = editor.model;
-						const document = model.document;
-						const rootNames = document.getRootNames();
-						const maxCount = this.colorTableView.documentColorsCount;
-						const documentColors = this.colorTableView.documentColors;
-						documentColors.clear();
-
-						for ( const rootName of rootNames ) {
-							const root = document.getRoot( rootName );
-							const range = model.createRangeIn( root );
-							for ( const node of range.getItems() ) {
-								if ( node.is( 'textProxy' ) && node.hasAttribute( this.componentName ) ) {
-									this.addColorToDocumentColors( node.getAttribute( this.componentName ) );
-									if ( documentColors.length >= maxCount ) {
-										break;
-									}
-								}
-							}
-							if ( documentColors.length >= maxCount ) {
-								break;
-							}
-						}
+						this.findAndSetDocumentColors();
 					}
 				} );
 			}
@@ -168,6 +147,34 @@ export default class ColorUI extends Plugin {
 		} );
 	}
 
+	/**
+	 * Method scans through editor's content and search for text node attributes with name {@link #commandName}.
+	 * Found entries are set as document colors. Previously stored document colors are lost with that process.
+	 *
+	 * @private
+	 */
+	findAndSetDocumentColors() {
+		const model = this.editor.model;
+		const document = model.document;
+		const maxCount = this.colorTableView.documentColorsCount;
+		const documentColors = this.colorTableView.documentColors;
+
+		documentColors.clear();
+
+		for ( const rootName of document.getRootNames() ) {
+			const root = document.getRoot( rootName );
+			const range = model.createRangeIn( root );
+			for ( const node of range.getItems() ) {
+				if ( node.is( 'textProxy' ) && node.hasAttribute( this.componentName ) ) {
+					this.addColorToDocumentColors( node.getAttribute( this.componentName ) );
+					if ( documentColors.length >= maxCount ) {
+						return;
+					}
+				}
+			}
+		}
+	}
+
 	/**
 	 * Method tries to find a color on predefined color list to prevent of creating custom color from it.
 	 * If color is found, then it is added to the {@link module:font/ui/colortableview~ColorTableView#documentColors} model.
@@ -179,6 +186,7 @@ export default class ColorUI extends Plugin {
 	addColorToDocumentColors( color ) {
 		const predefinedColor = this.colorTableView.colorDefinitions
 			.find( definition => definition.color === color );
+
 		if ( !predefinedColor ) {
 			this.colorTableView.documentColors.add( {
 				color,

+ 1 - 1
packages/ckeditor5-font/tests/manual/font-color.html

@@ -11,7 +11,7 @@
 	<p><span style="color:hsl(270, 75%, 60%);background-color:#d82;">10. color:hsl(270, 75%, 60%);background-color:#d82;</span></p>
 </div>
 
-<div id="color-box" style="margin-top: 10px;border:1px solid black;padding:5px;">
+<div id="color-box" style="margin-top: 10px; border: 1px solid black; padding: 5px">
 	<label>
 		Color:
 		<input type="text" name="color" id="color">