mentionui.js 44 KB

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