Procházet zdrojové kódy

Introduced unlink command.

Oskar Wrobel před 9 roky
rodič
revize
4a8a0e78f3

+ 3 - 1
packages/ckeditor5-link/src/linkengine.js

@@ -8,6 +8,7 @@ import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
 import buildViewConverter from '../engine/conversion/buildviewconverter.js';
 import AttributeElement from '../engine/view/attributeelement.js';
 import LinkCommand from './linkcommand.js';
+import UnlinkCommand from './unlinkcommand.js';
 
 /**
  * The link engine feature.
@@ -42,7 +43,8 @@ export default class LinkEngine extends Feature {
 				value: viewElement.getAttribute( 'href' )
 			} ) );
 
-		// Create link command.
+		// Create linking commands.
 		editor.commands.set( 'link', new LinkCommand( editor ) );
+		editor.commands.set( 'unlink', new UnlinkCommand( editor ) );
 	}
 }

+ 90 - 0
packages/ckeditor5-link/src/unlinkcommand.js

@@ -0,0 +1,90 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Command from '../core/command/command.js';
+import Range from '../engine/model/range.js';
+
+/**
+ * The unlink command. It is used by the {@link Link.Link link feature}.
+ *
+ * @memberOf link
+ * @extends core.command.Command
+ */
+export default class LinkCommand extends Command {
+	/**
+	 * Executes the command.
+	 *
+	 * When selection is collapsed then whole link will be unlinked. {@link engine.model.TreeWalker TreeWalker} is
+	 * looking for link bounds going forward and backward from the caret position.
+	 *
+	 * If selection is non-collapsed then only selected range will be unlinked.
+	 *
+	 * @protected
+	 */
+	_doExecute() {
+		const document = this.editor.document;
+		const selection = document.selection;
+		const ranges = selection.getRanges();
+		let rangesToUpdate = [];
+
+		document.enqueueChanges( () => {
+			// When selection is collapsed we have to find link bounds.
+			if ( selection.isCollapsed ) {
+				// Loop through each selection.
+				for ( let range of ranges ) {
+					// Get searching area.
+					const parentNodeRange = Range.createIn( range.start.parent );
+
+					// Create walker instances for going forward and backward.
+					const walker = parentNodeRange.getWalker( { startPosition: range.start } );
+					const backwardWalker = parentNodeRange.getWalker( { startPosition: range.start, direction: 'backward' } );
+
+					// Store current link attribute value.
+					const attributeValue = selection.getAttribute( 'link' );
+
+					// Search link bounds and store found range.
+					rangesToUpdate.push(
+						new Range( getLinkBound( backwardWalker, attributeValue ), getLinkBound( walker, attributeValue ) )
+					);
+				}
+			} else {
+				// When selection is non-collapsed then we are unlinking selected ranges.
+				rangesToUpdate = ranges;
+			}
+
+			// Keep it as one undo step.
+			const batch = document.batch();
+
+			// Remove attribute from each range.
+			for ( let range of rangesToUpdate ) {
+				batch.removeAttribute( range, 'link' );
+			}
+
+			// Remove attribute from selection.
+			selection.removeAttribute( 'link' );
+		} );
+	}
+}
+
+// Walk trough the range and search for link bound.
+//
+// @param {engine.model.TreeWalker} walker TreeWalker instance.
+// @param {String} linkValue Value of searching link.
+// @returns {engine.model.Position} Position of link bound.
+function getLinkBound( walker, linkValue ) {
+	let lastPosition = walker.position;
+	let current = walker.next();
+
+	while ( !current.done ) {
+		if ( current.value.item.getAttribute( 'link' ) != linkValue ) {
+			return lastPosition;
+		}
+
+		lastPosition = walker.position;
+		current = walker.next();
+	}
+
+	return lastPosition;
+}

+ 9 - 0
packages/ckeditor5-link/tests/linkengine.js

@@ -5,6 +5,7 @@
 
 import LinkEngine from '/ckeditor5/link/linkengine.js';
 import LinkCommand from '/ckeditor5/link/linkcommand.js';
+import UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
 import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
 import { getData as getModelData, setData as setModelData } from '/tests/engine/_utils/model.js';
 import { getData as getViewData } from '/tests/engine/_utils/view.js';
@@ -41,6 +42,14 @@ describe( 'LinkEngine', () => {
 
 			expect( command ).to.be.instanceOf( LinkCommand );
 		} );
+
+		it( 'should register unlink command', () => {
+			expect( editor.commands.has( 'unlink' ) ).to.be.true;
+
+			const command = editor.commands.get( 'unlink' );
+
+			expect( command ).to.be.instanceOf( UnlinkCommand );
+		} );
 	} );
 
 	describe( 'data pipeline conversions', () => {

+ 148 - 0
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -0,0 +1,148 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import ModelTestEditor from '/tests/core/_utils/modeltesteditor.js';
+import UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
+import { setData, getData } from '/tests/engine/_utils/model.js';
+
+describe( 'UnlinkCommand', () => {
+	let editor, document, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				document = editor.document;
+				command = new UnlinkCommand( editor );
+
+				// Allow text in $root.
+				document.schema.allow( { name: '$text', inside: '$root' } );
+
+				// Allow text with link attribute in paragraph.
+				document.schema.registerItem( 'p', '$block' );
+				document.schema.allow( { name: '$text', attributes: 'link', inside: '$root' } );
+			} );
+	} );
+
+	afterEach( () => {
+		command.destroy();
+	} );
+
+	describe( '_doExecute', () => {
+		describe( 'non-collapsed selection', () => {
+			it( 'should remove link attribute from selected text', () => {
+				setData( document, '<$text link="url">f[ooba]r</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text link="url">f</$text>[ooba]<$text link="url">r</$text>' );
+			} );
+
+			it( 'should remove link attribute from selected text and do not modified other attributes', () => {
+				setData( document, '<$text bold="true" link="url">f[ooba]r</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) )
+					.to.equal( '<$text bold="true" link="url">f</$text>[<$text bold="true">ooba</$text>]<$text bold="true" link="url">r</$text>' );
+			} );
+
+			it( 'should remove link attribute from selected text when attributes have different value', () => {
+				setData( document, '[<$text link="url">foo</$text><$text link="other url">bar</$text>]' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '[foobar]' );
+			} );
+		} );
+
+		describe( 'collapsed selection', () => {
+			it( 'should remove link attribute from selection siblings with the same attribute value', () => {
+				setData( document, '<$text link="url">foo[]bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foo[]bar' );
+			} );
+
+			it( 'should remove link attribute from selection siblings with the same attribute value and do not ' +
+				'modified other attributes',
+			() => {
+				setData( document, '<$text bold="true" link="url">foo[]bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text bold="true">foo[]bar</$text>' );
+			} );
+
+			it(
+				'should remove link attribute from selection siblings with the same attribute value and do nothing ' +
+				'with other value links',
+			() => {
+				setData( document, '<$text link="other url">fo</$text><$text link="url">o[]b</$text><$text link="other url">ar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<$text link="other url">fo</$text>o[]b<$text link="other url">ar</$text>' );
+			} );
+
+			it( 'should do nothing with the same value links when there is a link with other value between', () => {
+					setData(
+						document,
+						'<$text link="same url">f</$text>' +
+						'<$text link="other url">o</$text>' +
+						'<$text link="same url">o[]b</$text>' +
+						'<$text link="other url">a</$text>' +
+						'<$text link="same url">r</$text>'
+					);
+
+					command._doExecute();
+
+					expect( getData( document ) )
+						.to.equal(
+							'<$text link="same url">f</$text>' +
+							'<$text link="other url">o</$text>' +
+							'o[]b' +
+							'<$text link="other url">a</$text>' +
+							'<$text link="same url">r</$text>'
+						);
+				} );
+
+			it( 'should remove link attribute from selection siblings only in the same parent as selection parent', () => {
+				setData( document, '<p><$text link="url">fo[]o</$text></p><p><$text link="url">bar</$text></p>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '<p>fo[]o</p><p><$text link="url">bar</$text></p>' );
+			} );
+
+			it( 'should remove link attribute from selection siblings when selection is at the end of link', () => {
+				setData( document, '<$text link="url">foobar</$text>[]' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foobar[]' );
+			} );
+
+			it( 'should remove link attribute from selection siblings when selection is at the beginning of link', () => {
+				setData( document, '[]<$text link="url">foobar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( '[]foobar' );
+			} );
+
+			it( 'should remove link attribute from selection siblings on the left side when selection is between two ' +
+				'elements with different link attributes',
+			() => {
+				setData( document, '<$text link="url">foo</$text>[]<$text link="other url">bar</$text>' );
+
+				command._doExecute();
+
+				expect( getData( document ) ).to.equal( 'foo[]<$text link="other url">bar</$text>' );
+			} );
+		} );
+	} );
+} );