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

Link highlighting is now done in post fixer.

Szymon Kupś 7 лет назад
Родитель
Сommit
2442859313

+ 15 - 25
packages/ckeditor5-link/src/linkediting.js

@@ -8,7 +8,11 @@
  */
  */
 
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
-import { downcastAttributeToElement, downcastMarkerToHighlight } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
+import {
+	downcastAttributeToElement,
+	downcastMarkerToHighlight,
+	createViewElementFromHighlightDescriptor
+} from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
 import { upcastElementToAttribute } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 import { upcastElementToAttribute } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 import LinkCommand from './linkcommand';
 import LinkCommand from './linkcommand';
 import UnlinkCommand from './unlinkcommand';
 import UnlinkCommand from './unlinkcommand';
@@ -18,7 +22,6 @@ import findLinkRange from './findlinkrange';
 import '../theme/link.css';
 import '../theme/link.css';
 import DocumentSelection from '@ckeditor/ckeditor5-engine/src/model/documentselection';
 import DocumentSelection from '@ckeditor/ckeditor5-engine/src/model/documentselection';
 import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
 import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
-import ViewAttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
 
 
 /**
 /**
  * The link engine feature.
  * The link engine feature.
@@ -89,24 +92,24 @@ export default class LinkEditing extends Plugin {
 			} ) );
 			} ) );
 
 
 		// Create marker over whole link when selection has "linkHref" attribute.
 		// Create marker over whole link when selection has "linkHref" attribute.
-		doc.on( 'change', () => {
+		doc.registerPostFixer( writer => {
 			const selection = doc.selection;
 			const selection = doc.selection;
+			const marker = model.markers.get( 'linkBoundaries' );
 
 
 			// Create marker over link when selection is inside or remove marker otherwise.
 			// Create marker over link when selection is inside or remove marker otherwise.
 			if ( selection.hasAttribute( 'linkHref' ) ) {
 			if ( selection.hasAttribute( 'linkHref' ) ) {
 				const modelRange = findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ) );
 				const modelRange = findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ) );
-				const marker = model.markers.get( 'linkBoundaries' );
 
 
 				if ( !marker || !marker.getRange().isEqual( modelRange ) ) {
 				if ( !marker || !marker.getRange().isEqual( modelRange ) ) {
-					model.change( writer => {
-						writer.setMarker( 'linkBoundaries', modelRange );
-					} );
+					writer.setMarker( 'linkBoundaries', modelRange );
+					return true;
 				}
 				}
-			} else if ( model.markers.has( 'linkBoundaries' ) ) {
-				model.change( writer => {
-					writer.removeMarker( 'linkBoundaries' );
-				} );
+			} else if ( marker ) {
+				writer.removeMarker( 'linkBoundaries' );
+				return true;
 			}
 			}
+
+			return false;
 		} );
 		} );
 
 
 		// Custom converter for selection's "linkHref" attribute - when collapsed selection has this attribute it is
 		// Custom converter for selection's "linkHref" attribute - when collapsed selection has this attribute it is
@@ -128,22 +131,9 @@ export default class LinkEditing extends Plugin {
 
 
 			const writer = conversionApi.writer;
 			const writer = conversionApi.writer;
 			const viewSelection = writer.document.selection;
 			const viewSelection = writer.document.selection;
-			const wrapper = new HighlightAttributeElement( 'span', { class: 'ck-link_selected' } );
-			wrapper._priority = highlightDescriptor.priority;
-			wrapper._setCustomProperty( 'highlightDescriptorId', highlightDescriptor.id );
+			const wrapper = createViewElementFromHighlightDescriptor( highlightDescriptor );
 
 
 			conversionApi.writer.wrap( viewSelection.getFirstRange(), wrapper );
 			conversionApi.writer.wrap( viewSelection.getFirstRange(), wrapper );
 		} );
 		} );
 	}
 	}
 }
 }
-
-// Private class used to wrap selection position until https://github.com/ckeditor/ckeditor5-engine/issues/1303 is solved.
-class HighlightAttributeElement extends ViewAttributeElement {
-	isSimilar( otherElement ) {
-		if ( otherElement.is( 'attributeElement' ) ) {
-			return this.getCustomProperty( 'highlightDescriptorId' ) === otherElement.getCustomProperty( 'highlightDescriptorId' );
-		}
-
-		return false;
-	}
-}

+ 2 - 0
packages/ckeditor5-link/tests/manual/linkhighlight.md

@@ -4,3 +4,5 @@
 1. Move selection inside linked text, it should stay highlighted.
 1. Move selection inside linked text, it should stay highlighted.
 1. Check if it works correctly with two-step caret movement when entering and leaving linked text.
 1. Check if it works correctly with two-step caret movement when entering and leaving linked text.
 1. Use enter to split linked text, check if links are highlighted separately.
 1. Use enter to split linked text, check if links are highlighted separately.
+
+NOTE: No additional undo steps should be added when preparing steps 1-3.