mentionui.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 originalPunctuationSupport = featureDetection.isPunctuationGroupSupported;
  364. before( () => {
  365. featureDetection.isPunctuationGroupSupported = 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.isPunctuationGroupSupported = originalPunctuationSupport;
  376. } );
  377. it( 'returns a simplified RegExp for browsers not supporting Unicode punctuation groups', () => {
  378. featureDetection.isPunctuationGroupSupported = false;
  379. createRegExp( '@', 2 );
  380. sinon.assert.calledOnce( regExpStub );
  381. sinon.assert.calledWithExactly( regExpStub, '(^|[ \\(\\[{"\'])([@])([_a-zA-Z0-9À-ž]{2,}?)$', 'u' );
  382. } );
  383. it( 'returns a ES2018 RegExp for browsers supporting Unicode punctuation groups', () => {
  384. featureDetection.isPunctuationGroupSupported = true;
  385. createRegExp( '@', 2 );
  386. sinon.assert.calledOnce( regExpStub );
  387. sinon.assert.calledWithExactly( regExpStub, '(^|[ \\p{Ps}\\p{Pi}"\'])([@])([_a-zA-Z0-9À-ž]{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.isPunctuationGroupSupported );
  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. writer.setAttribute( 'mention', { id: '@Lily', _uid: 1234 }, doc.selection.getFirstRange() );
  473. } );
  474. model.change( writer => {
  475. writer.setSelection( doc.getRoot().getChild( 0 ), 7 );
  476. } );
  477. return waitForDebounce()
  478. .then( () => {
  479. expect( panelView.isVisible ).to.be.false;
  480. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  481. } );
  482. } );
  483. it( 'should not show panel when selection is at the end of a mention', () => {
  484. setData( model, '<paragraph>foo [@Lily] bar</paragraph>' );
  485. model.change( writer => {
  486. writer.setAttribute( 'mention', { id: '@Lily', _uid: 1234 }, doc.selection.getFirstRange() );
  487. } );
  488. model.change( writer => {
  489. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  490. } );
  491. return waitForDebounce()
  492. .then( () => {
  493. expect( panelView.isVisible ).to.be.false;
  494. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  495. } );
  496. } );
  497. it( 'should not show panel when selection is not collapsed', () => {
  498. setData( model, '<paragraph>foo []</paragraph>' );
  499. model.change( writer => {
  500. writer.insertText( '@', doc.selection.getFirstPosition() );
  501. } );
  502. return waitForDebounce()
  503. .then( () => {
  504. expect( panelView.isVisible ).to.be.true;
  505. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  506. model.change( () => {
  507. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  508. } );
  509. } )
  510. .then( waitForDebounce )
  511. .then( () => {
  512. expect( panelView.isVisible ).to.be.false;
  513. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  514. } );
  515. } );
  516. it( 'should not show panel when selection is changing (non-collapsed)', () => {
  517. setData( model, '<paragraph>foo []</paragraph>' );
  518. model.change( writer => {
  519. writer.insertText( '@', doc.selection.getFirstPosition() );
  520. } );
  521. return waitForDebounce()
  522. .then( () => {
  523. expect( panelView.isVisible ).to.be.true;
  524. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  525. model.change( () => {
  526. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  527. } );
  528. } )
  529. .then( waitForDebounce )
  530. .then( () => {
  531. expect( panelView.isVisible ).to.be.false;
  532. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  533. } )
  534. .then( () => {
  535. model.change( () => {
  536. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  537. } );
  538. } )
  539. .then( waitForDebounce )
  540. .then( () => {
  541. expect( panelView.isVisible ).to.be.false;
  542. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  543. } );
  544. } );
  545. it( 'should not show panel when selection is after existing mention', () => {
  546. setData( model, '<paragraph>foo [@Lily] bar[]</paragraph>' );
  547. model.change( writer => {
  548. writer.setAttribute( 'mention', { id: '@Lily', _uid: 1234 }, doc.selection.getFirstRange() );
  549. } );
  550. return waitForDebounce()
  551. .then( () => {
  552. expect( panelView.isVisible ).to.be.false;
  553. model.change( writer => {
  554. writer.setSelection( doc.getRoot().getChild( 0 ), 8 );
  555. } );
  556. } )
  557. .then( waitForDebounce )
  558. .then( () => {
  559. expect( panelView.isVisible ).to.be.false;
  560. } );
  561. } );
  562. it( 'should not show panel when selection moves inside existing mention', () => {
  563. setData( model, '<paragraph>foo [@Lily] bar</paragraph>' );
  564. model.change( writer => {
  565. writer.setAttribute( 'mention', { id: '@Lily', _uid: 1234 }, doc.selection.getFirstRange() );
  566. } );
  567. return waitForDebounce()
  568. .then( () => {
  569. model.change( writer => {
  570. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  571. } );
  572. } )
  573. .then( waitForDebounce )
  574. .then( () => {
  575. expect( panelView.isVisible ).to.be.false;
  576. model.change( writer => {
  577. writer.setSelection( doc.getRoot().getChild( 0 ), 8 );
  578. } );
  579. } )
  580. .then( waitForDebounce )
  581. .then( () => {
  582. expect( panelView.isVisible ).to.be.false;
  583. } );
  584. } );
  585. it( 'should show filtered results for matched text', () => {
  586. setData( model, '<paragraph>foo []</paragraph>' );
  587. model.change( writer => {
  588. writer.insertText( '@', doc.selection.getFirstPosition() );
  589. } );
  590. model.change( writer => {
  591. writer.insertText( 'T', doc.selection.getFirstPosition() );
  592. } );
  593. return waitForDebounce()
  594. .then( () => {
  595. expect( panelView.isVisible ).to.be.true;
  596. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  597. expect( mentionsView.items ).to.have.length( 1 );
  598. } );
  599. } );
  600. it( 'should focus the first item in panel', () => {
  601. setData( model, '<paragraph>foo []</paragraph>' );
  602. model.change( writer => {
  603. writer.insertText( '@', doc.selection.getFirstPosition() );
  604. } );
  605. return waitForDebounce()
  606. .then( () => {
  607. const button = mentionsView.items.get( 0 ).children.get( 0 );
  608. expect( button.isOn ).to.be.true;
  609. } );
  610. } );
  611. it( 'should hide panel if no matched items', () => {
  612. setData( model, '<paragraph>foo []</paragraph>' );
  613. model.change( writer => {
  614. writer.insertText( '@', doc.selection.getFirstPosition() );
  615. } );
  616. return waitForDebounce()
  617. .then( () => expect( panelView.isVisible ).to.be.true )
  618. .then( () => {
  619. model.change( writer => {
  620. writer.insertText( 'x', doc.selection.getFirstPosition() );
  621. } );
  622. } )
  623. .then( waitForDebounce )
  624. .then( () => {
  625. expect( panelView.isVisible ).to.be.false;
  626. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  627. expect( mentionsView.items ).to.have.length( 0 );
  628. } );
  629. } );
  630. it( 'should hide panel when text was unmatched', () => {
  631. setData( model, '<paragraph>foo []</paragraph>' );
  632. model.change( writer => {
  633. writer.insertText( '@', doc.selection.getFirstPosition() );
  634. } );
  635. return waitForDebounce()
  636. .then( () => expect( panelView.isVisible ).to.be.true )
  637. .then( () => {
  638. model.change( writer => {
  639. const end = doc.selection.getFirstPosition();
  640. const start = end.getShiftedBy( -1 );
  641. writer.remove( writer.createRange( start, end ) );
  642. } );
  643. } )
  644. .then( waitForDebounce )
  645. .then( () => expect( panelView.isVisible ).to.be.false );
  646. } );
  647. } );
  648. describe( 'asynchronous list with custom trigger', () => {
  649. beforeEach( () => {
  650. const issuesNumbers = [ '#100', '#101', '#102', '#103' ];
  651. return createClassicTestEditor( {
  652. feeds: [
  653. {
  654. marker: '#',
  655. feed: feedText => {
  656. return new Promise( resolve => {
  657. setTimeout( () => {
  658. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  659. }, 20 );
  660. } );
  661. }
  662. }
  663. ]
  664. } );
  665. } );
  666. it( 'should show panel for matched marker', () => {
  667. setData( model, '<paragraph>foo []</paragraph>' );
  668. model.change( writer => {
  669. writer.insertText( '#', doc.selection.getFirstPosition() );
  670. } );
  671. return waitForDebounce()
  672. .then( () => {
  673. expect( panelView.isVisible ).to.be.true;
  674. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  675. expect( mentionsView.items ).to.have.length( 4 );
  676. } );
  677. } );
  678. it( 'should show filtered results for matched text', () => {
  679. setData( model, '<paragraph>foo []</paragraph>' );
  680. model.change( writer => {
  681. writer.insertText( '#', doc.selection.getFirstPosition() );
  682. } );
  683. model.change( writer => {
  684. writer.insertText( '2', doc.selection.getFirstPosition() );
  685. } );
  686. return waitForDebounce()
  687. .then( () => {
  688. expect( panelView.isVisible ).to.be.true;
  689. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  690. expect( mentionsView.items ).to.have.length( 1 );
  691. } );
  692. } );
  693. it( 'should hide panel if no matched items', () => {
  694. setData( model, '<paragraph>foo []</paragraph>' );
  695. model.change( writer => {
  696. writer.insertText( '#', doc.selection.getFirstPosition() );
  697. } );
  698. return waitForDebounce()
  699. .then( () => expect( panelView.isVisible ).to.be.true )
  700. .then( () => {
  701. model.change( writer => {
  702. writer.insertText( 'x', doc.selection.getFirstPosition() );
  703. } );
  704. } )
  705. .then( waitForDebounce )
  706. .then( () => {
  707. expect( panelView.isVisible ).to.be.false;
  708. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  709. expect( mentionsView.items ).to.have.length( 0 );
  710. } );
  711. } );
  712. it( 'should hide panel when text was unmatched', () => {
  713. setData( model, '<paragraph>foo []</paragraph>' );
  714. model.change( writer => {
  715. writer.insertText( '#', doc.selection.getFirstPosition() );
  716. } );
  717. return waitForDebounce()
  718. .then( () => expect( panelView.isVisible ).to.be.true )
  719. .then( () => {
  720. model.change( writer => {
  721. const end = doc.selection.getFirstPosition();
  722. const start = end.getShiftedBy( -1 );
  723. writer.remove( writer.createRange( start, end ) );
  724. } );
  725. } )
  726. .then( waitForDebounce )
  727. .then( () => expect( panelView.isVisible ).to.be.false );
  728. } );
  729. } );
  730. function testOpeningPunctuationCharacter( character, skip = false ) {
  731. it( `should show panel for matched marker after a "${ character }" character`, function() {
  732. if ( skip ) {
  733. this.skip();
  734. }
  735. setData( model, '<paragraph>[] foo</paragraph>' );
  736. model.change( writer => {
  737. writer.insertText( character, doc.selection.getFirstPosition() );
  738. } );
  739. model.change( writer => {
  740. writer.insertText( '@', doc.selection.getFirstPosition() );
  741. } );
  742. return waitForDebounce()
  743. .then( () => {
  744. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  745. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  746. expect( mentionsView.items ).to.have.length( 5 );
  747. } );
  748. } );
  749. }
  750. } );
  751. describe( 'panel behavior', () => {
  752. it( 'should close the opened panel on esc', () => {
  753. return createClassicTestEditor( staticConfig )
  754. .then( () => {
  755. setData( model, '<paragraph>foo []</paragraph>' );
  756. model.change( writer => {
  757. writer.insertText( '@', doc.selection.getFirstPosition() );
  758. } );
  759. } )
  760. .then( waitForDebounce )
  761. .then( () => {
  762. expect( panelView.isVisible ).to.be.true;
  763. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  764. fireKeyDownEvent( {
  765. keyCode: keyCodes.esc,
  766. preventDefault: sinon.spy(),
  767. stopPropagation: sinon.spy()
  768. } );
  769. expect( panelView.isVisible ).to.be.false;
  770. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  771. } );
  772. } );
  773. it( 'should close the opened panel when click outside the panel', () => {
  774. return createClassicTestEditor( staticConfig )
  775. .then( () => {
  776. setData( model, '<paragraph>foo []</paragraph>' );
  777. model.change( writer => {
  778. writer.insertText( '@', doc.selection.getFirstPosition() );
  779. } );
  780. } )
  781. .then( waitForDebounce )
  782. .then( () => {
  783. expect( panelView.isVisible ).to.be.true;
  784. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  785. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  786. expect( panelView.isVisible ).to.be.false;
  787. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  788. } );
  789. } );
  790. it( 'should hide the panel on selection change', () => {
  791. return createClassicTestEditor( staticConfig )
  792. .then( () => {
  793. setData( model, '<paragraph>foo []</paragraph>' );
  794. model.change( writer => {
  795. writer.insertText( '@', doc.selection.getFirstPosition() );
  796. } );
  797. } )
  798. .then( waitForDebounce )
  799. .then( () => {
  800. expect( panelView.isVisible ).to.be.true;
  801. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  802. model.change( writer => {
  803. // Place position at the beginning of a paragraph.
  804. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  805. } );
  806. expect( panelView.isVisible ).to.be.false;
  807. expect( mentionsView.position ).to.be.undefined;
  808. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  809. } );
  810. } );
  811. it( 'should hide the panel on selection change triggered by mouse click', () => {
  812. return createClassicTestEditor( staticConfig )
  813. .then( () => {
  814. setData( model, '<paragraph>foo []</paragraph>' );
  815. model.change( writer => {
  816. writer.insertText( '@', doc.selection.getFirstPosition() );
  817. } );
  818. } )
  819. .then( waitForDebounce )
  820. .then( () => {
  821. expect( panelView.isVisible ).to.be.true;
  822. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  823. // This happens when user clicks outside the panel view and selection is changed.
  824. // Two panel closing mechanisms are run:
  825. // - clickOutsideHandler
  826. // - unmatched text in text watcher
  827. // which may fail when trying to remove mention marker twice.
  828. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  829. model.change( writer => {
  830. // Place position at the beginning of a paragraph.
  831. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  832. } );
  833. expect( panelView.isVisible ).to.be.false;
  834. expect( mentionsView.position ).to.be.undefined;
  835. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  836. } );
  837. } );
  838. describe( 'default list item', () => {
  839. // Create map of expected feed items as objects as they will be stored internally.
  840. const feedItems = staticConfig.feeds[ 0 ].feed.map( text => ( { text: `${ text }`, id: `${ text }` } ) );
  841. beforeEach( () => {
  842. return createClassicTestEditor( staticConfig );
  843. } );
  844. describe( 'on arrows', () => {
  845. it( 'should cycle down on arrow down', () => {
  846. setData( model, '<paragraph>foo []</paragraph>' );
  847. model.change( writer => {
  848. writer.insertText( '@', doc.selection.getFirstPosition() );
  849. } );
  850. return waitForDebounce()
  851. .then( () => {
  852. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  853. const keyEvtData = {
  854. keyCode: keyCodes.arrowdown,
  855. preventDefault: sinon.spy(),
  856. stopPropagation: sinon.spy()
  857. };
  858. fireKeyDownEvent( keyEvtData );
  859. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  860. fireKeyDownEvent( keyEvtData );
  861. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  862. fireKeyDownEvent( keyEvtData );
  863. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  864. fireKeyDownEvent( keyEvtData );
  865. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  866. fireKeyDownEvent( keyEvtData );
  867. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  868. } );
  869. } );
  870. it( 'should cycle up on arrow up', () => {
  871. setData( model, '<paragraph>foo []</paragraph>' );
  872. model.change( writer => {
  873. writer.insertText( '@', doc.selection.getFirstPosition() );
  874. } );
  875. return waitForDebounce()
  876. .then( () => {
  877. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  878. const keyEvtData = {
  879. keyCode: keyCodes.arrowup,
  880. preventDefault: sinon.spy(),
  881. stopPropagation: sinon.spy()
  882. };
  883. fireKeyDownEvent( keyEvtData );
  884. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  885. fireKeyDownEvent( keyEvtData );
  886. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  887. fireKeyDownEvent( keyEvtData );
  888. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  889. fireKeyDownEvent( keyEvtData );
  890. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  891. fireKeyDownEvent( keyEvtData );
  892. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  893. } );
  894. } );
  895. it( 'should not cycle with only one item in the list', () => {
  896. setData( model, '<paragraph>foo []</paragraph>' );
  897. const keyDownEvtData = {
  898. keyCode: keyCodes.arrowdown,
  899. preventDefault: sinon.spy(),
  900. stopPropagation: sinon.spy()
  901. };
  902. const keyUpEvtData = {
  903. keyCode: keyCodes.arrowdown,
  904. preventDefault: sinon.spy(),
  905. stopPropagation: sinon.spy()
  906. };
  907. model.change( writer => {
  908. writer.insertText( '@T', doc.selection.getFirstPosition() );
  909. } );
  910. return waitForDebounce()
  911. .then( () => {
  912. expectChildViewsIsOnState( [ true ] );
  913. fireKeyDownEvent( keyDownEvtData );
  914. expectChildViewsIsOnState( [ true ] );
  915. fireKeyDownEvent( keyUpEvtData );
  916. expectChildViewsIsOnState( [ true ] );
  917. } );
  918. } );
  919. } );
  920. describe( 'on "execute" keys', () => {
  921. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  922. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  923. testExecuteKey( 'space', keyCodes.space, feedItems );
  924. } );
  925. } );
  926. describe( 'default list item with custom feed', () => {
  927. const issues = [
  928. { id: '@Ted' },
  929. { id: '@Barney' },
  930. { id: '@Robin' },
  931. { id: '@Lily' },
  932. { id: '@Marshal' }
  933. ];
  934. beforeEach( () => {
  935. return createClassicTestEditor( {
  936. feeds: [
  937. {
  938. marker: '@',
  939. feed: feedText => issues.filter( issue => issue.id.includes( feedText ) )
  940. }
  941. ]
  942. } );
  943. } );
  944. it( 'should show panel for matched marker', () => {
  945. setData( model, '<paragraph>foo []</paragraph>' );
  946. model.change( writer => {
  947. writer.insertText( '@', doc.selection.getFirstPosition() );
  948. } );
  949. return waitForDebounce()
  950. .then( () => {
  951. expect( panelView.isVisible ).to.be.true;
  952. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  953. expect( mentionsView.items ).to.have.length( 5 );
  954. } );
  955. } );
  956. } );
  957. describe( 'custom list item (string)', () => {
  958. const issues = [
  959. { id: '@1002', title: 'Some bug in editor.' },
  960. { id: '@1003', title: 'Introduce this feature.' },
  961. { id: '@1004', title: 'Missing docs.' },
  962. { id: '@1005', title: 'Another bug.' },
  963. { id: '@1006', title: 'More bugs' }
  964. ];
  965. beforeEach( () => {
  966. return createClassicTestEditor( {
  967. feeds: [
  968. {
  969. marker: '@',
  970. feed: issues,
  971. itemRenderer: item => item.title
  972. }
  973. ]
  974. } );
  975. } );
  976. it( 'should show panel for matched marker', () => {
  977. setData( model, '<paragraph>foo []</paragraph>' );
  978. model.change( writer => {
  979. writer.insertText( '@', doc.selection.getFirstPosition() );
  980. } );
  981. return waitForDebounce()
  982. .then( () => {
  983. expect( panelView.isVisible ).to.be.true;
  984. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  985. expect( mentionsView.items ).to.have.length( 5 );
  986. } );
  987. } );
  988. describe( 'keys', () => {
  989. describe( 'on arrows', () => {
  990. it( 'should cycle down on arrow down', () => {
  991. setData( model, '<paragraph>foo []</paragraph>' );
  992. model.change( writer => {
  993. writer.insertText( '@', doc.selection.getFirstPosition() );
  994. } );
  995. return waitForDebounce()
  996. .then( () => {
  997. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  998. const keyEvtData = {
  999. keyCode: keyCodes.arrowdown,
  1000. preventDefault: sinon.spy(),
  1001. stopPropagation: sinon.spy()
  1002. };
  1003. fireKeyDownEvent( keyEvtData );
  1004. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1005. fireKeyDownEvent( keyEvtData );
  1006. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1007. fireKeyDownEvent( keyEvtData );
  1008. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1009. fireKeyDownEvent( keyEvtData );
  1010. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1011. fireKeyDownEvent( keyEvtData );
  1012. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1013. } );
  1014. } );
  1015. it( 'should cycle up on arrow up', () => {
  1016. setData( model, '<paragraph>foo []</paragraph>' );
  1017. model.change( writer => {
  1018. writer.insertText( '@', doc.selection.getFirstPosition() );
  1019. } );
  1020. return waitForDebounce()
  1021. .then( () => {
  1022. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1023. const keyEvtData = {
  1024. keyCode: keyCodes.arrowup,
  1025. preventDefault: sinon.spy(),
  1026. stopPropagation: sinon.spy()
  1027. };
  1028. fireKeyDownEvent( keyEvtData );
  1029. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1030. fireKeyDownEvent( keyEvtData );
  1031. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1032. fireKeyDownEvent( keyEvtData );
  1033. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1034. fireKeyDownEvent( keyEvtData );
  1035. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1036. fireKeyDownEvent( keyEvtData );
  1037. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1038. } );
  1039. } );
  1040. } );
  1041. describe( 'on "execute" keys', () => {
  1042. testExecuteKey( 'enter', keyCodes.enter, issues );
  1043. testExecuteKey( 'tab', keyCodes.tab, issues );
  1044. testExecuteKey( 'space', keyCodes.space, issues );
  1045. } );
  1046. } );
  1047. } );
  1048. describe( 'custom list item (DOM Element)', () => {
  1049. const issues = [
  1050. { id: '@1002', title: 'Some bug in editor.' },
  1051. { id: '@1003', title: 'Introduce this feature.' },
  1052. { id: '@1004', title: 'Missing docs.' },
  1053. { id: '@1005', title: 'Another bug.' },
  1054. { id: '@1006', title: 'More bugs' }
  1055. ];
  1056. beforeEach( () => {
  1057. return createClassicTestEditor( {
  1058. feeds: [
  1059. {
  1060. marker: '@',
  1061. feed: feedText => {
  1062. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  1063. },
  1064. itemRenderer: item => {
  1065. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  1066. span.innerHTML = `<span id="issue-${ item.id.slice( 1 ) }">@${ item.title }</span>`;
  1067. return span;
  1068. }
  1069. }
  1070. ]
  1071. } );
  1072. } );
  1073. it( 'should show panel for matched marker', () => {
  1074. setData( model, '<paragraph>foo []</paragraph>' );
  1075. model.change( writer => {
  1076. writer.insertText( '@', doc.selection.getFirstPosition() );
  1077. } );
  1078. return waitForDebounce()
  1079. .then( () => {
  1080. expect( panelView.isVisible ).to.be.true;
  1081. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1082. expect( mentionsView.items ).to.have.length( 5 );
  1083. } );
  1084. } );
  1085. describe( 'keys', () => {
  1086. describe( 'on arrows', () => {
  1087. it( 'should cycle down on arrow down', () => {
  1088. setData( model, '<paragraph>foo []</paragraph>' );
  1089. model.change( writer => {
  1090. writer.insertText( '@', doc.selection.getFirstPosition() );
  1091. } );
  1092. return waitForDebounce()
  1093. .then( () => {
  1094. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1095. const keyEvtData = {
  1096. keyCode: keyCodes.arrowdown,
  1097. preventDefault: sinon.spy(),
  1098. stopPropagation: sinon.spy()
  1099. };
  1100. fireKeyDownEvent( keyEvtData );
  1101. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1102. fireKeyDownEvent( keyEvtData );
  1103. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1104. fireKeyDownEvent( keyEvtData );
  1105. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1106. fireKeyDownEvent( keyEvtData );
  1107. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1108. fireKeyDownEvent( keyEvtData );
  1109. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1110. } );
  1111. } );
  1112. it( 'should cycle up on arrow up', () => {
  1113. setData( model, '<paragraph>foo []</paragraph>' );
  1114. model.change( writer => {
  1115. writer.insertText( '@', doc.selection.getFirstPosition() );
  1116. } );
  1117. return waitForDebounce()
  1118. .then( () => {
  1119. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1120. const keyEvtData = {
  1121. keyCode: keyCodes.arrowup,
  1122. preventDefault: sinon.spy(),
  1123. stopPropagation: sinon.spy()
  1124. };
  1125. fireKeyDownEvent( keyEvtData );
  1126. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1127. fireKeyDownEvent( keyEvtData );
  1128. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1129. fireKeyDownEvent( keyEvtData );
  1130. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1131. fireKeyDownEvent( keyEvtData );
  1132. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1133. fireKeyDownEvent( keyEvtData );
  1134. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1135. } );
  1136. } );
  1137. } );
  1138. describe( 'on "execute" keys', () => {
  1139. testExecuteKey( 'enter', keyCodes.enter, issues );
  1140. testExecuteKey( 'tab', keyCodes.tab, issues );
  1141. testExecuteKey( 'space', keyCodes.space, issues );
  1142. } );
  1143. describe( 'mouse', () => {
  1144. it( 'should execute selected button on mouse click', () => {
  1145. setData( model, '<paragraph>foo []</paragraph>' );
  1146. model.change( writer => {
  1147. writer.insertText( '@', doc.selection.getFirstPosition() );
  1148. } );
  1149. const command = editor.commands.get( 'mention' );
  1150. const spy = testUtils.sinon.spy( command, 'execute' );
  1151. return waitForDebounce()
  1152. .then( () => {
  1153. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1154. const element = panelView.element.querySelector( '#issue-1004' );
  1155. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  1156. sinon.assert.calledOnce( spy );
  1157. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1158. const item = issues[ 2 ];
  1159. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  1160. expect( commandOptions ).to.have.property( 'marker', '@' );
  1161. expect( commandOptions ).to.have.property( 'range' );
  1162. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1163. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1164. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1165. } );
  1166. } );
  1167. } );
  1168. } );
  1169. } );
  1170. describe( 'multiple feeds configuration', () => {
  1171. beforeEach( () => {
  1172. return createClassicTestEditor( {
  1173. feeds: [
  1174. {
  1175. marker: '@',
  1176. feed: [ '@a1', '@a2', '@a3' ]
  1177. },
  1178. {
  1179. marker: '$',
  1180. feed: [ '$a1', '$a2', '$a3', '$a4', '$a5' ]
  1181. }
  1182. ]
  1183. } );
  1184. } );
  1185. it( 'should show panel for matched marker', () => {
  1186. setData( model, '<paragraph>foo []</paragraph>' );
  1187. model.change( writer => {
  1188. writer.insertText( '@', doc.selection.getFirstPosition() );
  1189. } );
  1190. return waitForDebounce()
  1191. .then( () => {
  1192. expect( panelView.isVisible ).to.be.true;
  1193. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1194. expect( mentionsView.items ).to.have.length( 3 );
  1195. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1196. } )
  1197. .then( waitForDebounce )
  1198. .then( () => {
  1199. expect( panelView.isVisible ).to.be.false;
  1200. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1201. model.change( writer => {
  1202. writer.insertText( '$', doc.selection.getFirstPosition() );
  1203. } );
  1204. } )
  1205. .then( waitForDebounce )
  1206. .then( () => {
  1207. expect( panelView.isVisible ).to.be.true;
  1208. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1209. expect( mentionsView.items ).to.have.length( 5 );
  1210. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1211. } )
  1212. .then( waitForDebounce )
  1213. .then( () => {
  1214. expect( panelView.isVisible ).to.be.false;
  1215. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1216. model.change( writer => {
  1217. writer.insertText( '@', doc.selection.getFirstPosition() );
  1218. } );
  1219. } )
  1220. .then( waitForDebounce )
  1221. .then( () => {
  1222. expect( panelView.isVisible ).to.be.true;
  1223. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1224. expect( mentionsView.items ).to.have.length( 3 );
  1225. } );
  1226. } );
  1227. } );
  1228. function testExecuteKey( name, keyCode, feedItems ) {
  1229. it( 'should execute selected button on ' + name, () => {
  1230. setData( model, '<paragraph>foo []</paragraph>' );
  1231. model.change( writer => {
  1232. writer.insertText( '@', doc.selection.getFirstPosition() );
  1233. } );
  1234. const command = editor.commands.get( 'mention' );
  1235. const spy = testUtils.sinon.spy( command, 'execute' );
  1236. return waitForDebounce()
  1237. .then( () => {
  1238. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1239. fireKeyDownEvent( {
  1240. keyCode: keyCodes.arrowup,
  1241. preventDefault: sinon.spy(),
  1242. stopPropagation: sinon.spy()
  1243. } );
  1244. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1245. fireKeyDownEvent( {
  1246. keyCode,
  1247. preventDefault: sinon.spy(),
  1248. stopPropagation: sinon.spy()
  1249. } );
  1250. sinon.assert.calledOnce( spy );
  1251. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  1252. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1253. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1254. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  1255. } );
  1256. } );
  1257. it( 'should do nothing if panel is not visible on ' + name, () => {
  1258. setData( model, '<paragraph>foo []</paragraph>' );
  1259. model.change( writer => {
  1260. writer.insertText( '@', doc.selection.getFirstPosition() );
  1261. } );
  1262. const command = editor.commands.get( 'mention' );
  1263. const spy = testUtils.sinon.spy( command, 'execute' );
  1264. return waitForDebounce()
  1265. .then( () => {
  1266. expect( panelView.isVisible ).to.be.true;
  1267. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1268. fireKeyDownEvent( {
  1269. keyCode: keyCodes.esc,
  1270. preventDefault: sinon.spy(),
  1271. stopPropagation: sinon.spy()
  1272. } );
  1273. expect( panelView.isVisible ).to.be.false;
  1274. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1275. fireKeyDownEvent( {
  1276. keyCode,
  1277. preventDefault: sinon.spy(),
  1278. stopPropagation: sinon.spy()
  1279. } );
  1280. sinon.assert.notCalled( spy );
  1281. expect( panelView.isVisible ).to.be.false;
  1282. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1283. } );
  1284. } );
  1285. }
  1286. } );
  1287. describe( 'execute', () => {
  1288. beforeEach( () => createClassicTestEditor( staticConfig ) );
  1289. it( 'should call the mention command with proper options', () => {
  1290. setData( model, '<paragraph>foo []</paragraph>' );
  1291. model.change( writer => {
  1292. writer.insertText( '@', doc.selection.getFirstPosition() );
  1293. } );
  1294. const command = editor.commands.get( 'mention' );
  1295. const spy = testUtils.sinon.spy( command, 'execute' );
  1296. return waitForDebounce()
  1297. .then( () => {
  1298. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1299. sinon.assert.calledOnce( spy );
  1300. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1301. assertCommandOptions( commandOptions, '@', { id: '@Barney', text: '@Barney' } );
  1302. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1303. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1304. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1305. } );
  1306. } );
  1307. it( 'should hide panel on execute', () => {
  1308. setData( model, '<paragraph>foo []</paragraph>' );
  1309. model.change( writer => {
  1310. writer.insertText( '@', doc.selection.getFirstPosition() );
  1311. } );
  1312. return waitForDebounce()
  1313. .then( () => {
  1314. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1315. expect( panelView.isVisible ).to.be.false;
  1316. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1317. } );
  1318. } );
  1319. it( 'should focus view after command execution', () => {
  1320. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  1321. setData( model, '<paragraph>foo []</paragraph>' );
  1322. model.change( writer => {
  1323. writer.insertText( '@', doc.selection.getFirstPosition() );
  1324. } );
  1325. return waitForDebounce()
  1326. .then( () => {
  1327. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1328. sinon.assert.calledOnce( focusSpy );
  1329. } );
  1330. } );
  1331. } );
  1332. function createClassicTestEditor( mentionConfig ) {
  1333. return ClassicTestEditor
  1334. .create( editorElement, {
  1335. plugins: [ Paragraph, MentionEditing, MentionUI ],
  1336. mention: mentionConfig
  1337. } )
  1338. .then( newEditor => {
  1339. editor = newEditor;
  1340. model = editor.model;
  1341. doc = model.document;
  1342. editingView = editor.editing.view;
  1343. mentionUI = editor.plugins.get( MentionUI );
  1344. panelView = editor.plugins.get( ContextualBalloon ).view;
  1345. mentionsView = mentionUI._mentionsView;
  1346. } );
  1347. }
  1348. function waitForDebounce() {
  1349. return new Promise( resolve => {
  1350. setTimeout( () => {
  1351. resolve();
  1352. }, 50 );
  1353. } );
  1354. }
  1355. function fireKeyDownEvent( options ) {
  1356. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  1357. const eventData = new DomEventData( editingView.document, {
  1358. target: document.body
  1359. }, options );
  1360. editingView.document.fire( eventInfo, eventData );
  1361. }
  1362. function stubSelectionRects( rects ) {
  1363. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  1364. // Mock selection rect.
  1365. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  1366. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  1367. sinon.stub( domRange, 'getClientRects' )
  1368. .returns( rects );
  1369. return domRange;
  1370. } );
  1371. }
  1372. function expectChildViewsIsOnState( expectedState ) {
  1373. const childViews = [ ...mentionsView.items ].map( item => item.children.get( 0 ) );
  1374. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  1375. }
  1376. function assertCommandOptions( commandOptions, marker, item ) {
  1377. expect( commandOptions ).to.have.property( 'marker', marker );
  1378. expect( commandOptions ).to.have.property( 'range' );
  1379. expect( commandOptions ).to.have.property( 'mention' );
  1380. const mentionForCommand = commandOptions.mention;
  1381. for ( const key of Object.keys( item ) ) {
  1382. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  1383. }
  1384. }
  1385. } );