mentionui.js 53 KB

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