mentionui.js 54 KB

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