mentionui.js 26 KB

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