8
0

mentionui.js 65 KB

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