8
0

mentionui.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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. 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( 'unicode', () => {
  649. beforeEach( () => {
  650. return createClassicTestEditor( {
  651. feeds: [
  652. {
  653. // Always return 5 items
  654. feed: () => [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ],
  655. marker: '@'
  656. }
  657. ]
  658. } );
  659. } );
  660. it( 'should open panel for unicode character ב', function() {
  661. if ( !featureDetection.isUnicodeGroupSupported ) {
  662. this.skip();
  663. }
  664. setData( model, '<paragraph>foo []</paragraph>' );
  665. model.change( writer => {
  666. writer.insertText( '@ב', doc.selection.getFirstPosition() );
  667. } );
  668. return waitForDebounce()
  669. .then( () => {
  670. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  671. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  672. expect( mentionsView.items ).to.have.length( 5 );
  673. } );
  674. } );
  675. } );
  676. describe( 'asynchronous list with custom trigger', () => {
  677. beforeEach( () => {
  678. const issuesNumbers = [ '#100', '#101', '#102', '#103' ];
  679. return createClassicTestEditor( {
  680. feeds: [
  681. {
  682. marker: '#',
  683. feed: feedText => {
  684. return new Promise( resolve => {
  685. setTimeout( () => {
  686. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  687. }, 20 );
  688. } );
  689. }
  690. }
  691. ]
  692. } );
  693. } );
  694. it( 'should show panel for matched marker', () => {
  695. setData( model, '<paragraph>foo []</paragraph>' );
  696. model.change( writer => {
  697. writer.insertText( '#', doc.selection.getFirstPosition() );
  698. } );
  699. return waitForDebounce()
  700. .then( () => {
  701. expect( panelView.isVisible ).to.be.true;
  702. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  703. expect( mentionsView.items ).to.have.length( 4 );
  704. } );
  705. } );
  706. it( 'should show filtered results for matched text', () => {
  707. setData( model, '<paragraph>foo []</paragraph>' );
  708. model.change( writer => {
  709. writer.insertText( '#', doc.selection.getFirstPosition() );
  710. } );
  711. model.change( writer => {
  712. writer.insertText( '2', doc.selection.getFirstPosition() );
  713. } );
  714. return waitForDebounce()
  715. .then( () => {
  716. expect( panelView.isVisible ).to.be.true;
  717. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  718. expect( mentionsView.items ).to.have.length( 1 );
  719. } );
  720. } );
  721. it( 'should hide panel if no matched items', () => {
  722. setData( model, '<paragraph>foo []</paragraph>' );
  723. model.change( writer => {
  724. writer.insertText( '#', doc.selection.getFirstPosition() );
  725. } );
  726. return waitForDebounce()
  727. .then( () => expect( panelView.isVisible ).to.be.true )
  728. .then( () => {
  729. model.change( writer => {
  730. writer.insertText( 'x', doc.selection.getFirstPosition() );
  731. } );
  732. } )
  733. .then( waitForDebounce )
  734. .then( () => {
  735. expect( panelView.isVisible ).to.be.false;
  736. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  737. expect( mentionsView.items ).to.have.length( 0 );
  738. } );
  739. } );
  740. it( 'should hide panel when text was unmatched', () => {
  741. setData( model, '<paragraph>foo []</paragraph>' );
  742. model.change( writer => {
  743. writer.insertText( '#', doc.selection.getFirstPosition() );
  744. } );
  745. return waitForDebounce()
  746. .then( () => expect( panelView.isVisible ).to.be.true )
  747. .then( () => {
  748. model.change( writer => {
  749. const end = doc.selection.getFirstPosition();
  750. const start = end.getShiftedBy( -1 );
  751. writer.remove( writer.createRange( start, end ) );
  752. } );
  753. } )
  754. .then( waitForDebounce )
  755. .then( () => expect( panelView.isVisible ).to.be.false );
  756. } );
  757. } );
  758. function testOpeningPunctuationCharacter( character, skip = false ) {
  759. it( `should show panel for matched marker after a "${ character }" character`, function() {
  760. if ( skip ) {
  761. this.skip();
  762. }
  763. setData( model, '<paragraph>[] foo</paragraph>' );
  764. model.change( writer => {
  765. writer.insertText( character, doc.selection.getFirstPosition() );
  766. } );
  767. model.change( writer => {
  768. writer.insertText( '@', doc.selection.getFirstPosition() );
  769. } );
  770. return waitForDebounce()
  771. .then( () => {
  772. expect( panelView.isVisible, 'panel is visible' ).to.be.true;
  773. expect( editor.model.markers.has( 'mention' ), 'marker is inserted' ).to.be.true;
  774. expect( mentionsView.items ).to.have.length( 5 );
  775. } );
  776. } );
  777. }
  778. } );
  779. describe( 'panel behavior', () => {
  780. it( 'should close the opened panel on esc', () => {
  781. return createClassicTestEditor( staticConfig )
  782. .then( () => {
  783. setData( model, '<paragraph>foo []</paragraph>' );
  784. model.change( writer => {
  785. writer.insertText( '@', doc.selection.getFirstPosition() );
  786. } );
  787. } )
  788. .then( waitForDebounce )
  789. .then( () => {
  790. expect( panelView.isVisible ).to.be.true;
  791. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  792. fireKeyDownEvent( {
  793. keyCode: keyCodes.esc,
  794. preventDefault: sinon.spy(),
  795. stopPropagation: sinon.spy()
  796. } );
  797. expect( panelView.isVisible ).to.be.false;
  798. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  799. } );
  800. } );
  801. it( 'should close the opened panel when click outside the panel', () => {
  802. return createClassicTestEditor( staticConfig )
  803. .then( () => {
  804. setData( model, '<paragraph>foo []</paragraph>' );
  805. model.change( writer => {
  806. writer.insertText( '@', doc.selection.getFirstPosition() );
  807. } );
  808. } )
  809. .then( waitForDebounce )
  810. .then( () => {
  811. expect( panelView.isVisible ).to.be.true;
  812. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  813. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  814. expect( panelView.isVisible ).to.be.false;
  815. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  816. } );
  817. } );
  818. it( 'should hide the panel on selection change', () => {
  819. return createClassicTestEditor( staticConfig )
  820. .then( () => {
  821. setData( model, '<paragraph>foo []</paragraph>' );
  822. model.change( writer => {
  823. writer.insertText( '@', doc.selection.getFirstPosition() );
  824. } );
  825. } )
  826. .then( waitForDebounce )
  827. .then( () => {
  828. expect( panelView.isVisible ).to.be.true;
  829. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  830. model.change( writer => {
  831. // Place position at the beginning of a paragraph.
  832. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  833. } );
  834. expect( panelView.isVisible ).to.be.false;
  835. expect( mentionsView.position ).to.be.undefined;
  836. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  837. } );
  838. } );
  839. it( 'should hide the panel on selection change triggered by mouse click', () => {
  840. return createClassicTestEditor( staticConfig )
  841. .then( () => {
  842. setData( model, '<paragraph>foo []</paragraph>' );
  843. model.change( writer => {
  844. writer.insertText( '@', doc.selection.getFirstPosition() );
  845. } );
  846. } )
  847. .then( waitForDebounce )
  848. .then( () => {
  849. expect( panelView.isVisible ).to.be.true;
  850. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  851. // This happens when user clicks outside the panel view and selection is changed.
  852. // Two panel closing mechanisms are run:
  853. // - clickOutsideHandler
  854. // - unmatched text in text watcher
  855. // which may fail when trying to remove mention marker twice.
  856. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  857. model.change( writer => {
  858. // Place position at the beginning of a paragraph.
  859. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  860. } );
  861. expect( panelView.isVisible ).to.be.false;
  862. expect( mentionsView.position ).to.be.undefined;
  863. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  864. } );
  865. } );
  866. describe( 'default list item', () => {
  867. // Create map of expected feed items as objects as they will be stored internally.
  868. const feedItems = staticConfig.feeds[ 0 ].feed.map( text => ( { text: `${ text }`, id: `${ text }` } ) );
  869. beforeEach( () => {
  870. return createClassicTestEditor( staticConfig );
  871. } );
  872. describe( 'on arrows', () => {
  873. it( 'should cycle down on arrow down', () => {
  874. setData( model, '<paragraph>foo []</paragraph>' );
  875. model.change( writer => {
  876. writer.insertText( '@', doc.selection.getFirstPosition() );
  877. } );
  878. return waitForDebounce()
  879. .then( () => {
  880. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  881. const keyEvtData = {
  882. keyCode: keyCodes.arrowdown,
  883. preventDefault: sinon.spy(),
  884. stopPropagation: sinon.spy()
  885. };
  886. fireKeyDownEvent( keyEvtData );
  887. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  888. fireKeyDownEvent( keyEvtData );
  889. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  890. fireKeyDownEvent( keyEvtData );
  891. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  892. fireKeyDownEvent( keyEvtData );
  893. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  894. fireKeyDownEvent( keyEvtData );
  895. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  896. } );
  897. } );
  898. it( 'should cycle up on arrow up', () => {
  899. setData( model, '<paragraph>foo []</paragraph>' );
  900. model.change( writer => {
  901. writer.insertText( '@', doc.selection.getFirstPosition() );
  902. } );
  903. return waitForDebounce()
  904. .then( () => {
  905. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  906. const keyEvtData = {
  907. keyCode: keyCodes.arrowup,
  908. preventDefault: sinon.spy(),
  909. stopPropagation: sinon.spy()
  910. };
  911. fireKeyDownEvent( keyEvtData );
  912. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  913. fireKeyDownEvent( keyEvtData );
  914. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  915. fireKeyDownEvent( keyEvtData );
  916. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  917. fireKeyDownEvent( keyEvtData );
  918. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  919. fireKeyDownEvent( keyEvtData );
  920. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  921. } );
  922. } );
  923. it( 'should not cycle with only one item in the list', () => {
  924. setData( model, '<paragraph>foo []</paragraph>' );
  925. const keyDownEvtData = {
  926. keyCode: keyCodes.arrowdown,
  927. preventDefault: sinon.spy(),
  928. stopPropagation: sinon.spy()
  929. };
  930. const keyUpEvtData = {
  931. keyCode: keyCodes.arrowdown,
  932. preventDefault: sinon.spy(),
  933. stopPropagation: sinon.spy()
  934. };
  935. model.change( writer => {
  936. writer.insertText( '@T', doc.selection.getFirstPosition() );
  937. } );
  938. return waitForDebounce()
  939. .then( () => {
  940. expectChildViewsIsOnState( [ true ] );
  941. fireKeyDownEvent( keyDownEvtData );
  942. expectChildViewsIsOnState( [ true ] );
  943. fireKeyDownEvent( keyUpEvtData );
  944. expectChildViewsIsOnState( [ true ] );
  945. } );
  946. } );
  947. } );
  948. describe( 'on "execute" keys', () => {
  949. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  950. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  951. testExecuteKey( 'space', keyCodes.space, feedItems );
  952. } );
  953. } );
  954. describe( 'default list item with custom feed', () => {
  955. const issues = [
  956. { id: '@Ted' },
  957. { id: '@Barney' },
  958. { id: '@Robin' },
  959. { id: '@Lily' },
  960. { id: '@Marshal' }
  961. ];
  962. beforeEach( () => {
  963. return createClassicTestEditor( {
  964. feeds: [
  965. {
  966. marker: '@',
  967. feed: feedText => issues.filter( issue => issue.id.includes( feedText ) )
  968. }
  969. ]
  970. } );
  971. } );
  972. it( 'should show panel for matched marker', () => {
  973. setData( model, '<paragraph>foo []</paragraph>' );
  974. model.change( writer => {
  975. writer.insertText( '@', doc.selection.getFirstPosition() );
  976. } );
  977. return waitForDebounce()
  978. .then( () => {
  979. expect( panelView.isVisible ).to.be.true;
  980. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  981. expect( mentionsView.items ).to.have.length( 5 );
  982. } );
  983. } );
  984. } );
  985. describe( 'custom list item (string)', () => {
  986. const issues = [
  987. { id: '@1002', title: 'Some bug in editor.' },
  988. { id: '@1003', title: 'Introduce this feature.' },
  989. { id: '@1004', title: 'Missing docs.' },
  990. { id: '@1005', title: 'Another bug.' },
  991. { id: '@1006', title: 'More bugs' }
  992. ];
  993. beforeEach( () => {
  994. return createClassicTestEditor( {
  995. feeds: [
  996. {
  997. marker: '@',
  998. feed: issues,
  999. itemRenderer: item => item.title
  1000. }
  1001. ]
  1002. } );
  1003. } );
  1004. it( 'should show panel for matched marker', () => {
  1005. setData( model, '<paragraph>foo []</paragraph>' );
  1006. model.change( writer => {
  1007. writer.insertText( '@', doc.selection.getFirstPosition() );
  1008. } );
  1009. return waitForDebounce()
  1010. .then( () => {
  1011. expect( panelView.isVisible ).to.be.true;
  1012. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1013. expect( mentionsView.items ).to.have.length( 5 );
  1014. } );
  1015. } );
  1016. describe( 'keys', () => {
  1017. describe( 'on arrows', () => {
  1018. it( 'should cycle down on arrow down', () => {
  1019. setData( model, '<paragraph>foo []</paragraph>' );
  1020. model.change( writer => {
  1021. writer.insertText( '@', doc.selection.getFirstPosition() );
  1022. } );
  1023. return waitForDebounce()
  1024. .then( () => {
  1025. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1026. const keyEvtData = {
  1027. keyCode: keyCodes.arrowdown,
  1028. preventDefault: sinon.spy(),
  1029. stopPropagation: sinon.spy()
  1030. };
  1031. fireKeyDownEvent( keyEvtData );
  1032. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1033. fireKeyDownEvent( keyEvtData );
  1034. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1035. fireKeyDownEvent( keyEvtData );
  1036. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1037. fireKeyDownEvent( keyEvtData );
  1038. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1039. fireKeyDownEvent( keyEvtData );
  1040. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1041. } );
  1042. } );
  1043. it( 'should cycle up on arrow up', () => {
  1044. setData( model, '<paragraph>foo []</paragraph>' );
  1045. model.change( writer => {
  1046. writer.insertText( '@', doc.selection.getFirstPosition() );
  1047. } );
  1048. return waitForDebounce()
  1049. .then( () => {
  1050. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1051. const keyEvtData = {
  1052. keyCode: keyCodes.arrowup,
  1053. preventDefault: sinon.spy(),
  1054. stopPropagation: sinon.spy()
  1055. };
  1056. fireKeyDownEvent( keyEvtData );
  1057. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1058. fireKeyDownEvent( keyEvtData );
  1059. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1060. fireKeyDownEvent( keyEvtData );
  1061. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1062. fireKeyDownEvent( keyEvtData );
  1063. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1064. fireKeyDownEvent( keyEvtData );
  1065. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1066. } );
  1067. } );
  1068. } );
  1069. describe( 'on "execute" keys', () => {
  1070. testExecuteKey( 'enter', keyCodes.enter, issues );
  1071. testExecuteKey( 'tab', keyCodes.tab, issues );
  1072. testExecuteKey( 'space', keyCodes.space, issues );
  1073. } );
  1074. } );
  1075. } );
  1076. describe( 'custom list item (DOM Element)', () => {
  1077. const issues = [
  1078. { id: '@1002', title: 'Some bug in editor.' },
  1079. { id: '@1003', title: 'Introduce this feature.' },
  1080. { id: '@1004', title: 'Missing docs.' },
  1081. { id: '@1005', title: 'Another bug.' },
  1082. { id: '@1006', title: 'More bugs' }
  1083. ];
  1084. beforeEach( () => {
  1085. return createClassicTestEditor( {
  1086. feeds: [
  1087. {
  1088. marker: '@',
  1089. feed: feedText => {
  1090. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  1091. },
  1092. itemRenderer: item => {
  1093. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  1094. span.innerHTML = `<span id="issue-${ item.id.slice( 1 ) }">@${ item.title }</span>`;
  1095. return span;
  1096. }
  1097. }
  1098. ]
  1099. } );
  1100. } );
  1101. it( 'should show panel for matched marker', () => {
  1102. setData( model, '<paragraph>foo []</paragraph>' );
  1103. model.change( writer => {
  1104. writer.insertText( '@', doc.selection.getFirstPosition() );
  1105. } );
  1106. return waitForDebounce()
  1107. .then( () => {
  1108. expect( panelView.isVisible ).to.be.true;
  1109. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1110. expect( mentionsView.items ).to.have.length( 5 );
  1111. } );
  1112. } );
  1113. describe( 'keys', () => {
  1114. describe( 'on arrows', () => {
  1115. it( 'should cycle down on arrow down', () => {
  1116. setData( model, '<paragraph>foo []</paragraph>' );
  1117. model.change( writer => {
  1118. writer.insertText( '@', doc.selection.getFirstPosition() );
  1119. } );
  1120. return waitForDebounce()
  1121. .then( () => {
  1122. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1123. const keyEvtData = {
  1124. keyCode: keyCodes.arrowdown,
  1125. preventDefault: sinon.spy(),
  1126. stopPropagation: sinon.spy()
  1127. };
  1128. fireKeyDownEvent( keyEvtData );
  1129. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1130. fireKeyDownEvent( keyEvtData );
  1131. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1132. fireKeyDownEvent( keyEvtData );
  1133. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1134. fireKeyDownEvent( keyEvtData );
  1135. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1136. fireKeyDownEvent( keyEvtData );
  1137. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1138. } );
  1139. } );
  1140. it( 'should cycle up on arrow up', () => {
  1141. setData( model, '<paragraph>foo []</paragraph>' );
  1142. model.change( writer => {
  1143. writer.insertText( '@', doc.selection.getFirstPosition() );
  1144. } );
  1145. return waitForDebounce()
  1146. .then( () => {
  1147. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1148. const keyEvtData = {
  1149. keyCode: keyCodes.arrowup,
  1150. preventDefault: sinon.spy(),
  1151. stopPropagation: sinon.spy()
  1152. };
  1153. fireKeyDownEvent( keyEvtData );
  1154. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1155. fireKeyDownEvent( keyEvtData );
  1156. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  1157. fireKeyDownEvent( keyEvtData );
  1158. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  1159. fireKeyDownEvent( keyEvtData );
  1160. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  1161. fireKeyDownEvent( keyEvtData );
  1162. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1163. } );
  1164. } );
  1165. } );
  1166. describe( 'on "execute" keys', () => {
  1167. testExecuteKey( 'enter', keyCodes.enter, issues );
  1168. testExecuteKey( 'tab', keyCodes.tab, issues );
  1169. testExecuteKey( 'space', keyCodes.space, issues );
  1170. } );
  1171. describe( 'mouse', () => {
  1172. it( 'should execute selected button on mouse click', () => {
  1173. setData( model, '<paragraph>foo []</paragraph>' );
  1174. model.change( writer => {
  1175. writer.insertText( '@', doc.selection.getFirstPosition() );
  1176. } );
  1177. const command = editor.commands.get( 'mention' );
  1178. const spy = testUtils.sinon.spy( command, 'execute' );
  1179. return waitForDebounce()
  1180. .then( () => {
  1181. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1182. const element = panelView.element.querySelector( '#issue-1004' );
  1183. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  1184. sinon.assert.calledOnce( spy );
  1185. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1186. const item = issues[ 2 ];
  1187. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  1188. expect( commandOptions ).to.have.property( 'marker', '@' );
  1189. expect( commandOptions ).to.have.property( 'range' );
  1190. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1191. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1192. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1193. } );
  1194. } );
  1195. } );
  1196. } );
  1197. } );
  1198. describe( 'multiple feeds configuration', () => {
  1199. beforeEach( () => {
  1200. return createClassicTestEditor( {
  1201. feeds: [
  1202. {
  1203. marker: '@',
  1204. feed: [ '@a1', '@a2', '@a3' ]
  1205. },
  1206. {
  1207. marker: '$',
  1208. feed: [ '$a1', '$a2', '$a3', '$a4', '$a5' ]
  1209. }
  1210. ]
  1211. } );
  1212. } );
  1213. it( 'should show panel for matched marker', () => {
  1214. setData( model, '<paragraph>foo []</paragraph>' );
  1215. model.change( writer => {
  1216. writer.insertText( '@', doc.selection.getFirstPosition() );
  1217. } );
  1218. return waitForDebounce()
  1219. .then( () => {
  1220. expect( panelView.isVisible ).to.be.true;
  1221. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1222. expect( mentionsView.items ).to.have.length( 3 );
  1223. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1224. } )
  1225. .then( waitForDebounce )
  1226. .then( () => {
  1227. expect( panelView.isVisible ).to.be.false;
  1228. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1229. model.change( writer => {
  1230. writer.insertText( '$', doc.selection.getFirstPosition() );
  1231. } );
  1232. } )
  1233. .then( waitForDebounce )
  1234. .then( () => {
  1235. expect( panelView.isVisible ).to.be.true;
  1236. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1237. expect( mentionsView.items ).to.have.length( 5 );
  1238. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1239. } )
  1240. .then( waitForDebounce )
  1241. .then( () => {
  1242. expect( panelView.isVisible ).to.be.false;
  1243. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1244. model.change( writer => {
  1245. writer.insertText( '@', doc.selection.getFirstPosition() );
  1246. } );
  1247. } )
  1248. .then( waitForDebounce )
  1249. .then( () => {
  1250. expect( panelView.isVisible ).to.be.true;
  1251. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1252. expect( mentionsView.items ).to.have.length( 3 );
  1253. } );
  1254. } );
  1255. } );
  1256. function testExecuteKey( name, keyCode, feedItems ) {
  1257. it( 'should execute selected button 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. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  1267. fireKeyDownEvent( {
  1268. keyCode: keyCodes.arrowup,
  1269. preventDefault: sinon.spy(),
  1270. stopPropagation: sinon.spy()
  1271. } );
  1272. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  1273. fireKeyDownEvent( {
  1274. keyCode,
  1275. preventDefault: sinon.spy(),
  1276. stopPropagation: sinon.spy()
  1277. } );
  1278. sinon.assert.calledOnce( spy );
  1279. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  1280. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1281. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1282. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  1283. } );
  1284. } );
  1285. it( 'should do nothing if panel is not visible on ' + name, () => {
  1286. setData( model, '<paragraph>foo []</paragraph>' );
  1287. model.change( writer => {
  1288. writer.insertText( '@', doc.selection.getFirstPosition() );
  1289. } );
  1290. const command = editor.commands.get( 'mention' );
  1291. const spy = testUtils.sinon.spy( command, 'execute' );
  1292. return waitForDebounce()
  1293. .then( () => {
  1294. expect( panelView.isVisible ).to.be.true;
  1295. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  1296. fireKeyDownEvent( {
  1297. keyCode: keyCodes.esc,
  1298. preventDefault: sinon.spy(),
  1299. stopPropagation: sinon.spy()
  1300. } );
  1301. expect( panelView.isVisible ).to.be.false;
  1302. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1303. fireKeyDownEvent( {
  1304. keyCode,
  1305. preventDefault: sinon.spy(),
  1306. stopPropagation: sinon.spy()
  1307. } );
  1308. sinon.assert.notCalled( spy );
  1309. expect( panelView.isVisible ).to.be.false;
  1310. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1311. } );
  1312. } );
  1313. }
  1314. } );
  1315. describe( 'execute', () => {
  1316. beforeEach( () => createClassicTestEditor( staticConfig ) );
  1317. it( 'should call the mention command with proper options', () => {
  1318. setData( model, '<paragraph>foo []</paragraph>' );
  1319. model.change( writer => {
  1320. writer.insertText( '@', doc.selection.getFirstPosition() );
  1321. } );
  1322. const command = editor.commands.get( 'mention' );
  1323. const spy = testUtils.sinon.spy( command, 'execute' );
  1324. return waitForDebounce()
  1325. .then( () => {
  1326. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1327. sinon.assert.calledOnce( spy );
  1328. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  1329. assertCommandOptions( commandOptions, '@', { id: '@Barney', text: '@Barney' } );
  1330. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  1331. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  1332. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  1333. } );
  1334. } );
  1335. it( 'should hide panel on execute', () => {
  1336. setData( model, '<paragraph>foo []</paragraph>' );
  1337. model.change( writer => {
  1338. writer.insertText( '@', doc.selection.getFirstPosition() );
  1339. } );
  1340. return waitForDebounce()
  1341. .then( () => {
  1342. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1343. expect( panelView.isVisible ).to.be.false;
  1344. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  1345. } );
  1346. } );
  1347. it( 'should focus view after command execution', () => {
  1348. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  1349. setData( model, '<paragraph>foo []</paragraph>' );
  1350. model.change( writer => {
  1351. writer.insertText( '@', doc.selection.getFirstPosition() );
  1352. } );
  1353. return waitForDebounce()
  1354. .then( () => {
  1355. mentionsView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  1356. sinon.assert.calledOnce( focusSpy );
  1357. } );
  1358. } );
  1359. } );
  1360. function createClassicTestEditor( mentionConfig ) {
  1361. return ClassicTestEditor
  1362. .create( editorElement, {
  1363. plugins: [ Paragraph, MentionEditing, MentionUI ],
  1364. mention: mentionConfig
  1365. } )
  1366. .then( newEditor => {
  1367. editor = newEditor;
  1368. model = editor.model;
  1369. doc = model.document;
  1370. editingView = editor.editing.view;
  1371. mentionUI = editor.plugins.get( MentionUI );
  1372. panelView = editor.plugins.get( ContextualBalloon ).view;
  1373. mentionsView = mentionUI._mentionsView;
  1374. } );
  1375. }
  1376. function waitForDebounce() {
  1377. return new Promise( resolve => {
  1378. setTimeout( () => {
  1379. resolve();
  1380. }, 50 );
  1381. } );
  1382. }
  1383. function fireKeyDownEvent( options ) {
  1384. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  1385. const eventData = new DomEventData( editingView.document, {
  1386. target: document.body
  1387. }, options );
  1388. editingView.document.fire( eventInfo, eventData );
  1389. }
  1390. function stubSelectionRects( rects ) {
  1391. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  1392. // Mock selection rect.
  1393. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  1394. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  1395. sinon.stub( domRange, 'getClientRects' )
  1396. .returns( rects );
  1397. return domRange;
  1398. } );
  1399. }
  1400. function expectChildViewsIsOnState( expectedState ) {
  1401. const childViews = [ ...mentionsView.items ].map( item => item.children.get( 0 ) );
  1402. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  1403. }
  1404. function assertCommandOptions( commandOptions, marker, item ) {
  1405. expect( commandOptions ).to.have.property( 'marker', marker );
  1406. expect( commandOptions ).to.have.property( 'range' );
  1407. expect( commandOptions ).to.have.property( 'mention' );
  1408. const mentionForCommand = commandOptions.mention;
  1409. for ( const key of Object.keys( item ) ) {
  1410. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  1411. }
  1412. }
  1413. } );