Sfoglia il codice sorgente

Added binding for active feature. Improved docs.

Oskar Wrobel 9 anni fa
parent
commit
645127a972

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

@@ -10,11 +10,11 @@ import ButtonView from '../ui/button/buttonview.js';
 import Model from '../ui/model.js';
 
 /**
- * The bold feature. It introduces the Bold button and the <kbd>Ctrl+B</kbd> keystroke.
+ * The link feature.
  *
- * It uses the {@link basic-styles.BoldEngine bold engine feature}.
+ * It uses the {@link basic-styles.LinkEngine link engine feature}.
  *
- * @memberOf basic-styles
+ * @memberOf link
  * @extends core.Feature
  */
 export default class Link extends Feature {
@@ -45,9 +45,10 @@ export default class Link extends Feature {
 		buttonModel.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
 
 		// Execute command.
-		this.listenTo( buttonModel, 'execute', () => editor.execute( 'link', 'http://www.cksource.com' ) );
+		const hrefValue = 'http://www.cksource.com'; // Temporary href value.
+		this.listenTo( buttonModel, 'execute', () => editor.execute( 'link', hrefValue ) );
 
-		// Add bold button to feature components.
+		// Add link button to feature components.
 		editor.ui.featureComponents.add( 'link', ButtonController, ButtonView, buttonModel );
 	}
 }

+ 27 - 24
packages/ckeditor5-link/src/linkcommand.js

@@ -8,17 +8,33 @@ import getSchemaValidRanges from '../core/command/helpers/getschemavalidranges.j
 import isAttributeAllowedInSelection from '../core/command/helpers/isattributeallowedinselection.js';
 
 /**
- * An extension of basic {@link core.command.Command} class, which provides utilities for a command that toggle a single
- * attribute on a text or element with value `true`. ToggleAttributeCommand uses {@link engine.model.Document#selection}
- * to decide which nodes (if any) should be changed, and applies or removes attributes from them.
- * See {@link engine.view.Converter#execute} for more.
+ * The link command. It is used by the {@link Link.Link link feature}.
  *
- * The command checks {@link engine.model.Document#schema} to decide if it should be enabled.
- * See {@link engine.view.Converter#checkSchema} for more.
- *
- * @memberOf core.command
+ * @memberOf link
+ * @extends core.command.Command
  */
-export default class ToggleAttributeCommand extends Command {
+export default class LinkCommand extends Command {
+	/**
+	 * @see core.command.Command
+	 * @param {core.editor.Editor} editor
+	 */
+	constructor( editor ) {
+		super( editor );
+
+		/**
+		 * Flag indicating whether command is active. For collapsed selection it means that typed characters will have
+		 * the command's attribute set. For range selection it means that all nodes inside have the attribute applied.
+		 *
+		 * @observable
+		 * @member {Boolean} core.command.ToggleAttributeCommand#value
+		 */
+		this.set( 'value', false );
+
+		this.listenTo( this.editor.document.selection, 'change:attribute', () => {
+			this.value = this.editor.document.selection.hasAttribute( 'link' );
+		} );
+	}
+
 	/**
 	 * Checks if {@link engine.model.Document#schema} allows to create attribute in {@link engine.model.Document#selection}
 	 *
@@ -32,23 +48,10 @@ export default class ToggleAttributeCommand extends Command {
 	}
 
 	/**
-	 * Executes the command: adds or removes attributes to nodes or selection.
-	 *
-	 * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
-	 *
-	 * The execution result differs, depending on the {@link engine.model.Document#selection}:
-	 * * if selection is on a range, the command applies the attribute on all nodes in that ranges
-	 * (if they are allowed to have this attribute by the {@link engine.model.Schema schema}),
-	 * * if selection is collapsed in non-empty node, the command applies attribute to the {@link engine.model.Document#selection}
-	 * itself (note that typed characters copy attributes from selection),
-	 * * if selection is collapsed in empty node, the command applies attribute to the parent node of selection (note
-	 * that selection inherits all attributes from a node if it is in empty node).
-	 *
-	 * If the command is disabled (`isEnabled == false`) when it is executed, nothing will happen.
+	 * Executes the command if it is enabled.
 	 *
 	 * @private
-	 * @param {Boolean} [forceValue] If set it will force command behavior. If `true`, command will apply attribute,
-	 * otherwise command will remove attribute. If not set, command will look for it's current value to decide what it should do.
+	 * @param {String} href Link destination.
 	 */
 	_doExecute( href ) {
 		const document = this.editor.document;