8
0
Просмотр исходного кода

Fix bug when state of switch buttons was not reset when link form was cancelled.

Mateusz Samsel 6 лет назад
Родитель
Сommit
1aae04d25b
2 измененных файлов с 26 добавлено и 1 удалено
  1. 22 1
      packages/ckeditor5-link/src/linkcommand.js
  2. 4 0
      packages/ckeditor5-link/src/linkui.js

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

@@ -41,6 +41,15 @@ export default class LinkCommand extends Command {
 		this.manualDecorators = new Collection();
 	}
 
+	/**
+	 * Synchronize state of the decorator with actually present elements in the model.
+	 */
+	restoreManualDecoratorStates() {
+		for ( const manualDecorator of this.manualDecorators ) {
+			manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
+		}
+	}
+
 	/**
 	 * @inheritDoc
 	 */
@@ -51,7 +60,7 @@ export default class LinkCommand extends Command {
 		this.value = doc.selection.getAttribute( 'linkHref' );
 
 		for ( const manualDecorator of this.manualDecorators ) {
-			manualDecorator.value = doc.selection.getAttribute( manualDecorator.id ) || false;
+			manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
 		}
 
 		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
@@ -192,4 +201,16 @@ export default class LinkCommand extends Command {
 			}
 		} );
 	}
+
+	/**
+	 * Method provides information if given decorator is present in currently processed selection.
+	 *
+	 * @private
+	 * @param {String} decoratorName name of a link decorator used in the model
+	 * @returns {Boolean} Information if a given decorator is currently present in a selection
+	 */
+	_getDecoratorStateFromModel( decoratorName ) {
+		const doc = this.editor.model.document;
+		return doc.selection.getAttribute( decoratorName ) || false;
+	}
 }

+ 4 - 0
packages/ckeditor5-link/src/linkui.js

@@ -319,6 +319,10 @@ export default class LinkUI extends Plugin {
 	_closeFormView() {
 		const linkCommand = this.editor.commands.get( 'link' );
 
+		// Reset manual decorator states to represent current model state. This case is important to reset switch buttons,
+		// when user cancel editing form.
+		linkCommand.restoreManualDecoratorStates();
+
 		if ( linkCommand.value !== undefined ) {
 			this._removeFormView();
 		} else {