mentionui.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. describe( 'asynchronous list with custom trigger', () => {
  127. beforeEach( () => {
  128. const issuesNumbers = [ '100', '101', '102', '103' ];
  129. return createClassicTestEditor( [
  130. {
  131. marker: '#',
  132. feed: feedText => {
  133. return new Promise( resolve => {
  134. setTimeout( () => {
  135. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  136. }, 20 );
  137. } );
  138. }
  139. }
  140. ] );
  141. } );
  142. it( 'should show panel for matched marker', () => {
  143. setData( model, '<paragraph>foo []</paragraph>' );
  144. model.change( writer => {
  145. writer.insertText( '#', doc.selection.getFirstPosition() );
  146. } );
  147. return waitForDebounce()
  148. .then( () => {
  149. expect( mentionUI.panelView.isVisible ).to.be.true;
  150. expect( mentionUI._mentionsView.listView.items ).to.have.length( 4 );
  151. } );
  152. } );
  153. it( 'should show filtered results for matched text', () => {
  154. setData( model, '<paragraph>foo []</paragraph>' );
  155. model.change( writer => {
  156. writer.insertText( '#', doc.selection.getFirstPosition() );
  157. } );
  158. model.change( writer => {
  159. writer.insertText( '2', doc.selection.getFirstPosition() );
  160. } );
  161. return waitForDebounce()
  162. .then( () => {
  163. expect( mentionUI.panelView.isVisible ).to.be.true;
  164. expect( mentionUI._mentionsView.listView.items ).to.have.length( 1 );
  165. } );
  166. } );
  167. it( 'should hide panel if no matched items', () => {
  168. setData( model, '<paragraph>foo []</paragraph>' );
  169. model.change( writer => {
  170. writer.insertText( '#', doc.selection.getFirstPosition() );
  171. } );
  172. return waitForDebounce()
  173. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  174. .then( () => {
  175. model.change( writer => {
  176. writer.insertText( 'x', doc.selection.getFirstPosition() );
  177. } );
  178. } )
  179. .then( waitForDebounce )
  180. .then( () => {
  181. expect( mentionUI.panelView.isVisible ).to.be.false;
  182. expect( mentionUI._mentionsView.listView.items ).to.have.length( 0 );
  183. } );
  184. } );
  185. it( 'should hide panel when text was unmatched', () => {
  186. setData( model, '<paragraph>foo []</paragraph>' );
  187. model.change( writer => {
  188. writer.insertText( '#', doc.selection.getFirstPosition() );
  189. } );
  190. return waitForDebounce()
  191. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  192. .then( () => {
  193. model.change( writer => {
  194. const end = doc.selection.getFirstPosition();
  195. const start = end.getShiftedBy( -1 );
  196. writer.remove( writer.createRange( start, end ) );
  197. } );
  198. } )
  199. .then( waitForDebounce )
  200. .then( () => expect( mentionUI.panelView.isVisible ).to.be.false );
  201. } );
  202. } );
  203. } );
  204. describe( 'execute', () => {
  205. beforeEach( () => createClassicTestEditor( staticConfig ) );
  206. it( 'should call the mention command with proper options', () => {
  207. setData( model, '<paragraph>foo []</paragraph>' );
  208. model.change( writer => {
  209. writer.insertText( '@', doc.selection.getFirstPosition() );
  210. } );
  211. const command = editor.commands.get( 'mention' );
  212. const spy = testUtils.sinon.spy( command, 'execute' );
  213. return waitForDebounce()
  214. .then( () => {
  215. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  216. sinon.assert.calledOnce( spy );
  217. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  218. expect( commandOptions ).to.have.property( 'mention', 'Barney' );
  219. expect( commandOptions ).to.have.property( 'marker', '@' );
  220. expect( commandOptions ).to.have.property( 'range' );
  221. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  222. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  223. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  224. } );
  225. } );
  226. it( 'should hide panel on execute', () => {
  227. setData( model, '<paragraph>foo []</paragraph>' );
  228. model.change( writer => {
  229. writer.insertText( '@', doc.selection.getFirstPosition() );
  230. } );
  231. return waitForDebounce()
  232. .then( () => {
  233. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  234. expect( mentionUI.panelView.isVisible ).to.be.false;
  235. } );
  236. } );
  237. } );
  238. function createClassicTestEditor( mentionConfig ) {
  239. return ClassicTestEditor
  240. .create( editorElement, {
  241. plugins: [ Paragraph, MentionEditing, MentionUI ],
  242. mention: mentionConfig
  243. } )
  244. .then( newEditor => {
  245. editor = newEditor;
  246. model = editor.model;
  247. doc = model.document;
  248. editingView = editor.editing.view;
  249. mentionUI = editor.plugins.get( MentionUI );
  250. editingView.attachDomRoot( editorElement );
  251. // Focus the engine.
  252. editingView.document.isFocused = true;
  253. editingView.getDomRoot().focus();
  254. // Remove all selection ranges from DOM before testing.
  255. window.getSelection().removeAllRanges();
  256. } );
  257. }
  258. function waitForDebounce() {
  259. return new Promise( resolve => {
  260. setTimeout( () => {
  261. resolve();
  262. }, 50 );
  263. } );
  264. }
  265. } );