mentionui.js 54 KB

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