mentionui.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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', { id: '@John', _uid: 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', { id: '@John', _uid: 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', { id: '@John', _uid: 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( text => ( { text, id: `@${ text }` } ) );
  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( 'default list item with custom feed', () => {
  681. const issues = [
  682. { id: '@Ted' },
  683. { id: '@Barney' },
  684. { id: '@Robin' },
  685. { id: '@Lily' },
  686. { id: '@Marhsal' }
  687. ];
  688. beforeEach( () => {
  689. return createClassicTestEditor( {
  690. feeds: [
  691. {
  692. marker: '@',
  693. feed: feedText => issues.filter( issue => issue.id.includes( feedText ) )
  694. }
  695. ]
  696. } );
  697. } );
  698. it( 'should show panel for matched marker', () => {
  699. setData( model, '<paragraph>foo []</paragraph>' );
  700. model.change( writer => {
  701. writer.insertText( '@', doc.selection.getFirstPosition() );
  702. } );
  703. return waitForDebounce()
  704. .then( () => {
  705. expect( panelView.isVisible ).to.be.true;
  706. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  707. expect( listView.items ).to.have.length( 5 );
  708. } );
  709. } );
  710. } );
  711. describe( 'custom list item', () => {
  712. const issues = [
  713. { id: '1002', title: 'Some bug in editor.' },
  714. { id: '1003', title: 'Introduce this feature.' },
  715. { id: '1004', title: 'Missing docs.' },
  716. { id: '1005', title: 'Another bug.' },
  717. { id: '1006', title: 'More bugs' }
  718. ];
  719. beforeEach( () => {
  720. return createClassicTestEditor( {
  721. feeds: [
  722. {
  723. marker: '@',
  724. feed: feedText => {
  725. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  726. },
  727. itemRenderer: item => {
  728. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  729. span.innerHTML = `<span id="issue-${ item.id }">@${ item.title }</span>`;
  730. return span;
  731. }
  732. }
  733. ]
  734. } );
  735. } );
  736. it( 'should show panel for matched marker', () => {
  737. setData( model, '<paragraph>foo []</paragraph>' );
  738. model.change( writer => {
  739. writer.insertText( '@', doc.selection.getFirstPosition() );
  740. } );
  741. return waitForDebounce()
  742. .then( () => {
  743. expect( panelView.isVisible ).to.be.true;
  744. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  745. expect( listView.items ).to.have.length( 5 );
  746. } );
  747. } );
  748. describe( 'keys', () => {
  749. describe( 'on arrows', () => {
  750. it( 'should cycle down on arrow down', () => {
  751. setData( model, '<paragraph>foo []</paragraph>' );
  752. model.change( writer => {
  753. writer.insertText( '@', doc.selection.getFirstPosition() );
  754. } );
  755. return waitForDebounce()
  756. .then( () => {
  757. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  758. const keyEvtData = {
  759. keyCode: keyCodes.arrowdown,
  760. preventDefault: sinon.spy(),
  761. stopPropagation: sinon.spy()
  762. };
  763. fireKeyDownEvent( keyEvtData );
  764. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  765. fireKeyDownEvent( keyEvtData );
  766. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  767. fireKeyDownEvent( keyEvtData );
  768. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  769. fireKeyDownEvent( keyEvtData );
  770. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  771. fireKeyDownEvent( keyEvtData );
  772. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  773. } );
  774. } );
  775. it( 'should cycle up on arrow up', () => {
  776. setData( model, '<paragraph>foo []</paragraph>' );
  777. model.change( writer => {
  778. writer.insertText( '@', doc.selection.getFirstPosition() );
  779. } );
  780. return waitForDebounce()
  781. .then( () => {
  782. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  783. const keyEvtData = {
  784. keyCode: keyCodes.arrowup,
  785. preventDefault: sinon.spy(),
  786. stopPropagation: sinon.spy()
  787. };
  788. fireKeyDownEvent( keyEvtData );
  789. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  790. fireKeyDownEvent( keyEvtData );
  791. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  792. fireKeyDownEvent( keyEvtData );
  793. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  794. fireKeyDownEvent( keyEvtData );
  795. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  796. fireKeyDownEvent( keyEvtData );
  797. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  798. } );
  799. } );
  800. } );
  801. describe( 'on "execute" keys', () => {
  802. testExecuteKey( 'enter', keyCodes.enter, issues );
  803. testExecuteKey( 'tab', keyCodes.tab, issues );
  804. testExecuteKey( 'space', keyCodes.space, issues );
  805. } );
  806. describe( 'mouse', () => {
  807. it( 'should execute selected button on mouse click', () => {
  808. setData( model, '<paragraph>foo []</paragraph>' );
  809. model.change( writer => {
  810. writer.insertText( '@', doc.selection.getFirstPosition() );
  811. } );
  812. const command = editor.commands.get( 'mention' );
  813. const spy = testUtils.sinon.spy( command, 'execute' );
  814. return waitForDebounce()
  815. .then( () => {
  816. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  817. const element = panelView.element.querySelector( '#issue-1004' );
  818. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  819. sinon.assert.calledOnce( spy );
  820. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  821. const item = issues[ 2 ];
  822. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  823. expect( commandOptions ).to.have.property( 'marker', '@' );
  824. expect( commandOptions ).to.have.property( 'range' );
  825. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  826. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  827. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  828. } );
  829. } );
  830. } );
  831. } );
  832. } );
  833. function testExecuteKey( name, keyCode, feedItems ) {
  834. it( 'should execute selected button on ' + name, () => {
  835. setData( model, '<paragraph>foo []</paragraph>' );
  836. model.change( writer => {
  837. writer.insertText( '@', doc.selection.getFirstPosition() );
  838. } );
  839. const command = editor.commands.get( 'mention' );
  840. const spy = testUtils.sinon.spy( command, 'execute' );
  841. return waitForDebounce()
  842. .then( () => {
  843. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  844. fireKeyDownEvent( {
  845. keyCode: keyCodes.arrowup,
  846. preventDefault: sinon.spy(),
  847. stopPropagation: sinon.spy()
  848. } );
  849. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  850. fireKeyDownEvent( {
  851. keyCode,
  852. preventDefault: sinon.spy(),
  853. stopPropagation: sinon.spy()
  854. } );
  855. sinon.assert.calledOnce( spy );
  856. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  857. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  858. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  859. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  860. } );
  861. } );
  862. it( 'should do nothing if panel is not visible on ' + name, () => {
  863. setData( model, '<paragraph>foo []</paragraph>' );
  864. model.change( writer => {
  865. writer.insertText( '@', doc.selection.getFirstPosition() );
  866. } );
  867. const command = editor.commands.get( 'mention' );
  868. const spy = testUtils.sinon.spy( command, 'execute' );
  869. return waitForDebounce()
  870. .then( () => {
  871. expect( panelView.isVisible ).to.be.true;
  872. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  873. fireKeyDownEvent( {
  874. keyCode: keyCodes.esc,
  875. preventDefault: sinon.spy(),
  876. stopPropagation: sinon.spy()
  877. } );
  878. expect( panelView.isVisible ).to.be.false;
  879. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  880. fireKeyDownEvent( {
  881. keyCode,
  882. preventDefault: sinon.spy(),
  883. stopPropagation: sinon.spy()
  884. } );
  885. sinon.assert.notCalled( spy );
  886. expect( panelView.isVisible ).to.be.false;
  887. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  888. } );
  889. } );
  890. }
  891. } );
  892. describe( 'execute', () => {
  893. beforeEach( () => createClassicTestEditor( staticConfig ) );
  894. it( 'should call the mention command with proper options', () => {
  895. setData( model, '<paragraph>foo []</paragraph>' );
  896. model.change( writer => {
  897. writer.insertText( '@', doc.selection.getFirstPosition() );
  898. } );
  899. const command = editor.commands.get( 'mention' );
  900. const spy = testUtils.sinon.spy( command, 'execute' );
  901. return waitForDebounce()
  902. .then( () => {
  903. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  904. sinon.assert.calledOnce( spy );
  905. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  906. assertCommandOptions( commandOptions, '@', { id: '@Barney', text: 'Barney' } );
  907. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  908. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  909. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  910. } );
  911. } );
  912. it( 'should hide panel on execute', () => {
  913. setData( model, '<paragraph>foo []</paragraph>' );
  914. model.change( writer => {
  915. writer.insertText( '@', doc.selection.getFirstPosition() );
  916. } );
  917. return waitForDebounce()
  918. .then( () => {
  919. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  920. expect( panelView.isVisible ).to.be.false;
  921. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  922. } );
  923. } );
  924. it( 'should focus view after command execution', () => {
  925. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  926. setData( model, '<paragraph>foo []</paragraph>' );
  927. model.change( writer => {
  928. writer.insertText( '@', doc.selection.getFirstPosition() );
  929. } );
  930. return waitForDebounce()
  931. .then( () => {
  932. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  933. sinon.assert.calledOnce( focusSpy );
  934. } );
  935. } );
  936. } );
  937. function createClassicTestEditor( mentionConfig ) {
  938. return ClassicTestEditor
  939. .create( editorElement, {
  940. plugins: [ Paragraph, MentionEditing, MentionUI ],
  941. mention: mentionConfig
  942. } )
  943. .then( newEditor => {
  944. editor = newEditor;
  945. model = editor.model;
  946. doc = model.document;
  947. editingView = editor.editing.view;
  948. mentionUI = editor.plugins.get( MentionUI );
  949. panelView = mentionUI.panelView;
  950. mentionsView = mentionUI._mentionsView;
  951. listView = mentionsView.listView;
  952. editingView.attachDomRoot( editorElement );
  953. // Focus the engine.
  954. editingView.document.isFocused = true;
  955. editingView.getDomRoot().focus();
  956. // Remove all selection ranges from DOM before testing.
  957. window.getSelection().removeAllRanges();
  958. } );
  959. }
  960. function waitForDebounce() {
  961. return new Promise( resolve => {
  962. setTimeout( () => {
  963. resolve();
  964. }, 50 );
  965. } );
  966. }
  967. function fireKeyDownEvent( options ) {
  968. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  969. const eventData = new DomEventData( editingView.document, {
  970. target: document.body
  971. }, options );
  972. editingView.document.fire( eventInfo, eventData );
  973. }
  974. function stubSelectionRects( rects ) {
  975. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  976. // Mock selection rect.
  977. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  978. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  979. sinon.stub( domRange, 'getClientRects' )
  980. .returns( rects );
  981. return domRange;
  982. } );
  983. }
  984. function expectChildViewsIsOnState( expectedState ) {
  985. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  986. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  987. }
  988. function assertCommandOptions( commandOptions, marker, item ) {
  989. expect( commandOptions ).to.have.property( 'marker', marker );
  990. expect( commandOptions ).to.have.property( 'range' );
  991. expect( commandOptions ).to.have.property( 'mention' );
  992. const mentionForCommand = commandOptions.mention;
  993. for ( const key of Object.keys( item ) ) {
  994. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  995. }
  996. }
  997. } );