mentionui.js 54 KB

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