mentionui.js 49 KB

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