8
0

mentionui.js 24 KB

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