mentionui.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  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. let feedCallbackStub, feedCallbackTimeout, feedCallbackCallTimes;
  717. beforeEach( () => {
  718. const issuesNumbers = [ '#100', '#101', '#102', '#103' ];
  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 show filtered results for matched text', () => {
  752. setData( model, '<paragraph>foo []</paragraph>' );
  753. model.change( writer => {
  754. writer.insertText( '#', doc.selection.getFirstPosition() );
  755. } );
  756. model.change( writer => {
  757. writer.insertText( '2', doc.selection.getFirstPosition() );
  758. } );
  759. return waitForDebounce()
  760. .then( () => {
  761. expect( panelView.isVisible ).to.be.true;
  762. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  763. expect( mentionsView.items ).to.have.length( 1 );
  764. } );
  765. } );
  766. it( 'should hide panel if no matched items', () => {
  767. setData( model, '<paragraph>foo []</paragraph>' );
  768. model.change( writer => {
  769. writer.insertText( '#', doc.selection.getFirstPosition() );
  770. } );
  771. return waitForDebounce()
  772. .then( () => expect( panelView.isVisible ).to.be.true )
  773. .then( () => {
  774. model.change( writer => {
  775. writer.insertText( 'x', doc.selection.getFirstPosition() );
  776. } );
  777. } )
  778. .then( waitForDebounce )
  779. .then( () => {
  780. expect( panelView.isVisible ).to.be.false;
  781. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  782. expect( mentionsView.items ).to.have.length( 0 );
  783. } );
  784. } );
  785. it( 'should hide panel when text was unmatched', () => {
  786. setData( model, '<paragraph>foo []</paragraph>' );
  787. model.change( writer => {
  788. writer.insertText( '#', doc.selection.getFirstPosition() );
  789. } );
  790. return waitForDebounce()
  791. .then( () => expect( panelView.isVisible ).to.be.true )
  792. .then( () => {
  793. model.change( writer => {
  794. const end = doc.selection.getFirstPosition();
  795. const start = end.getShiftedBy( -1 );
  796. writer.remove( writer.createRange( start, end ) );
  797. } );
  798. } )
  799. .then( waitForDebounce )
  800. .then( () => expect( panelView.isVisible ).to.be.false );
  801. } );
  802. it( 'should show panel debounced', () => {
  803. setData( model, '<paragraph>foo []</paragraph>' );
  804. model.change( writer => {
  805. writer.insertText( '#', doc.selection.getFirstPosition() );
  806. } );
  807. sinon.assert.notCalled( feedCallbackStub );
  808. return Promise.resolve()
  809. .then( wait( 20 ) )
  810. .then( () => {
  811. sinon.assert.notCalled( feedCallbackStub );
  812. model.change( writer => {
  813. writer.insertText( '1', doc.selection.getFirstPosition() );
  814. } );
  815. } )
  816. .then( wait( 20 ) )
  817. .then( () => {
  818. sinon.assert.notCalled( feedCallbackStub );
  819. model.change( writer => {
  820. writer.insertText( '0', doc.selection.getFirstPosition() );
  821. } );
  822. } )
  823. .then( waitForDebounce )
  824. .then( () => {
  825. sinon.assert.calledOnce( feedCallbackStub );
  826. // Should be called with all typed letters before debounce.
  827. sinon.assert.calledWithExactly( feedCallbackStub, '10' );
  828. expect( panelView.isVisible ).to.be.true;
  829. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  830. expect( mentionsView.items ).to.have.length( 4 );
  831. } );
  832. } );
  833. it( 'should discard requested feed if they came out of order', () => {
  834. setData( model, '<paragraph>foo []</paragraph>' );
  835. model.change( writer => {
  836. writer.insertText( '#', doc.selection.getFirstPosition() );
  837. } );
  838. sinon.assert.notCalled( feedCallbackStub );
  839. const panelShowSpy = sinon.spy( panelView, 'show' );
  840. // Increase the response time to extend the debounce time out.
  841. feedCallbackTimeout = 300;
  842. return Promise.resolve()
  843. .then( wait( 20 ) )
  844. .then( () => {
  845. sinon.assert.notCalled( feedCallbackStub );
  846. model.change( writer => {
  847. writer.insertText( '1', doc.selection.getFirstPosition() );
  848. } );
  849. } )
  850. .then( waitForDebounce )
  851. .then( () => {
  852. sinon.assert.calledOnce( feedCallbackStub );
  853. sinon.assert.calledWithExactly( feedCallbackStub, '1' );
  854. expect( panelView.isVisible, 'panel is hidden' ).to.be.false;
  855. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  856. // Make second callback resolve before first.
  857. feedCallbackTimeout = 50;
  858. model.change( writer => {
  859. writer.insertText( '0', doc.selection.getFirstPosition() );
  860. } );
  861. } )
  862. .then( wait( 300 ) ) // Wait longer so the longer callback will be resolved.
  863. .then( () => {
  864. sinon.assert.calledTwice( feedCallbackStub );
  865. sinon.assert.calledWithExactly( feedCallbackStub.getCall( 1 ), '10' );
  866. sinon.assert.calledOnce( panelShowSpy );
  867. expect( feedCallbackCallTimes ).to.equal( 2 );
  868. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  869. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  870. expect( mentionsView.items ).to.have.length( 4 );
  871. } );
  872. } );
  873. it( 'should discard requested feed if mention UI is hidden', () => {
  874. setData( model, '<paragraph>foo []</paragraph>' );
  875. model.change( writer => {
  876. writer.insertText( '#', doc.selection.getFirstPosition() );
  877. } );
  878. sinon.assert.notCalled( feedCallbackStub );
  879. feedCallbackTimeout = 200;
  880. return Promise.resolve()
  881. .then( waitForDebounce )
  882. .then( () => {
  883. expect( panelView.isVisible ).to.be.false; // Should be still hidden;
  884. // Should be called with empty string.
  885. sinon.assert.calledWithExactly( feedCallbackStub, '' );
  886. model.change( writer => {
  887. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  888. } );
  889. } )
  890. .then( waitForDebounce )
  891. .then( () => {
  892. expect( panelView.isVisible ).to.be.false;
  893. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  894. } );
  895. } );
  896. it( 'should log warning if requested feed failed', () => {
  897. setData( model, '<paragraph>foo []</paragraph>' );
  898. feedCallbackStub.returns( Promise.reject( 'Request timeout' ) );
  899. const spy = sinon.spy( console, 'warn' );
  900. model.change( writer => {
  901. writer.insertText( '#', doc.selection.getFirstPosition() );
  902. } );
  903. return waitForDebounce()
  904. .then( () => {
  905. expect( panelView.isVisible, 'panel is hidden' ).to.be.false;
  906. expect( editor.model.markers.has( 'mention' ), 'there is no marker' ).to.be.false;
  907. sinon.assert.calledWithExactly( spy, 'mention-feed-callback-error: Could not obtain mention autocomplete feed.' );
  908. } );
  909. } );
  910. it( 'should not fail if marker was removed', () => {
  911. setData( model, '<paragraph>foo []</paragraph>' );
  912. const selectFirstMentionSpy = sinon.spy( mentionsView, 'selectFirst' );
  913. model.change( writer => {
  914. writer.insertText( '#', doc.selection.getFirstPosition() );
  915. } );
  916. sinon.assert.notCalled( feedCallbackStub );
  917. // Increase the response time to extend the debounce time out.
  918. feedCallbackTimeout = 500;
  919. return Promise.resolve()
  920. .then( waitForDebounce )
  921. .then( wait( 20 ) )
  922. .then( () => {
  923. model.change( writer => {
  924. writer.setSelection( doc.getRoot().getChild( 0 ), 2 );
  925. } );
  926. } )
  927. .then( wait( 20 ) )
  928. .then( () => {
  929. feedCallbackTimeout = 1000;
  930. model.change( writer => {
  931. writer.setSelection( doc.getRoot().getChild( 0 ), 'end' );
  932. } );
  933. } )
  934. .then( wait( 500 ) )
  935. .then( () => {
  936. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  937. // If there were any errors this will not get called.
  938. // The errors might come from unhandled promise rejections errors.
  939. sinon.assert.calledOnce( selectFirstMentionSpy );
  940. } );
  941. } );
  942. it( 'should not show panel if selection was moved during fetching a feed', () => {
  943. setData( model, '<paragraph>foo [#101] bar</paragraph><paragraph></paragraph>' );
  944. model.change( writer => {
  945. writer.setAttribute( 'mention', { id: '#101', _uid: 1234 }, doc.selection.getFirstRange() );
  946. } );
  947. // Increase the response time to extend the debounce time out.
  948. feedCallbackTimeout = 300;
  949. model.change( writer => {
  950. writer.setSelection( doc.getRoot().getChild( 1 ), 0 );
  951. writer.insertText( '#', doc.selection.getFirstPosition() );
  952. } );
  953. sinon.assert.notCalled( feedCallbackStub );
  954. return Promise.resolve()
  955. .then( waitForDebounce )
  956. .then( () => {
  957. sinon.assert.calledOnce( feedCallbackStub );
  958. model.change( writer => {
  959. writer.setSelection( doc.getRoot().getChild( 0 ), 6 );
  960. } );
  961. expect( panelView.isVisible ).to.be.false;
  962. } )
  963. .then( waitForDebounce )
  964. .then( wait( 20 ) )
  965. .then( () => {
  966. expect( panelView.isVisible ).to.be.false;
  967. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  968. } );
  969. } );
  970. } );
  971. function testOpeningPunctuationCharacter( character, skip = false ) {
  972. it( `should show panel for matched marker after a "${ character }" character`, function() {
  973. if ( skip ) {
  974. this.skip();
  975. }
  976. setData( model, '<paragraph>[] foo</paragraph>' );
  977. model.change( writer => {
  978. writer.insertText( character, doc.selection.getFirstPosition() );
  979. } );
  980. model.change( writer => {
  981. writer.insertText( '@', doc.selection.getFirstPosition() );
  982. } );
  983. return waitForDebounce()
  984. .then( () => {
  985. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  986. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  987. expect( mentionsView.items ).to.have.length( 5 );
  988. } );
  989. } );
  990. }
  991. } );
  992. describe( 'panel behavior', () => {
  993. it( 'should close the opened panel on esc', () => {
  994. return createClassicTestEditor( staticConfig )
  995. .then( () => {
  996. setData( model, '<paragraph>foo []</paragraph>' );
  997. model.change( writer => {
  998. writer.insertText( '@', doc.selection.getFirstPosition() );
  999. } );
  1000. } )
  1001. .then( waitForDebounce )
  1002. .then( () => {
  1003. expect( panelView.isVisible ).to.be.true;
  1004. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1005. fireKeyDownEvent( {
  1006. keyCode: keyCodes.esc,
  1007. preventDefault: sinon.spy(),
  1008. stopPropagation: sinon.spy()
  1009. } );
  1010. expect( panelView.isVisible ).to.be.false;
  1011. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1012. } );
  1013. } );
  1014. it( 'should close the opened panel when click outside the panel', () => {
  1015. return createClassicTestEditor( staticConfig )
  1016. .then( () => {
  1017. setData( model, '<paragraph>foo []</paragraph>' );
  1018. model.change( writer => {
  1019. writer.insertText( '@', doc.selection.getFirstPosition() );
  1020. } );
  1021. } )
  1022. .then( waitForDebounce )
  1023. .then( () => {
  1024. expect( panelView.isVisible ).to.be.true;
  1025. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1026. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  1027. expect( panelView.isVisible ).to.be.false;
  1028. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1029. } );
  1030. } );
  1031. it( 'should hide the panel on selection change', () => {
  1032. return createClassicTestEditor( staticConfig )
  1033. .then( () => {
  1034. setData( model, '<paragraph>foo []</paragraph>' );
  1035. model.change( writer => {
  1036. writer.insertText( '@', doc.selection.getFirstPosition() );
  1037. } );
  1038. } )
  1039. .then( waitForDebounce )
  1040. .then( () => {
  1041. expect( panelView.isVisible ).to.be.true;
  1042. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1043. model.change( writer => {
  1044. // Place position at the beginning of a paragraph.
  1045. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  1046. } );
  1047. expect( panelView.isVisible ).to.be.false;
  1048. expect( mentionsView.position ).to.be.undefined;
  1049. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1050. } );
  1051. } );
  1052. it( 'should hide the panel on selection change triggered by mouse click', () => {
  1053. return createClassicTestEditor( staticConfig )
  1054. .then( () => {
  1055. setData( model, '<paragraph>foo []</paragraph>' );
  1056. model.change( writer => {
  1057. writer.insertText( '@', doc.selection.getFirstPosition() );
  1058. } );
  1059. } )
  1060. .then( waitForDebounce )
  1061. .then( () => {
  1062. expect( panelView.isVisible ).to.be.true;
  1063. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1064. // This happens when user clicks outside the panel view and selection is changed.
  1065. // Two panel closing mechanisms are run:
  1066. // - clickOutsideHandler
  1067. // - unmatched text in text watcher
  1068. // which may fail when trying to remove mention marker twice.
  1069. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  1070. model.change( writer => {
  1071. // Place position at the beginning of a paragraph.
  1072. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  1073. } );
  1074. expect( panelView.isVisible ).to.be.false;
  1075. expect( mentionsView.position ).to.be.undefined;
  1076. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1077. } );
  1078. } );
  1079. describe( 'default list item', () => {
  1080. // Create map of expected feed items as objects as they will be stored internally.
  1081. const feedItems = staticConfig.feeds[ 0 ].feed.map( text => ( { text: `${ text }`, id: `${ text }` } ) );
  1082. beforeEach( () => {
  1083. return createClassicTestEditor( staticConfig );
  1084. } );
  1085. describe( 'on arrows', () => {
  1086. it( 'should cycle down on arrow down', () => {
  1087. setData( model, '<paragraph>foo []</paragraph>' );
  1088. model.change( writer => {
  1089. writer.insertText( '@', doc.selection.getFirstPosition() );
  1090. } );
  1091. return waitForDebounce()
  1092. .then( () => {
  1093. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1094. const keyEvtData = {
  1095. keyCode: keyCodes.arrowdown,
  1096. preventDefault: sinon.spy(),
  1097. stopPropagation: sinon.spy()
  1098. };
  1099. fireKeyDownEvent( keyEvtData );
  1100. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1101. fireKeyDownEvent( keyEvtData );
  1102. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1103. fireKeyDownEvent( keyEvtData );
  1104. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1105. fireKeyDownEvent( keyEvtData );
  1106. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1107. fireKeyDownEvent( keyEvtData );
  1108. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1109. } );
  1110. } );
  1111. it( 'should cycle up on arrow up', () => {
  1112. setData( model, '<paragraph>foo []</paragraph>' );
  1113. model.change( writer => {
  1114. writer.insertText( '@', doc.selection.getFirstPosition() );
  1115. } );
  1116. return waitForDebounce()
  1117. .then( () => {
  1118. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1119. const keyEvtData = {
  1120. keyCode: keyCodes.arrowup,
  1121. preventDefault: sinon.spy(),
  1122. stopPropagation: sinon.spy()
  1123. };
  1124. fireKeyDownEvent( keyEvtData );
  1125. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1126. fireKeyDownEvent( keyEvtData );
  1127. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1128. fireKeyDownEvent( keyEvtData );
  1129. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1130. fireKeyDownEvent( keyEvtData );
  1131. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1132. fireKeyDownEvent( keyEvtData );
  1133. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1134. } );
  1135. } );
  1136. it( 'should not cycle with only one item in the list', () => {
  1137. setData( model, '<paragraph>foo []</paragraph>' );
  1138. const keyDownEvtData = {
  1139. keyCode: keyCodes.arrowdown,
  1140. preventDefault: sinon.spy(),
  1141. stopPropagation: sinon.spy()
  1142. };
  1143. const keyUpEvtData = {
  1144. keyCode: keyCodes.arrowdown,
  1145. preventDefault: sinon.spy(),
  1146. stopPropagation: sinon.spy()
  1147. };
  1148. model.change( writer => {
  1149. writer.insertText( '@T', doc.selection.getFirstPosition() );
  1150. } );
  1151. return waitForDebounce()
  1152. .then( () => {
  1153. expectChildViewsIsOnState( [ true ] );
  1154. fireKeyDownEvent( keyDownEvtData );
  1155. expectChildViewsIsOnState( [ true ] );
  1156. fireKeyDownEvent( keyUpEvtData );
  1157. expectChildViewsIsOnState( [ true ] );
  1158. } );
  1159. } );
  1160. } );
  1161. describe( 'on "execute" keys', () => {
  1162. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  1163. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  1164. testExecuteKey( 'space', keyCodes.space, feedItems );
  1165. } );
  1166. } );
  1167. describe( 'default list item with custom feed', () => {
  1168. const issues = [
  1169. { id: '@Ted' },
  1170. { id: '@Barney' },
  1171. { id: '@Robin' },
  1172. { id: '@Lily' },
  1173. { id: '@Marshal' }
  1174. ];
  1175. beforeEach( () => {
  1176. return createClassicTestEditor( {
  1177. feeds: [
  1178. {
  1179. marker: '@',
  1180. feed: feedText => issues.filter( issue => issue.id.includes( feedText ) )
  1181. }
  1182. ]
  1183. } );
  1184. } );
  1185. it( 'should show panel for matched marker', () => {
  1186. setData( model, '<paragraph>foo []</paragraph>' );
  1187. model.change( writer => {
  1188. writer.insertText( '@', doc.selection.getFirstPosition() );
  1189. } );
  1190. return waitForDebounce()
  1191. .then( () => {
  1192. expect( panelView.isVisible ).to.be.true;
  1193. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1194. expect( mentionsView.items ).to.have.length( 5 );
  1195. } );
  1196. } );
  1197. } );
  1198. describe( 'custom list item (string)', () => {
  1199. const issues = [
  1200. { id: '@1002', title: 'Some bug in editor.' },
  1201. { id: '@1003', title: 'Introduce this feature.' },
  1202. { id: '@1004', title: 'Missing docs.' },
  1203. { id: '@1005', title: 'Another bug.' },
  1204. { id: '@1006', title: 'More bugs' }
  1205. ];
  1206. beforeEach( () => {
  1207. return createClassicTestEditor( {
  1208. feeds: [
  1209. {
  1210. marker: '@',
  1211. feed: issues,
  1212. itemRenderer: item => item.title
  1213. }
  1214. ]
  1215. } );
  1216. } );
  1217. it( 'should show panel for matched marker', () => {
  1218. setData( model, '<paragraph>foo []</paragraph>' );
  1219. model.change( writer => {
  1220. writer.insertText( '@', doc.selection.getFirstPosition() );
  1221. } );
  1222. return waitForDebounce()
  1223. .then( () => {
  1224. expect( panelView.isVisible ).to.be.true;
  1225. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1226. expect( mentionsView.items ).to.have.length( 5 );
  1227. } );
  1228. } );
  1229. describe( 'keys', () => {
  1230. describe( 'on arrows', () => {
  1231. it( 'should cycle down on arrow down', () => {
  1232. setData( model, '<paragraph>foo []</paragraph>' );
  1233. model.change( writer => {
  1234. writer.insertText( '@', doc.selection.getFirstPosition() );
  1235. } );
  1236. return waitForDebounce()
  1237. .then( () => {
  1238. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1239. const keyEvtData = {
  1240. keyCode: keyCodes.arrowdown,
  1241. preventDefault: sinon.spy(),
  1242. stopPropagation: sinon.spy()
  1243. };
  1244. fireKeyDownEvent( keyEvtData );
  1245. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1246. fireKeyDownEvent( keyEvtData );
  1247. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1248. fireKeyDownEvent( keyEvtData );
  1249. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1250. fireKeyDownEvent( keyEvtData );
  1251. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1252. fireKeyDownEvent( keyEvtData );
  1253. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1254. } );
  1255. } );
  1256. it( 'should cycle up on arrow up', () => {
  1257. setData( model, '<paragraph>foo []</paragraph>' );
  1258. model.change( writer => {
  1259. writer.insertText( '@', doc.selection.getFirstPosition() );
  1260. } );
  1261. return waitForDebounce()
  1262. .then( () => {
  1263. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1264. const keyEvtData = {
  1265. keyCode: keyCodes.arrowup,
  1266. preventDefault: sinon.spy(),
  1267. stopPropagation: sinon.spy()
  1268. };
  1269. fireKeyDownEvent( keyEvtData );
  1270. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1271. fireKeyDownEvent( keyEvtData );
  1272. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1273. fireKeyDownEvent( keyEvtData );
  1274. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1275. fireKeyDownEvent( keyEvtData );
  1276. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1277. fireKeyDownEvent( keyEvtData );
  1278. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1279. } );
  1280. } );
  1281. } );
  1282. describe( 'on "execute" keys', () => {
  1283. testExecuteKey( 'enter', keyCodes.enter, issues );
  1284. testExecuteKey( 'tab', keyCodes.tab, issues );
  1285. testExecuteKey( 'space', keyCodes.space, issues );
  1286. } );
  1287. } );
  1288. } );
  1289. describe( 'custom list item (DOM Element)', () => {
  1290. const issues = [
  1291. { id: '@1002', title: 'Some bug in editor.' },
  1292. { id: '@1003', title: 'Introduce this feature.' },
  1293. { id: '@1004', title: 'Missing docs.' },
  1294. { id: '@1005', title: 'Another bug.' },
  1295. { id: '@1006', title: 'More bugs' }
  1296. ];
  1297. beforeEach( () => {
  1298. return createClassicTestEditor( {
  1299. feeds: [
  1300. {
  1301. marker: '@',
  1302. feed: feedText => {
  1303. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  1304. },
  1305. itemRenderer: item => {
  1306. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  1307. span.innerHTML = `<span id="issue-${ item.id.slice( 1 ) }">@${ item.title }</span>`;
  1308. return span;
  1309. }
  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. describe( 'keys', () => {
  1327. describe( 'on arrows', () => {
  1328. it( 'should cycle down on arrow down', () => {
  1329. setData( model, '<paragraph>foo []</paragraph>' );
  1330. model.change( writer => {
  1331. writer.insertText( '@', doc.selection.getFirstPosition() );
  1332. } );
  1333. return waitForDebounce()
  1334. .then( () => {
  1335. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1336. const keyEvtData = {
  1337. keyCode: keyCodes.arrowdown,
  1338. preventDefault: sinon.spy(),
  1339. stopPropagation: sinon.spy()
  1340. };
  1341. fireKeyDownEvent( keyEvtData );
  1342. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1343. fireKeyDownEvent( keyEvtData );
  1344. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1345. fireKeyDownEvent( keyEvtData );
  1346. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1347. fireKeyDownEvent( keyEvtData );
  1348. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1349. fireKeyDownEvent( keyEvtData );
  1350. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1351. } );
  1352. } );
  1353. it( 'should cycle up on arrow up', () => {
  1354. setData( model, '<paragraph>foo []</paragraph>' );
  1355. model.change( writer => {
  1356. writer.insertText( '@', doc.selection.getFirstPosition() );
  1357. } );
  1358. return waitForDebounce()
  1359. .then( () => {
  1360. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1361. const keyEvtData = {
  1362. keyCode: keyCodes.arrowup,
  1363. preventDefault: sinon.spy(),
  1364. stopPropagation: sinon.spy()
  1365. };
  1366. fireKeyDownEvent( keyEvtData );
  1367. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1368. fireKeyDownEvent( keyEvtData );
  1369. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1370. fireKeyDownEvent( keyEvtData );
  1371. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1372. fireKeyDownEvent( keyEvtData );
  1373. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1374. fireKeyDownEvent( keyEvtData );
  1375. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1376. } );
  1377. } );
  1378. } );
  1379. describe( 'on "execute" keys', () => {
  1380. testExecuteKey( 'enter', keyCodes.enter, issues );
  1381. testExecuteKey( 'tab', keyCodes.tab, issues );
  1382. testExecuteKey( 'space', keyCodes.space, issues );
  1383. } );
  1384. describe( 'mouse', () => {
  1385. it( 'should execute selected button on mouse click', () => {
  1386. setData( model, '<paragraph>foo []</paragraph>' );
  1387. model.change( writer => {
  1388. writer.insertText( '@', doc.selection.getFirstPosition() );
  1389. } );
  1390. const command = editor.commands.get( 'mention' );
  1391. const spy = testUtils.sinon.spy( command, 'execute' );
  1392. return waitForDebounce()
  1393. .then( () => {
  1394. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1395. const element = panelView.element.querySelector( '#issue-1004' );
  1396. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  1397. sinon.assert.calledOnce( spy );
  1398. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1399. const item = issues[ 2 ];
  1400. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  1401. expect( commandOptions ).to.have.property( 'marker', '@' );
  1402. expect( commandOptions ).to.have.property( 'range' );
  1403. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1404. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1405. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1406. } );
  1407. } );
  1408. } );
  1409. } );
  1410. } );
  1411. describe( 'multiple feeds configuration', () => {
  1412. beforeEach( () => {
  1413. return createClassicTestEditor( {
  1414. feeds: [
  1415. {
  1416. marker: '@',
  1417. feed: [ '@a1', '@a2', '@a3' ]
  1418. },
  1419. {
  1420. marker: '$',
  1421. feed: [ '$a1', '$a2', '$a3', '$a4', '$a5' ]
  1422. }
  1423. ]
  1424. } );
  1425. } );
  1426. it( 'should show panel for matched marker', () => {
  1427. setData( model, '<paragraph>foo []</paragraph>' );
  1428. model.change( writer => {
  1429. writer.insertText( '@', doc.selection.getFirstPosition() );
  1430. } );
  1431. return waitForDebounce()
  1432. .then( () => {
  1433. expect( panelView.isVisible ).to.be.true;
  1434. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1435. expect( mentionsView.items ).to.have.length( 3 );
  1436. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1437. } )
  1438. .then( waitForDebounce )
  1439. .then( () => {
  1440. expect( panelView.isVisible ).to.be.false;
  1441. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1442. model.change( writer => {
  1443. writer.insertText( '$', doc.selection.getFirstPosition() );
  1444. } );
  1445. } )
  1446. .then( waitForDebounce )
  1447. .then( () => {
  1448. expect( panelView.isVisible ).to.be.true;
  1449. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1450. expect( mentionsView.items ).to.have.length( 5 );
  1451. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1452. } )
  1453. .then( waitForDebounce )
  1454. .then( () => {
  1455. expect( panelView.isVisible ).to.be.false;
  1456. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1457. model.change( writer => {
  1458. writer.insertText( '@', doc.selection.getFirstPosition() );
  1459. } );
  1460. } )
  1461. .then( waitForDebounce )
  1462. .then( () => {
  1463. expect( panelView.isVisible ).to.be.true;
  1464. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1465. expect( mentionsView.items ).to.have.length( 3 );
  1466. } );
  1467. } );
  1468. } );
  1469. function testExecuteKey( name, keyCode, feedItems ) {
  1470. it( 'should execute selected button on ' + name, () => {
  1471. setData( model, '<paragraph>foo []</paragraph>' );
  1472. model.change( writer => {
  1473. writer.insertText( '@', doc.selection.getFirstPosition() );
  1474. } );
  1475. const command = editor.commands.get( 'mention' );
  1476. const spy = testUtils.sinon.spy( command, 'execute' );
  1477. return waitForDebounce()
  1478. .then( () => {
  1479. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1480. fireKeyDownEvent( {
  1481. keyCode: keyCodes.arrowup,
  1482. preventDefault: sinon.spy(),
  1483. stopPropagation: sinon.spy()
  1484. } );
  1485. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1486. fireKeyDownEvent( {
  1487. keyCode,
  1488. preventDefault: sinon.spy(),
  1489. stopPropagation: sinon.spy()
  1490. } );
  1491. sinon.assert.calledOnce( spy );
  1492. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  1493. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1494. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1495. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  1496. } );
  1497. } );
  1498. it( 'should do nothing if panel is not visible on ' + name, () => {
  1499. setData( model, '<paragraph>foo []</paragraph>' );
  1500. model.change( writer => {
  1501. writer.insertText( '@', doc.selection.getFirstPosition() );
  1502. } );
  1503. const command = editor.commands.get( 'mention' );
  1504. const spy = testUtils.sinon.spy( command, 'execute' );
  1505. return waitForDebounce()
  1506. .then( () => {
  1507. expect( panelView.isVisible ).to.be.true;
  1508. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1509. fireKeyDownEvent( {
  1510. keyCode: keyCodes.esc,
  1511. preventDefault: sinon.spy(),
  1512. stopPropagation: sinon.spy()
  1513. } );
  1514. expect( panelView.isVisible ).to.be.false;
  1515. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1516. fireKeyDownEvent( {
  1517. keyCode,
  1518. preventDefault: sinon.spy(),
  1519. stopPropagation: sinon.spy()
  1520. } );
  1521. sinon.assert.notCalled( spy );
  1522. expect( panelView.isVisible ).to.be.false;
  1523. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1524. } );
  1525. } );
  1526. }
  1527. } );
  1528. describe( 'execute', () => {
  1529. beforeEach( () => createClassicTestEditor( staticConfig ) );
  1530. it( 'should call the mention command with proper options', () => {
  1531. setData( model, '<paragraph>foo []</paragraph>' );
  1532. model.change( writer => {
  1533. writer.insertText( '@', doc.selection.getFirstPosition() );
  1534. } );
  1535. const command = editor.commands.get( 'mention' );
  1536. const spy = testUtils.sinon.spy( command, 'execute' );
  1537. return waitForDebounce()
  1538. .then( () => {
  1539. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1540. sinon.assert.calledOnce( spy );
  1541. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1542. assertCommandOptions( commandOptions, '@', { id: '@Barney', text: '@Barney' } );
  1543. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1544. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1545. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1546. } );
  1547. } );
  1548. it( 'should hide panel on execute', () => {
  1549. setData( model, '<paragraph>foo []</paragraph>' );
  1550. model.change( writer => {
  1551. writer.insertText( '@', doc.selection.getFirstPosition() );
  1552. } );
  1553. return waitForDebounce()
  1554. .then( () => {
  1555. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1556. expect( panelView.isVisible ).to.be.false;
  1557. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1558. } );
  1559. } );
  1560. it( 'should focus view after command execution', () => {
  1561. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  1562. setData( model, '<paragraph>foo []</paragraph>' );
  1563. model.change( writer => {
  1564. writer.insertText( '@', doc.selection.getFirstPosition() );
  1565. } );
  1566. return waitForDebounce()
  1567. .then( () => {
  1568. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1569. sinon.assert.calledOnce( focusSpy );
  1570. } );
  1571. } );
  1572. } );
  1573. function createClassicTestEditor( mentionConfig ) {
  1574. return ClassicTestEditor
  1575. .create( editorElement, {
  1576. plugins: [ Paragraph, MentionEditing, MentionUI ],
  1577. mention: mentionConfig
  1578. } )
  1579. .then( newEditor => {
  1580. editor = newEditor;
  1581. model = editor.model;
  1582. doc = model.document;
  1583. editingView = editor.editing.view;
  1584. mentionUI = editor.plugins.get( MentionUI );
  1585. panelView = editor.plugins.get( ContextualBalloon ).view;
  1586. mentionsView = mentionUI._mentionsView;
  1587. } );
  1588. }
  1589. function wait( timeout ) {
  1590. return () => new Promise( resolve => {
  1591. setTimeout( () => {
  1592. resolve();
  1593. }, timeout );
  1594. } );
  1595. }
  1596. function waitForDebounce() {
  1597. return wait( 180 )();
  1598. }
  1599. function fireKeyDownEvent( options ) {
  1600. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  1601. const eventData = new DomEventData( editingView.document, {
  1602. target: document.body
  1603. }, options );
  1604. editingView.document.fire( eventInfo, eventData );
  1605. }
  1606. function stubSelectionRects( rects ) {
  1607. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  1608. // Mock selection rect.
  1609. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  1610. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  1611. sinon.stub( domRange, 'getClientRects' )
  1612. .returns( rects );
  1613. return domRange;
  1614. } );
  1615. }
  1616. function expectChildViewsIsOnState( expectedState ) {
  1617. const childViews = [ ...mentionsView.items ].map( item => item.children.get( 0 ) );
  1618. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  1619. }
  1620. function assertCommandOptions( commandOptions, marker, item ) {
  1621. expect( commandOptions ).to.have.property( 'marker', marker );
  1622. expect( commandOptions ).to.have.property( 'range' );
  1623. expect( commandOptions ).to.have.property( 'mention' );
  1624. const mentionForCommand = commandOptions.mention;
  1625. for ( const key of Object.keys( item ) ) {
  1626. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  1627. }
  1628. }
  1629. } );