8
0

mentionui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import MentionUI from '../src/mentionui';
  13. import MentionEditing from '../src/mentionediting';
  14. import MentionsView from '../src/ui/mentionsview';
  15. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'BalloonToolbar', () => {
  17. let editor, model, doc, editingView, mentionUI, editorElement, mentionsView, panelView, listView;
  18. const staticConfig = [
  19. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  20. ];
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. } );
  26. afterEach( () => {
  27. sinon.restore();
  28. editorElement.remove();
  29. return editor.destroy();
  30. } );
  31. it( 'should create a plugin instance', () => {
  32. return createClassicTestEditor().then( () => {
  33. expect( mentionUI ).to.instanceOf( Plugin );
  34. expect( mentionUI ).to.instanceOf( MentionUI );
  35. } );
  36. } );
  37. describe( 'pluginName', () => {
  38. it( 'should return plugin by its name', () => {
  39. return createClassicTestEditor().then( () => {
  40. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  41. } );
  42. } );
  43. } );
  44. describe( 'child views', () => {
  45. beforeEach( () => createClassicTestEditor() );
  46. describe( 'panelView', () => {
  47. it( 'should create a view instance', () => {
  48. expect( panelView ).to.instanceof( BalloonPanelView );
  49. } );
  50. it( 'should be added to the ui.view.body collection', () => {
  51. expect( Array.from( editor.ui.view.body ) ).to.include( panelView );
  52. } );
  53. it( 'should have disabled arrow', () => {
  54. expect( panelView.withArrow ).to.be.false;
  55. } );
  56. it( 'should have added MentionView as a child', () => {
  57. expect( panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  58. } );
  59. } );
  60. } );
  61. describe( 'typing integration', () => {
  62. describe( 'static list with default trigger', () => {
  63. beforeEach( () => {
  64. return createClassicTestEditor( staticConfig );
  65. } );
  66. it( 'should show panel for matched marker', () => {
  67. setData( model, '<paragraph>foo []</paragraph>' );
  68. model.change( writer => {
  69. writer.insertText( '@', doc.selection.getFirstPosition() );
  70. } );
  71. return waitForDebounce()
  72. .then( () => {
  73. expect( panelView.isVisible ).to.be.true;
  74. expect( listView.items ).to.have.length( 5 );
  75. } );
  76. } );
  77. it( 'should show filtered results for matched text', () => {
  78. setData( model, '<paragraph>foo []</paragraph>' );
  79. model.change( writer => {
  80. writer.insertText( '@', doc.selection.getFirstPosition() );
  81. } );
  82. model.change( writer => {
  83. writer.insertText( 'T', doc.selection.getFirstPosition() );
  84. } );
  85. return waitForDebounce()
  86. .then( () => {
  87. expect( panelView.isVisible ).to.be.true;
  88. expect( listView.items ).to.have.length( 1 );
  89. } );
  90. } );
  91. it( 'should focus the first item in panel', () => {
  92. setData( model, '<paragraph>foo []</paragraph>' );
  93. model.change( writer => {
  94. writer.insertText( '@', doc.selection.getFirstPosition() );
  95. } );
  96. return waitForDebounce()
  97. .then( () => {
  98. const button = listView.items.get( 0 ).children.get( 0 );
  99. expect( button.isOn ).to.be.true;
  100. } );
  101. } );
  102. it( 'should hide panel if no matched items', () => {
  103. setData( model, '<paragraph>foo []</paragraph>' );
  104. model.change( writer => {
  105. writer.insertText( '@', doc.selection.getFirstPosition() );
  106. } );
  107. return waitForDebounce()
  108. .then( () => expect( panelView.isVisible ).to.be.true )
  109. .then( () => {
  110. model.change( writer => {
  111. writer.insertText( 'x', doc.selection.getFirstPosition() );
  112. } );
  113. } )
  114. .then( waitForDebounce )
  115. .then( () => {
  116. expect( panelView.isVisible ).to.be.false;
  117. expect( listView.items ).to.have.length( 0 );
  118. } );
  119. } );
  120. it( 'should hide panel when text was unmatched', () => {
  121. setData( model, '<paragraph>foo []</paragraph>' );
  122. model.change( writer => {
  123. writer.insertText( '@', doc.selection.getFirstPosition() );
  124. } );
  125. return waitForDebounce()
  126. .then( () => expect( panelView.isVisible ).to.be.true )
  127. .then( () => {
  128. model.change( writer => {
  129. const end = doc.selection.getFirstPosition();
  130. const start = end.getShiftedBy( -1 );
  131. writer.remove( writer.createRange( start, end ) );
  132. } );
  133. } )
  134. .then( waitForDebounce )
  135. .then( () => expect( panelView.isVisible ).to.be.false );
  136. } );
  137. } );
  138. describe( 'asynchronous list with custom trigger', () => {
  139. beforeEach( () => {
  140. const issuesNumbers = [ '100', '101', '102', '103' ];
  141. return createClassicTestEditor( [
  142. {
  143. marker: '#',
  144. feed: feedText => {
  145. return new Promise( resolve => {
  146. setTimeout( () => {
  147. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  148. }, 20 );
  149. } );
  150. }
  151. }
  152. ] );
  153. } );
  154. it( 'should show panel for matched marker', () => {
  155. setData( model, '<paragraph>foo []</paragraph>' );
  156. model.change( writer => {
  157. writer.insertText( '#', doc.selection.getFirstPosition() );
  158. } );
  159. return waitForDebounce()
  160. .then( () => {
  161. expect( panelView.isVisible ).to.be.true;
  162. expect( listView.items ).to.have.length( 4 );
  163. } );
  164. } );
  165. it( 'should show filtered results for matched text', () => {
  166. setData( model, '<paragraph>foo []</paragraph>' );
  167. model.change( writer => {
  168. writer.insertText( '#', doc.selection.getFirstPosition() );
  169. } );
  170. model.change( writer => {
  171. writer.insertText( '2', doc.selection.getFirstPosition() );
  172. } );
  173. return waitForDebounce()
  174. .then( () => {
  175. expect( panelView.isVisible ).to.be.true;
  176. expect( listView.items ).to.have.length( 1 );
  177. } );
  178. } );
  179. it( 'should hide panel if no matched items', () => {
  180. setData( model, '<paragraph>foo []</paragraph>' );
  181. model.change( writer => {
  182. writer.insertText( '#', doc.selection.getFirstPosition() );
  183. } );
  184. return waitForDebounce()
  185. .then( () => expect( panelView.isVisible ).to.be.true )
  186. .then( () => {
  187. model.change( writer => {
  188. writer.insertText( 'x', doc.selection.getFirstPosition() );
  189. } );
  190. } )
  191. .then( waitForDebounce )
  192. .then( () => {
  193. expect( panelView.isVisible ).to.be.false;
  194. expect( listView.items ).to.have.length( 0 );
  195. } );
  196. } );
  197. it( 'should hide panel when text was unmatched', () => {
  198. setData( model, '<paragraph>foo []</paragraph>' );
  199. model.change( writer => {
  200. writer.insertText( '#', doc.selection.getFirstPosition() );
  201. } );
  202. return waitForDebounce()
  203. .then( () => expect( panelView.isVisible ).to.be.true )
  204. .then( () => {
  205. model.change( writer => {
  206. const end = doc.selection.getFirstPosition();
  207. const start = end.getShiftedBy( -1 );
  208. writer.remove( writer.createRange( start, end ) );
  209. } );
  210. } )
  211. .then( waitForDebounce )
  212. .then( () => expect( panelView.isVisible ).to.be.false );
  213. } );
  214. } );
  215. } );
  216. describe( 'itemRenderer', () => {
  217. beforeEach( () => {
  218. const issues = [
  219. { id: '1002', title: 'Some bug in editor.' },
  220. { id: '1003', title: 'Introduce this feature.' },
  221. { id: '1004', title: 'Missing docs.' }
  222. ];
  223. return createClassicTestEditor( [
  224. {
  225. marker: '#',
  226. feed: feedText => {
  227. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  228. },
  229. itemRenderer: item => {
  230. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  231. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  232. return span;
  233. }
  234. }
  235. ] );
  236. } );
  237. it( 'should show panel for matched marker', () => {
  238. setData( model, '<paragraph>foo []</paragraph>' );
  239. model.change( writer => {
  240. writer.insertText( '#', doc.selection.getFirstPosition() );
  241. } );
  242. return waitForDebounce()
  243. .then( () => {
  244. expect( panelView.isVisible ).to.be.true;
  245. expect( listView.items ).to.have.length( 3 );
  246. } );
  247. } );
  248. } );
  249. describe( 'execute', () => {
  250. beforeEach( () => createClassicTestEditor( staticConfig ) );
  251. it( 'should call the mention command with proper options', () => {
  252. setData( model, '<paragraph>foo []</paragraph>' );
  253. model.change( writer => {
  254. writer.insertText( '@', doc.selection.getFirstPosition() );
  255. } );
  256. const command = editor.commands.get( 'mention' );
  257. const spy = testUtils.sinon.spy( command, 'execute' );
  258. return waitForDebounce()
  259. .then( () => {
  260. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  261. sinon.assert.calledOnce( spy );
  262. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  263. expect( commandOptions ).to.have.property( 'mention', 'Barney' );
  264. expect( commandOptions ).to.have.property( 'marker', '@' );
  265. expect( commandOptions ).to.have.property( 'range' );
  266. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  267. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  268. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  269. } );
  270. } );
  271. it( 'should hide panel on execute', () => {
  272. setData( model, '<paragraph>foo []</paragraph>' );
  273. model.change( writer => {
  274. writer.insertText( '@', doc.selection.getFirstPosition() );
  275. } );
  276. return waitForDebounce()
  277. .then( () => {
  278. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  279. expect( panelView.isVisible ).to.be.false;
  280. } );
  281. } );
  282. } );
  283. function createClassicTestEditor( mentionConfig ) {
  284. return ClassicTestEditor
  285. .create( editorElement, {
  286. plugins: [ Paragraph, MentionEditing, MentionUI ],
  287. mention: mentionConfig
  288. } )
  289. .then( newEditor => {
  290. editor = newEditor;
  291. model = editor.model;
  292. doc = model.document;
  293. editingView = editor.editing.view;
  294. mentionUI = editor.plugins.get( MentionUI );
  295. panelView = mentionUI.panelView;
  296. mentionsView = mentionUI._mentionsView;
  297. listView = mentionsView.listView;
  298. editingView.attachDomRoot( editorElement );
  299. // Focus the engine.
  300. editingView.document.isFocused = true;
  301. editingView.getDomRoot().focus();
  302. // Remove all selection ranges from DOM before testing.
  303. window.getSelection().removeAllRanges();
  304. } );
  305. }
  306. function waitForDebounce() {
  307. return new Promise( resolve => {
  308. setTimeout( () => {
  309. resolve();
  310. }, 50 );
  311. } );
  312. }
  313. } );