8
0
Prechádzať zdrojové kódy

Rename custom attribtues to manualdecorators, to keep consistency. Fix test related to that and docs links.

Mateusz Samsel 6 rokov pred
rodič
commit
d7a3e31cd5

+ 22 - 18
packages/ckeditor5-link/src/linkcommand.js

@@ -32,12 +32,12 @@ export default class LinkCommand extends Command {
 		/**
 		 * Keeps collection of {@link module:link/utils~ManualDecorator}
 		 * corresponding to {@link module:link/link~LinkConfig#decorators}.
-		 * You can consider it as a model of states for custom attributes added to links.
+		 * You can consider it as a model with states of manual decorators added to currently selected link.
 		 *
 		 * @readonly
 		 * @type {module:utils/collection~Collection}
 		 */
-		this.customAttributes = new Collection();
+		this.manualDecorators = new Collection();
 	}
 
 	/**
@@ -49,8 +49,8 @@ export default class LinkCommand extends Command {
 
 		this.value = doc.selection.getAttribute( 'linkHref' );
 
-		for ( const customAttr of this.customAttributes ) {
-			customAttr.value = doc.selection.getAttribute( customAttr.id ) || false;
+		for ( const manualDecorator of this.manualDecorators ) {
+			manualDecorator.value = doc.selection.getAttribute( manualDecorator.id ) || false;
 		}
 
 		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
@@ -71,20 +71,20 @@ export default class LinkCommand extends Command {
 	 *
 	 * @fires execute
 	 * @param {String} href Link destination.
+	 * @param {Object} [manualDecorators={}] Keeps information about turned on and off manual decorators applied with command.
 	 */
-	execute( href, customAttrs = {} ) {
+	execute( href, manualDecorators = {} ) {
 		const model = this.editor.model;
 		const selection = model.document.selection;
+		// Stores information about manual decorators to turn them on/off when command is applied.
+		const truthyManualDecorators = [];
+		const falsyManualDecorators = [];
 
-		// Stores information about custom attributes to turn on/off.
-		const truthyCustomAttributes = [];
-		const falsyCustomAttributes = [];
-
-		for ( const name in customAttrs ) {
-			if ( customAttrs[ name ] ) {
-				truthyCustomAttributes.push( name );
+		for ( const name in manualDecorators ) {
+			if ( manualDecorators[ name ] ) {
+				truthyManualDecorators.push( name );
 			} else {
-				falsyCustomAttributes.push( name );
+				falsyManualDecorators.push( name );
 			}
 		}
 
@@ -99,10 +99,12 @@ export default class LinkCommand extends Command {
 					const linkRange = findLinkRange( position, selection.getAttribute( 'linkHref' ), model );
 
 					writer.setAttribute( 'linkHref', href, linkRange );
-					truthyCustomAttributes.forEach( item => {
+
+					truthyManualDecorators.forEach( item => {
 						writer.setAttribute( item, true, linkRange );
 					} );
-					falsyCustomAttributes.forEach( item => {
+
+					falsyManualDecorators.forEach( item => {
 						writer.removeAttribute( item, linkRange );
 					} );
 
@@ -117,7 +119,7 @@ export default class LinkCommand extends Command {
 
 					attributes.set( 'linkHref', href );
 
-					truthyCustomAttributes.forEach( item => {
+					truthyManualDecorators.forEach( item => {
 						attributes.set( item, true );
 					} );
 
@@ -135,10 +137,12 @@ export default class LinkCommand extends Command {
 
 				for ( const range of ranges ) {
 					writer.setAttribute( 'linkHref', href, range );
-					truthyCustomAttributes.forEach( item => {
+
+					truthyManualDecorators.forEach( item => {
 						writer.setAttribute( item, true, range );
 					} );
-					falsyCustomAttributes.forEach( item => {
+
+					falsyManualDecorators.forEach( item => {
 						writer.removeAttribute( item, range );
 					} );
 				}

+ 11 - 8
packages/ckeditor5-link/src/linkediting.js

@@ -79,8 +79,8 @@ export default class LinkEditing extends Plugin {
 
 		const linkDecorators = editor.config.get( 'link.decorators' ) || [];
 
-		this.enableAutomaticDecorators( linkDecorators.filter( item => item.mode === DECORATOR_AUTOMATIC ) );
-		this.enableManualDecorators( linkDecorators.filter( item => item.mode === DECORATOR_MANUAL ) );
+		this._enableAutomaticDecorators( linkDecorators.filter( item => item.mode === DECORATOR_AUTOMATIC ) );
+		this._enableManualDecorators( linkDecorators.filter( item => item.mode === DECORATOR_MANUAL ) );
 
 		// Enable two-step caret movement for `linkHref` attribute.
 		bindTwoStepCaretToAttribute( editor.editing.view, editor.model, this, 'linkHref' );
@@ -101,7 +101,7 @@ export default class LinkEditing extends Plugin {
 	 * @private
 	 * @param {Array.<module:link/link~LinkDecoratorAutomaticOption>} automaticDecoratorDefinitions
 	 */
-	enableAutomaticDecorators( automaticDecoratorDefinitions ) {
+	_enableAutomaticDecorators( automaticDecoratorDefinitions ) {
 		const editor = this.editor;
 		const automaticDecorators = new AutomaticDecorators();
 		// Adds default decorator for external links.
@@ -127,7 +127,7 @@ export default class LinkEditing extends Plugin {
 	/**
 	 * Method process {@link module:link/link~LinkDecoratorManualOption} by transformation those configuration options into
 	 * {@link module:link/utils~ManualDecorator}. Manual decorators are added to
-	 * {@link module:link/linkcommand~LinkCommand#customAttributes} collections, which might be considered as a model
+	 * {@link module:link/linkcommand~LinkCommand#manualDecorators} collections, which might be considered as a model
 	 * for manual decorators state. It also provides proper
 	 * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement attributeToElement} converter for each
 	 * manual decorator and extends {@link module:engine/model/schema~Schema model's schema} with adequate model attributes.
@@ -135,27 +135,30 @@ export default class LinkEditing extends Plugin {
 	 * @private
 	 * @param {Array.<module:link/link~LinkDecoratorManualOption>} manualDecoratorDefinitions
 	 */
-	enableManualDecorators( manualDecoratorDefinitions ) {
+	_enableManualDecorators( manualDecoratorDefinitions ) {
 		if ( !manualDecoratorDefinitions.length ) {
 			return;
 		}
 
 		const editor = this.editor;
 		const command = editor.commands.get( 'link' );
-		const attrCollection = command.customAttributes;
+		const manualDecorators = command.manualDecorators;
 
 		manualDecoratorDefinitions.forEach( ( decorator, index ) => {
 			const decoratorName = `linkManualDecorator${ index }`;
+
 			editor.model.schema.extend( '$text', { allowAttributes: decoratorName } );
 
-			attrCollection.add( new ManualDecorator( Object.assign( { id: decoratorName }, decorator ) ) );
+			// Keeps reference to manual decorator to decode its name to attributes during downcast.
+			manualDecorators.add( new ManualDecorator( Object.assign( { id: decoratorName }, decorator ) ) );
+
 			editor.conversion.for( 'downcast' ).attributeToElement( {
 				model: decoratorName,
 				view: ( manualDecoratorName, writer ) => {
 					if ( manualDecoratorName ) {
 						const element = writer.createAttributeElement(
 							'a',
-							attrCollection.get( decoratorName ).attributes,
+							manualDecorators.get( decoratorName ).attributes,
 							{
 								priority: 5
 							}

+ 5 - 5
packages/ckeditor5-link/src/linkui.js

@@ -144,7 +144,7 @@ export default class LinkUI extends Plugin {
 		const editor = this.editor;
 		const linkCommand = editor.commands.get( 'link' );
 
-		const formView = new LinkFormView( editor.locale, linkCommand.customAttributes );
+		const formView = new LinkFormView( editor.locale, linkCommand.manualDecorators );
 
 		formView.urlInputView.bind( 'value' ).to( linkCommand, 'value' );
 
@@ -154,13 +154,13 @@ export default class LinkUI extends Plugin {
 
 		// Execute link command after clicking the "Save" button.
 		this.listenTo( formView, 'submit', () => {
-			const customAttributes = {};
+			const manualDecorators = {};
 
-			for ( const switchButton of formView.customAttributesView ) {
-				customAttributes[ switchButton.name ] = switchButton.isOn;
+			for ( const switchButton of formView.manualDecoratorsUIView ) {
+				manualDecorators[ switchButton.name ] = switchButton.isOn;
 			}
 
-			editor.execute( 'link', formView.urlInputView.inputView.element.value, customAttributes );
+			editor.execute( 'link', formView.urlInputView.inputView.element.value, manualDecorators );
 			this._closeFormView();
 		} );
 

+ 16 - 16
packages/ckeditor5-link/src/ui/linkformview.js

@@ -38,10 +38,10 @@ export default class LinkFormView extends View {
 	 * Also see {@link #render}.
 	 *
 	 * @param {module:utils/locale~Locale} [locale] The localization services instance.
-	 * @param {module:utils/collection~Collection} [customAttributes] Reference to custom attributes in
-	 * {@link module:link/linkcommand~LinkCommand#customAttributes}.
+	 * @param {module:utils/collection~Collection} [manualDecorators] Reference to custom attributes in
+	 * {@link module:link/linkcommand~LinkCommand#manualDecorators}.
 	 */
-	constructor( locale, customAttributes ) {
+	constructor( locale, manualDecorators ) {
 		super( locale );
 
 		const t = locale.t;
@@ -85,21 +85,21 @@ export default class LinkFormView extends View {
 		this.cancelButtonView = this._createButton( t( 'Cancel' ), cancelIcon, 'ck-button-cancel', 'cancel' );
 
 		/**
-		 * Keeps reference to {@link module:link/linkcommand~LinkCommand#customAttributes}.
+		 * Keeps reference to {@link module:link/linkcommand~LinkCommand#manualDecorators}.
 		 *
 		 * @readonly
 		 * @type {model:utils/collection~Collection}
 		 */
-		this.customAttributes = customAttributes;
+		this.manualDecorators = manualDecorators;
 
 		/**
-		 * Keeps reference to {@link module:ui/button/switchbuttonview~SwitchButtonView} made based on {@link #customAttributes}.
-		 * It use {@link #_createCustomAttributesView} to generate proper collection.
+		 * Keeps reference to {@link module:ui/button/switchbuttonview~SwitchButtonView} made based on {@link #manualDecorators}.
+		 * It use {@link #_createManualDecoratorsUIView} to generate proper collection.
 		 *
 		 * @readonly
 		 * @type {module:ui/viewcollection~ViewCollection}
 		 */
-		this.customAttributesView = this._createCustomAttributesView();
+		this.manualDecoratorsUIView = this._createManualDecoratorsUIView();
 
 		/**
 		 * Collection of views used as children elements in {@link module:link/ui/linkformview~LinkFormView}.
@@ -168,7 +168,7 @@ export default class LinkFormView extends View {
 		// Focus order should be different than position in DOM. Save/Cancel buttons should be focused at the end.
 		const childViews = [
 			this.urlInputView,
-			...this.customAttributesView,
+			...this.manualDecoratorsUIView,
 			this.saveButtonView,
 			this.cancelButtonView
 		];
@@ -243,15 +243,15 @@ export default class LinkFormView extends View {
 
 	/**
 	 * Prepare {@link module:ui/viewcollection~ViewCollection} of {@link module:ui/button/switchbuttonview~SwitchButtonView}
-	 * made based on {@link #customAttributes}
+	 * made based on {@link #manualDecorators}
 	 *
 	 * @private
 	 * @returns {module:ui/viewcollection~ViewCollection} of Switch Buttons.
 	 */
-	_createCustomAttributesView() {
+	_createManualDecoratorsUIView() {
 		const switches = this.createCollection();
 
-		switches.bindTo( this.customAttributes ).using( item => {
+		switches.bindTo( this.manualDecorators ).using( item => {
 			const switchButton = new SwitchButtonView( this.locale );
 
 			switchButton.set( {
@@ -272,8 +272,8 @@ export default class LinkFormView extends View {
 	}
 
 	/**
-	 * Creates {@link #children} for {@link module:link/ui/linkformview~LinkFormView}. If there exist {@link #customAttributes},
-	 * Then additional View wrapping all {@link #customAttributesView} will be added as a child of LinkFormView.
+	 * Creates {@link #children} for {@link module:link/ui/linkformview~LinkFormView}. If there exist {@link #manualDecorators},
+	 * Then additional View wrapping all {@link #manualDecoratorsUIView} will be added as a child of LinkFormView.
 	 *
 	 * @private
 	 * @returns {module:ui/viewcollection~ViewCollection} children of LinkFormView.
@@ -285,11 +285,11 @@ export default class LinkFormView extends View {
 		children.add( this.saveButtonView );
 		children.add( this.cancelButtonView );
 
-		if ( this.customAttributes.length ) {
+		if ( this.manualDecorators.length ) {
 			const additionalButtonsView = new View();
 			additionalButtonsView.setTemplate( {
 				tag: 'ul',
-				children: this.customAttributesView.map( switchButton => ( {
+				children: this.manualDecoratorsUIView.map( switchButton => ( {
 					tag: 'li',
 					children: [ switchButton ],
 					attributes: {

+ 1 - 1
packages/ckeditor5-link/src/unlinkcommand.js

@@ -47,7 +47,7 @@ export default class UnlinkCommand extends Command {
 				writer.removeAttribute( 'linkHref', range );
 				// If there are registered custom attributes, then remove them during unlink.
 				if ( linkCommand ) {
-					for ( const manualDecorator of linkCommand.customAttributes ) {
+					for ( const manualDecorator of linkCommand.manualDecorators ) {
 						writer.removeAttribute( manualDecorator.id, range );
 					}
 				}

+ 2 - 2
packages/ckeditor5-link/tests/linkcommand.js

@@ -270,14 +270,14 @@ describe( 'LinkCommand', () => {
 					model = editor.model;
 					command = new LinkCommand( editor );
 
-					command.customAttributes.add( new ManualDecorator( {
+					command.manualDecorators.add( new ManualDecorator( {
 						id: 'linkManualDecorator0',
 						label: 'Foo',
 						attributes: {
 							class: 'Foo'
 						}
 					} ) );
-					command.customAttributes.add( new ManualDecorator( {
+					command.manualDecorators.add( new ManualDecorator( {
 						id: 'linkManualDecorator1',
 						label: 'Bar',
 						attributes: {

+ 2 - 2
packages/ckeditor5-link/tests/linkui.js

@@ -1004,8 +1004,8 @@ describe( 'LinkUI', () => {
 
 						setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
 						expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
-						expect( formView.customAttributesView.length ).to.equal( 1 );
-						expect( formView.customAttributesView.first.isOn ).to.be.true;
+						expect( formView.manualDecoratorsUIView.length ).to.equal( 1 );
+						expect( formView.manualDecoratorsUIView.first.isOn ).to.be.true;
 
 						formView.fire( 'submit' );
 

+ 5 - 5
packages/ckeditor5-link/tests/ui/linkformview.js

@@ -226,17 +226,17 @@ describe( 'LinkFormView', () => {
 			collection.clear();
 		} );
 		it( 'switch buttons reflects state of customAttributes', () => {
-			expect( view.customAttributesView.length ).to.equal( 3 );
+			expect( view.manualDecoratorsUIView.length ).to.equal( 3 );
 
-			expect( view.customAttributesView.get( 0 ) ).to.deep.include( {
+			expect( view.manualDecoratorsUIView.get( 0 ) ).to.deep.include( {
 				name: 'decorator1',
 				label: 'Foo'
 			} );
-			expect( view.customAttributesView.get( 1 ) ).to.deep.include( {
+			expect( view.manualDecoratorsUIView.get( 1 ) ).to.deep.include( {
 				name: 'decorator2',
 				label: 'Download'
 			} );
-			expect( view.customAttributesView.get( 2 ) ).to.deep.include( {
+			expect( view.manualDecoratorsUIView.get( 2 ) ).to.deep.include( {
 				name: 'decorator3',
 				label: 'Multi'
 			} );
@@ -244,7 +244,7 @@ describe( 'LinkFormView', () => {
 
 		it( 'reacts on switch button changes', () => {
 			const modelItem = collection.first;
-			const viewItem = view.customAttributesView.first;
+			const viewItem = view.manualDecoratorsUIView.first;
 
 			expect( modelItem.value ).to.be.undefined;
 			expect( viewItem.isOn ).to.be.undefined;