mentionediting.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import MentionEditing from '../src/mentionediting';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'MentionEditing', () => {
  11. testUtils.createSinonSandbox();
  12. it( 'should be named', () => {
  13. expect( MentionEditing.pluginName ).to.equal( 'MentionEditing' );
  14. } );
  15. describe( 'init()', () => {
  16. let editor, model, doc;
  17. it( 'should be loaded', () => {
  18. return createTestEditor()
  19. .then( newEditor => {
  20. expect( newEditor.plugins.get( MentionEditing ) ).to.be.instanceOf( MentionEditing );
  21. } );
  22. } );
  23. it( 'should set proper schema rules', () => {
  24. return createTestEditor()
  25. .then( newEditor => {
  26. model = newEditor.model;
  27. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'mention' ) ).to.be.true;
  28. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'mention' ) ).to.be.true;
  29. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'mention' ) ).to.be.true;
  30. expect( model.schema.checkAttribute( [ '$block' ], 'mention' ) ).to.be.false;
  31. } );
  32. } );
  33. describe( 'conversion in the data pipeline', () => {
  34. beforeEach( () => {
  35. return createTestEditor()
  36. .then( newEditor => {
  37. editor = newEditor;
  38. model = editor.model;
  39. doc = model.document;
  40. } );
  41. } );
  42. it( 'should convert <span class="mention" data-mention="John"> to mention attribute', () => {
  43. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  44. expect( getModelData( model, { withoutSelection: true } ) )
  45. .to.equal( '<paragraph>foo <$text mention="{"name":"John"}">@John</$text> bar</paragraph>' );
  46. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  47. expect( textNode ).to.not.be.null;
  48. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  49. expect( textNode.getAttribute( 'mention' ) ).to.deep.equal( { name: 'John' } );
  50. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  51. } );
  52. it( 'should remove mention on adding a text inside mention', () => {
  53. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  54. model.change( writer => {
  55. const paragraph = doc.getRoot().getChild( 0 );
  56. writer.setSelection( paragraph, 6 );
  57. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  58. } );
  59. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  60. } );
  61. it( 'should remove mention on removing a text inside mention', () => {
  62. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  63. const paragraph = doc.getRoot().getChild( 0 );
  64. model.change( writer => {
  65. writer.setSelection( paragraph, 6 );
  66. } );
  67. model.enqueueChange( () => {
  68. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  69. model.deleteContent( doc.selection );
  70. } );
  71. expect( editor.getData() ).to.equal( '<p>foo @ohn bar</p>' );
  72. } );
  73. it( 'should remove mention on removing a text at the and of a mention', () => {
  74. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  75. const paragraph = doc.getRoot().getChild( 0 );
  76. // Set selection at the end of a John.
  77. model.change( writer => {
  78. writer.setSelection( paragraph, 9 );
  79. } );
  80. model.enqueueChange( () => {
  81. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  82. model.deleteContent( doc.selection );
  83. } );
  84. expect( editor.getData() ).to.equal( '<p>foo @Joh bar</p>' );
  85. } );
  86. it( 'should not remove mention on removing a text just after a mention', () => {
  87. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  88. const paragraph = doc.getRoot().getChild( 0 );
  89. // Set selection before bar.
  90. model.change( writer => {
  91. writer.setSelection( paragraph, 10 );
  92. } );
  93. model.enqueueChange( () => {
  94. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  95. model.deleteContent( doc.selection );
  96. } );
  97. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  98. } );
  99. it( 'should set also other styles in inserted text', () => {
  100. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  101. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  102. setModelData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
  103. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 3 );
  104. editor.execute( 'mention', {
  105. mention: { name: 'John' },
  106. range: model.createRange( start, start.getShiftedBy( 5 ) )
  107. } );
  108. expect( editor.getData() ).to.equal(
  109. '<p>' +
  110. '<strong>foo</strong>' +
  111. '<span class="mention" data-mention="John">' +
  112. '<strong>@John</strong>' +
  113. '</span>' +
  114. '<strong> bar</strong>' +
  115. '</p>'
  116. );
  117. } );
  118. } );
  119. describe( 'typing integration', () => {
  120. it( 'should remove mention attribute from a selection if selection is on right side of a mention', () => {
  121. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  122. model.change( writer => {
  123. const paragraph = doc.getRoot().getChild( 0 );
  124. writer.setSelection( paragraph, 9 );
  125. } );
  126. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  127. } );
  128. it( 'should allow to type after a mention', () => {
  129. editor.setData( '<p>foo <span class="mention" data-mention="John">@John</span>bar</p>' );
  130. model.change( writer => {
  131. const paragraph = doc.getRoot().getChild( 0 );
  132. writer.setSelection( paragraph, 9 );
  133. writer.insertText( ' ', paragraph, 9 );
  134. } );
  135. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
  136. } );
  137. } );
  138. describe( 'postFixer', () => {
  139. it( 'should..', () => {} );
  140. } );
  141. describe( 'integration', () => {
  142. describe( 'basic styles', () => {
  143. } );
  144. } );
  145. } );
  146. function createTestEditor( mentionConfig ) {
  147. return VirtualTestEditor
  148. .create( {
  149. plugins: [ Paragraph, MentionEditing ],
  150. mention: mentionConfig
  151. } );
  152. }
  153. } );