8
0
Maciej Gołaszewski 6 лет назад
Родитель
Сommit
5ee0b5b4c1

+ 17 - 2
packages/ckeditor5-mention/src/mentionediting.js

@@ -56,12 +56,27 @@ export default class MentionEditing extends Plugin {
 		editor.model.schema.extend( '$text', { allowAttributes: 'mention' } );
 
 		// Remove mention attribute if text was edited.
+		editor.model.document.registerPostFixer( writer => {
+			const selection = editor.model.document.selection;
+			const focus = selection.focus;
+
+			if ( selection.hasAttribute( 'mention' ) && selection.isCollapsed && focus.nodeBefore && focus.nodeBefore.is( 'text' ) ) {
+				writer.removeSelectionAttribute( 'mention' );
+
+				return true;
+			}
+		} );
+
 		editor.model.document.registerPostFixer( writer => {
 			const changes = editor.model.document.differ.getChanges();
 
 			for ( const change of changes ) {
-				if ( change.name == '$text' && change.position.textNode.hasAttribute( 'mention' ) ) {
-					writer.removeAttribute( 'mention', change.position.textNode );
+				if ( change.type == 'insert' || change.type == 'remove' ) {
+					const textNode = change.position.textNode;
+
+					if ( change.name == '$text' && textNode && textNode.hasAttribute( 'mention' ) ) {
+						writer.removeAttribute( 'mention', textNode );
+					}
 				}
 			}
 		} );

+ 38 - 0
packages/ckeditor5-mention/tests/mentionediting.js

@@ -86,6 +86,44 @@ describe( 'MentionEditing', () => {
 
 				expect( editor.getData() ).to.equal( '<p>foo Jhn bar</p>' );
 			} );
+
+			it( 'should work on insert text to an empty node', () => {
+				editor.setData( '<p></p>' );
+
+				model.change( writer => {
+					const paragraph = doc.getRoot().getChild( 0 );
+
+					writer.insertText( 'foo', paragraph, 0 );
+				} );
+
+				expect( editor.getData() ).to.equal( '<p>foo</p>' );
+			} );
+
+			it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
+				editor.setData( '<p>foo <span class="mention" data-mention="John">John</span>bar</p>' );
+
+				model.change( writer => {
+					const paragraph = doc.getRoot().getChild( 0 );
+
+					writer.setSelection( paragraph, 8 );
+				} );
+
+				expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
+			} );
+
+			it( 'should allow to type after a mention', () => {
+				editor.setData( '<p>foo <span class="mention" data-mention="John">John</span>bar</p>' );
+
+				model.change( writer => {
+					const paragraph = doc.getRoot().getChild( 0 );
+
+					writer.setSelection( paragraph, 8 );
+
+					writer.insertText( ' ', paragraph, 8 );
+				} );
+
+				expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
+			} );
 		} );
 	} );