mentionui.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global window, document, setTimeout, Event, console */
  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 ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  16. import env from '@ckeditor/ckeditor5-utils/src/env';
  17. import MentionUI, { createRegExp } from '../src/mentionui';
  18. import MentionEditing from '../src/mentionediting';
  19. import MentionsView from '../src/ui/mentionsview';
  20. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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. assertCKEditorError( error, /mentionconfig-incorrect-marker/, null, { marker: undefined } );
  58. } );
  59. } );
  60. it( 'should throw if marker is empty string', () => {
  61. return createClassicTestEditor( { feeds: [ { marker: '', feed: [ 'a' ] } ] } ).catch( error => {
  62. assertCKEditorError( error, /mentionconfig-incorrect-marker/, null, { marker: '' } );
  63. } );
  64. } );
  65. it( 'should throw if marker is longer then 1 character', () => {
  66. return createClassicTestEditor( { feeds: [ { marker: '$$', feed: [ 'a' ] } ] } ).catch( error => {
  67. assertCKEditorError( error, /mentionconfig-incorrect-marker/, null, { marker: '$$' } );
  68. } );
  69. } );
  70. } );
  71. describe( 'pluginName', () => {
  72. it( 'should return plugin by its name', () => {
  73. return createClassicTestEditor().then( () => {
  74. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  75. } );
  76. } );
  77. } );
  78. describe( 'contextual balloon', () => {
  79. let balloonAddSpy;
  80. beforeEach( () => {
  81. return createClassicTestEditor( staticConfig )
  82. .then( () => {
  83. setData( model, '<paragraph>foo []</paragraph>' );
  84. const contextualBalloon = editor.plugins.get( ContextualBalloon );
  85. balloonAddSpy = sinon.spy( contextualBalloon, 'add' );
  86. model.change( writer => {
  87. writer.insertText( '@', doc.selection.getFirstPosition() );
  88. } );
  89. } )
  90. .then( waitForDebounce );
  91. } );
  92. it( 'should disable arrow', () => {
  93. sinon.assert.calledOnce( balloonAddSpy );
  94. sinon.assert.calledWithExactly( balloonAddSpy, sinon.match( data => data.singleViewMode ) );
  95. expect( panelView.isVisible ).to.be.true;
  96. expect( panelView.withArrow ).to.be.false;
  97. } );
  98. it( 'should add MentionView to a panel', () => {
  99. expect( editor.plugins.get( ContextualBalloon ).visibleView ).to.be.instanceof( MentionsView );
  100. } );
  101. } );
  102. describe( 'position', () => {
  103. let pinSpy;
  104. const caretRect = {
  105. bottom: 118,
  106. height: 18,
  107. left: 500,
  108. right: 501,
  109. top: 100,
  110. width: 1
  111. };
  112. const balloonRect = {
  113. bottom: 150,
  114. height: 150,
  115. left: 0,
  116. right: 200,
  117. top: 0,
  118. width: 200
  119. };
  120. beforeEach( () => {
  121. return createClassicTestEditor( staticConfig ).then( () => {
  122. pinSpy = sinon.spy( panelView, 'pin' );
  123. } );
  124. } );
  125. it( 'should properly calculate position data', () => {
  126. const editableElement = editingView.document.selection.editableElement;
  127. setData( model, '<paragraph>foo []</paragraph>' );
  128. stubSelectionRects( [ caretRect ] );
  129. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  130. model.change( writer => {
  131. writer.insertText( '@', doc.selection.getFirstPosition() );
  132. } );
  133. return waitForDebounce()
  134. .then( () => {
  135. const pinArgument = pinSpy.firstCall.args[ 0 ];
  136. const { target, positions, limiter, fitInViewport } = pinArgument;
  137. expect( positions ).to.have.length( 4 );
  138. // Mention UI should set limiter to the editable area.
  139. expect( limiter() ).to.equal( editingView.domConverter.mapViewToDom( editableElement ) );
  140. expect( fitInViewport ).to.be.undefined;
  141. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  142. const mentionMarker = editor.model.markers.get( 'mention' );
  143. const focus = doc.selection.focus;
  144. const expectedRange = editor.model.createRange( focus.getShiftedBy( -1 ), focus );
  145. // It should create a model marker for matcher marker character ('@').
  146. expect( expectedRange.isEqual( mentionMarker.getRange() ) ).to.be.true;
  147. const toViewRangeSpy = sinon.spy( editor.editing.mapper, 'toViewRange' );
  148. expect( target() ).to.deep.equal( caretRect );
  149. sinon.assert.calledOnce( toViewRangeSpy );
  150. const range = toViewRangeSpy.firstCall.args[ 0 ];
  151. expect( mentionMarker.getRange().isEqual( range ), 'Should position to mention marker.' );
  152. const caretSouthEast = positions[ 0 ];
  153. const caretSouthWest = positions[ 1 ];
  154. const caretNorthEast = positions[ 2 ];
  155. const caretNorthWest = positions[ 3 ];
  156. expect( caretSouthEast( caretRect, balloonRect ) ).to.deep.equal( {
  157. left: 501,
  158. name: 'caret_se',
  159. top: 121
  160. } );
  161. expect( caretSouthWest( caretRect, balloonRect ) ).to.deep.equal( {
  162. left: 301,
  163. name: 'caret_sw',
  164. top: 121
  165. } );
  166. expect( caretNorthEast( caretRect, balloonRect ) ).to.deep.equal( {
  167. left: 501,
  168. name: 'caret_ne',
  169. top: -53
  170. } );
  171. expect( caretNorthWest( caretRect, balloonRect ) ).to.deep.equal( {
  172. left: 301,
  173. name: 'caret_nw',
  174. top: -53
  175. } );
  176. } );
  177. } );
  178. it( 'should re-calculate position on typing and stay on selected position', () => {
  179. setData( model, '<paragraph>foo []</paragraph>' );
  180. stubSelectionRects( [ caretRect ] );
  181. model.change( writer => {
  182. writer.insertText( '@', doc.selection.getFirstPosition() );
  183. } );
  184. let positionAfterFirstShow;
  185. return waitForDebounce()
  186. .then( () => {
  187. sinon.assert.calledOnce( pinSpy );
  188. const pinArgument = pinSpy.firstCall.args[ 0 ];
  189. const { positions } = pinArgument;
  190. expect( positions ).to.have.length( 4 );
  191. positionAfterFirstShow = mentionsView.position;
  192. model.change( writer => {
  193. writer.insertText( 't', doc.selection.getFirstPosition() );
  194. } );
  195. } )
  196. .then( waitForDebounce )
  197. .then( () => {
  198. sinon.assert.calledTwice( pinSpy );
  199. const pinArgument = pinSpy.secondCall.args[ 0 ];
  200. const { positions } = pinArgument;
  201. expect( positions, 'should reuse first matched position' ).to.have.length( 1 );
  202. expect( positions[ 0 ].name ).to.equal( positionAfterFirstShow );
  203. } );
  204. } );
  205. it( 'does not fail if selection has no #editableElement', () => {
  206. setData( model, '<paragraph>foo []</paragraph>' );
  207. stubSelectionRects( [ caretRect ] );
  208. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  209. model.change( writer => {
  210. writer.insertText( '@', doc.selection.getFirstPosition() );
  211. } );
  212. return waitForDebounce()
  213. .then( () => {
  214. const pinArgument = pinSpy.firstCall.args[ 0 ];
  215. const { limiter } = pinArgument;
  216. sinon.stub( editingView.document.selection, 'editableElement' ).value( null );
  217. // Should not break;
  218. expect( limiter() ).to.be.null;
  219. } );
  220. } );
  221. } );
  222. describe( 'typing integration', () => {
  223. it( 'should show panel for matched marker after typing minimum characters', () => {
  224. return createClassicTestEditor( { feeds: [ Object.assign( { minimumCharacters: 2 }, staticConfig.feeds[ 0 ] ) ] } )
  225. .then( () => {
  226. setData( model, '<paragraph>foo []</paragraph>' );
  227. model.change( writer => {
  228. writer.insertText( '@', doc.selection.getFirstPosition() );
  229. } );
  230. } )
  231. .then( () => {
  232. model.change( writer => {
  233. writer.insertText( 'B', doc.selection.getFirstPosition() );
  234. } );
  235. } )
  236. .then( waitForDebounce )
  237. .then( () => {
  238. expect( panelView.isVisible ).to.be.false;
  239. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  240. } )
  241. .then( waitForDebounce )
  242. .then( () => {
  243. model.change( writer => {
  244. writer.insertText( 'a', doc.selection.getFirstPosition() );
  245. } );
  246. } )
  247. .then( waitForDebounce )
  248. .then( () => {
  249. expect( panelView.isVisible ).to.be.true;
  250. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  251. expect( mentionsView.items ).to.have.length( 1 );
  252. model.change( writer => {
  253. writer.insertText( 'r', doc.selection.getFirstPosition() );
  254. } );
  255. } )
  256. .then( waitForDebounce )
  257. .then( () => {
  258. expect( panelView.isVisible ).to.be.true;
  259. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  260. expect( mentionsView.items ).to.have.length( 1 );
  261. } );
  262. } );
  263. it( 'should update the marker if the selection was moved from one valid position to another', () => {
  264. const spy = sinon.spy();
  265. return createClassicTestEditor( staticConfig )
  266. .then( () => {
  267. setData( model, '<paragraph>foo @ bar []</paragraph>' );
  268. model.change( writer => {
  269. writer.insertText( '@', doc.selection.getFirstPosition() );
  270. } );
  271. } )
  272. .then( waitForDebounce )
  273. .then( () => {
  274. expect( panelView.isVisible ).to.be.true;
  275. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  276. } )
  277. .then( () => {
  278. editor.model.markers.on( 'update', spy );
  279. model.change( writer => {
  280. writer.setSelection( doc.getRoot().getChild( 0 ), 5 );
  281. } );
  282. sinon.assert.calledOnce( spy );
  283. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  284. } );
  285. } );
  286. it( 'should not show panel when command is disabled', () => {
  287. return createClassicTestEditor( staticConfig )
  288. .then( () => {
  289. setData( model, '<paragraph>foo []</paragraph>' );
  290. const mentionCommand = editor.commands.get( 'mention' );
  291. mentionCommand.forceDisabled( 'mentionCommandDisableTest' );
  292. model.change( writer => {
  293. writer.insertText( '@', doc.selection.getFirstPosition() );
  294. } );
  295. } )
  296. .then( waitForDebounce )
  297. .then( () => {
  298. expect( panelView.isVisible ).to.be.false;
  299. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  300. } );
  301. } );
  302. describe( 'static list with large set of results', () => {
  303. const bigList = {
  304. marker: '@',
  305. feed: [
  306. '@a01', '@a02', '@a03', '@a04', '@a05', '@a06', '@a07', '@a08', '@a09', '@a10', '@a11', '@a12'
  307. ]
  308. };
  309. beforeEach( () => {
  310. return createClassicTestEditor( { feeds: [ bigList ] } );
  311. } );
  312. it( 'should show panel with no more then 10 items for default static feed', () => {
  313. setData( model, '<paragraph>foo []</paragraph>' );
  314. model.change( writer => {
  315. writer.insertText( '@', doc.selection.getFirstPosition() );
  316. } );
  317. return waitForDebounce()
  318. .then( () => {
  319. expect( panelView.isVisible ).to.be.true;
  320. expect( mentionsView.items ).to.have.length( 10 );
  321. } );
  322. } );
  323. it( 'should scroll mention panel to the selected item', () => {
  324. setData( model, '<paragraph>foo []</paragraph>' );
  325. model.change( writer => {
  326. writer.insertText( '@', doc.selection.getFirstPosition() );
  327. } );
  328. const arrowDownEvtData = {
  329. keyCode: keyCodes.arrowdown,
  330. preventDefault: sinon.spy(),
  331. stopPropagation: sinon.spy()
  332. };
  333. const arrowUpEvtData = {
  334. keyCode: keyCodes.arrowup,
  335. preventDefault: sinon.spy(),
  336. stopPropagation: sinon.spy()
  337. };
  338. return waitForDebounce()
  339. .then( () => {
  340. // The scroll test highly depends on browser styles.
  341. // Some CI test environments might not load theme which will result that tests will not render on CI as locally.
  342. // To make this test repeatable across different environments it enforces mentions view size to 100px...
  343. const reset = 'padding:0px;margin:0px;border:0 none;line-height: 1em;';
  344. const mentionElementSpy = testUtils.sinon.spy( mentionsView.element, 'scrollTop', [ 'set' ] );
  345. mentionsView.element.style = `min-height:100px;height:100px;max-height:100px;${ reset };`;
  346. // ...and each list view item size to 25px...
  347. Array.from( mentionsView.items ).forEach( item => {
  348. const listItemElement = item.children.get( 0 ).element;
  349. listItemElement.style = `min-height:unset;height:25px;max-height:25px;${ reset };min-width:12em;`;
  350. } );
  351. // ...so after those changes it is safe to assume that:
  352. // - base offset is 0
  353. // - only 4 items are visible at once
  354. // - if scrolled to the last element scrollTop will be set to 150px. The 150px is the offset of the 7th item in the
  355. // list as last four (7, 8, 9 & 10) will be visible.
  356. expect( panelView.isVisible ).to.be.true;
  357. expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
  358. sinon.assert.callCount( mentionElementSpy.set, 0 );
  359. fireKeyDownEvent( arrowDownEvtData );
  360. expectChildViewsIsOnState( [ false, true, false, false, false, false, false, false, false, false ] );
  361. expect( mentionsView.element.scrollTop ).to.equal( 0 );
  362. sinon.assert.callCount( mentionElementSpy.set, 0 );
  363. fireKeyDownEvent( arrowUpEvtData );
  364. expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
  365. expect( mentionsView.element.scrollTop ).to.equal( 0 );
  366. sinon.assert.callCount( mentionElementSpy.set, 0 );
  367. fireKeyDownEvent( arrowUpEvtData );
  368. expectChildViewsIsOnState( [ false, false, false, false, false, false, false, false, false, true ] );
  369. // We want 150, but sometimes we get e.g. 151.
  370. expect( mentionsView.element.scrollTop ).to.be.within( 140, 160, 'last item highlighted' );
  371. sinon.assert.callCount( mentionElementSpy.set, 1 );
  372. fireKeyDownEvent( arrowDownEvtData );
  373. expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
  374. // We want 0, but sometimes we get e.g. 1. (Firefox)
  375. expect( mentionsView.element.scrollTop ).to.be.within( 0, 10 );
  376. sinon.assert.callCount( mentionElementSpy.set, 2 );
  377. } );
  378. } );
  379. } );
  380. describe( 'ES2018 RegExp Unicode property escapes fallback', () => {
  381. let regExpStub;
  382. // Cache the original value to restore it after the tests.
  383. const originalGroupSupport = env.features.isRegExpUnicodePropertySupported;
  384. before( () => {
  385. env.features.isRegExpUnicodePropertySupported = false;
  386. } );
  387. beforeEach( () => {
  388. return createClassicTestEditor( staticConfig )
  389. .then( editor => {
  390. regExpStub = sinon.stub( window, 'RegExp' );
  391. return editor;
  392. } );
  393. } );
  394. after( () => {
  395. env.features.isRegExpUnicodePropertySupported = originalGroupSupport;
  396. } );
  397. it( 'returns a simplified RegExp for browsers not supporting Unicode punctuation groups', () => {
  398. env.features.isRegExpUnicodePropertySupported = false;
  399. createRegExp( '@', 2 );
  400. sinon.assert.calledOnce( regExpStub );
  401. sinon.assert.calledWithExactly( regExpStub, '(?:^|[ \\(\\[{"\'])([@])([\\S]{2,})$', 'u' );
  402. } );
  403. it( 'returns a ES2018 RegExp for browsers supporting Unicode punctuation groups', () => {
  404. env.features.isRegExpUnicodePropertySupported = true;
  405. createRegExp( '@', 2 );
  406. sinon.assert.calledOnce( regExpStub );
  407. sinon.assert.calledWithExactly( regExpStub, '(?:^|[ \\p{Ps}\\p{Pi}"\'])([@])([\\S]{2,})$', 'u' );
  408. } );
  409. } );
  410. describe( 'static list with default trigger', () => {
  411. beforeEach( () => {
  412. return createClassicTestEditor( staticConfig );
  413. } );
  414. it( 'should show panel for matched marker', () => {
  415. setData( model, '<paragraph>foo []</paragraph>' );
  416. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  417. model.change( writer => {
  418. writer.insertText( '@', doc.selection.getFirstPosition() );
  419. } );
  420. return waitForDebounce()
  421. .then( () => {
  422. expect( panelView.isVisible ).to.be.true;
  423. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  424. expect( mentionsView.items ).to.have.length( 5 );
  425. } );
  426. } );
  427. it( 'should show panel for matched marker at the beginning of paragraph', () => {
  428. setData( model, '<paragraph>[] foo</paragraph>' );
  429. model.change( writer => {
  430. writer.insertText( '@', doc.selection.getFirstPosition() );
  431. } );
  432. return waitForDebounce()
  433. .then( () => {
  434. expect( panelView.isVisible ).to.be.true;
  435. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  436. expect( mentionsView.items ).to.have.length( 5 );
  437. } );
  438. } );
  439. it( 'should show panel for matched marker after a <softBreak>', () => {
  440. model.schema.register( 'softBreak', {
  441. allowWhere: '$text',
  442. isInline: true
  443. } );
  444. editor.conversion.for( 'upcast' )
  445. .elementToElement( {
  446. model: 'softBreak',
  447. view: 'br'
  448. } );
  449. editor.conversion.for( 'downcast' )
  450. .elementToElement( {
  451. model: 'softBreak',
  452. view: ( modelElement, { writer } ) => writer.createEmptyElement( 'br' )
  453. } );
  454. setData( model, '<paragraph>abc<softBreak></softBreak>[] foo</paragraph>' );
  455. model.change( writer => {
  456. writer.insertText( '@', doc.selection.getFirstPosition() );
  457. } );
  458. return waitForDebounce()
  459. .then( () => {
  460. expect( panelView.isVisible ).to.be.true;
  461. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  462. expect( mentionsView.items ).to.have.length( 5 );
  463. } );
  464. } );
  465. // Opening parenthesis type characters that should be supported on all environments.
  466. for ( const character of [ '(', '\'', '"', '[', '{' ] ) {
  467. testOpeningPunctuationCharacter( character );
  468. }
  469. // Excerpt of opening parenthesis type characters that tests ES2018 Unicode property escapes on supported environment.
  470. for ( const character of [
  471. // Belongs to Ps (Punctuation, Open) group:
  472. '〈', '„', '﹛', '⦅', '{',
  473. // Belongs to Pi (Punctuation, Initial quote) group:
  474. '«', '‹', '⸌', ' ⸂', '⸠'
  475. ] ) {
  476. testOpeningPunctuationCharacter( character, !env.features.isRegExpUnicodePropertySupported );
  477. }
  478. it( 'should not show panel for marker in the middle of other word', () => {
  479. setData( model, '<paragraph>foo[]</paragraph>' );
  480. model.change( writer => {
  481. writer.insertText( '@', doc.selection.getFirstPosition() );
  482. } );
  483. return waitForDebounce()
  484. .then( () => {
  485. expect( panelView.isVisible ).to.be.false;
  486. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  487. } );
  488. } );
  489. it( 'should not show panel when selection is inside a mention', () => {
  490. setData( model, '<paragraph>foo @Lily bar[]</paragraph>' );
  491. model.change( writer => {
  492. const range = writer.createRange(
  493. writer.createPositionAt( doc.getRoot().getChild( 0 ), 4 ),
  494. writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 )
  495. );
  496. writer.setAttribute( 'mention', { id: '@Lily', uid: 1234 }, range );
  497. } );
  498. return waitForDebounce()
  499. .then( () => {
  500. model.change( writer => {
  501. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  502. } );
  503. expect( panelView.isVisible ).to.be.false;
  504. model.change( writer => {
  505. writer.setSelection( doc.getRoot().getChild( 0 ), 7 );
  506. } );
  507. } )
  508. .then( () => {
  509. expect( panelView.isVisible ).to.be.false;
  510. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  511. } );
  512. } );
  513. it( 'should not show panel when selection is at the end of a mention', () => {
  514. setData( model, '<paragraph>foo @Lily bar[]</paragraph>' );
  515. model.change( writer => {
  516. const range = writer.createRange(
  517. writer.createPositionAt( doc.getRoot().getChild( 0 ), 4 ),
  518. writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 )
  519. );
  520. writer.setAttribute( 'mention', { id: '@Lily', uid: 1234 }, range );
  521. } );
  522. return waitForDebounce()
  523. .then( () => {
  524. model.change( writer => {
  525. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  526. } );
  527. } )
  528. .then( () => {
  529. expect( panelView.isVisible ).to.be.false;
  530. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  531. } );
  532. } );
  533. it( 'should not show panel when selection is not collapsed', () => {
  534. setData( model, '<paragraph>foo []</paragraph>' );
  535. model.change( writer => {
  536. writer.insertText( '@', doc.selection.getFirstPosition() );
  537. } );
  538. return waitForDebounce()
  539. .then( () => {
  540. expect( panelView.isVisible ).to.be.true;
  541. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  542. model.change( () => {
  543. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  544. } );
  545. } )
  546. .then( waitForDebounce )
  547. .then( () => {
  548. expect( panelView.isVisible ).to.be.false;
  549. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  550. } );
  551. } );
  552. it( 'should not show panel when selection is changing (non-collapsed)', () => {
  553. setData( model, '<paragraph>foo []</paragraph>' );
  554. model.change( writer => {
  555. writer.insertText( '@', doc.selection.getFirstPosition() );
  556. } );
  557. return waitForDebounce()
  558. .then( () => {
  559. expect( panelView.isVisible ).to.be.true;
  560. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  561. model.change( () => {
  562. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  563. } );
  564. } )
  565. .then( waitForDebounce )
  566. .then( () => {
  567. expect( panelView.isVisible ).to.be.false;
  568. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  569. } )
  570. .then( () => {
  571. model.change( () => {
  572. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  573. } );
  574. } )
  575. .then( waitForDebounce )
  576. .then( () => {
  577. expect( panelView.isVisible ).to.be.false;
  578. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  579. } );
  580. } );
  581. it( 'should not show panel when selection is after existing mention', () => {
  582. setData( model, '<paragraph>foo [@Lily] bar[]</paragraph>' );
  583. model.change( writer => {
  584. writer.setAttribute( 'mention', { id: '@Lily', uid: 1234 }, doc.selection.getFirstRange() );
  585. } );
  586. return waitForDebounce()
  587. .then( () => {
  588. expect( panelView.isVisible ).to.be.false;
  589. model.change( writer => {
  590. writer.setSelection( doc.getRoot().getChild( 0 ), 8 );
  591. } );
  592. } )
  593. .then( waitForDebounce )
  594. .then( () => {
  595. expect( panelView.isVisible ).to.be.false;
  596. } );
  597. } );
  598. it( 'should not show panel when selection moves inside existing mention', () => {
  599. setData( model, '<paragraph>foo @Lily bar[]</paragraph>' );
  600. model.change( writer => {
  601. const range = writer.createRange(
  602. writer.createPositionAt( doc.getRoot().getChild( 0 ), 4 ),
  603. writer.createPositionAt( doc.getRoot().getChild( 0 ), 9 )
  604. );
  605. writer.setAttribute( 'mention', { id: '@Lily', uid: 1234 }, range );
  606. } );
  607. return waitForDebounce()
  608. .then( () => {
  609. model.change( writer => {
  610. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  611. } );
  612. } )
  613. .then( waitForDebounce )
  614. .then( () => {
  615. expect( panelView.isVisible ).to.be.false;
  616. model.change( writer => {
  617. writer.setSelection( doc.getRoot().getChild( 0 ), 8 );
  618. } );
  619. } )
  620. .then( waitForDebounce )
  621. .then( () => {
  622. expect( panelView.isVisible ).to.be.false;
  623. } );
  624. } );
  625. it( 'should show filtered results for matched text', () => {
  626. setData( model, '<paragraph>foo []</paragraph>' );
  627. model.change( writer => {
  628. writer.insertText( '@', doc.selection.getFirstPosition() );
  629. } );
  630. model.change( writer => {
  631. writer.insertText( 'T', doc.selection.getFirstPosition() );
  632. } );
  633. return waitForDebounce()
  634. .then( () => {
  635. expect( panelView.isVisible ).to.be.true;
  636. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  637. expect( mentionsView.items ).to.have.length( 1 );
  638. } );
  639. } );
  640. it( 'should focus the first item in panel', () => {
  641. setData( model, '<paragraph>foo []</paragraph>' );
  642. model.change( writer => {
  643. writer.insertText( '@', doc.selection.getFirstPosition() );
  644. } );
  645. return waitForDebounce()
  646. .then( () => {
  647. const button = mentionsView.items.get( 0 ).children.get( 0 );
  648. expect( button.isOn ).to.be.true;
  649. } );
  650. } );
  651. it( 'should hide panel if no matched items', () => {
  652. setData( model, '<paragraph>foo []</paragraph>' );
  653. model.change( writer => {
  654. writer.insertText( '@', doc.selection.getFirstPosition() );
  655. } );
  656. return waitForDebounce()
  657. .then( () => expect( panelView.isVisible ).to.be.true )
  658. .then( () => {
  659. model.change( writer => {
  660. writer.insertText( 'x', doc.selection.getFirstPosition() );
  661. } );
  662. } )
  663. .then( waitForDebounce )
  664. .then( () => {
  665. expect( panelView.isVisible ).to.be.false;
  666. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  667. expect( mentionsView.items ).to.have.length( 0 );
  668. } );
  669. } );
  670. it( 'should hide panel when text was unmatched', () => {
  671. setData( model, '<paragraph>foo []</paragraph>' );
  672. model.change( writer => {
  673. writer.insertText( '@', doc.selection.getFirstPosition() );
  674. } );
  675. return waitForDebounce()
  676. .then( () => expect( panelView.isVisible ).to.be.true )
  677. .then( () => {
  678. model.change( writer => {
  679. const end = doc.selection.getFirstPosition();
  680. const start = end.getShiftedBy( -1 );
  681. writer.remove( writer.createRange( start, end ) );
  682. } );
  683. } )
  684. .then( waitForDebounce )
  685. .then( () => expect( panelView.isVisible ).to.be.false );
  686. } );
  687. } );
  688. describe( 'unicode', () => {
  689. beforeEach( () => {
  690. return createClassicTestEditor( {
  691. feeds: [
  692. {
  693. // Always return 5 items
  694. feed: [ '@תַפּוּחַ', '@אַגָס', '@apple', '@pear' ],
  695. marker: '@'
  696. }
  697. ]
  698. } );
  699. } );
  700. it( 'should open panel for unicode character ב', function() {
  701. if ( !env.features.isRegExpUnicodePropertySupported ) {
  702. this.skip();
  703. }
  704. setData( model, '<paragraph>foo []</paragraph>' );
  705. model.change( writer => {
  706. writer.insertText( '@ס', doc.selection.getFirstPosition() );
  707. } );
  708. return waitForDebounce()
  709. .then( () => {
  710. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  711. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  712. expect( mentionsView.items ).to.have.length( 1 );
  713. } );
  714. } );
  715. } );
  716. describe( 'callback function using data from editor', () => {
  717. beforeEach( () => {
  718. return createClassicTestEditor( {
  719. feeds: [
  720. {
  721. marker: '#',
  722. feed() {
  723. expect( this ).to.equal( editor );
  724. return Promise.resolve( [ 'foo', 'bar' ] );
  725. }
  726. }
  727. ]
  728. } );
  729. } );
  730. it( 'should bind the instance panel for matched marker', () => {
  731. setData( model, '<paragraph>foo []</paragraph>' );
  732. model.change( writer => {
  733. writer.insertText( '#', doc.selection.getFirstPosition() );
  734. } );
  735. return waitForDebounce()
  736. .then( () => {
  737. expect( panelView.isVisible ).to.be.true;
  738. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  739. expect( mentionsView.items ).to.have.length( 2 );
  740. } );
  741. } );
  742. } );
  743. describe( 'asynchronous list with custom trigger', () => {
  744. const issuesNumbers = [ '#100', '#101', '#102', '#103' ];
  745. let feedCallbackStub, feedCallbackTimeout, feedCallbackCallTimes;
  746. beforeEach( () => {
  747. feedCallbackTimeout = 20;
  748. feedCallbackCallTimes = 0;
  749. function feedCallback( feedText ) {
  750. return new Promise( resolve => {
  751. setTimeout( () => {
  752. feedCallbackCallTimes++;
  753. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  754. }, feedCallbackTimeout );
  755. } );
  756. }
  757. feedCallbackStub = testUtils.sinon.stub().callsFake( feedCallback );
  758. return createClassicTestEditor( {
  759. feeds: [
  760. {
  761. marker: '#',
  762. feed: feedCallbackStub
  763. }
  764. ]
  765. } );
  766. } );
  767. it( 'should show panel for matched marker', () => {
  768. setData( model, '<paragraph>foo []</paragraph>' );
  769. model.change( writer => {
  770. writer.insertText( '#', doc.selection.getFirstPosition() );
  771. } );
  772. return waitForDebounce()
  773. .then( () => {
  774. expect( panelView.isVisible ).to.be.true;
  775. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  776. expect( mentionsView.items ).to.have.length( 4 );
  777. } );
  778. } );
  779. it( 'should fire requestFeed:response when request feed return a response', () => {
  780. setData( model, '<paragraph>foo []</paragraph>' );
  781. const eventSpy = sinon.spy();
  782. mentionUI.on( 'requestFeed:response', eventSpy );
  783. model.change( writer => {
  784. writer.insertText( '#', doc.selection.getFirstPosition() );
  785. } );
  786. return waitForDebounce()
  787. .then( () => {
  788. sinon.assert.calledOnce( eventSpy );
  789. sinon.assert.calledWithExactly(
  790. eventSpy,
  791. sinon.match.any,
  792. {
  793. feed: issuesNumbers,
  794. marker: '#',
  795. feedText: ''
  796. }
  797. );
  798. expect( panelView.isVisible ).to.be.true;
  799. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  800. expect( mentionsView.items ).to.have.length( 4 );
  801. } );
  802. } );
  803. it( 'should show filtered results for matched text', () => {
  804. setData( model, '<paragraph>foo []</paragraph>' );
  805. model.change( writer => {
  806. writer.insertText( '#', doc.selection.getFirstPosition() );
  807. } );
  808. model.change( writer => {
  809. writer.insertText( '2', doc.selection.getFirstPosition() );
  810. } );
  811. return waitForDebounce()
  812. .then( () => {
  813. expect( panelView.isVisible ).to.be.true;
  814. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  815. expect( mentionsView.items ).to.have.length( 1 );
  816. } );
  817. } );
  818. it( 'should hide panel if no matched items', () => {
  819. setData( model, '<paragraph>foo []</paragraph>' );
  820. model.change( writer => {
  821. writer.insertText( '#', doc.selection.getFirstPosition() );
  822. } );
  823. return waitForDebounce()
  824. .then( () => expect( panelView.isVisible ).to.be.true )
  825. .then( () => {
  826. model.change( writer => {
  827. writer.insertText( 'x', doc.selection.getFirstPosition() );
  828. } );
  829. } )
  830. .then( waitForDebounce )
  831. .then( () => {
  832. expect( panelView.isVisible ).to.be.false;
  833. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  834. expect( mentionsView.items ).to.have.length( 0 );
  835. } );
  836. } );
  837. it( 'should hide panel when text was unmatched', () => {
  838. setData( model, '<paragraph>foo []</paragraph>' );
  839. model.change( writer => {
  840. writer.insertText( '#', doc.selection.getFirstPosition() );
  841. } );
  842. return waitForDebounce()
  843. .then( () => expect( panelView.isVisible ).to.be.true )
  844. .then( () => {
  845. model.change( writer => {
  846. const end = doc.selection.getFirstPosition();
  847. const start = end.getShiftedBy( -1 );
  848. writer.remove( writer.createRange( start, end ) );
  849. } );
  850. } )
  851. .then( waitForDebounce )
  852. .then( () => expect( panelView.isVisible ).to.be.false );
  853. } );
  854. it( 'should show panel debounced', () => {
  855. setData( model, '<paragraph>foo []</paragraph>' );
  856. model.change( writer => {
  857. writer.insertText( '#', doc.selection.getFirstPosition() );
  858. } );
  859. sinon.assert.notCalled( feedCallbackStub );
  860. return Promise.resolve()
  861. .then( wait( 20 ) )
  862. .then( () => {
  863. sinon.assert.notCalled( feedCallbackStub );
  864. model.change( writer => {
  865. writer.insertText( '1', doc.selection.getFirstPosition() );
  866. } );
  867. } )
  868. .then( wait( 20 ) )
  869. .then( () => {
  870. sinon.assert.notCalled( feedCallbackStub );
  871. model.change( writer => {
  872. writer.insertText( '0', doc.selection.getFirstPosition() );
  873. } );
  874. } )
  875. .then( waitForDebounce )
  876. .then( () => {
  877. sinon.assert.calledOnce( feedCallbackStub );
  878. // Should be called with all typed letters before debounce.
  879. sinon.assert.calledWithExactly( feedCallbackStub, '10' );
  880. expect( panelView.isVisible ).to.be.true;
  881. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  882. expect( mentionsView.items ).to.have.length( 4 );
  883. } );
  884. } );
  885. it( 'should discard requested feed if they came out of order', () => {
  886. setData( model, '<paragraph>foo []</paragraph>' );
  887. model.change( writer => {
  888. writer.insertText( '#', doc.selection.getFirstPosition() );
  889. } );
  890. sinon.assert.notCalled( feedCallbackStub );
  891. const panelShowSpy = sinon.spy( panelView, 'show' );
  892. // Increase the response time to extend the debounce time out.
  893. feedCallbackTimeout = 300;
  894. return Promise.resolve()
  895. .then( wait( 20 ) )
  896. .then( () => {
  897. sinon.assert.notCalled( feedCallbackStub );
  898. model.change( writer => {
  899. writer.insertText( '1', doc.selection.getFirstPosition() );
  900. } );
  901. } )
  902. .then( waitForDebounce )
  903. .then( () => {
  904. sinon.assert.calledOnce( feedCallbackStub );
  905. sinon.assert.calledWithExactly( feedCallbackStub, '1' );
  906. expect( panelView.isVisible, 'panel is hidden' ).to.be.false;
  907. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  908. // Make second callback resolve before first.
  909. feedCallbackTimeout = 50;
  910. model.change( writer => {
  911. writer.insertText( '0', doc.selection.getFirstPosition() );
  912. } );
  913. } )
  914. .then( wait( 300 ) ) // Wait longer so the longer callback will be resolved.
  915. .then( () => {
  916. sinon.assert.calledTwice( feedCallbackStub );
  917. sinon.assert.calledWithExactly( feedCallbackStub.getCall( 1 ), '10' );
  918. sinon.assert.calledOnce( panelShowSpy );
  919. expect( feedCallbackCallTimes ).to.equal( 2 );
  920. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  921. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  922. expect( mentionsView.items ).to.have.length( 4 );
  923. } );
  924. } );
  925. it( 'should fire requestFeed:discarded event when requested feed came out of order', () => {
  926. setData( model, '<paragraph>foo []</paragraph>' );
  927. model.change( writer => {
  928. writer.insertText( '#', doc.selection.getFirstPosition() );
  929. } );
  930. sinon.assert.notCalled( feedCallbackStub );
  931. const panelShowSpy = sinon.spy( panelView, 'show' );
  932. const eventSpy = sinon.spy();
  933. mentionUI.on( 'requestFeed:discarded', eventSpy );
  934. // Increase the response time to extend the debounce time out.
  935. feedCallbackTimeout = 300;
  936. return Promise.resolve()
  937. .then( wait( 20 ) )
  938. .then( () => {
  939. sinon.assert.notCalled( feedCallbackStub );
  940. model.change( writer => {
  941. writer.insertText( '1', doc.selection.getFirstPosition() );
  942. } );
  943. } )
  944. .then( waitForDebounce )
  945. .then( () => {
  946. sinon.assert.calledOnce( feedCallbackStub );
  947. sinon.assert.calledWithExactly( feedCallbackStub, '1' );
  948. expect( panelView.isVisible, 'panel is hidden' ).to.be.false;
  949. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  950. // Make second callback resolve before first.
  951. feedCallbackTimeout = 50;
  952. model.change( writer => {
  953. writer.insertText( '0', doc.selection.getFirstPosition() );
  954. } );
  955. } )
  956. .then( wait( 300 ) ) // Wait longer so the longer callback will be resolved.
  957. .then( () => {
  958. sinon.assert.calledTwice( feedCallbackStub );
  959. sinon.assert.calledWithExactly( feedCallbackStub.getCall( 1 ), '10' );
  960. sinon.assert.calledOnce( panelShowSpy );
  961. sinon.assert.calledOnce( eventSpy );
  962. sinon.assert.calledWithExactly(
  963. eventSpy,
  964. sinon.match.any,
  965. {
  966. feed: issuesNumbers,
  967. marker: '#',
  968. feedText: '1'
  969. }
  970. );
  971. expect( feedCallbackCallTimes ).to.equal( 2 );
  972. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  973. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  974. expect( mentionsView.items ).to.have.length( 4 );
  975. } );
  976. } );
  977. it( 'should discard requested feed if mention UI is hidden', () => {
  978. setData( model, '<paragraph>foo []</paragraph>' );
  979. model.change( writer => {
  980. writer.insertText( '#', doc.selection.getFirstPosition() );
  981. } );
  982. sinon.assert.notCalled( feedCallbackStub );
  983. feedCallbackTimeout = 200;
  984. return Promise.resolve()
  985. .then( waitForDebounce )
  986. .then( () => {
  987. expect( panelView.isVisible ).to.be.false; // Should be still hidden;
  988. // Should be called with empty string.
  989. sinon.assert.calledWithExactly( feedCallbackStub, '' );
  990. model.change( writer => {
  991. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  992. } );
  993. } )
  994. .then( waitForDebounce )
  995. .then( () => {
  996. expect( panelView.isVisible ).to.be.false;
  997. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  998. } );
  999. } );
  1000. it( 'should fire requestFeed:error and log warning if requested feed failed', () => {
  1001. setData( model, '<paragraph>foo []</paragraph>' );
  1002. feedCallbackStub.returns( Promise.reject( 'Request timeout' ) );
  1003. const warnSpy = sinon.stub( console, 'warn' );
  1004. const eventSpy = sinon.spy();
  1005. mentionUI.on( 'requestFeed:error', eventSpy );
  1006. model.change( writer => {
  1007. writer.insertText( '#', doc.selection.getFirstPosition() );
  1008. } );
  1009. return waitForDebounce()
  1010. .then( () => {
  1011. expect( panelView.isVisible, 'panel is hidden' ).to.be.false;
  1012. expect( editor.model.markers.has( 'mention' ), 'there is no marker' ).to.be.false;
  1013. sinon.assert.calledWithExactly(
  1014. warnSpy,
  1015. sinon.match( /^mention-feed-callback-error/ ),
  1016. sinon.match( { marker: '#' } ),
  1017. sinon.match.string // Link to the documentation
  1018. );
  1019. sinon.assert.calledOnce( eventSpy );
  1020. } );
  1021. } );
  1022. it( 'should not fail if marker was removed', () => {
  1023. setData( model, '<paragraph>foo []</paragraph>' );
  1024. const selectFirstMentionSpy = sinon.spy( mentionsView, 'selectFirst' );
  1025. model.change( writer => {
  1026. writer.insertText( '#', doc.selection.getFirstPosition() );
  1027. } );
  1028. sinon.assert.notCalled( feedCallbackStub );
  1029. // Increase the response time to extend the debounce time out.
  1030. feedCallbackTimeout = 500;
  1031. return Promise.resolve()
  1032. .then( waitForDebounce )
  1033. .then( wait( 20 ) )
  1034. .then( () => {
  1035. model.change( writer => {
  1036. writer.setSelection( doc.getRoot().getChild( 0 ), 2 );
  1037. } );
  1038. } )
  1039. .then( wait( 20 ) )
  1040. .then( () => {
  1041. feedCallbackTimeout = 1000;
  1042. model.change( writer => {
  1043. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  1044. } );
  1045. } )
  1046. .then( wait( 500 ) )
  1047. .then( () => {
  1048. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  1049. // If there were any errors this will not get called.
  1050. // The errors might come from unhandled promise rejections errors.
  1051. sinon.assert.calledOnce( selectFirstMentionSpy );
  1052. } );
  1053. } );
  1054. it( 'should not show panel if selection was moved during fetching a feed', () => {
  1055. setData( model, '<paragraph>foo [#101] bar</paragraph><paragraph></paragraph>' );
  1056. model.change( writer => {
  1057. writer.setAttribute( 'mention', { id: '#101', uid: 1234 }, doc.selection.getFirstRange() );
  1058. } );
  1059. // Increase the response time to extend the debounce time out.
  1060. feedCallbackTimeout = 300;
  1061. model.change( writer => {
  1062. writer.setSelection( doc.getRoot().getChild( 1 ), 0 );
  1063. writer.insertText( '#', doc.selection.getFirstPosition() );
  1064. } );
  1065. sinon.assert.notCalled( feedCallbackStub );
  1066. return Promise.resolve()
  1067. .then( waitForDebounce )
  1068. .then( () => {
  1069. sinon.assert.calledOnce( feedCallbackStub );
  1070. model.change( writer => {
  1071. writer.setSelection( doc.getRoot().getChild( 0 ), 6 );
  1072. } );
  1073. expect( panelView.isVisible ).to.be.false;
  1074. } )
  1075. .then( waitForDebounce )
  1076. .then( wait( 20 ) )
  1077. .then( () => {
  1078. expect( panelView.isVisible ).to.be.false;
  1079. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1080. } );
  1081. } );
  1082. } );
  1083. function testOpeningPunctuationCharacter( character, skip = false ) {
  1084. it( `should show panel for matched marker after a "${ character }" character`, function() {
  1085. if ( skip ) {
  1086. this.skip();
  1087. }
  1088. setData( model, '<paragraph>[] foo</paragraph>' );
  1089. model.change( writer => {
  1090. writer.insertText( character, doc.selection.getFirstPosition() );
  1091. } );
  1092. model.change( writer => {
  1093. writer.insertText( '@', doc.selection.getFirstPosition() );
  1094. } );
  1095. return waitForDebounce()
  1096. .then( () => {
  1097. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  1098. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  1099. expect( mentionsView.items ).to.have.length( 5 );
  1100. } );
  1101. } );
  1102. }
  1103. } );
  1104. describe( 'panel behavior', () => {
  1105. it( 'should close the opened panel on esc', () => {
  1106. return createClassicTestEditor( staticConfig )
  1107. .then( () => {
  1108. setData( model, '<paragraph>foo []</paragraph>' );
  1109. model.change( writer => {
  1110. writer.insertText( '@', doc.selection.getFirstPosition() );
  1111. } );
  1112. } )
  1113. .then( waitForDebounce )
  1114. .then( () => {
  1115. expect( panelView.isVisible ).to.be.true;
  1116. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1117. fireKeyDownEvent( {
  1118. keyCode: keyCodes.esc,
  1119. preventDefault: sinon.spy(),
  1120. stopPropagation: sinon.spy()
  1121. } );
  1122. expect( panelView.isVisible ).to.be.false;
  1123. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1124. } );
  1125. } );
  1126. it( 'should close the opened panel when click outside the panel', () => {
  1127. return createClassicTestEditor( staticConfig )
  1128. .then( () => {
  1129. setData( model, '<paragraph>foo []</paragraph>' );
  1130. model.change( writer => {
  1131. writer.insertText( '@', doc.selection.getFirstPosition() );
  1132. } );
  1133. } )
  1134. .then( waitForDebounce )
  1135. .then( () => {
  1136. expect( panelView.isVisible ).to.be.true;
  1137. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1138. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  1139. expect( panelView.isVisible ).to.be.false;
  1140. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1141. } );
  1142. } );
  1143. it( 'should hide the panel on selection change', () => {
  1144. return createClassicTestEditor( staticConfig )
  1145. .then( () => {
  1146. setData( model, '<paragraph>foo []</paragraph>' );
  1147. model.change( writer => {
  1148. writer.insertText( '@', doc.selection.getFirstPosition() );
  1149. } );
  1150. } )
  1151. .then( waitForDebounce )
  1152. .then( () => {
  1153. expect( panelView.isVisible ).to.be.true;
  1154. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1155. model.change( writer => {
  1156. // Place position at the beginning of a paragraph.
  1157. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  1158. } );
  1159. expect( panelView.isVisible ).to.be.false;
  1160. expect( mentionsView.position ).to.be.undefined;
  1161. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1162. } );
  1163. } );
  1164. it( 'should hide the panel on selection change triggered by mouse click', () => {
  1165. return createClassicTestEditor( staticConfig )
  1166. .then( () => {
  1167. setData( model, '<paragraph>foo []</paragraph>' );
  1168. model.change( writer => {
  1169. writer.insertText( '@', doc.selection.getFirstPosition() );
  1170. } );
  1171. } )
  1172. .then( waitForDebounce )
  1173. .then( () => {
  1174. expect( panelView.isVisible ).to.be.true;
  1175. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1176. // This happens when user clicks outside the panel view and selection is changed.
  1177. // Two panel closing mechanisms are run:
  1178. // - clickOutsideHandler
  1179. // - unmatched text in text watcher
  1180. // which may fail when trying to remove mention marker twice.
  1181. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  1182. model.change( writer => {
  1183. // Place position at the beginning of a paragraph.
  1184. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  1185. } );
  1186. expect( panelView.isVisible ).to.be.false;
  1187. expect( mentionsView.position ).to.be.undefined;
  1188. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1189. } );
  1190. } );
  1191. describe( 'default list item', () => {
  1192. // Create map of expected feed items as objects as they will be stored internally.
  1193. const feedItems = staticConfig.feeds[ 0 ].feed.map( text => ( { text: `${ text }`, id: `${ text }` } ) );
  1194. beforeEach( () => {
  1195. return createClassicTestEditor( staticConfig );
  1196. } );
  1197. describe( 'on arrows', () => {
  1198. it( 'should cycle down on arrow down', () => {
  1199. setData( model, '<paragraph>foo []</paragraph>' );
  1200. model.change( writer => {
  1201. writer.insertText( '@', doc.selection.getFirstPosition() );
  1202. } );
  1203. return waitForDebounce()
  1204. .then( () => {
  1205. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1206. const keyEvtData = {
  1207. keyCode: keyCodes.arrowdown,
  1208. preventDefault: sinon.spy(),
  1209. stopPropagation: sinon.spy()
  1210. };
  1211. fireKeyDownEvent( keyEvtData );
  1212. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1213. fireKeyDownEvent( keyEvtData );
  1214. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1215. fireKeyDownEvent( keyEvtData );
  1216. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1217. fireKeyDownEvent( keyEvtData );
  1218. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1219. fireKeyDownEvent( keyEvtData );
  1220. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1221. } );
  1222. } );
  1223. it( 'should cycle up on arrow up', () => {
  1224. setData( model, '<paragraph>foo []</paragraph>' );
  1225. model.change( writer => {
  1226. writer.insertText( '@', doc.selection.getFirstPosition() );
  1227. } );
  1228. return waitForDebounce()
  1229. .then( () => {
  1230. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1231. const keyEvtData = {
  1232. keyCode: keyCodes.arrowup,
  1233. preventDefault: sinon.spy(),
  1234. stopPropagation: sinon.spy()
  1235. };
  1236. fireKeyDownEvent( keyEvtData );
  1237. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1238. fireKeyDownEvent( keyEvtData );
  1239. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1240. fireKeyDownEvent( keyEvtData );
  1241. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1242. fireKeyDownEvent( keyEvtData );
  1243. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1244. fireKeyDownEvent( keyEvtData );
  1245. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1246. } );
  1247. } );
  1248. it( 'should not cycle with only one item in the list', () => {
  1249. setData( model, '<paragraph>foo []</paragraph>' );
  1250. const keyDownEvtData = {
  1251. keyCode: keyCodes.arrowdown,
  1252. preventDefault: sinon.spy(),
  1253. stopPropagation: sinon.spy()
  1254. };
  1255. const keyUpEvtData = {
  1256. keyCode: keyCodes.arrowdown,
  1257. preventDefault: sinon.spy(),
  1258. stopPropagation: sinon.spy()
  1259. };
  1260. model.change( writer => {
  1261. writer.insertText( '@T', doc.selection.getFirstPosition() );
  1262. } );
  1263. return waitForDebounce()
  1264. .then( () => {
  1265. expectChildViewsIsOnState( [ true ] );
  1266. fireKeyDownEvent( keyDownEvtData );
  1267. expectChildViewsIsOnState( [ true ] );
  1268. fireKeyDownEvent( keyUpEvtData );
  1269. expectChildViewsIsOnState( [ true ] );
  1270. } );
  1271. } );
  1272. } );
  1273. describe( 'on "execute" keys', () => {
  1274. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  1275. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  1276. } );
  1277. describe( 'on other keys', () => {
  1278. it( 'should do nothing on space', async () => {
  1279. setData( model, '<paragraph>foo []</paragraph>' );
  1280. model.change( writer => {
  1281. writer.insertText( '@', doc.selection.getFirstPosition() );
  1282. } );
  1283. const command = editor.commands.get( 'mention' );
  1284. const spy = testUtils.sinon.spy( command, 'execute' );
  1285. await waitForDebounce();
  1286. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1287. fireKeyDownEvent( {
  1288. keyCodes: keyCodes.space,
  1289. preventDefault: sinon.spy(),
  1290. stopPropagation: sinon.spy()
  1291. } );
  1292. sinon.assert.notCalled( spy );
  1293. } );
  1294. } );
  1295. } );
  1296. describe( 'default list item with custom feed', () => {
  1297. const issues = [
  1298. { id: '@Ted' },
  1299. { id: '@Barney' },
  1300. { id: '@Robin' },
  1301. { id: '@Lily' },
  1302. { id: '@Marshal' }
  1303. ];
  1304. beforeEach( () => {
  1305. return createClassicTestEditor( {
  1306. feeds: [
  1307. {
  1308. marker: '@',
  1309. feed: feedText => issues.filter( issue => issue.id.includes( feedText ) )
  1310. }
  1311. ]
  1312. } );
  1313. } );
  1314. it( 'should show panel for matched marker', () => {
  1315. setData( model, '<paragraph>foo []</paragraph>' );
  1316. model.change( writer => {
  1317. writer.insertText( '@', doc.selection.getFirstPosition() );
  1318. } );
  1319. return waitForDebounce()
  1320. .then( () => {
  1321. expect( panelView.isVisible ).to.be.true;
  1322. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1323. expect( mentionsView.items ).to.have.length( 5 );
  1324. } );
  1325. } );
  1326. } );
  1327. describe( 'custom list item (string)', () => {
  1328. const issues = [
  1329. { id: '@1002', title: 'Some bug in editor.' },
  1330. { id: '@1003', title: 'Introduce this feature.' },
  1331. { id: '@1004', title: 'Missing docs.' },
  1332. { id: '@1005', title: 'Another bug.' },
  1333. { id: '@1006', title: 'More bugs' }
  1334. ];
  1335. beforeEach( () => {
  1336. return createClassicTestEditor( {
  1337. feeds: [
  1338. {
  1339. marker: '@',
  1340. feed: issues,
  1341. itemRenderer: item => item.title
  1342. }
  1343. ]
  1344. } );
  1345. } );
  1346. it( 'should show panel for matched marker', () => {
  1347. setData( model, '<paragraph>foo []</paragraph>' );
  1348. model.change( writer => {
  1349. writer.insertText( '@', doc.selection.getFirstPosition() );
  1350. } );
  1351. return waitForDebounce()
  1352. .then( () => {
  1353. expect( panelView.isVisible ).to.be.true;
  1354. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1355. expect( mentionsView.items ).to.have.length( 5 );
  1356. } );
  1357. } );
  1358. describe( 'keys', () => {
  1359. describe( 'on arrows', () => {
  1360. it( 'should cycle down on arrow down', () => {
  1361. setData( model, '<paragraph>foo []</paragraph>' );
  1362. model.change( writer => {
  1363. writer.insertText( '@', doc.selection.getFirstPosition() );
  1364. } );
  1365. return waitForDebounce()
  1366. .then( () => {
  1367. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1368. const keyEvtData = {
  1369. keyCode: keyCodes.arrowdown,
  1370. preventDefault: sinon.spy(),
  1371. stopPropagation: sinon.spy()
  1372. };
  1373. fireKeyDownEvent( keyEvtData );
  1374. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1375. fireKeyDownEvent( keyEvtData );
  1376. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1377. fireKeyDownEvent( keyEvtData );
  1378. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1379. fireKeyDownEvent( keyEvtData );
  1380. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1381. fireKeyDownEvent( keyEvtData );
  1382. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1383. } );
  1384. } );
  1385. it( 'should cycle up on arrow up', () => {
  1386. setData( model, '<paragraph>foo []</paragraph>' );
  1387. model.change( writer => {
  1388. writer.insertText( '@', doc.selection.getFirstPosition() );
  1389. } );
  1390. return waitForDebounce()
  1391. .then( () => {
  1392. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1393. const keyEvtData = {
  1394. keyCode: keyCodes.arrowup,
  1395. preventDefault: sinon.spy(),
  1396. stopPropagation: sinon.spy()
  1397. };
  1398. fireKeyDownEvent( keyEvtData );
  1399. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1400. fireKeyDownEvent( keyEvtData );
  1401. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1402. fireKeyDownEvent( keyEvtData );
  1403. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1404. fireKeyDownEvent( keyEvtData );
  1405. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1406. fireKeyDownEvent( keyEvtData );
  1407. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1408. } );
  1409. } );
  1410. } );
  1411. describe( 'on "execute" keys', () => {
  1412. testExecuteKey( 'enter', keyCodes.enter, issues );
  1413. testExecuteKey( 'tab', keyCodes.tab, issues );
  1414. } );
  1415. } );
  1416. } );
  1417. describe( 'custom list item (DOM Element)', () => {
  1418. const issues = [
  1419. { id: '@1002', title: 'Some bug in editor.' },
  1420. { id: '@1003', title: 'Introduce this feature.' },
  1421. { id: '@1004', title: 'Missing docs.' },
  1422. { id: '@1005', title: 'Another bug.' },
  1423. { id: '@1006', title: 'More bugs' }
  1424. ];
  1425. beforeEach( () => {
  1426. return createClassicTestEditor( {
  1427. feeds: [
  1428. {
  1429. marker: '@',
  1430. feed: feedText => {
  1431. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  1432. },
  1433. itemRenderer: item => {
  1434. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  1435. span.innerHTML = `<span id="issue-${ item.id.slice( 1 ) }">@${ item.title }</span>`;
  1436. return span;
  1437. }
  1438. }
  1439. ]
  1440. } );
  1441. } );
  1442. it( 'should show panel for matched marker', () => {
  1443. setData( model, '<paragraph>foo []</paragraph>' );
  1444. model.change( writer => {
  1445. writer.insertText( '@', doc.selection.getFirstPosition() );
  1446. } );
  1447. return waitForDebounce()
  1448. .then( () => {
  1449. expect( panelView.isVisible ).to.be.true;
  1450. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1451. expect( mentionsView.items ).to.have.length( 5 );
  1452. } );
  1453. } );
  1454. describe( 'keys', () => {
  1455. describe( 'on arrows', () => {
  1456. it( 'should cycle down on arrow down', () => {
  1457. setData( model, '<paragraph>foo []</paragraph>' );
  1458. model.change( writer => {
  1459. writer.insertText( '@', doc.selection.getFirstPosition() );
  1460. } );
  1461. return waitForDebounce()
  1462. .then( () => {
  1463. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1464. const keyEvtData = {
  1465. keyCode: keyCodes.arrowdown,
  1466. preventDefault: sinon.spy(),
  1467. stopPropagation: sinon.spy()
  1468. };
  1469. fireKeyDownEvent( keyEvtData );
  1470. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1471. fireKeyDownEvent( keyEvtData );
  1472. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1473. fireKeyDownEvent( keyEvtData );
  1474. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1475. fireKeyDownEvent( keyEvtData );
  1476. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1477. fireKeyDownEvent( keyEvtData );
  1478. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1479. } );
  1480. } );
  1481. it( 'should cycle up on arrow up', () => {
  1482. setData( model, '<paragraph>foo []</paragraph>' );
  1483. model.change( writer => {
  1484. writer.insertText( '@', doc.selection.getFirstPosition() );
  1485. } );
  1486. return waitForDebounce()
  1487. .then( () => {
  1488. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1489. const keyEvtData = {
  1490. keyCode: keyCodes.arrowup,
  1491. preventDefault: sinon.spy(),
  1492. stopPropagation: sinon.spy()
  1493. };
  1494. fireKeyDownEvent( keyEvtData );
  1495. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1496. fireKeyDownEvent( keyEvtData );
  1497. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1498. fireKeyDownEvent( keyEvtData );
  1499. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1500. fireKeyDownEvent( keyEvtData );
  1501. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1502. fireKeyDownEvent( keyEvtData );
  1503. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1504. } );
  1505. } );
  1506. } );
  1507. describe( 'on "execute" keys', () => {
  1508. testExecuteKey( 'enter', keyCodes.enter, issues );
  1509. testExecuteKey( 'tab', keyCodes.tab, issues );
  1510. } );
  1511. describe( 'mouse', () => {
  1512. it( 'should execute selected button on mouse click', () => {
  1513. setData( model, '<paragraph>foo []</paragraph>' );
  1514. model.change( writer => {
  1515. writer.insertText( '@', doc.selection.getFirstPosition() );
  1516. } );
  1517. const command = editor.commands.get( 'mention' );
  1518. const spy = testUtils.sinon.spy( command, 'execute' );
  1519. return waitForDebounce()
  1520. .then( () => {
  1521. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1522. const element = panelView.element.querySelector( '#issue-1004' );
  1523. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  1524. sinon.assert.calledOnce( spy );
  1525. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1526. const item = issues[ 2 ];
  1527. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  1528. expect( commandOptions ).to.have.property( 'marker', '@' );
  1529. expect( commandOptions ).to.have.property( 'range' );
  1530. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1531. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1532. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1533. } );
  1534. } );
  1535. } );
  1536. } );
  1537. } );
  1538. describe( 'multiple feeds configuration', () => {
  1539. beforeEach( () => {
  1540. return createClassicTestEditor( {
  1541. feeds: [
  1542. {
  1543. marker: '@',
  1544. feed: [ '@a1', '@a2', '@a3' ]
  1545. },
  1546. {
  1547. marker: '$',
  1548. feed: [
  1549. '$a1', '$a2', '$a3', '$a4',
  1550. // A case of mention with a marker character from other feed. See #6398.
  1551. '$a@'
  1552. ]
  1553. }
  1554. ]
  1555. } );
  1556. } );
  1557. it( 'should show panel for matched marker', () => {
  1558. setData( model, '<paragraph>foo []</paragraph>' );
  1559. model.change( writer => {
  1560. writer.insertText( '@', doc.selection.getFirstPosition() );
  1561. } );
  1562. return waitForDebounce()
  1563. .then( () => {
  1564. expect( panelView.isVisible ).to.be.true;
  1565. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1566. expect( mentionsView.items ).to.have.length( 3 );
  1567. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1568. } )
  1569. .then( waitForDebounce )
  1570. .then( () => {
  1571. expect( panelView.isVisible ).to.be.false;
  1572. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1573. model.change( writer => {
  1574. writer.insertText( '$', doc.selection.getFirstPosition() );
  1575. } );
  1576. } )
  1577. .then( waitForDebounce )
  1578. .then( () => {
  1579. expect( panelView.isVisible ).to.be.true;
  1580. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1581. expect( mentionsView.items ).to.have.length( 5 );
  1582. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1583. } )
  1584. .then( waitForDebounce )
  1585. .then( () => {
  1586. expect( panelView.isVisible ).to.be.false;
  1587. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1588. model.change( writer => {
  1589. writer.insertText( '@', doc.selection.getFirstPosition() );
  1590. } );
  1591. } )
  1592. .then( waitForDebounce )
  1593. .then( () => {
  1594. expect( panelView.isVisible ).to.be.true;
  1595. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1596. expect( mentionsView.items ).to.have.length( 3 );
  1597. } );
  1598. } );
  1599. it( 'should show panel for matched marker if it contains the other configured marker', () => {
  1600. setData( model, '<paragraph>foo []</paragraph>' );
  1601. model.change( writer => {
  1602. writer.insertText( '@', doc.selection.getFirstPosition() );
  1603. } );
  1604. return waitForDebounce()
  1605. .then( () => {
  1606. expect( panelView.isVisible ).to.be.true;
  1607. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1608. expect( mentionsView.items ).to.have.length( 3 );
  1609. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1610. } )
  1611. .then( waitForDebounce )
  1612. .then( () => {
  1613. expect( panelView.isVisible ).to.be.false;
  1614. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1615. model.change( writer => {
  1616. writer.insertText( '$a', doc.selection.getFirstPosition() );
  1617. } );
  1618. } )
  1619. .then( waitForDebounce )
  1620. .then( () => {
  1621. model.change( writer => {
  1622. writer.insertText( '@', doc.selection.getFirstPosition() );
  1623. } );
  1624. } )
  1625. .then( waitForDebounce )
  1626. .then( () => {
  1627. expect( panelView.isVisible ).to.be.true;
  1628. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1629. } );
  1630. } );
  1631. } );
  1632. function testExecuteKey( name, keyCode, feedItems ) {
  1633. it( 'should execute selected button on ' + name, () => {
  1634. setData( model, '<paragraph>foo []</paragraph>' );
  1635. model.change( writer => {
  1636. writer.insertText( '@', doc.selection.getFirstPosition() );
  1637. } );
  1638. const command = editor.commands.get( 'mention' );
  1639. const spy = testUtils.sinon.spy( command, 'execute' );
  1640. return waitForDebounce()
  1641. .then( () => {
  1642. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1643. fireKeyDownEvent( {
  1644. keyCode: keyCodes.arrowup,
  1645. preventDefault: sinon.spy(),
  1646. stopPropagation: sinon.spy()
  1647. } );
  1648. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1649. fireKeyDownEvent( {
  1650. keyCode,
  1651. preventDefault: sinon.spy(),
  1652. stopPropagation: sinon.spy()
  1653. } );
  1654. sinon.assert.calledOnce( spy );
  1655. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  1656. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1657. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1658. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  1659. } );
  1660. } );
  1661. it( 'should do nothing if panel is not visible on ' + name, () => {
  1662. setData( model, '<paragraph>foo []</paragraph>' );
  1663. model.change( writer => {
  1664. writer.insertText( '@', doc.selection.getFirstPosition() );
  1665. } );
  1666. const command = editor.commands.get( 'mention' );
  1667. const spy = testUtils.sinon.spy( command, 'execute' );
  1668. return waitForDebounce()
  1669. .then( () => {
  1670. expect( panelView.isVisible ).to.be.true;
  1671. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1672. fireKeyDownEvent( {
  1673. keyCode: keyCodes.esc,
  1674. preventDefault: sinon.spy(),
  1675. stopPropagation: sinon.spy()
  1676. } );
  1677. expect( panelView.isVisible ).to.be.false;
  1678. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1679. fireKeyDownEvent( {
  1680. keyCode,
  1681. preventDefault: sinon.spy(),
  1682. stopPropagation: sinon.spy()
  1683. } );
  1684. sinon.assert.notCalled( spy );
  1685. expect( panelView.isVisible ).to.be.false;
  1686. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1687. } );
  1688. } );
  1689. }
  1690. } );
  1691. describe( 'execute', () => {
  1692. beforeEach( () => createClassicTestEditor( staticConfig ) );
  1693. it( 'should call the mention command with proper options', () => {
  1694. setData( model, '<paragraph>foo []</paragraph>' );
  1695. model.change( writer => {
  1696. writer.insertText( '@', doc.selection.getFirstPosition() );
  1697. } );
  1698. const command = editor.commands.get( 'mention' );
  1699. const spy = testUtils.sinon.spy( command, 'execute' );
  1700. return waitForDebounce()
  1701. .then( () => {
  1702. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1703. sinon.assert.calledOnce( spy );
  1704. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1705. assertCommandOptions( commandOptions, '@', { id: '@Barney', text: '@Barney' } );
  1706. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1707. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1708. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1709. } );
  1710. } );
  1711. it( 'should hide panel on execute', () => {
  1712. setData( model, '<paragraph>foo []</paragraph>' );
  1713. model.change( writer => {
  1714. writer.insertText( '@', doc.selection.getFirstPosition() );
  1715. } );
  1716. return waitForDebounce()
  1717. .then( () => {
  1718. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1719. expect( panelView.isVisible ).to.be.false;
  1720. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1721. } );
  1722. } );
  1723. it( 'should focus view after command execution', () => {
  1724. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  1725. setData( model, '<paragraph>foo []</paragraph>' );
  1726. model.change( writer => {
  1727. writer.insertText( '@', doc.selection.getFirstPosition() );
  1728. } );
  1729. return waitForDebounce()
  1730. .then( () => {
  1731. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1732. sinon.assert.calledOnce( focusSpy );
  1733. } );
  1734. } );
  1735. } );
  1736. function createClassicTestEditor( mentionConfig ) {
  1737. return ClassicTestEditor
  1738. .create( editorElement, {
  1739. plugins: [ Paragraph, MentionEditing, MentionUI ],
  1740. mention: mentionConfig
  1741. } )
  1742. .then( newEditor => {
  1743. editor = newEditor;
  1744. model = editor.model;
  1745. doc = model.document;
  1746. editingView = editor.editing.view;
  1747. mentionUI = editor.plugins.get( MentionUI );
  1748. panelView = editor.plugins.get( ContextualBalloon ).view;
  1749. mentionsView = mentionUI._mentionsView;
  1750. } );
  1751. }
  1752. function wait( timeout ) {
  1753. return () => new Promise( resolve => {
  1754. setTimeout( () => {
  1755. resolve();
  1756. }, timeout );
  1757. } );
  1758. }
  1759. function waitForDebounce() {
  1760. return wait( 180 )();
  1761. }
  1762. function fireKeyDownEvent( options ) {
  1763. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  1764. const eventData = new DomEventData( editingView.document, {
  1765. target: document.body
  1766. }, options );
  1767. editingView.document.fire( eventInfo, eventData );
  1768. }
  1769. function stubSelectionRects( rects ) {
  1770. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  1771. // Mock selection rect.
  1772. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  1773. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  1774. sinon.stub( domRange, 'getClientRects' )
  1775. .returns( rects );
  1776. return domRange;
  1777. } );
  1778. }
  1779. function expectChildViewsIsOnState( expectedState ) {
  1780. const childViews = [ ...mentionsView.items ].map( item => item.children.get( 0 ) );
  1781. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  1782. }
  1783. function assertCommandOptions( commandOptions, marker, item ) {
  1784. expect( commandOptions ).to.have.property( 'marker', marker );
  1785. expect( commandOptions ).to.have.property( 'range' );
  1786. expect( commandOptions ).to.have.property( 'mention' );
  1787. const mentionForCommand = commandOptions.mention;
  1788. for ( const key of Object.keys( item ) ) {
  1789. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  1790. }
  1791. }
  1792. } );