8
0

mention-integration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, setTimeout */
  6. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Table from '@ckeditor/ckeditor5-table/src/table';
  10. import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
  11. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  12. import Link from '@ckeditor/ckeditor5-link/src/link';
  13. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  14. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { parse as parseView, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. import MentionEditing from '../src/mentionediting';
  19. import Mention from '../src/mention';
  20. import MentionUI from '../src/mentionui';
  21. describe( 'Mention feature - integration', () => {
  22. let div, editor, model, doc;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. div = document.createElement( 'div' );
  26. document.body.appendChild( div );
  27. } );
  28. afterEach( () => {
  29. div.remove();
  30. return editor.destroy();
  31. } );
  32. describe( 'with undo', () => {
  33. beforeEach( () => {
  34. return ClassicTestEditor.create( div, { plugins: [ Paragraph, MentionEditing, UndoEditing ] } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. model = editor.model;
  38. doc = model.document;
  39. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  40. editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  41. } );
  42. } );
  43. // Failing test. See ckeditor/ckeditor5#1645.
  44. it( 'should restore removed mention on adding a text inside mention', () => {
  45. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  46. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  47. model.change( writer => {
  48. const paragraph = doc.getRoot().getChild( 0 );
  49. writer.setSelection( paragraph, 6 );
  50. writer.insertText( 'a', doc.selection.getAttributes(), writer.createPositionAt( paragraph, 6 ) );
  51. } );
  52. expect( editor.getData() ).to.equal( '<p>foo @Jaohn bar</p>' );
  53. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jaohn bar</p>' );
  54. editor.execute( 'undo' );
  55. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  56. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  57. .to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  58. } );
  59. // Failing test. See ckeditor/ckeditor5#1645.
  60. it( 'should restore removed mention on removing a text inside mention', () => {
  61. editor.setData( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  62. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  63. model.change( writer => {
  64. const paragraph = doc.getRoot().getChild( 0 );
  65. writer.setSelection( paragraph, 7 );
  66. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codepoint' } );
  67. model.deleteContent( doc.selection );
  68. } );
  69. expect( editor.getData() ).to.equal( '<p>foo @Jhn bar</p>' );
  70. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo @Jhn bar</p>' );
  71. editor.execute( 'undo' );
  72. expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  73. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  74. .to.equal( '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>' );
  75. } );
  76. it( 'should work with attribute post-fixer (beginning formatted)', () => {
  77. testAttributePostFixer(
  78. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  79. '<p><strong>foo <span class="mention" data-mention="@John">@John</span></strong> bar</p>',
  80. () => {
  81. model.change( writer => {
  82. const paragraph = doc.getRoot().getChild( 0 );
  83. const start = writer.createPositionAt( paragraph, 0 );
  84. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  85. writer.setSelection( range );
  86. writer.setAttribute( 'bold', true, range );
  87. } );
  88. } );
  89. } );
  90. it( 'should work with attribute post-fixer (end formatted)', () => {
  91. testAttributePostFixer(
  92. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  93. '<p>foo <strong><span class="mention" data-mention="@John">@John</span> ba</strong>r</p>',
  94. () => {
  95. model.change( writer => {
  96. const paragraph = doc.getRoot().getChild( 0 );
  97. const start = writer.createPositionAt( paragraph, 6 );
  98. const range = writer.createRange( start, start.getShiftedBy( 6 ) );
  99. writer.setSelection( range );
  100. writer.setAttribute( 'bold', true, range );
  101. } );
  102. } );
  103. } );
  104. it( 'should work with attribute post-fixer (middle formatted)', () => {
  105. testAttributePostFixer(
  106. '<p>foo <span class="mention" data-mention="@John">@John</span> bar</p>',
  107. '<p>foo <strong><span class="mention" data-mention="@John">@John</span></strong> bar</p>',
  108. () => {
  109. model.change( writer => {
  110. const paragraph = doc.getRoot().getChild( 0 );
  111. const start = writer.createPositionAt( paragraph, 6 );
  112. const range = writer.createRange( start, start.getShiftedBy( 1 ) );
  113. writer.setSelection( range );
  114. writer.setAttribute( 'bold', true, range );
  115. } );
  116. } );
  117. } );
  118. function testAttributePostFixer( initialData, expectedData, testCallback ) {
  119. editor.setData( initialData );
  120. expect( editor.getData() ).to.equal( initialData );
  121. testCallback();
  122. expect( editor.getData() )
  123. .to.equal( expectedData );
  124. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  125. .to.equal( expectedData );
  126. editor.execute( 'undo' );
  127. expect( editor.getData() ).to.equal( initialData );
  128. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  129. .to.equal( initialData );
  130. editor.execute( 'redo' );
  131. expect( editor.getData() )
  132. .to.equal( expectedData );
  133. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  134. .to.equal( expectedData );
  135. }
  136. } );
  137. describe( 'with clipboard', () => {
  138. let clipboard;
  139. beforeEach( () => {
  140. return ClassicTestEditor
  141. .create( div, { plugins: [ Clipboard, Paragraph, BlockQuote, MentionEditing, UndoEditing ] } )
  142. .then( newEditor => {
  143. editor = newEditor;
  144. model = editor.model;
  145. doc = model.document;
  146. clipboard = editor.plugins.get( 'Clipboard' );
  147. } );
  148. } );
  149. it( 'should not fix broken mention inside pasted content', () => {
  150. editor.setData( '<p>foobar</p>' );
  151. model.change( writer => {
  152. writer.setSelection( doc.getRoot().getChild( 0 ), 3 );
  153. } );
  154. clipboard.fire( 'inputTransformation', {
  155. content: parseView( '<blockquote><p>xxx<span class="mention" data-mention="@John">@Joh</span></p></blockquote>' )
  156. } );
  157. const expectedData = '<p>foo</p>' +
  158. '<blockquote><p>xxx<span class="mention" data-mention="@John">@Joh</span></p></blockquote>' +
  159. '<p>bar</p>';
  160. expect( editor.getData() )
  161. .to.equal( expectedData );
  162. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  163. .to.equal( expectedData );
  164. } );
  165. } );
  166. describe( 'with table toolbar', () => {
  167. beforeEach( () => {
  168. return ClassicTestEditor
  169. .create( div, {
  170. plugins: [ Paragraph, Table, TableToolbar, Mention ],
  171. table: {
  172. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  173. },
  174. mention: {
  175. feeds: [
  176. {
  177. marker: '@',
  178. feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
  179. }
  180. ]
  181. }
  182. } )
  183. .then( newEditor => {
  184. editor = newEditor;
  185. model = editor.model;
  186. doc = model.document;
  187. } );
  188. } );
  189. it( 'should work with table toolbar', () => {
  190. editor.ui.focusTracker.isFocused = true;
  191. setData( model, '<table><tableRow><tableCell><paragraph>foo []</paragraph></tableCell></tableRow></table>' );
  192. const balloon = editor.plugins.get( 'ContextualBalloon' );
  193. const panelView = balloon.view;
  194. const mentionUI = editor.plugins.get( MentionUI );
  195. const mentionsView = mentionUI._mentionsView;
  196. return new Promise( resolve => setTimeout( resolve, 200 ) )
  197. .then( () => {
  198. model.change( writer => {
  199. writer.insertText( '@', doc.selection.getFirstPosition() );
  200. } );
  201. } )
  202. .then( () => new Promise( resolve => setTimeout( resolve, 200 ) ) )
  203. .then( () => {
  204. expect( panelView.isVisible ).to.be.true;
  205. expect( balloon.visibleView === mentionsView ).to.be.true;
  206. model.change( writer => {
  207. writer.setSelection( doc.getRoot().getNodeByPath( [ 0, 0, 0, 0 ] ), '1' );
  208. } );
  209. expect( panelView.isVisible ).to.be.true;
  210. expect( balloon.visibleView === mentionsView ).to.be.false;
  211. } );
  212. } );
  213. } );
  214. describe( 'with link toolbar', () => {
  215. beforeEach( () => {
  216. return ClassicTestEditor
  217. .create( div, {
  218. plugins: [ Paragraph, Link, Delete, Mention ],
  219. mention: {
  220. feeds: [
  221. {
  222. marker: '@',
  223. feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
  224. }
  225. ]
  226. }
  227. } )
  228. .then( newEditor => {
  229. editor = newEditor;
  230. model = editor.model;
  231. doc = model.document;
  232. } );
  233. } );
  234. it( 'should work with link toolbar', () => {
  235. editor.ui.focusTracker.isFocused = true;
  236. setData( model, '<paragraph>[]</paragraph>' );
  237. const balloon = editor.plugins.get( 'ContextualBalloon' );
  238. const panelView = balloon.view;
  239. const mentionUI = editor.plugins.get( MentionUI );
  240. const mentionsView = mentionUI._mentionsView;
  241. return new Promise( resolve => setTimeout( resolve, 200 ) )
  242. .then( () => {
  243. const model = editor.model;
  244. // Show link UI
  245. editor.execute( 'link', '@' );
  246. // The link is not being selected after inserting it. We need to put the selection manually. See #1016.
  247. model.change( writer => {
  248. writer.setSelection( writer.createRangeOn( model.document.getRoot().getChild( 0 ).getChild( 0 ) ) );
  249. } );
  250. editor.editing.view.document.fire( 'click' );
  251. expect( panelView.isVisible ).to.be.true;
  252. expect( balloon.visibleView === mentionsView ).to.be.false; // LinkUI
  253. model.change( writer => {
  254. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  255. } );
  256. } )
  257. .then( () => new Promise( resolve => setTimeout( resolve, 200 ) ) )
  258. .then( () => {
  259. expect( panelView.isVisible ).to.be.true;
  260. expect( balloon.visibleView === mentionsView ).to.be.true;
  261. editor.execute( 'delete' );
  262. expect( panelView.isVisible ).to.be.false;
  263. } );
  264. } );
  265. } );
  266. } );