8
0

mentionui.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. it( 'should close the opened panel on esc', () => {
  301. return createClassicTestEditor( staticConfig )
  302. .then( () => {
  303. setData( model, '<paragraph>foo []</paragraph>' );
  304. model.change( writer => {
  305. writer.insertText( '@', doc.selection.getFirstPosition() );
  306. } );
  307. } )
  308. .then( waitForDebounce )
  309. .then( () => {
  310. expect( panelView.isVisible ).to.be.true;
  311. fireKeyDownEvent( {
  312. keyCode: keyCodes.esc,
  313. preventDefault: sinon.spy(),
  314. stopPropagation: sinon.spy()
  315. } );
  316. expect( panelView.isVisible ).to.be.false;
  317. } );
  318. } );
  319. it( 'should hide the panel when click outside', () => {
  320. return createClassicTestEditor( staticConfig )
  321. .then( () => {
  322. setData( model, '<paragraph>foo []</paragraph>' );
  323. model.change( writer => {
  324. writer.insertText( '@', doc.selection.getFirstPosition() );
  325. } );
  326. } )
  327. .then( waitForDebounce )
  328. .then( () => {
  329. expect( panelView.isVisible ).to.be.true;
  330. model.change( writer => {
  331. // Place position at the begging of a paragraph.
  332. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  333. } );
  334. expect( panelView.isVisible ).to.be.false;
  335. } );
  336. } );
  337. describe( 'default list item', () => {
  338. const feedItems = staticConfig[ 0 ].feed.map( name => ( { name } ) );
  339. beforeEach( () => {
  340. return createClassicTestEditor( staticConfig );
  341. } );
  342. describe( 'on arrows', () => {
  343. it( 'should cycle down on arrow down', () => {
  344. setData( model, '<paragraph>foo []</paragraph>' );
  345. model.change( writer => {
  346. writer.insertText( '@', doc.selection.getFirstPosition() );
  347. } );
  348. return waitForDebounce()
  349. .then( () => {
  350. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  351. const keyEvtData = {
  352. keyCode: keyCodes.arrowdown,
  353. preventDefault: sinon.spy(),
  354. stopPropagation: sinon.spy()
  355. };
  356. fireKeyDownEvent( keyEvtData );
  357. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  358. fireKeyDownEvent( keyEvtData );
  359. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  360. fireKeyDownEvent( keyEvtData );
  361. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  362. fireKeyDownEvent( keyEvtData );
  363. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  364. fireKeyDownEvent( keyEvtData );
  365. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  366. } );
  367. } );
  368. it( 'should cycle up on arrow up', () => {
  369. setData( model, '<paragraph>foo []</paragraph>' );
  370. model.change( writer => {
  371. writer.insertText( '@', doc.selection.getFirstPosition() );
  372. } );
  373. return waitForDebounce()
  374. .then( () => {
  375. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  376. const keyEvtData = {
  377. keyCode: keyCodes.arrowup,
  378. preventDefault: sinon.spy(),
  379. stopPropagation: sinon.spy()
  380. };
  381. fireKeyDownEvent( keyEvtData );
  382. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  383. fireKeyDownEvent( keyEvtData );
  384. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  385. fireKeyDownEvent( keyEvtData );
  386. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  387. fireKeyDownEvent( keyEvtData );
  388. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  389. fireKeyDownEvent( keyEvtData );
  390. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  391. } );
  392. } );
  393. } );
  394. describe( 'on "execute" keys', () => {
  395. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  396. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  397. testExecuteKey( 'space', keyCodes.space, feedItems );
  398. } );
  399. } );
  400. describe( 'custom list item', () => {
  401. const issues = [
  402. { id: '1002', title: 'Some bug in editor.' },
  403. { id: '1003', title: 'Introduce this feature.' },
  404. { id: '1004', title: 'Missing docs.' },
  405. { id: '1005', title: 'Another bug.' },
  406. { id: '1006', title: 'More bugs' }
  407. ];
  408. beforeEach( () => {
  409. return createClassicTestEditor( [
  410. {
  411. marker: '@',
  412. feed: feedText => {
  413. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  414. },
  415. itemRenderer: item => {
  416. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  417. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  418. return span;
  419. }
  420. }
  421. ] );
  422. } );
  423. it( 'should show panel for matched marker', () => {
  424. setData( model, '<paragraph>foo []</paragraph>' );
  425. model.change( writer => {
  426. writer.insertText( '@', doc.selection.getFirstPosition() );
  427. } );
  428. return waitForDebounce()
  429. .then( () => {
  430. expect( panelView.isVisible ).to.be.true;
  431. expect( listView.items ).to.have.length( 5 );
  432. } );
  433. } );
  434. describe( 'keys', () => {
  435. describe( 'on arrows', () => {
  436. it( 'should cycle down on arrow down', () => {
  437. setData( model, '<paragraph>foo []</paragraph>' );
  438. model.change( writer => {
  439. writer.insertText( '@', doc.selection.getFirstPosition() );
  440. } );
  441. return waitForDebounce()
  442. .then( () => {
  443. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  444. const keyEvtData = {
  445. keyCode: keyCodes.arrowdown,
  446. preventDefault: sinon.spy(),
  447. stopPropagation: sinon.spy()
  448. };
  449. fireKeyDownEvent( keyEvtData );
  450. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  451. fireKeyDownEvent( keyEvtData );
  452. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  453. fireKeyDownEvent( keyEvtData );
  454. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  455. fireKeyDownEvent( keyEvtData );
  456. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  457. fireKeyDownEvent( keyEvtData );
  458. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  459. } );
  460. } );
  461. it( 'should cycle up on arrow up', () => {
  462. setData( model, '<paragraph>foo []</paragraph>' );
  463. model.change( writer => {
  464. writer.insertText( '@', doc.selection.getFirstPosition() );
  465. } );
  466. return waitForDebounce()
  467. .then( () => {
  468. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  469. const keyEvtData = {
  470. keyCode: keyCodes.arrowup,
  471. preventDefault: sinon.spy(),
  472. stopPropagation: sinon.spy()
  473. };
  474. fireKeyDownEvent( keyEvtData );
  475. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  476. fireKeyDownEvent( keyEvtData );
  477. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  478. fireKeyDownEvent( keyEvtData );
  479. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  480. fireKeyDownEvent( keyEvtData );
  481. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  482. fireKeyDownEvent( keyEvtData );
  483. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  484. } );
  485. } );
  486. } );
  487. describe( 'on "execute" keys', () => {
  488. testExecuteKey( 'enter', keyCodes.enter, issues );
  489. testExecuteKey( 'tab', keyCodes.tab, issues );
  490. testExecuteKey( 'space', keyCodes.space, issues );
  491. } );
  492. } );
  493. } );
  494. function testExecuteKey( name, keyCode, feedItems ) {
  495. it( 'should execute selected button on ' + name, () => {
  496. setData( model, '<paragraph>foo []</paragraph>' );
  497. model.change( writer => {
  498. writer.insertText( '@', doc.selection.getFirstPosition() );
  499. } );
  500. const command = editor.commands.get( 'mention' );
  501. const spy = testUtils.sinon.spy( command, 'execute' );
  502. return waitForDebounce()
  503. .then( () => {
  504. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  505. fireKeyDownEvent( {
  506. keyCode: keyCodes.arrowup,
  507. preventDefault: sinon.spy(),
  508. stopPropagation: sinon.spy()
  509. } );
  510. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  511. fireKeyDownEvent( {
  512. keyCode,
  513. preventDefault: sinon.spy(),
  514. stopPropagation: sinon.spy()
  515. } );
  516. sinon.assert.calledOnce( spy );
  517. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  518. const item = feedItems[ 4 ];
  519. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  520. expect( commandOptions ).to.have.property( 'marker', '@' );
  521. expect( commandOptions ).to.have.property( 'range' );
  522. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  523. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  524. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  525. } );
  526. } );
  527. }
  528. } );
  529. describe( 'execute', () => {
  530. beforeEach( () => createClassicTestEditor( staticConfig ) );
  531. it( 'should call the mention command with proper options', () => {
  532. setData( model, '<paragraph>foo []</paragraph>' );
  533. model.change( writer => {
  534. writer.insertText( '@', doc.selection.getFirstPosition() );
  535. } );
  536. const command = editor.commands.get( 'mention' );
  537. const spy = testUtils.sinon.spy( command, 'execute' );
  538. return waitForDebounce()
  539. .then( () => {
  540. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  541. sinon.assert.calledOnce( spy );
  542. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  543. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( { name: 'Barney' } );
  544. expect( commandOptions ).to.have.property( 'marker', '@' );
  545. expect( commandOptions ).to.have.property( 'range' );
  546. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  547. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  548. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  549. } );
  550. } );
  551. it( 'should hide panel on execute', () => {
  552. setData( model, '<paragraph>foo []</paragraph>' );
  553. model.change( writer => {
  554. writer.insertText( '@', doc.selection.getFirstPosition() );
  555. } );
  556. return waitForDebounce()
  557. .then( () => {
  558. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  559. expect( panelView.isVisible ).to.be.false;
  560. } );
  561. } );
  562. } );
  563. function createClassicTestEditor( mentionConfig ) {
  564. return ClassicTestEditor
  565. .create( editorElement, {
  566. plugins: [ Paragraph, MentionEditing, MentionUI ],
  567. mention: mentionConfig
  568. } )
  569. .then( newEditor => {
  570. editor = newEditor;
  571. model = editor.model;
  572. doc = model.document;
  573. editingView = editor.editing.view;
  574. mentionUI = editor.plugins.get( MentionUI );
  575. panelView = mentionUI.panelView;
  576. mentionsView = mentionUI._mentionsView;
  577. listView = mentionsView.listView;
  578. editingView.attachDomRoot( editorElement );
  579. // Focus the engine.
  580. editingView.document.isFocused = true;
  581. editingView.getDomRoot().focus();
  582. // Remove all selection ranges from DOM before testing.
  583. window.getSelection().removeAllRanges();
  584. } );
  585. }
  586. function waitForDebounce() {
  587. return new Promise( resolve => {
  588. setTimeout( () => {
  589. resolve();
  590. }, 50 );
  591. } );
  592. }
  593. function fireKeyDownEvent( options ) {
  594. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  595. const eventData = new DomEventData( editingView.document, {
  596. target: document.body
  597. }, options );
  598. editingView.document.fire( eventInfo, eventData );
  599. }
  600. function stubSelectionRects( rects ) {
  601. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  602. // Mock selection rect.
  603. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  604. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  605. sinon.stub( domRange, 'getClientRects' )
  606. .returns( rects );
  607. return domRange;
  608. } );
  609. }
  610. function expectChildViewsIsOnState( expectedState ) {
  611. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  612. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  613. }
  614. } );