mentionui.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. feeds: [
  23. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  24. ]
  25. };
  26. testUtils.createSinonSandbox();
  27. beforeEach( () => {
  28. editorElement = document.createElement( 'div' );
  29. document.body.appendChild( editorElement );
  30. } );
  31. afterEach( () => {
  32. sinon.restore();
  33. editorElement.remove();
  34. return editor.destroy();
  35. } );
  36. it( 'should create a plugin instance', () => {
  37. return createClassicTestEditor().then( () => {
  38. expect( mentionUI ).to.instanceOf( Plugin );
  39. expect( mentionUI ).to.instanceOf( MentionUI );
  40. } );
  41. } );
  42. describe( 'pluginName', () => {
  43. it( 'should return plugin by its name', () => {
  44. return createClassicTestEditor().then( () => {
  45. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  46. } );
  47. } );
  48. } );
  49. describe( 'child views', () => {
  50. beforeEach( () => createClassicTestEditor() );
  51. describe( 'panelView', () => {
  52. it( 'should create a view instance', () => {
  53. expect( panelView ).to.instanceof( BalloonPanelView );
  54. } );
  55. it( 'should be added to the ui.view.body collection', () => {
  56. expect( Array.from( editor.ui.view.body ) ).to.include( panelView );
  57. } );
  58. it( 'should have disabled arrow', () => {
  59. expect( panelView.withArrow ).to.be.false;
  60. } );
  61. it( 'should have added MentionView as a child', () => {
  62. expect( panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  63. } );
  64. } );
  65. } );
  66. describe( 'position', () => {
  67. let pinSpy;
  68. const caretRect = {
  69. bottom: 118,
  70. height: 18,
  71. left: 500,
  72. right: 501,
  73. top: 100,
  74. width: 1
  75. };
  76. const balloonRect = {
  77. bottom: 150,
  78. height: 150,
  79. left: 0,
  80. right: 200,
  81. top: 0,
  82. width: 200
  83. };
  84. beforeEach( () => {
  85. return createClassicTestEditor( staticConfig ).then( () => {
  86. pinSpy = sinon.spy( panelView, 'pin' );
  87. } );
  88. } );
  89. it( 'should properly calculate position data', () => {
  90. const editableElement = editingView.document.selection.editableElement;
  91. setData( model, '<paragraph>foo []</paragraph>' );
  92. stubSelectionRects( [ caretRect ] );
  93. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  94. model.change( writer => {
  95. writer.insertText( '@', doc.selection.getFirstPosition() );
  96. } );
  97. return waitForDebounce()
  98. .then( () => {
  99. const pinArgument = pinSpy.firstCall.args[ 0 ];
  100. const { target, positions, limiter, fitInViewport } = pinArgument;
  101. expect( fitInViewport ).to.be.true;
  102. expect( positions ).to.have.length( 4 );
  103. // Mention UI should set limiter to the editable area.
  104. expect( limiter() ).to.equal( editingView.domConverter.mapViewToDom( editableElement ) );
  105. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  106. const mentionMarker = editor.model.markers.get( 'mention' );
  107. const focus = doc.selection.focus;
  108. const expectedRange = editor.model.createRange( focus.getShiftedBy( -1 ), focus );
  109. // It should create a model marker for matcher marker character ('@').
  110. expect( expectedRange.isEqual( mentionMarker.getRange() ) ).to.be.true;
  111. const toViewRangeSpy = sinon.spy( editor.editing.mapper, 'toViewRange' );
  112. expect( target() ).to.deep.equal( caretRect );
  113. sinon.assert.calledOnce( toViewRangeSpy );
  114. const range = toViewRangeSpy.firstCall.args[ 0 ];
  115. expect( mentionMarker.getRange().isEqual( range ), 'Should position to mention marker.' );
  116. const caretSouthEast = positions[ 0 ];
  117. const caretNorthEast = positions[ 1 ];
  118. const caretSouthWest = positions[ 2 ];
  119. const caretNorthWest = positions[ 3 ];
  120. expect( caretSouthEast( caretRect, balloonRect ) ).to.deep.equal( {
  121. left: 501,
  122. name: 'caret_se',
  123. top: 121
  124. } );
  125. expect( caretNorthEast( caretRect, balloonRect ) ).to.deep.equal( {
  126. left: 501,
  127. name: 'caret_ne',
  128. top: -53
  129. } );
  130. expect( caretSouthWest( caretRect, balloonRect ) ).to.deep.equal( {
  131. left: 301,
  132. name: 'caret_sw',
  133. top: 121
  134. } );
  135. expect( caretNorthWest( caretRect, balloonRect ) ).to.deep.equal( {
  136. left: 301,
  137. name: 'caret_nw',
  138. top: -53
  139. } );
  140. } );
  141. } );
  142. it( 'should re-calculate position on typing and stay on selected position', () => {
  143. setData( model, '<paragraph>foo []</paragraph>' );
  144. stubSelectionRects( [ caretRect ] );
  145. model.change( writer => {
  146. writer.insertText( '@', doc.selection.getFirstPosition() );
  147. } );
  148. let positionAfterFirstShow;
  149. return waitForDebounce()
  150. .then( () => {
  151. sinon.assert.calledOnce( pinSpy );
  152. const pinArgument = pinSpy.firstCall.args[ 0 ];
  153. const { positions } = pinArgument;
  154. expect( positions ).to.have.length( 4 );
  155. positionAfterFirstShow = panelView.position;
  156. model.change( writer => {
  157. writer.insertText( 't', doc.selection.getFirstPosition() );
  158. } );
  159. } )
  160. .then( waitForDebounce )
  161. .then( () => {
  162. sinon.assert.calledTwice( pinSpy );
  163. const pinArgument = pinSpy.secondCall.args[ 0 ];
  164. const { positions } = pinArgument;
  165. expect( positions, 'should reuse first matched position' ).to.have.length( 1 );
  166. expect( positions[ 0 ].name ).to.equal( positionAfterFirstShow );
  167. } );
  168. } );
  169. it( 'does not fail if selection has no #editableElement', () => {
  170. setData( model, '<paragraph>foo []</paragraph>' );
  171. stubSelectionRects( [ caretRect ] );
  172. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  173. model.change( writer => {
  174. writer.insertText( '@', doc.selection.getFirstPosition() );
  175. } );
  176. return waitForDebounce()
  177. .then( () => {
  178. const pinArgument = pinSpy.firstCall.args[ 0 ];
  179. const { limiter } = pinArgument;
  180. sinon.stub( editingView.document.selection, 'editableElement' ).value( null );
  181. // Should not break;
  182. expect( limiter() ).to.be.null;
  183. } );
  184. } );
  185. } );
  186. describe( 'typing integration', () => {
  187. it( 'should show panel for matched marker after typing minimum characters', () => {
  188. return createClassicTestEditor( { feeds: [ Object.assign( { minimumCharacters: 2 }, staticConfig.feeds[ 0 ] ) ] } )
  189. .then( () => {
  190. setData( model, '<paragraph>foo []</paragraph>' );
  191. model.change( writer => {
  192. writer.insertText( '@', doc.selection.getFirstPosition() );
  193. } );
  194. } )
  195. .then( () => {
  196. model.change( writer => {
  197. writer.insertText( 'B', doc.selection.getFirstPosition() );
  198. } );
  199. } )
  200. .then( waitForDebounce )
  201. .then( () => {
  202. expect( panelView.isVisible ).to.be.false;
  203. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  204. } )
  205. .then( waitForDebounce )
  206. .then( () => {
  207. model.change( writer => {
  208. writer.insertText( 'a', doc.selection.getFirstPosition() );
  209. } );
  210. } )
  211. .then( waitForDebounce )
  212. .then( () => {
  213. expect( panelView.isVisible ).to.be.true;
  214. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  215. expect( listView.items ).to.have.length( 1 );
  216. model.change( writer => {
  217. writer.insertText( 'r', doc.selection.getFirstPosition() );
  218. } );
  219. } )
  220. .then( waitForDebounce )
  221. .then( () => {
  222. expect( panelView.isVisible ).to.be.true;
  223. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  224. expect( listView.items ).to.have.length( 1 );
  225. } );
  226. } );
  227. describe( 'static list with large set of results', () => {
  228. const bigList = {
  229. marker: '@',
  230. feed: [
  231. 'a01', 'a02', 'a03', 'a04', 'a05', 'a06', 'a07', 'a08', 'a09', 'a10', 'a11', 'a12'
  232. ]
  233. };
  234. beforeEach( () => {
  235. return createClassicTestEditor( { feeds: [ bigList ] } );
  236. } );
  237. it( 'should show panel with no more then 10 items for default static feed', () => {
  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( 10 );
  246. } );
  247. } );
  248. it( 'should scroll mention panel to the selected item', () => {
  249. setData( model, '<paragraph>foo []</paragraph>' );
  250. model.change( writer => {
  251. writer.insertText( '@', doc.selection.getFirstPosition() );
  252. } );
  253. return waitForDebounce()
  254. .then( () => {
  255. expect( panelView.isVisible ).to.be.true;
  256. expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
  257. const arrowDownEvtData = {
  258. keyCode: keyCodes.arrowdown,
  259. preventDefault: sinon.spy(),
  260. stopPropagation: sinon.spy()
  261. };
  262. const arrowUpEvtData = {
  263. keyCode: keyCodes.arrowup,
  264. preventDefault: sinon.spy(),
  265. stopPropagation: sinon.spy()
  266. };
  267. fireKeyDownEvent( arrowDownEvtData );
  268. expect( mentionsView.element.scrollTop ).to.equal( 0 );
  269. expectChildViewsIsOnState( [ false, true, false, false, false, false, false, false, false, false ] );
  270. fireKeyDownEvent( arrowUpEvtData );
  271. fireKeyDownEvent( arrowUpEvtData );
  272. expectChildViewsIsOnState( [ false, false, false, false, false, false, false, false, false, true ] );
  273. expect( mentionsView.element.scrollTop ).to.be.not.equal( 0 );
  274. fireKeyDownEvent( arrowDownEvtData );
  275. expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
  276. expect( mentionsView.element.scrollTop ).to.equal( 0 );
  277. } );
  278. } );
  279. } );
  280. describe( 'static list with default trigger', () => {
  281. beforeEach( () => {
  282. return createClassicTestEditor( staticConfig );
  283. } );
  284. it( 'should show panel for matched marker', () => {
  285. setData( model, '<paragraph>foo []</paragraph>' );
  286. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  287. model.change( writer => {
  288. writer.insertText( '@', doc.selection.getFirstPosition() );
  289. } );
  290. return waitForDebounce()
  291. .then( () => {
  292. expect( panelView.isVisible ).to.be.true;
  293. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  294. expect( listView.items ).to.have.length( 5 );
  295. } );
  296. } );
  297. it( 'should show panel for matched marker at the beginning of paragraph', () => {
  298. setData( model, '<paragraph>[] foo</paragraph>' );
  299. model.change( writer => {
  300. writer.insertText( '@', doc.selection.getFirstPosition() );
  301. } );
  302. return waitForDebounce()
  303. .then( () => {
  304. expect( panelView.isVisible ).to.be.true;
  305. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  306. expect( listView.items ).to.have.length( 5 );
  307. } );
  308. } );
  309. it( 'should not show panel for marker in the middle of other word', () => {
  310. setData( model, '<paragraph>foo[]</paragraph>' );
  311. model.change( writer => {
  312. writer.insertText( '@', doc.selection.getFirstPosition() );
  313. } );
  314. return waitForDebounce()
  315. .then( () => {
  316. expect( panelView.isVisible ).to.be.false;
  317. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  318. } );
  319. } );
  320. it( 'should not show panel when selection is inside a mention', () => {
  321. setData( model, '<paragraph>foo [@John] bar</paragraph>' );
  322. model.change( writer => {
  323. writer.setAttribute( 'mention', { name: 'John', _marker: '@', _id: 1234 }, doc.selection.getFirstRange() );
  324. } );
  325. model.change( writer => {
  326. writer.setSelection( doc.getRoot().getChild( 0 ), 7 );
  327. } );
  328. return waitForDebounce()
  329. .then( () => {
  330. expect( panelView.isVisible ).to.be.false;
  331. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  332. } );
  333. } );
  334. it( 'should not show panel when selection is at the end of a mention', () => {
  335. setData( model, '<paragraph>foo [@John] bar</paragraph>' );
  336. model.change( writer => {
  337. writer.setAttribute( 'mention', { name: 'John', _marker: '@', _id: 1234 }, doc.selection.getFirstRange() );
  338. } );
  339. model.change( writer => {
  340. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  341. } );
  342. return waitForDebounce()
  343. .then( () => {
  344. expect( panelView.isVisible ).to.be.false;
  345. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  346. } );
  347. } );
  348. it( 'should not show panel when selection is not collapsed', () => {
  349. setData( model, '<paragraph>foo []</paragraph>' );
  350. model.change( writer => {
  351. writer.insertText( '@', doc.selection.getFirstPosition() );
  352. } );
  353. return waitForDebounce()
  354. .then( () => {
  355. expect( panelView.isVisible ).to.be.true;
  356. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  357. model.change( () => {
  358. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  359. } );
  360. } )
  361. .then( waitForDebounce )
  362. .then( () => {
  363. expect( panelView.isVisible ).to.be.false;
  364. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  365. } );
  366. } );
  367. it( 'should not show panel when selection is after existing mention', () => {
  368. setData( model, '<paragraph>foo [@John] bar[]</paragraph>' );
  369. model.change( writer => {
  370. writer.setAttribute( 'mention', { name: 'John', _marker: '@', _id: 1234 }, doc.selection.getFirstRange() );
  371. } );
  372. return waitForDebounce()
  373. .then( () => {
  374. expect( panelView.isVisible ).to.be.false;
  375. model.change( writer => {
  376. writer.setSelection( doc.getRoot().getChild( 0 ), 8 );
  377. } );
  378. } )
  379. .then( waitForDebounce )
  380. .then( () => {
  381. expect( panelView.isVisible ).to.be.false;
  382. } );
  383. } );
  384. it( 'should show filtered results for matched text', () => {
  385. setData( model, '<paragraph>foo []</paragraph>' );
  386. model.change( writer => {
  387. writer.insertText( '@', doc.selection.getFirstPosition() );
  388. } );
  389. model.change( writer => {
  390. writer.insertText( 'T', doc.selection.getFirstPosition() );
  391. } );
  392. return waitForDebounce()
  393. .then( () => {
  394. expect( panelView.isVisible ).to.be.true;
  395. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  396. expect( listView.items ).to.have.length( 1 );
  397. } );
  398. } );
  399. it( 'should focus the first item in panel', () => {
  400. setData( model, '<paragraph>foo []</paragraph>' );
  401. model.change( writer => {
  402. writer.insertText( '@', doc.selection.getFirstPosition() );
  403. } );
  404. return waitForDebounce()
  405. .then( () => {
  406. const button = listView.items.get( 0 ).children.get( 0 );
  407. expect( button.isOn ).to.be.true;
  408. } );
  409. } );
  410. it( 'should hide panel if no matched items', () => {
  411. setData( model, '<paragraph>foo []</paragraph>' );
  412. model.change( writer => {
  413. writer.insertText( '@', doc.selection.getFirstPosition() );
  414. } );
  415. return waitForDebounce()
  416. .then( () => expect( panelView.isVisible ).to.be.true )
  417. .then( () => {
  418. model.change( writer => {
  419. writer.insertText( 'x', doc.selection.getFirstPosition() );
  420. } );
  421. } )
  422. .then( waitForDebounce )
  423. .then( () => {
  424. expect( panelView.isVisible ).to.be.false;
  425. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  426. expect( listView.items ).to.have.length( 0 );
  427. } );
  428. } );
  429. it( 'should hide panel when text was unmatched', () => {
  430. setData( model, '<paragraph>foo []</paragraph>' );
  431. model.change( writer => {
  432. writer.insertText( '@', doc.selection.getFirstPosition() );
  433. } );
  434. return waitForDebounce()
  435. .then( () => expect( panelView.isVisible ).to.be.true )
  436. .then( () => {
  437. model.change( writer => {
  438. const end = doc.selection.getFirstPosition();
  439. const start = end.getShiftedBy( -1 );
  440. writer.remove( writer.createRange( start, end ) );
  441. } );
  442. } )
  443. .then( waitForDebounce )
  444. .then( () => expect( panelView.isVisible ).to.be.false );
  445. } );
  446. } );
  447. describe( 'asynchronous list with custom trigger', () => {
  448. beforeEach( () => {
  449. const issuesNumbers = [ '100', '101', '102', '103' ];
  450. return createClassicTestEditor( {
  451. feeds: [
  452. {
  453. marker: '#',
  454. feed: feedText => {
  455. return new Promise( resolve => {
  456. setTimeout( () => {
  457. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  458. }, 20 );
  459. } );
  460. }
  461. }
  462. ]
  463. } );
  464. } );
  465. it( 'should show panel for matched marker', () => {
  466. setData( model, '<paragraph>foo []</paragraph>' );
  467. model.change( writer => {
  468. writer.insertText( '#', doc.selection.getFirstPosition() );
  469. } );
  470. return waitForDebounce()
  471. .then( () => {
  472. expect( panelView.isVisible ).to.be.true;
  473. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  474. expect( listView.items ).to.have.length( 4 );
  475. } );
  476. } );
  477. it( 'should show filtered results for matched text', () => {
  478. setData( model, '<paragraph>foo []</paragraph>' );
  479. model.change( writer => {
  480. writer.insertText( '#', doc.selection.getFirstPosition() );
  481. } );
  482. model.change( writer => {
  483. writer.insertText( '2', doc.selection.getFirstPosition() );
  484. } );
  485. return waitForDebounce()
  486. .then( () => {
  487. expect( panelView.isVisible ).to.be.true;
  488. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  489. expect( listView.items ).to.have.length( 1 );
  490. } );
  491. } );
  492. it( 'should hide panel if no matched items', () => {
  493. setData( model, '<paragraph>foo []</paragraph>' );
  494. model.change( writer => {
  495. writer.insertText( '#', doc.selection.getFirstPosition() );
  496. } );
  497. return waitForDebounce()
  498. .then( () => expect( panelView.isVisible ).to.be.true )
  499. .then( () => {
  500. model.change( writer => {
  501. writer.insertText( 'x', doc.selection.getFirstPosition() );
  502. } );
  503. } )
  504. .then( waitForDebounce )
  505. .then( () => {
  506. expect( panelView.isVisible ).to.be.false;
  507. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  508. expect( listView.items ).to.have.length( 0 );
  509. } );
  510. } );
  511. it( 'should hide panel when text was unmatched', () => {
  512. setData( model, '<paragraph>foo []</paragraph>' );
  513. model.change( writer => {
  514. writer.insertText( '#', doc.selection.getFirstPosition() );
  515. } );
  516. return waitForDebounce()
  517. .then( () => expect( panelView.isVisible ).to.be.true )
  518. .then( () => {
  519. model.change( writer => {
  520. const end = doc.selection.getFirstPosition();
  521. const start = end.getShiftedBy( -1 );
  522. writer.remove( writer.createRange( start, end ) );
  523. } );
  524. } )
  525. .then( waitForDebounce )
  526. .then( () => expect( panelView.isVisible ).to.be.false );
  527. } );
  528. } );
  529. } );
  530. describe( 'panel behavior', () => {
  531. it( 'should close the opened panel on esc', () => {
  532. return createClassicTestEditor( staticConfig )
  533. .then( () => {
  534. setData( model, '<paragraph>foo []</paragraph>' );
  535. model.change( writer => {
  536. writer.insertText( '@', doc.selection.getFirstPosition() );
  537. } );
  538. } )
  539. .then( waitForDebounce )
  540. .then( () => {
  541. expect( panelView.isVisible ).to.be.true;
  542. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  543. fireKeyDownEvent( {
  544. keyCode: keyCodes.esc,
  545. preventDefault: sinon.spy(),
  546. stopPropagation: sinon.spy()
  547. } );
  548. expect( panelView.isVisible ).to.be.false;
  549. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  550. } );
  551. } );
  552. it( 'should close the opened panel when click outside the panel', () => {
  553. return createClassicTestEditor( staticConfig )
  554. .then( () => {
  555. setData( model, '<paragraph>foo []</paragraph>' );
  556. model.change( writer => {
  557. writer.insertText( '@', doc.selection.getFirstPosition() );
  558. } );
  559. } )
  560. .then( waitForDebounce )
  561. .then( () => {
  562. expect( panelView.isVisible ).to.be.true;
  563. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  564. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  565. expect( panelView.isVisible ).to.be.false;
  566. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  567. } );
  568. } );
  569. it( 'should hide the panel on selection change', () => {
  570. return createClassicTestEditor( staticConfig )
  571. .then( () => {
  572. setData( model, '<paragraph>foo []</paragraph>' );
  573. model.change( writer => {
  574. writer.insertText( '@', doc.selection.getFirstPosition() );
  575. } );
  576. } )
  577. .then( waitForDebounce )
  578. .then( () => {
  579. expect( panelView.isVisible ).to.be.true;
  580. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  581. model.change( writer => {
  582. // Place position at the beginning of a paragraph.
  583. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  584. } );
  585. expect( panelView.isVisible ).to.be.false;
  586. expect( panelView.position ).to.be.undefined;
  587. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  588. } );
  589. } );
  590. it( 'should hide the panel on selection change triggered by mouse click', () => {
  591. return createClassicTestEditor( staticConfig )
  592. .then( () => {
  593. setData( model, '<paragraph>foo []</paragraph>' );
  594. model.change( writer => {
  595. writer.insertText( '@', doc.selection.getFirstPosition() );
  596. } );
  597. } )
  598. .then( waitForDebounce )
  599. .then( () => {
  600. expect( panelView.isVisible ).to.be.true;
  601. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  602. // This happens when user clicks outside the panel view and selection is changed.
  603. // Two panel closing mechanisms are run:
  604. // - clickOutsideHandler
  605. // - unmatched text in text watcher
  606. // which may fail when trying to remove mention marker twice.
  607. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  608. model.change( writer => {
  609. // Place position at the beginning of a paragraph.
  610. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  611. } );
  612. expect( panelView.isVisible ).to.be.false;
  613. expect( panelView.position ).to.be.undefined;
  614. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  615. } );
  616. } );
  617. describe( 'default list item', () => {
  618. const feedItems = staticConfig.feeds[ 0 ].feed.map( name => ( { name } ) );
  619. beforeEach( () => {
  620. return createClassicTestEditor( staticConfig );
  621. } );
  622. describe( 'on arrows', () => {
  623. it( 'should cycle down on arrow down', () => {
  624. setData( model, '<paragraph>foo []</paragraph>' );
  625. model.change( writer => {
  626. writer.insertText( '@', doc.selection.getFirstPosition() );
  627. } );
  628. return waitForDebounce()
  629. .then( () => {
  630. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  631. const keyEvtData = {
  632. keyCode: keyCodes.arrowdown,
  633. preventDefault: sinon.spy(),
  634. stopPropagation: sinon.spy()
  635. };
  636. fireKeyDownEvent( keyEvtData );
  637. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  638. fireKeyDownEvent( keyEvtData );
  639. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  640. fireKeyDownEvent( keyEvtData );
  641. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  642. fireKeyDownEvent( keyEvtData );
  643. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  644. fireKeyDownEvent( keyEvtData );
  645. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  646. } );
  647. } );
  648. it( 'should cycle up on arrow up', () => {
  649. setData( model, '<paragraph>foo []</paragraph>' );
  650. model.change( writer => {
  651. writer.insertText( '@', doc.selection.getFirstPosition() );
  652. } );
  653. return waitForDebounce()
  654. .then( () => {
  655. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  656. const keyEvtData = {
  657. keyCode: keyCodes.arrowup,
  658. preventDefault: sinon.spy(),
  659. stopPropagation: sinon.spy()
  660. };
  661. fireKeyDownEvent( keyEvtData );
  662. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  663. fireKeyDownEvent( keyEvtData );
  664. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  665. fireKeyDownEvent( keyEvtData );
  666. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  667. fireKeyDownEvent( keyEvtData );
  668. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  669. fireKeyDownEvent( keyEvtData );
  670. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  671. } );
  672. } );
  673. } );
  674. describe( 'on "execute" keys', () => {
  675. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  676. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  677. testExecuteKey( 'space', keyCodes.space, feedItems );
  678. } );
  679. } );
  680. describe( 'custom list item', () => {
  681. const issues = [
  682. { id: '1002', title: 'Some bug in editor.' },
  683. { id: '1003', title: 'Introduce this feature.' },
  684. { id: '1004', title: 'Missing docs.' },
  685. { id: '1005', title: 'Another bug.' },
  686. { id: '1006', title: 'More bugs' }
  687. ];
  688. beforeEach( () => {
  689. return createClassicTestEditor( {
  690. feeds: [
  691. {
  692. marker: '@',
  693. feed: feedText => {
  694. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  695. },
  696. itemRenderer: item => {
  697. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  698. span.innerHTML = `<span id="issue-${ item.id }">@${ item.title }</span>`;
  699. return span;
  700. }
  701. }
  702. ]
  703. } );
  704. } );
  705. it( 'should show panel for matched marker', () => {
  706. setData( model, '<paragraph>foo []</paragraph>' );
  707. model.change( writer => {
  708. writer.insertText( '@', doc.selection.getFirstPosition() );
  709. } );
  710. return waitForDebounce()
  711. .then( () => {
  712. expect( panelView.isVisible ).to.be.true;
  713. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  714. expect( listView.items ).to.have.length( 5 );
  715. } );
  716. } );
  717. describe( 'keys', () => {
  718. describe( 'on arrows', () => {
  719. it( 'should cycle down on arrow down', () => {
  720. setData( model, '<paragraph>foo []</paragraph>' );
  721. model.change( writer => {
  722. writer.insertText( '@', doc.selection.getFirstPosition() );
  723. } );
  724. return waitForDebounce()
  725. .then( () => {
  726. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  727. const keyEvtData = {
  728. keyCode: keyCodes.arrowdown,
  729. preventDefault: sinon.spy(),
  730. stopPropagation: sinon.spy()
  731. };
  732. fireKeyDownEvent( keyEvtData );
  733. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  734. fireKeyDownEvent( keyEvtData );
  735. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  736. fireKeyDownEvent( keyEvtData );
  737. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  738. fireKeyDownEvent( keyEvtData );
  739. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  740. fireKeyDownEvent( keyEvtData );
  741. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  742. } );
  743. } );
  744. it( 'should cycle up on arrow up', () => {
  745. setData( model, '<paragraph>foo []</paragraph>' );
  746. model.change( writer => {
  747. writer.insertText( '@', doc.selection.getFirstPosition() );
  748. } );
  749. return waitForDebounce()
  750. .then( () => {
  751. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  752. const keyEvtData = {
  753. keyCode: keyCodes.arrowup,
  754. preventDefault: sinon.spy(),
  755. stopPropagation: sinon.spy()
  756. };
  757. fireKeyDownEvent( keyEvtData );
  758. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  759. fireKeyDownEvent( keyEvtData );
  760. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  761. fireKeyDownEvent( keyEvtData );
  762. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  763. fireKeyDownEvent( keyEvtData );
  764. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  765. fireKeyDownEvent( keyEvtData );
  766. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  767. } );
  768. } );
  769. } );
  770. describe( 'on "execute" keys', () => {
  771. testExecuteKey( 'enter', keyCodes.enter, issues );
  772. testExecuteKey( 'tab', keyCodes.tab, issues );
  773. testExecuteKey( 'space', keyCodes.space, issues );
  774. } );
  775. describe( 'mouse', () => {
  776. it( 'should execute selected button on mouse click', () => {
  777. setData( model, '<paragraph>foo []</paragraph>' );
  778. model.change( writer => {
  779. writer.insertText( '@', doc.selection.getFirstPosition() );
  780. } );
  781. const command = editor.commands.get( 'mention' );
  782. const spy = testUtils.sinon.spy( command, 'execute' );
  783. return waitForDebounce()
  784. .then( () => {
  785. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  786. const element = panelView.element.querySelector( '#issue-1004' );
  787. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  788. sinon.assert.calledOnce( spy );
  789. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  790. const item = issues[ 2 ];
  791. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  792. expect( commandOptions ).to.have.property( 'marker', '@' );
  793. expect( commandOptions ).to.have.property( 'range' );
  794. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  795. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  796. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  797. } );
  798. } );
  799. } );
  800. } );
  801. } );
  802. function testExecuteKey( name, keyCode, feedItems ) {
  803. it( 'should execute selected button on ' + name, () => {
  804. setData( model, '<paragraph>foo []</paragraph>' );
  805. model.change( writer => {
  806. writer.insertText( '@', doc.selection.getFirstPosition() );
  807. } );
  808. const command = editor.commands.get( 'mention' );
  809. const spy = testUtils.sinon.spy( command, 'execute' );
  810. return waitForDebounce()
  811. .then( () => {
  812. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  813. fireKeyDownEvent( {
  814. keyCode: keyCodes.arrowup,
  815. preventDefault: sinon.spy(),
  816. stopPropagation: sinon.spy()
  817. } );
  818. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  819. fireKeyDownEvent( {
  820. keyCode,
  821. preventDefault: sinon.spy(),
  822. stopPropagation: sinon.spy()
  823. } );
  824. sinon.assert.calledOnce( spy );
  825. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  826. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  827. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  828. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  829. } );
  830. } );
  831. it( 'should do nothing if panel is not visible on ' + name, () => {
  832. setData( model, '<paragraph>foo []</paragraph>' );
  833. model.change( writer => {
  834. writer.insertText( '@', doc.selection.getFirstPosition() );
  835. } );
  836. const command = editor.commands.get( 'mention' );
  837. const spy = testUtils.sinon.spy( command, 'execute' );
  838. return waitForDebounce()
  839. .then( () => {
  840. expect( panelView.isVisible ).to.be.true;
  841. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  842. fireKeyDownEvent( {
  843. keyCode: keyCodes.esc,
  844. preventDefault: sinon.spy(),
  845. stopPropagation: sinon.spy()
  846. } );
  847. expect( panelView.isVisible ).to.be.false;
  848. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  849. fireKeyDownEvent( {
  850. keyCode,
  851. preventDefault: sinon.spy(),
  852. stopPropagation: sinon.spy()
  853. } );
  854. sinon.assert.notCalled( spy );
  855. expect( panelView.isVisible ).to.be.false;
  856. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  857. } );
  858. } );
  859. }
  860. } );
  861. describe( 'execute', () => {
  862. beforeEach( () => createClassicTestEditor( staticConfig ) );
  863. it( 'should call the mention command with proper options', () => {
  864. setData( model, '<paragraph>foo []</paragraph>' );
  865. model.change( writer => {
  866. writer.insertText( '@', doc.selection.getFirstPosition() );
  867. } );
  868. const command = editor.commands.get( 'mention' );
  869. const spy = testUtils.sinon.spy( command, 'execute' );
  870. return waitForDebounce()
  871. .then( () => {
  872. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  873. sinon.assert.calledOnce( spy );
  874. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  875. assertCommandOptions( commandOptions, '@', { name: 'Barney' } );
  876. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  877. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  878. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  879. } );
  880. } );
  881. it( 'should hide panel on execute', () => {
  882. setData( model, '<paragraph>foo []</paragraph>' );
  883. model.change( writer => {
  884. writer.insertText( '@', doc.selection.getFirstPosition() );
  885. } );
  886. return waitForDebounce()
  887. .then( () => {
  888. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  889. expect( panelView.isVisible ).to.be.false;
  890. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  891. } );
  892. } );
  893. it( 'should focus view after command execution', () => {
  894. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  895. setData( model, '<paragraph>foo []</paragraph>' );
  896. model.change( writer => {
  897. writer.insertText( '@', doc.selection.getFirstPosition() );
  898. } );
  899. return waitForDebounce()
  900. .then( () => {
  901. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  902. sinon.assert.calledOnce( focusSpy );
  903. } );
  904. } );
  905. } );
  906. function createClassicTestEditor( mentionConfig ) {
  907. return ClassicTestEditor
  908. .create( editorElement, {
  909. plugins: [ Paragraph, MentionEditing, MentionUI ],
  910. mention: mentionConfig
  911. } )
  912. .then( newEditor => {
  913. editor = newEditor;
  914. model = editor.model;
  915. doc = model.document;
  916. editingView = editor.editing.view;
  917. mentionUI = editor.plugins.get( MentionUI );
  918. panelView = mentionUI.panelView;
  919. mentionsView = mentionUI._mentionsView;
  920. listView = mentionsView.listView;
  921. editingView.attachDomRoot( editorElement );
  922. // Focus the engine.
  923. editingView.document.isFocused = true;
  924. editingView.getDomRoot().focus();
  925. // Remove all selection ranges from DOM before testing.
  926. window.getSelection().removeAllRanges();
  927. } );
  928. }
  929. function waitForDebounce() {
  930. return new Promise( resolve => {
  931. setTimeout( () => {
  932. resolve();
  933. }, 50 );
  934. } );
  935. }
  936. function fireKeyDownEvent( options ) {
  937. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  938. const eventData = new DomEventData( editingView.document, {
  939. target: document.body
  940. }, options );
  941. editingView.document.fire( eventInfo, eventData );
  942. }
  943. function stubSelectionRects( rects ) {
  944. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  945. // Mock selection rect.
  946. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  947. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  948. sinon.stub( domRange, 'getClientRects' )
  949. .returns( rects );
  950. return domRange;
  951. } );
  952. }
  953. function expectChildViewsIsOnState( expectedState ) {
  954. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  955. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  956. }
  957. function assertCommandOptions( commandOptions, marker, item ) {
  958. expect( commandOptions ).to.have.property( 'marker', marker );
  959. expect( commandOptions ).to.have.property( 'range' );
  960. expect( commandOptions ).to.have.property( 'mention' );
  961. const mentionForCommand = commandOptions.mention;
  962. for ( const key of Object.keys( item ) ) {
  963. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  964. }
  965. }
  966. } );