浏览代码

Inserting a mention should not break other attributes.

Maciej Gołaszewski 6 年之前
父节点
当前提交
b09ed69023
共有 2 个文件被更改,包括 37 次插入3 次删除
  1. 8 2
      packages/ckeditor5-mention/src/mentioncommand.js
  2. 29 1
      packages/ckeditor5-mention/tests/mentionediting.js

+ 8 - 2
packages/ckeditor5-mention/src/mentioncommand.js

@@ -8,6 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
+import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
 
 /**
  * The mention command.
@@ -48,8 +49,13 @@ export default class MentionCommand extends Command {
 		model.change( writer => {
 			writer.remove( range );
 
-			writer.insertText( `${ marker }${ name }`, { mention }, range.start );
-			writer.insertText( ' ', model.document.selection.focus );
+			const selectionAttributes = toMap( selection.getAttributes() );
+			const attributes = new Map( selectionAttributes.entries() );
+
+			attributes.set( 'mention', mention );
+
+			writer.insertText( `${ marker }${ name }`, attributes, range.start );
+			writer.insertText( ' ', selectionAttributes, model.document.selection.focus );
 		} );
 	}
 }

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

@@ -6,7 +6,7 @@
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import MentionEditing from '../src/mentionediting';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
-import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 
 describe( 'MentionEditing', () => {
@@ -131,6 +131,29 @@ describe( 'MentionEditing', () => {
 
 				expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
 			} );
+
+			it( 'should set also other styles in inserted text', () => {
+				model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
+				editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
+				setModelData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
+
+				const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 3 );
+
+				editor.execute( 'mention', {
+					mention: { name: 'John' },
+					range: model.createRange( start, start.getShiftedBy( 5 ) )
+				} );
+
+				expect( editor.getData() ).to.equal(
+					'<p>' +
+					'<strong>foo</strong>' +
+					'<span class="mention" data-mention="John">' +
+						'<strong>@John</strong>' +
+					'</span>' +
+					'<strong> bar</strong>' +
+					'</p>'
+				);
+			} );
 		} );
 
 		describe( 'typing integration', () => {
@@ -164,6 +187,11 @@ describe( 'MentionEditing', () => {
 		describe( 'postFixer', () => {
 			it( 'should..', () => {} );
 		} );
+
+		describe( 'integration', () => {
+			describe( 'basic styles', () => {
+			} );
+		} );
 	} );
 
 	function createTestEditor( mentionConfig ) {