8
0
Quellcode durchsuchen

Renamed command.format to command.value. Simplified command implementation.

Piotrek Koszuliński vor 9 Jahren
Ursprung
Commit
44f8a19d0c

+ 1 - 1
packages/ckeditor5-heading/src/formats.js

@@ -46,7 +46,7 @@ export default class Formats extends Feature {
 
 		// Bind dropdown model to command.
 		dropdownModel.bind( 'isEnabled' ).to( command, 'isEnabled' );
-		dropdownModel.bind( 'label' ).to( command, 'format', format => format.label );
+		dropdownModel.bind( 'label' ).to( command, 'value', format => format.label );
 
 		// Execute command when item from dropdown is selected.
 		this.listenTo( itemListModel, 'execute', ( evt, itemModel ) => {

+ 27 - 41
packages/ckeditor5-heading/src/formatscommand.js

@@ -13,9 +13,8 @@ export default class FormatsCommand extends Command {
 		super( editor );
 
 		this.formats = formats;
-		this.defaultFormat = this._getDefaultFormat();
 
-		this.set( 'format', this.defaultFormat );
+		this.set( 'value', this.defaultFormat );
 
 		// Listen on selection change and set current command's format to format in current selection.
 		this.listenTo( editor.document.selection, 'change', () => {
@@ -26,28 +25,35 @@ export default class FormatsCommand extends Command {
 				const format = this._getFormatById( block.name );
 
 				// TODO: What should happen if format is not found?
-				this.format = format;
+				this.value = format;
 			}
 		} );
 	}
 
-	_doExecute( formatId ) {
+	/**
+	 * The default format.
+	 *
+	 * @type {Object}
+	 */
+	get defaultFormat() {
+		// See https://github.com/ckeditor/ckeditor5/issues/98.
+		return this._getFormatById( 'paragraph' );
+	}
+
+	_doExecute( formatId = this.defaultFormat.id ) {
 		// TODO: What should happen if format is not found?
-		const document = this.editor.document;
-		const selection = document.selection;
-		const newValue = ( formatId === undefined ) ? this.defaultFormat.id : formatId;
+		const doc = this.editor.document;
+		const selection = doc.selection;
 		const startPosition = selection.getFirstPosition();
 		const elements = [];
-		let ranges = null;
-		let isSelectionBackward = false;
-		let remove = false;
-
+		// Storing selection ranges and direction to fix selection after renaming. See ckeditor5-engine#367.
+		const ranges = [ ...selection.getRanges() ];
+		const isSelectionBackward = selection.isBackward;
 		// If current format is same as new format - toggle already applied format back to default one.
-		if ( newValue === this.format.id ) {
-			remove = true;
-		}
+		const shouldRemove = ( formatId === this.value.id );
 
 		// Collect elements to change format.
+		// This implementation may not be future proof but it's satisfactory at this stage.
 		if ( selection.isCollapsed ) {
 			const block = findTopmostBlock( startPosition );
 
@@ -55,10 +61,6 @@ export default class FormatsCommand extends Command {
 				elements.push( block );
 			}
 		} else {
-			// Storing selection ranges and direction to fix selection after renaming. See ckeditor5-engine#367.
-			ranges = [ ...selection.getRanges() ];
-			isSelectionBackward = selection.isBackward;
-
 			for ( let range of ranges ) {
 				let startBlock = findTopmostBlock( range.start );
 				const endBlock = findTopmostBlock( range.end, false );
@@ -72,27 +74,25 @@ export default class FormatsCommand extends Command {
 			}
 		}
 
-		document.enqueueChanges( () => {
-			const batch = document.batch();
+		doc.enqueueChanges( () => {
+			const batch = doc.batch();
 
 			for ( let element of elements ) {
 				// When removing applied format.
-				if ( remove ) {
-					if ( element.name === newValue ) {
+				if ( shouldRemove ) {
+					if ( element.name === formatId ) {
 						batch.rename( this.defaultFormat.id, element );
 					}
 				}
 				// When applying new format.
 				else {
-					batch.rename( newValue, element );
+					batch.rename( formatId, element );
 				}
 			}
 
 			// If range's selection start/end is placed directly in renamed block - we need to restore it's position
 			// after renaming, because renaming puts new element there.
-			if ( ranges !== null ) {
-				document.selection.setRanges( ranges, isSelectionBackward );
-			}
+			doc.selection.setRanges( ranges, isSelectionBackward );
 		} );
 	}
 
@@ -104,21 +104,7 @@ export default class FormatsCommand extends Command {
 	 * @returns {Object}
 	 */
 	_getFormatById( id ) {
-		return this.formats.find( item => {
-			return item.id && item.id === id;
-		} );
-	}
-
-	/**
-	 * Returns default format.
-	 *
-	 * @private
-	 * @returns {Object}
-	 */
-	_getDefaultFormat() {
-		return this.formats.find( item => {
-			return item.default;
-		} );
+		return this.formats.find( item => item.id === id );
 	}
 }
 

+ 2 - 4
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', default: true },
+	{ id: 'paragraph', viewElement: 'p', label: 'Paragraph' },
 	{ id: 'heading1', viewElement: 'h2', label: 'Heading 1' },
 	{ id: 'heading2', viewElement: 'h3', label: 'Heading 2' },
 	{ id: 'heading3', viewElement: 'h4', label: 'Heading 3' }
@@ -25,8 +25,6 @@ export default class FormatsEngine extends Feature {
 
 	init() {
 		const editor = this.editor;
-		const document = editor.document;
-		const schema = document.schema;
 		const data = editor.data;
 		const editing = editor.editing;
 
@@ -34,7 +32,7 @@ export default class FormatsEngine extends Feature {
 			// Skip paragraph - it is defined in required Paragraph feature.
 			if ( format.id !== 'paragraph' ) {
 				// Schema.
-				schema.registerItem( format.id, '$block' );
+				editor.document.schema.registerItem( format.id, '$block' );
 
 				// Build converter from model to view for data and editing pipelines.
 				BuildModelConverterFor( data.modelToView, editing.modelToView )

+ 0 - 1
packages/ckeditor5-heading/tests/formats.html

@@ -1 +0,0 @@
-<div id="editor"></div>

+ 5 - 2
packages/ckeditor5-heading/tests/formats.js

@@ -17,7 +17,10 @@ describe( 'Formats', () => {
 	let editor, controller;
 
 	beforeEach( () => {
-		return ClassicTestEditor.create( document.getElementById( 'editor' ), {
+		const editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
+
+		return ClassicTestEditor.create( editorElement, {
 			features: [ Formats ],
 			toolbar: [ 'formats' ]
 		} )
@@ -72,7 +75,7 @@ describe( 'Formats', () => {
 
 		it( 'label', () => {
 			expect( model.label ).to.equal( 'Paragraph' );
-			command.format = command.formats[ 1 ];
+			command.value = command.formats[ 1 ];
 			expect( model.label ).to.equal( 'Heading 1' );
 		} );
 	} );

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

@@ -52,7 +52,7 @@ describe( 'FormatsCommand', () => {
 				const element = root.getChild( 0 );
 				document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
 
-				expect( command.format ).to.equal( format );
+				expect( command.value ).to.equal( format );
 			} );
 		}
 	} );