mentionui.js 53 KB

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