mentionui.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  10. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  15. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  16. import MentionUI from '../src/mentionui';
  17. import MentionEditing from '../src/mentionediting';
  18. import MentionsView from '../src/ui/mentionsview';
  19. describe( 'MentionUI', () => {
  20. let editor, model, doc, editingView, mentionUI, editorElement, mentionsView, panelView, listView;
  21. const staticConfig = {
  22. feeds: [
  23. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  24. ]
  25. };
  26. testUtils.createSinonSandbox();
  27. beforeEach( () => {
  28. editorElement = document.createElement( 'div' );
  29. document.body.appendChild( editorElement );
  30. } );
  31. afterEach( () => {
  32. sinon.restore();
  33. editorElement.remove();
  34. return editor.destroy();
  35. } );
  36. it( 'should create a plugin instance', () => {
  37. return createClassicTestEditor().then( () => {
  38. expect( mentionUI ).to.instanceOf( Plugin );
  39. expect( mentionUI ).to.instanceOf( MentionUI );
  40. } );
  41. } );
  42. describe( 'pluginName', () => {
  43. it( 'should return plugin by its name', () => {
  44. return createClassicTestEditor().then( () => {
  45. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  46. } );
  47. } );
  48. } );
  49. describe( 'child views', () => {
  50. beforeEach( () => createClassicTestEditor() );
  51. describe( 'panelView', () => {
  52. it( 'should create a view instance', () => {
  53. expect( panelView ).to.instanceof( BalloonPanelView );
  54. } );
  55. it( 'should be added to the ui.view.body collection', () => {
  56. expect( Array.from( editor.ui.view.body ) ).to.include( panelView );
  57. } );
  58. it( 'should have disabled arrow', () => {
  59. expect( panelView.withArrow ).to.be.false;
  60. } );
  61. it( 'should have added MentionView as a child', () => {
  62. expect( panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  63. } );
  64. } );
  65. } );
  66. describe( 'position', () => {
  67. let pinSpy;
  68. const caretRect = {
  69. bottom: 118,
  70. height: 18,
  71. left: 500,
  72. right: 501,
  73. top: 100,
  74. width: 1
  75. };
  76. const balloonRect = {
  77. bottom: 150,
  78. height: 150,
  79. left: 0,
  80. right: 200,
  81. top: 0,
  82. width: 200
  83. };
  84. beforeEach( () => {
  85. return createClassicTestEditor( staticConfig ).then( () => {
  86. pinSpy = sinon.spy( panelView, 'pin' );
  87. } );
  88. } );
  89. it( 'should properly calculate position data', () => {
  90. const editableElement = editingView.document.selection.editableElement;
  91. setData( model, '<paragraph>foo []</paragraph>' );
  92. stubSelectionRects( [ caretRect ] );
  93. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  94. model.change( writer => {
  95. writer.insertText( '@', doc.selection.getFirstPosition() );
  96. } );
  97. return waitForDebounce()
  98. .then( () => {
  99. const pinArgument = pinSpy.firstCall.args[ 0 ];
  100. const { target, positions, limiter, fitInViewport } = pinArgument;
  101. expect( fitInViewport ).to.be.true;
  102. expect( positions ).to.have.length( 4 );
  103. // Mention UI should set limiter to the editable area.
  104. expect( limiter() ).to.equal( editingView.domConverter.mapViewToDom( editableElement ) );
  105. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  106. const mentionMarker = editor.model.markers.get( 'mention' );
  107. const focus = doc.selection.focus;
  108. const expectedRange = editor.model.createRange( focus.getShiftedBy( -1 ), focus );
  109. // It should create a model marker for matcher marker character ('@').
  110. expect( expectedRange.isEqual( mentionMarker.getRange() ) ).to.be.true;
  111. const toViewRangeSpy = sinon.spy( editor.editing.mapper, 'toViewRange' );
  112. expect( target() ).to.deep.equal( caretRect );
  113. sinon.assert.calledOnce( toViewRangeSpy );
  114. const range = toViewRangeSpy.firstCall.args[ 0 ];
  115. expect( mentionMarker.getRange().isEqual( range ), 'Should position to mention marker.' );
  116. const caretSouthEast = positions[ 0 ];
  117. const caretNorthEast = positions[ 1 ];
  118. const caretSouthWest = positions[ 2 ];
  119. const caretNorthWest = positions[ 3 ];
  120. expect( caretSouthEast( caretRect, balloonRect ) ).to.deep.equal( {
  121. left: 501,
  122. name: 'caret_se',
  123. top: 123
  124. } );
  125. expect( caretNorthEast( caretRect, balloonRect ) ).to.deep.equal( {
  126. left: 501,
  127. name: 'caret_ne',
  128. top: -55
  129. } );
  130. expect( caretSouthWest( caretRect, balloonRect ) ).to.deep.equal( {
  131. left: 301,
  132. name: 'caret_sw',
  133. top: 123
  134. } );
  135. expect( caretNorthWest( caretRect, balloonRect ) ).to.deep.equal( {
  136. left: 301,
  137. name: 'caret_nw',
  138. top: -55
  139. } );
  140. } );
  141. } );
  142. it( 'should re-calculate position on typing', () => {
  143. setData( model, '<paragraph>foo []</paragraph>' );
  144. stubSelectionRects( [ caretRect ] );
  145. model.change( writer => {
  146. writer.insertText( '@', doc.selection.getFirstPosition() );
  147. } );
  148. return waitForDebounce()
  149. .then( () => {
  150. sinon.assert.calledOnce( pinSpy );
  151. model.change( writer => {
  152. writer.insertText( 't', doc.selection.getFirstPosition() );
  153. } );
  154. } )
  155. .then( waitForDebounce )
  156. .then( () => {
  157. sinon.assert.calledTwice( pinSpy );
  158. } );
  159. } );
  160. } );
  161. describe( 'typing integration', () => {
  162. it( 'should show panel for matched marker after typing minimum characters', () => {
  163. return createClassicTestEditor( { feeds: [ Object.assign( { minimumCharacters: 2 }, staticConfig.feeds[ 0 ] ) ] } )
  164. .then( () => {
  165. setData( model, '<paragraph>foo []</paragraph>' );
  166. model.change( writer => {
  167. writer.insertText( '@', doc.selection.getFirstPosition() );
  168. } );
  169. } )
  170. .then( () => {
  171. model.change( writer => {
  172. writer.insertText( 'B', doc.selection.getFirstPosition() );
  173. } );
  174. } )
  175. .then( waitForDebounce )
  176. .then( () => {
  177. expect( panelView.isVisible ).to.be.false;
  178. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  179. } )
  180. .then( waitForDebounce )
  181. .then( () => {
  182. model.change( writer => {
  183. writer.insertText( 'a', doc.selection.getFirstPosition() );
  184. } );
  185. } )
  186. .then( waitForDebounce )
  187. .then( () => {
  188. expect( panelView.isVisible ).to.be.true;
  189. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  190. expect( listView.items ).to.have.length( 1 );
  191. model.change( writer => {
  192. writer.insertText( 'r', doc.selection.getFirstPosition() );
  193. } );
  194. } )
  195. .then( waitForDebounce )
  196. .then( () => {
  197. expect( panelView.isVisible ).to.be.true;
  198. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  199. expect( listView.items ).to.have.length( 1 );
  200. } );
  201. } );
  202. describe( 'static list with default trigger', () => {
  203. beforeEach( () => {
  204. return createClassicTestEditor( staticConfig );
  205. } );
  206. it( 'should show panel for matched marker', () => {
  207. setData( model, '<paragraph>foo []</paragraph>' );
  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. expect( panelView.isVisible ).to.be.true;
  215. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  216. expect( listView.items ).to.have.length( 5 );
  217. } );
  218. } );
  219. it( 'should show panel for matched marker at the beginning of paragraph', () => {
  220. setData( model, '<paragraph>[] foo</paragraph>' );
  221. model.change( writer => {
  222. writer.insertText( '@', doc.selection.getFirstPosition() );
  223. } );
  224. return waitForDebounce()
  225. .then( () => {
  226. expect( panelView.isVisible ).to.be.true;
  227. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  228. expect( listView.items ).to.have.length( 5 );
  229. } );
  230. } );
  231. it( 'should not show panel for marker in the middle of other word', () => {
  232. setData( model, '<paragraph>foo[]</paragraph>' );
  233. model.change( writer => {
  234. writer.insertText( '@', doc.selection.getFirstPosition() );
  235. } );
  236. return waitForDebounce()
  237. .then( () => {
  238. expect( panelView.isVisible ).to.be.false;
  239. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  240. } );
  241. } );
  242. it( 'should not show panel when selection is inside a mention', () => {
  243. setData( model, '<paragraph>foo <$text mention="{\'name\':\'John\'}">@John</$text> bar</paragraph>' );
  244. model.change( writer => {
  245. writer.setSelection( doc.getRoot().getChild( 0 ), 7 );
  246. } );
  247. return waitForDebounce()
  248. .then( () => {
  249. expect( panelView.isVisible ).to.be.false;
  250. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  251. } );
  252. } );
  253. it( 'should not show panel when selection is at the end of a mention', () => {
  254. setData( model, '<paragraph>foo <$text mention="{\'name\':\'John\'}">@John</$text> bar</paragraph>' );
  255. model.change( writer => {
  256. writer.setSelection( doc.getRoot().getChild( 0 ), 9 );
  257. } );
  258. return waitForDebounce()
  259. .then( () => {
  260. expect( panelView.isVisible ).to.be.false;
  261. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  262. } );
  263. } );
  264. it( 'should not show panel when selection is not collapsed', () => {
  265. setData( model, '<paragraph>foo []</paragraph>' );
  266. model.change( writer => {
  267. writer.insertText( '@', doc.selection.getFirstPosition() );
  268. } );
  269. return waitForDebounce()
  270. .then( () => {
  271. expect( panelView.isVisible ).to.be.true;
  272. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  273. model.change( () => {
  274. model.modifySelection( doc.selection, { direction: 'backward', unit: 'character' } );
  275. } );
  276. } )
  277. .then( waitForDebounce )
  278. .then( () => {
  279. expect( panelView.isVisible ).to.be.false;
  280. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  281. } );
  282. } );
  283. it( 'should show filtered results for matched text', () => {
  284. setData( model, '<paragraph>foo []</paragraph>' );
  285. model.change( writer => {
  286. writer.insertText( '@', doc.selection.getFirstPosition() );
  287. } );
  288. model.change( writer => {
  289. writer.insertText( 'T', doc.selection.getFirstPosition() );
  290. } );
  291. return waitForDebounce()
  292. .then( () => {
  293. expect( panelView.isVisible ).to.be.true;
  294. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  295. expect( listView.items ).to.have.length( 1 );
  296. } );
  297. } );
  298. it( 'should focus the first item in panel', () => {
  299. setData( model, '<paragraph>foo []</paragraph>' );
  300. model.change( writer => {
  301. writer.insertText( '@', doc.selection.getFirstPosition() );
  302. } );
  303. return waitForDebounce()
  304. .then( () => {
  305. const button = listView.items.get( 0 ).children.get( 0 );
  306. expect( button.isOn ).to.be.true;
  307. } );
  308. } );
  309. it( 'should hide panel if no matched items', () => {
  310. setData( model, '<paragraph>foo []</paragraph>' );
  311. model.change( writer => {
  312. writer.insertText( '@', doc.selection.getFirstPosition() );
  313. } );
  314. return waitForDebounce()
  315. .then( () => expect( panelView.isVisible ).to.be.true )
  316. .then( () => {
  317. model.change( writer => {
  318. writer.insertText( 'x', doc.selection.getFirstPosition() );
  319. } );
  320. } )
  321. .then( waitForDebounce )
  322. .then( () => {
  323. expect( panelView.isVisible ).to.be.false;
  324. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  325. expect( listView.items ).to.have.length( 0 );
  326. } );
  327. } );
  328. it( 'should hide panel when text was unmatched', () => {
  329. setData( model, '<paragraph>foo []</paragraph>' );
  330. model.change( writer => {
  331. writer.insertText( '@', doc.selection.getFirstPosition() );
  332. } );
  333. return waitForDebounce()
  334. .then( () => expect( panelView.isVisible ).to.be.true )
  335. .then( () => {
  336. model.change( writer => {
  337. const end = doc.selection.getFirstPosition();
  338. const start = end.getShiftedBy( -1 );
  339. writer.remove( writer.createRange( start, end ) );
  340. } );
  341. } )
  342. .then( waitForDebounce )
  343. .then( () => expect( panelView.isVisible ).to.be.false );
  344. } );
  345. } );
  346. describe( 'asynchronous list with custom trigger', () => {
  347. beforeEach( () => {
  348. const issuesNumbers = [ '100', '101', '102', '103' ];
  349. return createClassicTestEditor( {
  350. feeds: [
  351. {
  352. marker: '#',
  353. feed: feedText => {
  354. return new Promise( resolve => {
  355. setTimeout( () => {
  356. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  357. }, 20 );
  358. } );
  359. }
  360. }
  361. ]
  362. } );
  363. } );
  364. it( 'should show panel for matched marker', () => {
  365. setData( model, '<paragraph>foo []</paragraph>' );
  366. model.change( writer => {
  367. writer.insertText( '#', doc.selection.getFirstPosition() );
  368. } );
  369. return waitForDebounce()
  370. .then( () => {
  371. expect( panelView.isVisible ).to.be.true;
  372. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  373. expect( listView.items ).to.have.length( 4 );
  374. } );
  375. } );
  376. it( 'should show filtered results for matched text', () => {
  377. setData( model, '<paragraph>foo []</paragraph>' );
  378. model.change( writer => {
  379. writer.insertText( '#', doc.selection.getFirstPosition() );
  380. } );
  381. model.change( writer => {
  382. writer.insertText( '2', doc.selection.getFirstPosition() );
  383. } );
  384. return waitForDebounce()
  385. .then( () => {
  386. expect( panelView.isVisible ).to.be.true;
  387. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  388. expect( listView.items ).to.have.length( 1 );
  389. } );
  390. } );
  391. it( 'should hide panel if no matched items', () => {
  392. setData( model, '<paragraph>foo []</paragraph>' );
  393. model.change( writer => {
  394. writer.insertText( '#', doc.selection.getFirstPosition() );
  395. } );
  396. return waitForDebounce()
  397. .then( () => expect( panelView.isVisible ).to.be.true )
  398. .then( () => {
  399. model.change( writer => {
  400. writer.insertText( 'x', doc.selection.getFirstPosition() );
  401. } );
  402. } )
  403. .then( waitForDebounce )
  404. .then( () => {
  405. expect( panelView.isVisible ).to.be.false;
  406. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  407. expect( listView.items ).to.have.length( 0 );
  408. } );
  409. } );
  410. it( 'should hide panel when text was unmatched', () => {
  411. setData( model, '<paragraph>foo []</paragraph>' );
  412. model.change( writer => {
  413. writer.insertText( '#', doc.selection.getFirstPosition() );
  414. } );
  415. return waitForDebounce()
  416. .then( () => expect( panelView.isVisible ).to.be.true )
  417. .then( () => {
  418. model.change( writer => {
  419. const end = doc.selection.getFirstPosition();
  420. const start = end.getShiftedBy( -1 );
  421. writer.remove( writer.createRange( start, end ) );
  422. } );
  423. } )
  424. .then( waitForDebounce )
  425. .then( () => expect( panelView.isVisible ).to.be.false );
  426. } );
  427. } );
  428. } );
  429. describe( 'panel behavior', () => {
  430. it( 'should close the opened panel on esc', () => {
  431. return createClassicTestEditor( staticConfig )
  432. .then( () => {
  433. setData( model, '<paragraph>foo []</paragraph>' );
  434. model.change( writer => {
  435. writer.insertText( '@', doc.selection.getFirstPosition() );
  436. } );
  437. } )
  438. .then( waitForDebounce )
  439. .then( () => {
  440. expect( panelView.isVisible ).to.be.true;
  441. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  442. fireKeyDownEvent( {
  443. keyCode: keyCodes.esc,
  444. preventDefault: sinon.spy(),
  445. stopPropagation: sinon.spy()
  446. } );
  447. expect( panelView.isVisible ).to.be.false;
  448. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  449. } );
  450. } );
  451. it( 'should close the opened panel when click outside the panel', () => {
  452. return createClassicTestEditor( staticConfig )
  453. .then( () => {
  454. setData( model, '<paragraph>foo []</paragraph>' );
  455. model.change( writer => {
  456. writer.insertText( '@', doc.selection.getFirstPosition() );
  457. } );
  458. } )
  459. .then( waitForDebounce )
  460. .then( () => {
  461. expect( panelView.isVisible ).to.be.true;
  462. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  463. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  464. expect( panelView.isVisible ).to.be.false;
  465. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  466. } );
  467. } );
  468. it( 'should hide the panel when click outside', () => {
  469. return createClassicTestEditor( staticConfig )
  470. .then( () => {
  471. setData( model, '<paragraph>foo []</paragraph>' );
  472. model.change( writer => {
  473. writer.insertText( '@', doc.selection.getFirstPosition() );
  474. } );
  475. } )
  476. .then( waitForDebounce )
  477. .then( () => {
  478. expect( panelView.isVisible ).to.be.true;
  479. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  480. model.change( writer => {
  481. // Place position at the begging of a paragraph.
  482. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  483. } );
  484. expect( panelView.isVisible ).to.be.false;
  485. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  486. } );
  487. } );
  488. describe( 'default list item', () => {
  489. const feedItems = staticConfig.feeds[ 0 ].feed.map( name => ( { name } ) );
  490. beforeEach( () => {
  491. return createClassicTestEditor( staticConfig );
  492. } );
  493. describe( 'on arrows', () => {
  494. it( 'should cycle down on arrow down', () => {
  495. setData( model, '<paragraph>foo []</paragraph>' );
  496. model.change( writer => {
  497. writer.insertText( '@', doc.selection.getFirstPosition() );
  498. } );
  499. return waitForDebounce()
  500. .then( () => {
  501. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  502. const keyEvtData = {
  503. keyCode: keyCodes.arrowdown,
  504. preventDefault: sinon.spy(),
  505. stopPropagation: sinon.spy()
  506. };
  507. fireKeyDownEvent( keyEvtData );
  508. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  509. fireKeyDownEvent( keyEvtData );
  510. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  511. fireKeyDownEvent( keyEvtData );
  512. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  513. fireKeyDownEvent( keyEvtData );
  514. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  515. fireKeyDownEvent( keyEvtData );
  516. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  517. } );
  518. } );
  519. it( 'should cycle up on arrow up', () => {
  520. setData( model, '<paragraph>foo []</paragraph>' );
  521. model.change( writer => {
  522. writer.insertText( '@', doc.selection.getFirstPosition() );
  523. } );
  524. return waitForDebounce()
  525. .then( () => {
  526. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  527. const keyEvtData = {
  528. keyCode: keyCodes.arrowup,
  529. preventDefault: sinon.spy(),
  530. stopPropagation: sinon.spy()
  531. };
  532. fireKeyDownEvent( keyEvtData );
  533. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  534. fireKeyDownEvent( keyEvtData );
  535. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  536. fireKeyDownEvent( keyEvtData );
  537. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  538. fireKeyDownEvent( keyEvtData );
  539. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  540. fireKeyDownEvent( keyEvtData );
  541. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  542. } );
  543. } );
  544. } );
  545. describe( 'on "execute" keys', () => {
  546. testExecuteKey( 'enter', keyCodes.enter, feedItems );
  547. testExecuteKey( 'tab', keyCodes.tab, feedItems );
  548. testExecuteKey( 'space', keyCodes.space, feedItems );
  549. } );
  550. } );
  551. describe( 'custom list item', () => {
  552. const issues = [
  553. { id: '1002', title: 'Some bug in editor.' },
  554. { id: '1003', title: 'Introduce this feature.' },
  555. { id: '1004', title: 'Missing docs.' },
  556. { id: '1005', title: 'Another bug.' },
  557. { id: '1006', title: 'More bugs' }
  558. ];
  559. beforeEach( () => {
  560. return createClassicTestEditor( {
  561. feeds: [
  562. {
  563. marker: '@',
  564. feed: feedText => {
  565. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  566. },
  567. itemRenderer: item => {
  568. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  569. span.innerHTML = `<span id="issue-${ item.id }">@${ item.title }</span>`;
  570. return span;
  571. }
  572. }
  573. ]
  574. } );
  575. } );
  576. it( 'should show panel for matched marker', () => {
  577. setData( model, '<paragraph>foo []</paragraph>' );
  578. model.change( writer => {
  579. writer.insertText( '@', doc.selection.getFirstPosition() );
  580. } );
  581. return waitForDebounce()
  582. .then( () => {
  583. expect( panelView.isVisible ).to.be.true;
  584. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  585. expect( listView.items ).to.have.length( 5 );
  586. } );
  587. } );
  588. describe( 'keys', () => {
  589. describe( 'on arrows', () => {
  590. it( 'should cycle down on arrow down', () => {
  591. setData( model, '<paragraph>foo []</paragraph>' );
  592. model.change( writer => {
  593. writer.insertText( '@', doc.selection.getFirstPosition() );
  594. } );
  595. return waitForDebounce()
  596. .then( () => {
  597. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  598. const keyEvtData = {
  599. keyCode: keyCodes.arrowdown,
  600. preventDefault: sinon.spy(),
  601. stopPropagation: sinon.spy()
  602. };
  603. fireKeyDownEvent( keyEvtData );
  604. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  605. fireKeyDownEvent( keyEvtData );
  606. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  607. fireKeyDownEvent( keyEvtData );
  608. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  609. fireKeyDownEvent( keyEvtData );
  610. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  611. fireKeyDownEvent( keyEvtData );
  612. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  613. } );
  614. } );
  615. it( 'should cycle up on arrow up', () => {
  616. setData( model, '<paragraph>foo []</paragraph>' );
  617. model.change( writer => {
  618. writer.insertText( '@', doc.selection.getFirstPosition() );
  619. } );
  620. return waitForDebounce()
  621. .then( () => {
  622. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  623. const keyEvtData = {
  624. keyCode: keyCodes.arrowup,
  625. preventDefault: sinon.spy(),
  626. stopPropagation: sinon.spy()
  627. };
  628. fireKeyDownEvent( keyEvtData );
  629. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  630. fireKeyDownEvent( keyEvtData );
  631. expectChildViewsIsOnState( [ false, false, false, true, false ] );
  632. fireKeyDownEvent( keyEvtData );
  633. expectChildViewsIsOnState( [ false, false, true, false, false ] );
  634. fireKeyDownEvent( keyEvtData );
  635. expectChildViewsIsOnState( [ false, true, false, false, false ] );
  636. fireKeyDownEvent( keyEvtData );
  637. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  638. } );
  639. } );
  640. } );
  641. describe( 'on "execute" keys', () => {
  642. testExecuteKey( 'enter', keyCodes.enter, issues );
  643. testExecuteKey( 'tab', keyCodes.tab, issues );
  644. testExecuteKey( 'space', keyCodes.space, issues );
  645. } );
  646. describe( 'mouse', () => {
  647. it( 'should execute selected button on mouse click', () => {
  648. setData( model, '<paragraph>foo []</paragraph>' );
  649. model.change( writer => {
  650. writer.insertText( '@', doc.selection.getFirstPosition() );
  651. } );
  652. const command = editor.commands.get( 'mention' );
  653. const spy = testUtils.sinon.spy( command, 'execute' );
  654. return waitForDebounce()
  655. .then( () => {
  656. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  657. const element = panelView.element.querySelector( '#issue-1004' );
  658. element.dispatchEvent( new Event( 'click', { bubbles: true } ) );
  659. sinon.assert.calledOnce( spy );
  660. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  661. const item = issues[ 2 ];
  662. expect( commandOptions ).to.have.property( 'mention' ).that.deep.equal( item );
  663. expect( commandOptions ).to.have.property( 'marker', '@' );
  664. expect( commandOptions ).to.have.property( 'range' );
  665. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  666. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  667. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  668. } );
  669. } );
  670. } );
  671. } );
  672. } );
  673. function testExecuteKey( name, keyCode, feedItems ) {
  674. it( 'should execute selected button on ' + name, () => {
  675. setData( model, '<paragraph>foo []</paragraph>' );
  676. model.change( writer => {
  677. writer.insertText( '@', doc.selection.getFirstPosition() );
  678. } );
  679. const command = editor.commands.get( 'mention' );
  680. const spy = testUtils.sinon.spy( command, 'execute' );
  681. return waitForDebounce()
  682. .then( () => {
  683. expectChildViewsIsOnState( [ true, false, false, false, false ] );
  684. fireKeyDownEvent( {
  685. keyCode: keyCodes.arrowup,
  686. preventDefault: sinon.spy(),
  687. stopPropagation: sinon.spy()
  688. } );
  689. expectChildViewsIsOnState( [ false, false, false, false, true ] );
  690. fireKeyDownEvent( {
  691. keyCode,
  692. preventDefault: sinon.spy(),
  693. stopPropagation: sinon.spy()
  694. } );
  695. sinon.assert.calledOnce( spy );
  696. assertCommandOptions( spy.getCall( 0 ).args[ 0 ], '@', feedItems[ 4 ] );
  697. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  698. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  699. expect( spy.getCall( 0 ).args[ 0 ].range.isEqual( expectedRange ) ).to.be.true;
  700. } );
  701. } );
  702. it( 'should do nothing if panel is not visible on ' + name, () => {
  703. setData( model, '<paragraph>foo []</paragraph>' );
  704. model.change( writer => {
  705. writer.insertText( '@', doc.selection.getFirstPosition() );
  706. } );
  707. const command = editor.commands.get( 'mention' );
  708. const spy = testUtils.sinon.spy( command, 'execute' );
  709. return waitForDebounce()
  710. .then( () => {
  711. expect( panelView.isVisible ).to.be.true;
  712. expect( editor.model.markers.has( 'mention' ) ).to.be.true;
  713. fireKeyDownEvent( {
  714. keyCode: keyCodes.esc,
  715. preventDefault: sinon.spy(),
  716. stopPropagation: sinon.spy()
  717. } );
  718. expect( panelView.isVisible ).to.be.false;
  719. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  720. fireKeyDownEvent( {
  721. keyCode,
  722. preventDefault: sinon.spy(),
  723. stopPropagation: sinon.spy()
  724. } );
  725. sinon.assert.notCalled( spy );
  726. expect( panelView.isVisible ).to.be.false;
  727. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  728. } );
  729. } );
  730. }
  731. } );
  732. describe( 'execute', () => {
  733. beforeEach( () => createClassicTestEditor( staticConfig ) );
  734. it( 'should call the mention command with proper options', () => {
  735. setData( model, '<paragraph>foo []</paragraph>' );
  736. model.change( writer => {
  737. writer.insertText( '@', doc.selection.getFirstPosition() );
  738. } );
  739. const command = editor.commands.get( 'mention' );
  740. const spy = testUtils.sinon.spy( command, 'execute' );
  741. return waitForDebounce()
  742. .then( () => {
  743. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  744. sinon.assert.calledOnce( spy );
  745. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  746. assertCommandOptions( commandOptions, '@', { name: 'Barney' } );
  747. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  748. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  749. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  750. } );
  751. } );
  752. it( 'should hide panel on execute', () => {
  753. setData( model, '<paragraph>foo []</paragraph>' );
  754. model.change( writer => {
  755. writer.insertText( '@', doc.selection.getFirstPosition() );
  756. } );
  757. return waitForDebounce()
  758. .then( () => {
  759. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  760. expect( panelView.isVisible ).to.be.false;
  761. expect( editor.model.markers.has( 'mention' ) ).to.be.false;
  762. } );
  763. } );
  764. } );
  765. function createClassicTestEditor( mentionConfig ) {
  766. return ClassicTestEditor
  767. .create( editorElement, {
  768. plugins: [ Paragraph, MentionEditing, MentionUI ],
  769. mention: mentionConfig
  770. } )
  771. .then( newEditor => {
  772. editor = newEditor;
  773. model = editor.model;
  774. doc = model.document;
  775. editingView = editor.editing.view;
  776. mentionUI = editor.plugins.get( MentionUI );
  777. panelView = mentionUI.panelView;
  778. mentionsView = mentionUI._mentionsView;
  779. listView = mentionsView.listView;
  780. editingView.attachDomRoot( editorElement );
  781. // Focus the engine.
  782. editingView.document.isFocused = true;
  783. editingView.getDomRoot().focus();
  784. // Remove all selection ranges from DOM before testing.
  785. window.getSelection().removeAllRanges();
  786. } );
  787. }
  788. function waitForDebounce() {
  789. return new Promise( resolve => {
  790. setTimeout( () => {
  791. resolve();
  792. }, 50 );
  793. } );
  794. }
  795. function fireKeyDownEvent( options ) {
  796. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  797. const eventData = new DomEventData( editingView.document, {
  798. target: document.body
  799. }, options );
  800. editingView.document.fire( eventInfo, eventData );
  801. }
  802. function stubSelectionRects( rects ) {
  803. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  804. // Mock selection rect.
  805. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  806. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  807. sinon.stub( domRange, 'getClientRects' )
  808. .returns( rects );
  809. return domRange;
  810. } );
  811. }
  812. function expectChildViewsIsOnState( expectedState ) {
  813. const childViews = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  814. expect( childViews.map( child => child.isOn ) ).to.deep.equal( expectedState );
  815. }
  816. function assertCommandOptions( commandOptions, marker, item ) {
  817. expect( commandOptions ).to.have.property( 'marker', marker );
  818. expect( commandOptions ).to.have.property( 'range' );
  819. expect( commandOptions ).to.have.property( 'mention' );
  820. const mentionForCommand = commandOptions.mention;
  821. for ( const key of Object.keys( item ) ) {
  822. expect( mentionForCommand[ key ] ).to.equal( item[ key ] );
  823. }
  824. }
  825. } );