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

+ 7 - 5
packages/ckeditor5-mention/src/mentionediting.js

@@ -59,23 +59,25 @@ export default class MentionEditing extends Plugin {
 						return;
 					}
 
-					return { name: dataMention };
+					return { name: dataMention, _id: uid() };
 				}
 			}
 		} );
 
 		editor.conversion.for( 'downcast' ).attributeToElement( {
 			model: 'mention',
-			view: ( modelAttributeValue, viewWriter ) => {
-				const mention = modelAttributeValue && modelAttributeValue.name || modelAttributeValue;
+			view: ( mention, viewWriter ) => {
+				if ( !mention ) {
+					return;
+				}
 
 				const attributes = {
 					class: 'mention',
-					'data-mention': mention
+					'data-mention': mention.name
 				};
 
 				const options = {
-					id: uid() // Set unique identifier as id option to not merge view attribute elements.
+					id: mention._id
 				};
 
 				return viewWriter.createAttributeElement( 'span', attributes, options );

+ 20 - 6
packages/ckeditor5-mention/tests/mentionediting.js

@@ -53,14 +53,12 @@ describe( 'MentionEditing', () => {
 			it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
 				editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
 
-				expect( getModelData( model, { withoutSelection: true } ) )
-					.to.equal( '<paragraph>foo <$text mention="{"name":"John"}">@John</$text> bar</paragraph>' );
-
 				const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
 
 				expect( textNode ).to.not.be.null;
 				expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
-				expect( textNode.getAttribute( 'mention' ) ).to.deep.equal( { name: 'John' } );
+				expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
+				expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
 
 				expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
 			} );
@@ -80,9 +78,14 @@ describe( 'MentionEditing', () => {
 
 				expect( paragraph.childCount ).to.equal( 2 );
 
-				assertTextNode( paragraph.getChild( 1 ) );
+				assertTextNode( paragraph.getChild( 0 ) );
 				assertTextNode( paragraph.getChild( 1 ) );
 
+				const firstMentionId = paragraph.getChild( 0 ).getAttribute( 'mention' )._id;
+				const secondMentionId = paragraph.getChild( 1 ).getAttribute( 'mention' )._id;
+
+				expect( firstMentionId ).to.not.equal( secondMentionId );
+
 				expect( editor.getData() ).to.equal(
 					'<p>' +
 					'<span class="mention" data-mention="John">@John</span>' +
@@ -93,13 +96,21 @@ describe( 'MentionEditing', () => {
 				function assertTextNode( textNode ) {
 					expect( textNode ).to.not.be.null;
 					expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
-					expect( textNode.getAttribute( 'mention' ) ).to.deep.equal( { name: 'John' } );
+					expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
+					expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
 				}
 			} );
 
 			it( 'should remove mention on adding a text inside mention', () => {
 				editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
 
+				const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
+
+				expect( textNode ).to.not.be.null;
+				expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
+				expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_id' );
+				expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'name', 'John' );
+
 				model.change( writer => {
 					const paragraph = doc.getRoot().getChild( 0 );
 
@@ -108,6 +119,9 @@ describe( 'MentionEditing', () => {
 					writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
 				} );
 
+				expect( getModelData( model, { withoutSelection: true } ) )
+					.to.equal( '<paragraph>foo @Jaohn bar</paragraph>' );
+
 				expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
 			} );