mentionui.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 panel for matched marker at the beginning of paragraph', () => {
  161. setData( model, '<paragraph>[] foo</paragraph>' );
  162. model.change( writer => {
  163. writer.insertText( '@', doc.selection.getFirstPosition() );
  164. } );
  165. return waitForDebounce()
  166. .then( () => {
  167. expect( panelView.isVisible ).to.be.true;
  168. expect( listView.items ).to.have.length( 5 );
  169. } );
  170. } );
  171. it( 'should not show panel for marker in the middle of other word', () => {
  172. setData( model, '<paragraph>foo[]</paragraph>' );
  173. model.change( writer => {
  174. writer.insertText( '@', doc.selection.getFirstPosition() );
  175. } );
  176. return waitForDebounce()
  177. .then( () => {
  178. expect( panelView.isVisible ).to.be.false;
  179. } );
  180. } );
  181. it( 'should not show panel when selection is inside a mention', () => {
  182. setData( model, '<paragraph>foo <$text mention="{\'name\':\'John\'}">@John</$text> bar</paragraph>' );
  183. model.change( writer => {
  184. writer.setSelection( doc.getRoot().getChild( 0 ), 7 );
  185. } );
  186. return waitForDebounce()
  187. .then( () => {
  188. expect( panelView.isVisible ).to.be.false;
  189. } );
  190. } );
  191. it( 'should not show panel when selection is at the end of a mention', () => {
  192. setData( model, '<paragraph>foo <$text mention="{\'name\':\'John\'}">@John</$text> bar</paragraph>' );
  193. model.change( writer => {
  194. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  195. } );
  196. return waitForDebounce()
  197. .then( () => {
  198. expect( panelView.isVisible ).to.be.false;
  199. } );
  200. } );
  201. it( 'should not show panel when selection is not collapsed', () => {
  202. setData( model, '<paragraph>foo []</paragraph>' );
  203. model.change( writer => {
  204. writer.insertText( '@', doc.selection.getFirstPosition() );
  205. } );
  206. model.change( () => {
  207. model.modifySelection( doc.selection, { direction: 'backward', unit: 'codePoint' } );
  208. } );
  209. return waitForDebounce()
  210. .then( () => {
  211. expect( panelView.isVisible ).to.be.false;
  212. } );
  213. } );
  214. it( 'should show filtered results for matched text', () => {
  215. setData( model, '<paragraph>foo []</paragraph>' );
  216. model.change( writer => {
  217. writer.insertText( '@', doc.selection.getFirstPosition() );
  218. } );
  219. model.change( writer => {
  220. writer.insertText( 'T', doc.selection.getFirstPosition() );
  221. } );
  222. return waitForDebounce()
  223. .then( () => {
  224. expect( panelView.isVisible ).to.be.true;
  225. expect( listView.items ).to.have.length( 1 );
  226. } );
  227. } );
  228. it( 'should focus the first item in panel', () => {
  229. setData( model, '<paragraph>foo []</paragraph>' );
  230. model.change( writer => {
  231. writer.insertText( '@', doc.selection.getFirstPosition() );
  232. } );
  233. return waitForDebounce()
  234. .then( () => {
  235. const button = listView.items.get( 0 ).children.get( 0 );
  236. expect( button.isOn ).to.be.true;
  237. } );
  238. } );
  239. it( 'should hide panel if no matched items', () => {
  240. setData( model, '<paragraph>foo []</paragraph>' );
  241. model.change( writer => {
  242. writer.insertText( '@', doc.selection.getFirstPosition() );
  243. } );
  244. return waitForDebounce()
  245. .then( () => expect( panelView.isVisible ).to.be.true )
  246. .then( () => {
  247. model.change( writer => {
  248. writer.insertText( 'x', doc.selection.getFirstPosition() );
  249. } );
  250. } )
  251. .then( waitForDebounce )
  252. .then( () => {
  253. expect( panelView.isVisible ).to.be.false;
  254. expect( listView.items ).to.have.length( 0 );
  255. } );
  256. } );
  257. it( 'should hide panel when text was unmatched', () => {
  258. setData( model, '<paragraph>foo []</paragraph>' );
  259. model.change( writer => {
  260. writer.insertText( '@', doc.selection.getFirstPosition() );
  261. } );
  262. return waitForDebounce()
  263. .then( () => expect( panelView.isVisible ).to.be.true )
  264. .then( () => {
  265. model.change( writer => {
  266. const end = doc.selection.getFirstPosition();
  267. const start = end.getShiftedBy( -1 );
  268. writer.remove( writer.createRange( start, end ) );
  269. } );
  270. } )
  271. .then( waitForDebounce )
  272. .then( () => expect( panelView.isVisible ).to.be.false );
  273. } );
  274. } );
  275. describe( 'asynchronous list with custom trigger', () => {
  276. beforeEach( () => {
  277. const issuesNumbers = [ '100', '101', '102', '103' ];
  278. return createClassicTestEditor( [
  279. {
  280. marker: '#',
  281. feed: feedText => {
  282. return new Promise( resolve => {
  283. setTimeout( () => {
  284. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  285. }, 20 );
  286. } );
  287. }
  288. }
  289. ] );
  290. } );
  291. it( 'should show panel for matched marker', () => {
  292. setData( model, '<paragraph>foo []</paragraph>' );
  293. model.change( writer => {
  294. writer.insertText( '#', doc.selection.getFirstPosition() );
  295. } );
  296. return waitForDebounce()
  297. .then( () => {
  298. expect( panelView.isVisible ).to.be.true;
  299. expect( listView.items ).to.have.length( 4 );
  300. } );
  301. } );
  302. it( 'should show filtered results for matched text', () => {
  303. setData( model, '<paragraph>foo []</paragraph>' );
  304. model.change( writer => {
  305. writer.insertText( '#', doc.selection.getFirstPosition() );
  306. } );
  307. model.change( writer => {
  308. writer.insertText( '2', doc.selection.getFirstPosition() );
  309. } );
  310. return waitForDebounce()
  311. .then( () => {
  312. expect( panelView.isVisible ).to.be.true;
  313. expect( listView.items ).to.have.length( 1 );
  314. } );
  315. } );
  316. it( 'should hide panel if no matched items', () => {
  317. setData( model, '<paragraph>foo []</paragraph>' );
  318. model.change( writer => {
  319. writer.insertText( '#', doc.selection.getFirstPosition() );
  320. } );
  321. return waitForDebounce()
  322. .then( () => expect( panelView.isVisible ).to.be.true )
  323. .then( () => {
  324. model.change( writer => {
  325. writer.insertText( 'x', doc.selection.getFirstPosition() );
  326. } );
  327. } )
  328. .then( waitForDebounce )
  329. .then( () => {
  330. expect( panelView.isVisible ).to.be.false;
  331. expect( listView.items ).to.have.length( 0 );
  332. } );
  333. } );
  334. it( 'should hide panel when text was unmatched', () => {
  335. setData( model, '<paragraph>foo []</paragraph>' );
  336. model.change( writer => {
  337. writer.insertText( '#', doc.selection.getFirstPosition() );
  338. } );
  339. return waitForDebounce()
  340. .then( () => expect( panelView.isVisible ).to.be.true )
  341. .then( () => {
  342. model.change( writer => {
  343. const end = doc.selection.getFirstPosition();
  344. const start = end.getShiftedBy( -1 );
  345. writer.remove( writer.createRange( start, end ) );
  346. } );
  347. } )
  348. .then( waitForDebounce )
  349. .then( () => expect( panelView.isVisible ).to.be.false );
  350. } );
  351. } );
  352. } );
  353. describe( 'panel behavior', () => {
  354. it( 'should close the opened panel on esc', () => {
  355. return createClassicTestEditor( staticConfig )
  356. .then( () => {
  357. setData( model, '<paragraph>foo []</paragraph>' );
  358. model.change( writer => {
  359. writer.insertText( '@', doc.selection.getFirstPosition() );
  360. } );
  361. } )
  362. .then( waitForDebounce )
  363. .then( () => {
  364. expect( panelView.isVisible ).to.be.true;
  365. fireKeyDownEvent( {
  366. keyCode: keyCodes.esc,
  367. preventDefault: sinon.spy(),
  368. stopPropagation: sinon.spy()
  369. } );
  370. expect( panelView.isVisible ).to.be.false;
  371. } );
  372. } );
  373. it( 'should hide the panel when click outside', () => {
  374. return createClassicTestEditor( staticConfig )
  375. .then( () => {
  376. setData( model, '<paragraph>foo []</paragraph>' );
  377. model.change( writer => {
  378. writer.insertText( '@', doc.selection.getFirstPosition() );
  379. } );
  380. } )
  381. .then( waitForDebounce )
  382. .then( () => {
  383. expect( panelView.isVisible ).to.be.true;
  384. model.change( writer => {
  385. // Place position at the begging of a paragraph.
  386. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  387. } );
  388. expect( panelView.isVisible ).to.be.false;
  389. } );
  390. } );
  391. describe( 'default list item', () => {
  392. const feedItems = staticConfig[ 0 ].feed.map( name => ( { name } ) );
  393. beforeEach( () => {
  394. return createClassicTestEditor( staticConfig );
  395. } );
  396. describe( 'on arrows', () => {
  397. it( 'should cycle down on arrow down', () => {
  398. setData( model, '<paragraph>foo []</paragraph>' );
  399. model.change( writer => {
  400. writer.insertText( '@', doc.selection.getFirstPosition() );
  401. } );
  402. return waitForDebounce()
  403. .then( () => {
  404. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  405. const keyEvtData = {
  406. keyCode: keyCodes.arrowdown,
  407. preventDefault: sinon.spy(),
  408. stopPropagation: sinon.spy()
  409. };
  410. fireKeyDownEvent( keyEvtData );
  411. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  412. fireKeyDownEvent( keyEvtData );
  413. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  414. fireKeyDownEvent( keyEvtData );
  415. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  416. fireKeyDownEvent( keyEvtData );
  417. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  418. fireKeyDownEvent( keyEvtData );
  419. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  420. } );
  421. } );
  422. it( 'should cycle up on arrow up', () => {
  423. setData( model, '<paragraph>foo []</paragraph>' );
  424. model.change( writer => {
  425. writer.insertText( '@', doc.selection.getFirstPosition() );
  426. } );
  427. return waitForDebounce()
  428. .then( () => {
  429. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  430. const keyEvtData = {
  431. keyCode: keyCodes.arrowup,
  432. preventDefault: sinon.spy(),
  433. stopPropagation: sinon.spy()
  434. };
  435. fireKeyDownEvent( keyEvtData );
  436. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  437. fireKeyDownEvent( keyEvtData );
  438. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  439. fireKeyDownEvent( keyEvtData );
  440. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  441. fireKeyDownEvent( keyEvtData );
  442. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  443. fireKeyDownEvent( keyEvtData );
  444. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  445. } );
  446. } );
  447. } );
  448. describe( 'on "execute" keys', () => {
  449. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  450. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  451. testExecuteKey( 'space', keyCodes.space, feedItems );
  452. } );
  453. } );
  454. describe( 'custom list item', () => {
  455. const issues = [
  456. { id: '1002', title: 'Some bug in editor.' },
  457. { id: '1003', title: 'Introduce this feature.' },
  458. { id: '1004', title: 'Missing docs.' },
  459. { id: '1005', title: 'Another bug.' },
  460. { id: '1006', title: 'More bugs' }
  461. ];
  462. beforeEach( () => {
  463. return createClassicTestEditor( [
  464. {
  465. marker: '@',
  466. feed: feedText => {
  467. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  468. },
  469. itemRenderer: item => {
  470. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  471. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  472. return span;
  473. }
  474. }
  475. ] );
  476. } );
  477. it( 'should show panel for matched marker', () => {
  478. setData( model, '<paragraph>foo []</paragraph>' );
  479. model.change( writer => {
  480. writer.insertText( '@', doc.selection.getFirstPosition() );
  481. } );
  482. return waitForDebounce()
  483. .then( () => {
  484. expect( panelView.isVisible ).to.be.true;
  485. expect( listView.items ).to.have.length( 5 );
  486. } );
  487. } );
  488. describe( 'keys', () => {
  489. describe( 'on arrows', () => {
  490. it( 'should cycle down on arrow down', () => {
  491. setData( model, '<paragraph>foo []</paragraph>' );
  492. model.change( writer => {
  493. writer.insertText( '@', doc.selection.getFirstPosition() );
  494. } );
  495. return waitForDebounce()
  496. .then( () => {
  497. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  498. const keyEvtData = {
  499. keyCode: keyCodes.arrowdown,
  500. preventDefault: sinon.spy(),
  501. stopPropagation: sinon.spy()
  502. };
  503. fireKeyDownEvent( keyEvtData );
  504. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  505. fireKeyDownEvent( keyEvtData );
  506. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  507. fireKeyDownEvent( keyEvtData );
  508. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  509. fireKeyDownEvent( keyEvtData );
  510. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  511. fireKeyDownEvent( keyEvtData );
  512. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  513. } );
  514. } );
  515. it( 'should cycle up on arrow up', () => {
  516. setData( model, '<paragraph>foo []</paragraph>' );
  517. model.change( writer => {
  518. writer.insertText( '@', doc.selection.getFirstPosition() );
  519. } );
  520. return waitForDebounce()
  521. .then( () => {
  522. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  523. const keyEvtData = {
  524. keyCode: keyCodes.arrowup,
  525. preventDefault: sinon.spy(),
  526. stopPropagation: sinon.spy()
  527. };
  528. fireKeyDownEvent( keyEvtData );
  529. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  530. fireKeyDownEvent( keyEvtData );
  531. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  532. fireKeyDownEvent( keyEvtData );
  533. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  534. fireKeyDownEvent( keyEvtData );
  535. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  536. fireKeyDownEvent( keyEvtData );
  537. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  538. } );
  539. } );
  540. } );
  541. describe( 'on "execute" keys', () => {
  542. testExecuteKey( 'enter', keyCodes.enter, issues );
  543. testExecuteKey( 'tab', keyCodes.tab, issues );
  544. testExecuteKey( 'space', keyCodes.space, issues );
  545. } );
  546. } );
  547. } );
  548. function testExecuteKey( name, keyCode, feedItems ) {
  549. it( 'should execute selected button on ' + name, () => {
  550. setData( model, '<paragraph>foo []</paragraph>' );
  551. model.change( writer => {
  552. writer.insertText( '@', doc.selection.getFirstPosition() );
  553. } );
  554. const command = editor.commands.get( 'mention' );
  555. const spy = testUtils.sinon.spy( command, 'execute' );
  556. return waitForDebounce()
  557. .then( () => {
  558. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  559. fireKeyDownEvent( {
  560. keyCode: keyCodes.arrowup,
  561. preventDefault: sinon.spy(),
  562. stopPropagation: sinon.spy()
  563. } );
  564. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  565. fireKeyDownEvent( {
  566. keyCode,
  567. preventDefault: sinon.spy(),
  568. stopPropagation: sinon.spy()
  569. } );
  570. sinon.assert.calledOnce( spy );
  571. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  572. const item = feedItems[ 4 ];
  573. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  574. expect( commandOptions ).to.have.property( 'marker', '@' );
  575. expect( commandOptions ).to.have.property( 'range' );
  576. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  577. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  578. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  579. } );
  580. } );
  581. }
  582. } );
  583. describe( 'execute', () => {
  584. beforeEach( () => createClassicTestEditor( staticConfig ) );
  585. it( 'should call the mention command with proper options', () => {
  586. setData( model, '<paragraph>foo []</paragraph>' );
  587. model.change( writer => {
  588. writer.insertText( '@', doc.selection.getFirstPosition() );
  589. } );
  590. const command = editor.commands.get( 'mention' );
  591. const spy = testUtils.sinon.spy( command, 'execute' );
  592. return waitForDebounce()
  593. .then( () => {
  594. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  595. sinon.assert.calledOnce( spy );
  596. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  597. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( { name: 'Barney' } );
  598. expect( commandOptions ).to.have.property( 'marker', '@' );
  599. expect( commandOptions ).to.have.property( 'range' );
  600. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  601. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  602. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  603. } );
  604. } );
  605. it( 'should hide panel on execute', () => {
  606. setData( model, '<paragraph>foo []</paragraph>' );
  607. model.change( writer => {
  608. writer.insertText( '@', doc.selection.getFirstPosition() );
  609. } );
  610. return waitForDebounce()
  611. .then( () => {
  612. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  613. expect( panelView.isVisible ).to.be.false;
  614. } );
  615. } );
  616. } );
  617. function createClassicTestEditor( mentionConfig ) {
  618. return ClassicTestEditor
  619. .create( editorElement, {
  620. plugins: [ Paragraph, MentionEditing, MentionUI ],
  621. mention: mentionConfig
  622. } )
  623. .then( newEditor => {
  624. editor = newEditor;
  625. model = editor.model;
  626. doc = model.document;
  627. editingView = editor.editing.view;
  628. mentionUI = editor.plugins.get( MentionUI );
  629. panelView = mentionUI.panelView;
  630. mentionsView = mentionUI._mentionsView;
  631. listView = mentionsView.listView;
  632. editingView.attachDomRoot( editorElement );
  633. // Focus the engine.
  634. editingView.document.isFocused = true;
  635. editingView.getDomRoot().focus();
  636. // Remove all selection ranges from DOM before testing.
  637. window.getSelection().removeAllRanges();
  638. } );
  639. }
  640. function waitForDebounce() {
  641. return new Promise( resolve => {
  642. setTimeout( () => {
  643. resolve();
  644. }, 50 );
  645. } );
  646. }
  647. function fireKeyDownEvent( options ) {
  648. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  649. const eventData = new DomEventData( editingView.document, {
  650. target: document.body
  651. }, options );
  652. editingView.document.fire( eventInfo, eventData );
  653. }
  654. function stubSelectionRects( rects ) {
  655. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  656. // Mock selection rect.
  657. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  658. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  659. sinon.stub( domRange, 'getClientRects' )
  660. .returns( rects );
  661. return domRange;
  662. } );
  663. }
  664. function expectChildViewsIsOnState( expectedState ) {
  665. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  666. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  667. }
  668. } );