Bläddra i källkod

Changed command value to format.

Szymon Kupś 9 år sedan
förälder
incheckning
80e47118d7

+ 38 - 7
packages/ckeditor5-heading/src/formatscommand.js

@@ -12,34 +12,53 @@ export default class FormatsCommand extends Command {
 		super( editor );
 
 		this.formats = formats;
-		this.defaultFormat = this.formats[ 0 ];
+		this.defaultFormat = this._getDefaultFormat();
 
-		this.set( 'value', this.formats[ 0 ] );
+		this.set( 'format', this.defaultFormat );
 
+		// Listen on selection change and set current command's format to format in current selection.
 		this.listenTo( editor.document.selection, 'change', () => {
 			const position = editor.document.selection.getFirstPosition();
 			const parent = position.parent;
-			this.value = parent.name;
+			const format = this._getFormatById( parent.name );
+
+			// TODO: What should happen if current format is not found? Is it possible?
+			if ( format !== undefined ) {
+				this.format = format;
+			}
 		} );
 	}
 
-	_doExecute( forceValue ) {
+	_doExecute( formatId ) {
+		// TODO: Check if format Id is valid.
 		const document = this.editor.document;
 		const selection = document.selection;
-		const newValue = ( forceValue === undefined ) ? 'paragraph' : forceValue;
+		const newValue = ( formatId === undefined ) ? this.defaultFormat.id : formatId;
 		const position = selection.getFirstPosition();
 		const elements = [];
 		let remove = false;
 
-		// If start position is same as new value - we are toggling already applied format back to default one.
-		if ( newValue === position.parent.name ) {
+		// If current format is same as new format - toggle already applied format back to default one.
+		if ( newValue === this.format.id ) {
 			remove = true;
 		}
 
 		if ( selection.isCollapsed ) {
 			elements.push( position.parent );
+		} else {
+			const ranges = selection.getRanges();
+
+			for ( let range in ranges ) {
+				const start = range.start;
+				const end = range.end;
+
+				console.log( start );
+			}
+
 		}
 
+		//TODO: when selection is not collapsed - gather all elements that needs to be renamed.
+
 		document.enqueueChanges( () => {
 			const batch = document.batch();
 
@@ -57,4 +76,16 @@ export default class FormatsCommand extends Command {
 			}
 		} );
 	}
+
+	_getFormatById( id ) {
+		return this.formats.find( ( item ) => {
+			return item.id && item.id === id;
+		} );
+	}
+
+	_getDefaultFormat() {
+		return this.formats.find( ( item ) => {
+			return item.default;
+		} );
+	}
 }

+ 2 - 2
packages/ckeditor5-heading/src/formatsengine.js

@@ -12,7 +12,7 @@ import Paragraph from '../paragraph/paragraph.js';
 import FormatsCommand from './formatscommand.js';
 
 const formats = [
-	{ id: 'paragraph', viewElement: 'p', label: 'Paragraph' },
+	{ id: 'paragraph', viewElement: 'p', label: 'Paragraph', default: true },
 	{ id: 'heading1', viewElement: 'h2', label: 'Heading 1' },
 	{ id: 'heading2', viewElement: 'h3', label: 'Heading 2' },
 	{ id: 'heading3', viewElement: 'h4', label: 'Heading 3' }
@@ -36,7 +36,7 @@ export default class FormatsEngine extends Feature {
 				// Schema.
 				schema.registerItem( format.id, '$block' );
 
-				// Build converter from model to view for data pipeline.
+				// Build converter from model to view for data and editing pipelines.
 				BuildModelConverterFor( data.modelToView, editing.modelToView )
 					.fromElement( format.id )
 					.toElement( format.viewElement );

+ 2 - 2
packages/ckeditor5-heading/tests/formatscommand.js

@@ -11,7 +11,7 @@ import Range from '/ckeditor5/engine/model/range.js';
 import { setData, getData } from '/tests/engine/_utils/model.js';
 
 const formats = [
-	{ id: 'paragraph', viewElement: 'p' },
+	{ id: 'paragraph', viewElement: 'p', default: true },
 	{ id: 'heading1', viewElement: 'h2' },
 	{ id: 'heading2', viewElement: 'h3' },
 	{ id: 'heading3', viewElement: 'h4' }
@@ -48,7 +48,7 @@ describe( 'FormatsCommand', () => {
 				const element = root.getChild( 0 );
 				document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
 
-				expect( command.value ).to.equal( format.id );
+				expect( command.format ).to.equal( format );
 			} );
 		}
 	} );