mentionui.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import MentionUI from '../src/mentionui';
  15. import MentionEditing from '../src/mentionediting';
  16. import MentionsView from '../src/ui/mentionsview';
  17. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  18. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  19. describe( 'MentionUI', () => {
  20. let editor, model, doc, editingView, mentionUI, editorElement, mentionsView, panelView, listView;
  21. const staticConfig = [
  22. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  23. ];
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. editorElement = document.createElement( 'div' );
  27. document.body.appendChild( editorElement );
  28. } );
  29. afterEach( () => {
  30. sinon.restore();
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. it( 'should create a plugin instance', () => {
  35. return createClassicTestEditor().then( () => {
  36. expect( mentionUI ).to.instanceOf( Plugin );
  37. expect( mentionUI ).to.instanceOf( MentionUI );
  38. } );
  39. } );
  40. describe( 'pluginName', () => {
  41. it( 'should return plugin by its name', () => {
  42. return createClassicTestEditor().then( () => {
  43. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  44. } );
  45. } );
  46. } );
  47. describe( 'child views', () => {
  48. beforeEach( () => createClassicTestEditor() );
  49. describe( 'panelView', () => {
  50. it( 'should create a view instance', () => {
  51. expect( panelView ).to.instanceof( BalloonPanelView );
  52. } );
  53. it( 'should be added to the ui.view.body collection', () => {
  54. expect( Array.from( editor.ui.view.body ) ).to.include( panelView );
  55. } );
  56. it( 'should have disabled arrow', () => {
  57. expect( panelView.withArrow ).to.be.false;
  58. } );
  59. it( 'should have added MentionView as a child', () => {
  60. expect( panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  61. } );
  62. } );
  63. } );
  64. describe( 'position', () => {
  65. let pinSpy;
  66. const caretRect = {
  67. bottom: 118,
  68. height: 18,
  69. left: 500,
  70. right: 501,
  71. top: 100,
  72. width: 1
  73. };
  74. const balloonRect = {
  75. bottom: 150,
  76. height: 150,
  77. left: 0,
  78. right: 200,
  79. top: 0,
  80. width: 200
  81. };
  82. beforeEach( () => {
  83. return createClassicTestEditor( staticConfig ).then( () => {
  84. pinSpy = sinon.spy( panelView, 'pin' );
  85. } );
  86. } );
  87. it( 'should properly calculate position data', () => {
  88. setData( model, '<paragraph>foo []</paragraph>' );
  89. stubSelectionRects( [ caretRect ] );
  90. model.change( writer => {
  91. writer.insertText( '@', doc.selection.getFirstPosition() );
  92. } );
  93. return waitForDebounce()
  94. .then( () => {
  95. const pinArgument = pinSpy.firstCall.args[ 0 ];
  96. const { target, positions } = pinArgument;
  97. expect( target() ).to.deep.equal( caretRect );
  98. expect( positions ).to.have.length( 4 );
  99. const caretSouthEast = positions[ 0 ];
  100. const caretNorthEast = positions[ 1 ];
  101. const caretSouthWest = positions[ 2 ];
  102. const caretNorthWest = positions[ 3 ];
  103. expect( caretSouthEast( caretRect, balloonRect ) ).to.deep.equal( {
  104. left: 501,
  105. name: 'caret_se',
  106. top: 123
  107. } );
  108. expect( caretNorthEast( caretRect, balloonRect ) ).to.deep.equal( {
  109. left: 501,
  110. name: 'caret_ne',
  111. top: -55
  112. } );
  113. expect( caretSouthWest( caretRect, balloonRect ) ).to.deep.equal( {
  114. left: 301,
  115. name: 'caret_sw',
  116. top: 123
  117. } );
  118. expect( caretNorthWest( caretRect, balloonRect ) ).to.deep.equal( {
  119. left: 301,
  120. name: 'caret_nw',
  121. top: -55
  122. } );
  123. } );
  124. } );
  125. it( 'should re-calculate position on typing', () => {
  126. setData( model, '<paragraph>foo []</paragraph>' );
  127. stubSelectionRects( [ caretRect ] );
  128. model.change( writer => {
  129. writer.insertText( '@', doc.selection.getFirstPosition() );
  130. } );
  131. return waitForDebounce()
  132. .then( () => {
  133. sinon.assert.calledOnce( pinSpy );
  134. model.change( writer => {
  135. writer.insertText( 't', doc.selection.getFirstPosition() );
  136. } );
  137. } )
  138. .then( waitForDebounce )
  139. .then( () => {
  140. sinon.assert.calledTwice( pinSpy );
  141. } );
  142. } );
  143. } );
  144. describe( 'typing integration', () => {
  145. describe( 'static list with default trigger', () => {
  146. beforeEach( () => {
  147. return createClassicTestEditor( staticConfig );
  148. } );
  149. it( 'should show panel for matched marker', () => {
  150. setData( model, '<paragraph>foo []</paragraph>' );
  151. model.change( writer => {
  152. writer.insertText( '@', doc.selection.getFirstPosition() );
  153. } );
  154. return waitForDebounce()
  155. .then( () => {
  156. expect( panelView.isVisible ).to.be.true;
  157. expect( listView.items ).to.have.length( 5 );
  158. } );
  159. } );
  160. it( 'should show filtered results for matched text', () => {
  161. setData( model, '<paragraph>foo []</paragraph>' );
  162. model.change( writer => {
  163. writer.insertText( '@', doc.selection.getFirstPosition() );
  164. } );
  165. model.change( writer => {
  166. writer.insertText( 'T', doc.selection.getFirstPosition() );
  167. } );
  168. return waitForDebounce()
  169. .then( () => {
  170. expect( panelView.isVisible ).to.be.true;
  171. expect( listView.items ).to.have.length( 1 );
  172. } );
  173. } );
  174. it( 'should focus the first item in panel', () => {
  175. setData( model, '<paragraph>foo []</paragraph>' );
  176. model.change( writer => {
  177. writer.insertText( '@', doc.selection.getFirstPosition() );
  178. } );
  179. return waitForDebounce()
  180. .then( () => {
  181. const button = listView.items.get( 0 ).children.get( 0 );
  182. expect( button.isOn ).to.be.true;
  183. } );
  184. } );
  185. it( 'should hide panel if no matched items', () => {
  186. setData( model, '<paragraph>foo []</paragraph>' );
  187. model.change( writer => {
  188. writer.insertText( '@', doc.selection.getFirstPosition() );
  189. } );
  190. return waitForDebounce()
  191. .then( () => expect( panelView.isVisible ).to.be.true )
  192. .then( () => {
  193. model.change( writer => {
  194. writer.insertText( 'x', doc.selection.getFirstPosition() );
  195. } );
  196. } )
  197. .then( waitForDebounce )
  198. .then( () => {
  199. expect( panelView.isVisible ).to.be.false;
  200. expect( listView.items ).to.have.length( 0 );
  201. } );
  202. } );
  203. it( 'should hide panel when text was unmatched', () => {
  204. setData( model, '<paragraph>foo []</paragraph>' );
  205. model.change( writer => {
  206. writer.insertText( '@', doc.selection.getFirstPosition() );
  207. } );
  208. return waitForDebounce()
  209. .then( () => expect( panelView.isVisible ).to.be.true )
  210. .then( () => {
  211. model.change( writer => {
  212. const end = doc.selection.getFirstPosition();
  213. const start = end.getShiftedBy( -1 );
  214. writer.remove( writer.createRange( start, end ) );
  215. } );
  216. } )
  217. .then( waitForDebounce )
  218. .then( () => expect( panelView.isVisible ).to.be.false );
  219. } );
  220. } );
  221. describe( 'asynchronous list with custom trigger', () => {
  222. beforeEach( () => {
  223. const issuesNumbers = [ '100', '101', '102', '103' ];
  224. return createClassicTestEditor( [
  225. {
  226. marker: '#',
  227. feed: feedText => {
  228. return new Promise( resolve => {
  229. setTimeout( () => {
  230. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  231. }, 20 );
  232. } );
  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( 4 );
  246. } );
  247. } );
  248. it( 'should show filtered results for matched text', () => {
  249. setData( model, '<paragraph>foo []</paragraph>' );
  250. model.change( writer => {
  251. writer.insertText( '#', doc.selection.getFirstPosition() );
  252. } );
  253. model.change( writer => {
  254. writer.insertText( '2', doc.selection.getFirstPosition() );
  255. } );
  256. return waitForDebounce()
  257. .then( () => {
  258. expect( panelView.isVisible ).to.be.true;
  259. expect( listView.items ).to.have.length( 1 );
  260. } );
  261. } );
  262. it( 'should hide panel if no matched items', () => {
  263. setData( model, '<paragraph>foo []</paragraph>' );
  264. model.change( writer => {
  265. writer.insertText( '#', doc.selection.getFirstPosition() );
  266. } );
  267. return waitForDebounce()
  268. .then( () => expect( panelView.isVisible ).to.be.true )
  269. .then( () => {
  270. model.change( writer => {
  271. writer.insertText( 'x', doc.selection.getFirstPosition() );
  272. } );
  273. } )
  274. .then( waitForDebounce )
  275. .then( () => {
  276. expect( panelView.isVisible ).to.be.false;
  277. expect( listView.items ).to.have.length( 0 );
  278. } );
  279. } );
  280. it( 'should hide panel when text was unmatched', () => {
  281. setData( model, '<paragraph>foo []</paragraph>' );
  282. model.change( writer => {
  283. writer.insertText( '#', doc.selection.getFirstPosition() );
  284. } );
  285. return waitForDebounce()
  286. .then( () => expect( panelView.isVisible ).to.be.true )
  287. .then( () => {
  288. model.change( writer => {
  289. const end = doc.selection.getFirstPosition();
  290. const start = end.getShiftedBy( -1 );
  291. writer.remove( writer.createRange( start, end ) );
  292. } );
  293. } )
  294. .then( waitForDebounce )
  295. .then( () => expect( panelView.isVisible ).to.be.false );
  296. } );
  297. } );
  298. } );
  299. describe( 'panel behavior', () => {
  300. describe( 'on esc', () => {
  301. it( 'should close the opened panel', () => {
  302. return createClassicTestEditor( staticConfig )
  303. .then( () => {
  304. setData( model, '<paragraph>foo []</paragraph>' );
  305. model.change( writer => {
  306. writer.insertText( '@', doc.selection.getFirstPosition() );
  307. } );
  308. } )
  309. .then( waitForDebounce )
  310. .then( () => {
  311. expect( panelView.isVisible ).to.be.true;
  312. fireKeyDownEvent( {
  313. keyCode: keyCodes.esc,
  314. preventDefault: sinon.spy(),
  315. stopPropagation: sinon.spy()
  316. } );
  317. expect( panelView.isVisible ).to.be.false;
  318. } );
  319. } );
  320. } );
  321. describe( 'default list item', () => {
  322. const feedItems = staticConfig[ 0 ].feed.map( name => ( { name } ) );
  323. beforeEach( () => {
  324. return createClassicTestEditor( staticConfig );
  325. } );
  326. describe( 'on arrows', () => {
  327. it( 'should cycle down on arrow down', () => {
  328. setData( model, '<paragraph>foo []</paragraph>' );
  329. model.change( writer => {
  330. writer.insertText( '@', doc.selection.getFirstPosition() );
  331. } );
  332. return waitForDebounce()
  333. .then( () => {
  334. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  335. const keyEvtData = {
  336. keyCode: keyCodes.arrowdown,
  337. preventDefault: sinon.spy(),
  338. stopPropagation: sinon.spy()
  339. };
  340. fireKeyDownEvent( keyEvtData );
  341. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  342. fireKeyDownEvent( keyEvtData );
  343. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  344. fireKeyDownEvent( keyEvtData );
  345. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  346. fireKeyDownEvent( keyEvtData );
  347. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  348. fireKeyDownEvent( keyEvtData );
  349. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  350. } );
  351. } );
  352. it( 'should cycle up on arrow up', () => {
  353. setData( model, '<paragraph>foo []</paragraph>' );
  354. model.change( writer => {
  355. writer.insertText( '@', doc.selection.getFirstPosition() );
  356. } );
  357. return waitForDebounce()
  358. .then( () => {
  359. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  360. const keyEvtData = {
  361. keyCode: keyCodes.arrowup,
  362. preventDefault: sinon.spy(),
  363. stopPropagation: sinon.spy()
  364. };
  365. fireKeyDownEvent( keyEvtData );
  366. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  367. fireKeyDownEvent( keyEvtData );
  368. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  369. fireKeyDownEvent( keyEvtData );
  370. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  371. fireKeyDownEvent( keyEvtData );
  372. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  373. fireKeyDownEvent( keyEvtData );
  374. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  375. } );
  376. } );
  377. } );
  378. describe( 'on "execute" keys', () => {
  379. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  380. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  381. testExecuteKey( 'space', keyCodes.space, feedItems );
  382. } );
  383. } );
  384. describe( 'custom list item', () => {
  385. const issues = [
  386. { id: '1002', title: 'Some bug in editor.' },
  387. { id: '1003', title: 'Introduce this feature.' },
  388. { id: '1004', title: 'Missing docs.' },
  389. { id: '1005', title: 'Another bug.' },
  390. { id: '1006', title: 'More bugs' }
  391. ];
  392. beforeEach( () => {
  393. return createClassicTestEditor( [
  394. {
  395. marker: '@',
  396. feed: feedText => {
  397. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  398. },
  399. itemRenderer: item => {
  400. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  401. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  402. return span;
  403. }
  404. }
  405. ] );
  406. } );
  407. it( 'should show panel for matched marker', () => {
  408. setData( model, '<paragraph>foo []</paragraph>' );
  409. model.change( writer => {
  410. writer.insertText( '@', doc.selection.getFirstPosition() );
  411. } );
  412. return waitForDebounce()
  413. .then( () => {
  414. expect( panelView.isVisible ).to.be.true;
  415. expect( listView.items ).to.have.length( 5 );
  416. } );
  417. } );
  418. describe( 'keys', () => {
  419. describe( 'on arrows', () => {
  420. it( 'should cycle down on arrow down', () => {
  421. setData( model, '<paragraph>foo []</paragraph>' );
  422. model.change( writer => {
  423. writer.insertText( '@', doc.selection.getFirstPosition() );
  424. } );
  425. return waitForDebounce()
  426. .then( () => {
  427. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  428. const keyEvtData = {
  429. keyCode: keyCodes.arrowdown,
  430. preventDefault: sinon.spy(),
  431. stopPropagation: sinon.spy()
  432. };
  433. fireKeyDownEvent( keyEvtData );
  434. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  435. fireKeyDownEvent( keyEvtData );
  436. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  437. fireKeyDownEvent( keyEvtData );
  438. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  439. fireKeyDownEvent( keyEvtData );
  440. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  441. fireKeyDownEvent( keyEvtData );
  442. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  443. } );
  444. } );
  445. it( 'should cycle up on arrow up', () => {
  446. setData( model, '<paragraph>foo []</paragraph>' );
  447. model.change( writer => {
  448. writer.insertText( '@', doc.selection.getFirstPosition() );
  449. } );
  450. return waitForDebounce()
  451. .then( () => {
  452. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  453. const keyEvtData = {
  454. keyCode: keyCodes.arrowup,
  455. preventDefault: sinon.spy(),
  456. stopPropagation: sinon.spy()
  457. };
  458. fireKeyDownEvent( keyEvtData );
  459. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  460. fireKeyDownEvent( keyEvtData );
  461. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  462. fireKeyDownEvent( keyEvtData );
  463. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  464. fireKeyDownEvent( keyEvtData );
  465. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  466. fireKeyDownEvent( keyEvtData );
  467. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  468. } );
  469. } );
  470. } );
  471. describe( 'on "execute" keys', () => {
  472. testExecuteKey( 'enter', keyCodes.enter, issues );
  473. testExecuteKey( 'tab', keyCodes.tab, issues );
  474. testExecuteKey( 'space', keyCodes.space, issues );
  475. } );
  476. } );
  477. } );
  478. function testExecuteKey( name, keyCode, feedItems ) {
  479. it( 'should execute selected button on ' + name, () => {
  480. setData( model, '<paragraph>foo []</paragraph>' );
  481. model.change( writer => {
  482. writer.insertText( '@', doc.selection.getFirstPosition() );
  483. } );
  484. const command = editor.commands.get( 'mention' );
  485. const spy = testUtils.sinon.spy( command, 'execute' );
  486. return waitForDebounce()
  487. .then( () => {
  488. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  489. fireKeyDownEvent( {
  490. keyCode: keyCodes.arrowup,
  491. preventDefault: sinon.spy(),
  492. stopPropagation: sinon.spy()
  493. } );
  494. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  495. fireKeyDownEvent( {
  496. keyCode,
  497. preventDefault: sinon.spy(),
  498. stopPropagation: sinon.spy()
  499. } );
  500. sinon.assert.calledOnce( spy );
  501. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  502. const item = feedItems[ 4 ];
  503. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  504. expect( commandOptions ).to.have.property( 'marker', '@' );
  505. expect( commandOptions ).to.have.property( 'range' );
  506. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  507. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  508. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  509. } );
  510. } );
  511. }
  512. } );
  513. describe( 'execute', () => {
  514. beforeEach( () => createClassicTestEditor( staticConfig ) );
  515. it( 'should call the mention command with proper options', () => {
  516. setData( model, '<paragraph>foo []</paragraph>' );
  517. model.change( writer => {
  518. writer.insertText( '@', doc.selection.getFirstPosition() );
  519. } );
  520. const command = editor.commands.get( 'mention' );
  521. const spy = testUtils.sinon.spy( command, 'execute' );
  522. return waitForDebounce()
  523. .then( () => {
  524. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  525. sinon.assert.calledOnce( spy );
  526. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  527. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( { name: 'Barney' } );
  528. expect( commandOptions ).to.have.property( 'marker', '@' );
  529. expect( commandOptions ).to.have.property( 'range' );
  530. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  531. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  532. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  533. } );
  534. } );
  535. it( 'should hide panel on execute', () => {
  536. setData( model, '<paragraph>foo []</paragraph>' );
  537. model.change( writer => {
  538. writer.insertText( '@', doc.selection.getFirstPosition() );
  539. } );
  540. return waitForDebounce()
  541. .then( () => {
  542. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  543. expect( panelView.isVisible ).to.be.false;
  544. } );
  545. } );
  546. } );
  547. function createClassicTestEditor( mentionConfig ) {
  548. return ClassicTestEditor
  549. .create( editorElement, {
  550. plugins: [ Paragraph, MentionEditing, MentionUI ],
  551. mention: mentionConfig
  552. } )
  553. .then( newEditor => {
  554. editor = newEditor;
  555. model = editor.model;
  556. doc = model.document;
  557. editingView = editor.editing.view;
  558. mentionUI = editor.plugins.get( MentionUI );
  559. panelView = mentionUI.panelView;
  560. mentionsView = mentionUI._mentionsView;
  561. listView = mentionsView.listView;
  562. editingView.attachDomRoot( editorElement );
  563. // Focus the engine.
  564. editingView.document.isFocused = true;
  565. editingView.getDomRoot().focus();
  566. // Remove all selection ranges from DOM before testing.
  567. window.getSelection().removeAllRanges();
  568. } );
  569. }
  570. function waitForDebounce() {
  571. return new Promise( resolve => {
  572. setTimeout( () => {
  573. resolve();
  574. }, 50 );
  575. } );
  576. }
  577. function fireKeyDownEvent( options ) {
  578. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  579. const eventData = new DomEventData( editingView.document, {
  580. target: document.body
  581. }, options );
  582. editingView.document.fire( eventInfo, eventData );
  583. }
  584. function stubSelectionRects( rects ) {
  585. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  586. // Mock selection rect.
  587. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  588. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  589. sinon.stub( domRange, 'getClientRects' )
  590. .returns( rects );
  591. return domRange;
  592. } );
  593. }
  594. function expectChildViewsIsOnState( expectedState ) {
  595. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  596. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  597. }
  598. } );