mentionui.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document, setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import MentionUI from '../src/mentionui';
  12. import MentionEditing from '../src/mentionediting';
  13. import MentionsView from '../src/ui/mentionsview';
  14. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. describe( 'BalloonToolbar', () => {
  16. let editor, model, doc, editingView, mentionUI, editorElement;
  17. const staticConfig = [
  18. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  19. ];
  20. testUtils.createSinonSandbox();
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. document.body.appendChild( editorElement );
  24. } );
  25. afterEach( () => {
  26. sinon.restore();
  27. editorElement.remove();
  28. return editor.destroy();
  29. } );
  30. it( 'should create a plugin instance', () => {
  31. return createClassicTestEditor().then( () => {
  32. expect( mentionUI ).to.instanceOf( Plugin );
  33. expect( mentionUI ).to.instanceOf( MentionUI );
  34. } );
  35. } );
  36. describe( 'pluginName', () => {
  37. it( 'should return plugin by its name', () => {
  38. return createClassicTestEditor().then( () => {
  39. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  40. } );
  41. } );
  42. } );
  43. describe( 'child views', () => {
  44. beforeEach( () => createClassicTestEditor() );
  45. describe( 'panelView', () => {
  46. it( 'should create a view instance', () => {
  47. expect( mentionUI.panelView ).to.instanceof( BalloonPanelView );
  48. } );
  49. it( 'should be added to the ui.view.body collection', () => {
  50. expect( Array.from( editor.ui.view.body ) ).to.include( mentionUI.panelView );
  51. } );
  52. it( 'should have disabled arrow', () => {
  53. expect( mentionUI.panelView.withArrow ).to.be.false;
  54. } );
  55. it( 'should have added MentionView as a child', () => {
  56. expect( mentionUI.panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  57. } );
  58. } );
  59. } );
  60. describe( 'typing integration', () => {
  61. describe( 'static list with default trigger', () => {
  62. beforeEach( () => {
  63. return createClassicTestEditor( staticConfig );
  64. } );
  65. it( 'should show panel for matched marker', () => {
  66. setData( model, '<paragraph>foo []</paragraph>' );
  67. model.change( writer => {
  68. writer.insertText( '@', doc.selection.getFirstPosition() );
  69. } );
  70. return waitForDebounce()
  71. .then( () => {
  72. expect( mentionUI.panelView.isVisible ).to.be.true;
  73. expect( mentionUI._mentionsView.listView.items ).to.have.length( 5 );
  74. } );
  75. } );
  76. it( 'should show filtered results for matched text', () => {
  77. setData( model, '<paragraph>foo []</paragraph>' );
  78. model.change( writer => {
  79. writer.insertText( '@', doc.selection.getFirstPosition() );
  80. } );
  81. model.change( writer => {
  82. writer.insertText( 'T', doc.selection.getFirstPosition() );
  83. } );
  84. return waitForDebounce()
  85. .then( () => {
  86. expect( mentionUI.panelView.isVisible ).to.be.true;
  87. expect( mentionUI._mentionsView.listView.items ).to.have.length( 1 );
  88. } );
  89. } );
  90. it( 'should hide panel if no matched items', () => {
  91. setData( model, '<paragraph>foo []</paragraph>' );
  92. model.change( writer => {
  93. writer.insertText( '@', doc.selection.getFirstPosition() );
  94. } );
  95. return waitForDebounce()
  96. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  97. .then( () => {
  98. model.change( writer => {
  99. writer.insertText( 'x', doc.selection.getFirstPosition() );
  100. } );
  101. } )
  102. .then( waitForDebounce )
  103. .then( () => {
  104. expect( mentionUI.panelView.isVisible ).to.be.false;
  105. expect( mentionUI._mentionsView.listView.items ).to.have.length( 0 );
  106. } );
  107. } );
  108. it( 'should hide panel when text was unmatched', () => {
  109. setData( model, '<paragraph>foo []</paragraph>' );
  110. model.change( writer => {
  111. writer.insertText( '@', doc.selection.getFirstPosition() );
  112. } );
  113. return waitForDebounce()
  114. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  115. .then( () => {
  116. model.change( writer => {
  117. const end = doc.selection.getFirstPosition();
  118. const start = end.getShiftedBy( -1 );
  119. writer.remove( writer.createRange( start, end ) );
  120. } );
  121. } )
  122. .then( waitForDebounce )
  123. .then( () => expect( mentionUI.panelView.isVisible ).to.be.false );
  124. } );
  125. } );
  126. } );
  127. describe( 'execute', () => {
  128. beforeEach( () => createClassicTestEditor( staticConfig ) );
  129. it( 'should call the mention command with proper options', () => {
  130. setData( model, '<paragraph>foo []</paragraph>' );
  131. model.change( writer => {
  132. writer.insertText( '@', doc.selection.getFirstPosition() );
  133. } );
  134. const command = editor.commands.get( 'mention' );
  135. const spy = testUtils.sinon.spy( command, 'execute' );
  136. return waitForDebounce()
  137. .then( () => {
  138. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  139. sinon.assert.calledOnce( spy );
  140. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  141. expect( commandOptions ).to.have.property( 'mention', 'Barney' );
  142. expect( commandOptions ).to.have.property( 'marker', '@' );
  143. expect( commandOptions ).to.have.property( 'range' );
  144. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  145. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  146. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  147. } );
  148. } );
  149. it( 'should hide panel on execute', () => {
  150. setData( model, '<paragraph>foo []</paragraph>' );
  151. model.change( writer => {
  152. writer.insertText( '@', doc.selection.getFirstPosition() );
  153. } );
  154. return waitForDebounce()
  155. .then( () => {
  156. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  157. expect( mentionUI.panelView.isVisible ).to.be.false;
  158. } );
  159. } );
  160. } );
  161. function createClassicTestEditor( mentionConfig ) {
  162. return ClassicTestEditor
  163. .create( editorElement, {
  164. plugins: [ Paragraph, MentionEditing, MentionUI ],
  165. mention: mentionConfig
  166. } )
  167. .then( newEditor => {
  168. editor = newEditor;
  169. model = editor.model;
  170. doc = model.document;
  171. editingView = editor.editing.view;
  172. mentionUI = editor.plugins.get( MentionUI );
  173. editingView.attachDomRoot( editorElement );
  174. // Focus the engine.
  175. editingView.document.isFocused = true;
  176. editingView.getDomRoot().focus();
  177. // Remove all selection ranges from DOM before testing.
  178. window.getSelection().removeAllRanges();
  179. } );
  180. }
  181. function waitForDebounce() {
  182. return new Promise( resolve => {
  183. setTimeout( () => {
  184. resolve();
  185. } );
  186. } );
  187. }
  188. } );