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

Remove mention attribute on changing mention text.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
2400357366

+ 11 - 0
packages/ckeditor5-mention/src/mentionediting.js

@@ -54,5 +54,16 @@ export default class MentionEditing extends Plugin {
 
 		// Allow fontSize attribute on text nodes.
 		editor.model.schema.extend( '$text', { allowAttributes: 'mention' } );
+
+		// Remove mention attribute if text was edited.
+		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 );
+				}
+			}
+		} );
 	}
 }

+ 30 - 1
packages/ckeditor5-mention/tests/mentionediting.js

@@ -17,7 +17,7 @@ describe( 'MentionEditing', () => {
 	} );
 
 	describe( 'init()', () => {
-		let editor, model;
+		let editor, model, doc;
 
 		it( 'should be loaded', () => {
 			return createTestEditor()
@@ -46,6 +46,7 @@ describe( 'MentionEditing', () => {
 					.then( newEditor => {
 						editor = newEditor;
 						model = editor.model;
+						doc = model.document;
 					} );
 			} );
 
@@ -57,6 +58,34 @@ describe( 'MentionEditing', () => {
 
 				expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">John</span> bar</p>' );
 			} );
+
+			it( 'should remove mention on adding a text inside 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, 5 );
+
+					writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
+				} );
+
+				expect( editor.getData() ).to.equal( '<p>foo Joahn bar</p>' );
+			} );
+
+			it( 'should remove mention on removing a text inside 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, 5 );
+
+					writer.remove( writer.createRange( writer.createPositionAt( paragraph, 5 ), writer.createPositionAt( paragraph, 6 ) ) );
+				} );
+
+				expect( editor.getData() ).to.equal( '<p>foo Jhn bar</p>' );
+			} );
 		} );
 	} );