8
0

mentionui.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 */
  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 MentionUI from '../src/mentionui';
  15. import MentionEditing from '../src/mentionediting';
  16. import MentionsView from '../src/ui/mentionsview';
  17. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  18. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  19. describe( 'MentionUI', () => {
  20. let editor, model, doc, editingView, mentionUI, editorElement, mentionsView, panelView, listView;
  21. const staticConfig = [
  22. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  23. ];
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. editorElement = document.createElement( 'div' );
  27. document.body.appendChild( editorElement );
  28. } );
  29. afterEach( () => {
  30. sinon.restore();
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. it( 'should create a plugin instance', () => {
  35. return createClassicTestEditor().then( () => {
  36. expect( mentionUI ).to.instanceOf( Plugin );
  37. expect( mentionUI ).to.instanceOf( MentionUI );
  38. } );
  39. } );
  40. describe( 'pluginName', () => {
  41. it( 'should return plugin by its name', () => {
  42. return createClassicTestEditor().then( () => {
  43. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  44. } );
  45. } );
  46. } );
  47. describe( 'child views', () => {
  48. beforeEach( () => createClassicTestEditor() );
  49. describe( 'panelView', () => {
  50. it( 'should create a view instance', () => {
  51. expect( panelView ).to.instanceof( BalloonPanelView );
  52. } );
  53. it( 'should be added to the ui.view.body collection', () => {
  54. expect( Array.from( editor.ui.view.body ) ).to.include( panelView );
  55. } );
  56. it( 'should have disabled arrow', () => {
  57. expect( panelView.withArrow ).to.be.false;
  58. } );
  59. it( 'should have added MentionView as a child', () => {
  60. expect( panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  61. } );
  62. } );
  63. } );
  64. describe( 'position', () => {
  65. let pinSpy;
  66. const caretRect = {
  67. bottom: 118,
  68. height: 18,
  69. left: 500,
  70. right: 501,
  71. top: 100,
  72. width: 1
  73. };
  74. const balloonRect = {
  75. bottom: 150,
  76. height: 150,
  77. left: 0,
  78. right: 200,
  79. top: 0,
  80. width: 200
  81. };
  82. beforeEach( () => {
  83. return createClassicTestEditor( staticConfig ).then( () => {
  84. pinSpy = sinon.spy( panelView, 'pin' );
  85. } );
  86. } );
  87. it( 'should properly calculate position data', () => {
  88. setData( model, '<paragraph>foo []</paragraph>' );
  89. stubSelectionRects( [ caretRect ] );
  90. model.change( writer => {
  91. writer.insertText( '@', doc.selection.getFirstPosition() );
  92. } );
  93. return waitForDebounce()
  94. .then( () => {
  95. const pinArgument = pinSpy.firstCall.args[ 0 ];
  96. const { target, positions } = pinArgument;
  97. expect( target() ).to.deep.equal( caretRect );
  98. expect( positions ).to.have.length( 4 );
  99. const caretSouthEast = positions[ 0 ];
  100. const caretNorthEast = positions[ 1 ];
  101. const caretSouthWest = positions[ 2 ];
  102. const caretNorthWest = positions[ 3 ];
  103. expect( caretSouthEast( caretRect, balloonRect ) ).to.deep.equal( {
  104. left: 501,
  105. name: 'caret_se',
  106. top: 123
  107. } );
  108. expect( caretNorthEast( caretRect, balloonRect ) ).to.deep.equal( {
  109. left: 501,
  110. name: 'caret_ne',
  111. top: -55
  112. } );
  113. expect( caretSouthWest( caretRect, balloonRect ) ).to.deep.equal( {
  114. left: 301,
  115. name: 'caret_sw',
  116. top: 123
  117. } );
  118. expect( caretNorthWest( caretRect, balloonRect ) ).to.deep.equal( {
  119. left: 301,
  120. name: 'caret_nw',
  121. top: -55
  122. } );
  123. } );
  124. } );
  125. it( 'should re-calculate position on typing', () => {
  126. setData( model, '<paragraph>foo []</paragraph>' );
  127. stubSelectionRects( [ caretRect ] );
  128. model.change( writer => {
  129. writer.insertText( '@', doc.selection.getFirstPosition() );
  130. } );
  131. return waitForDebounce()
  132. .then( () => {
  133. sinon.assert.calledOnce( pinSpy );
  134. model.change( writer => {
  135. writer.insertText( 't', doc.selection.getFirstPosition() );
  136. } );
  137. } ).then( () => {
  138. sinon.assert.calledTwice( pinSpy );
  139. } );
  140. } );
  141. } );
  142. describe( 'typing integration', () => {
  143. describe( 'static list with default trigger', () => {
  144. beforeEach( () => {
  145. return createClassicTestEditor( staticConfig );
  146. } );
  147. it( 'should show panel for matched marker', () => {
  148. setData( model, '<paragraph>foo []</paragraph>' );
  149. model.change( writer => {
  150. writer.insertText( '@', doc.selection.getFirstPosition() );
  151. } );
  152. return waitForDebounce()
  153. .then( () => {
  154. expect( panelView.isVisible ).to.be.true;
  155. expect( listView.items ).to.have.length( 5 );
  156. } );
  157. } );
  158. it( 'should show filtered results for matched text', () => {
  159. setData( model, '<paragraph>foo []</paragraph>' );
  160. model.change( writer => {
  161. writer.insertText( '@', doc.selection.getFirstPosition() );
  162. } );
  163. model.change( writer => {
  164. writer.insertText( 'T', doc.selection.getFirstPosition() );
  165. } );
  166. return waitForDebounce()
  167. .then( () => {
  168. expect( panelView.isVisible ).to.be.true;
  169. expect( listView.items ).to.have.length( 1 );
  170. } );
  171. } );
  172. it( 'should focus the first item in panel', () => {
  173. setData( model, '<paragraph>foo []</paragraph>' );
  174. model.change( writer => {
  175. writer.insertText( '@', doc.selection.getFirstPosition() );
  176. } );
  177. return waitForDebounce()
  178. .then( () => {
  179. const button = listView.items.get( 0 ).children.get( 0 );
  180. expect( button.isOn ).to.be.true;
  181. } );
  182. } );
  183. it( 'should hide panel if no matched items', () => {
  184. setData( model, '<paragraph>foo []</paragraph>' );
  185. model.change( writer => {
  186. writer.insertText( '@', doc.selection.getFirstPosition() );
  187. } );
  188. return waitForDebounce()
  189. .then( () => expect( panelView.isVisible ).to.be.true )
  190. .then( () => {
  191. model.change( writer => {
  192. writer.insertText( 'x', doc.selection.getFirstPosition() );
  193. } );
  194. } )
  195. .then( waitForDebounce )
  196. .then( () => {
  197. expect( panelView.isVisible ).to.be.false;
  198. expect( listView.items ).to.have.length( 0 );
  199. } );
  200. } );
  201. it( 'should hide panel when text was unmatched', () => {
  202. setData( model, '<paragraph>foo []</paragraph>' );
  203. model.change( writer => {
  204. writer.insertText( '@', doc.selection.getFirstPosition() );
  205. } );
  206. return waitForDebounce()
  207. .then( () => expect( panelView.isVisible ).to.be.true )
  208. .then( () => {
  209. model.change( writer => {
  210. const end = doc.selection.getFirstPosition();
  211. const start = end.getShiftedBy( -1 );
  212. writer.remove( writer.createRange( start, end ) );
  213. } );
  214. } )
  215. .then( waitForDebounce )
  216. .then( () => expect( panelView.isVisible ).to.be.false );
  217. } );
  218. } );
  219. describe( 'asynchronous list with custom trigger', () => {
  220. beforeEach( () => {
  221. const issuesNumbers = [ '100', '101', '102', '103' ];
  222. return createClassicTestEditor( [
  223. {
  224. marker: '#',
  225. feed: feedText => {
  226. return new Promise( resolve => {
  227. setTimeout( () => {
  228. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  229. }, 20 );
  230. } );
  231. }
  232. }
  233. ] );
  234. } );
  235. it( 'should show panel for matched marker', () => {
  236. setData( model, '<paragraph>foo []</paragraph>' );
  237. model.change( writer => {
  238. writer.insertText( '#', doc.selection.getFirstPosition() );
  239. } );
  240. return waitForDebounce()
  241. .then( () => {
  242. expect( panelView.isVisible ).to.be.true;
  243. expect( listView.items ).to.have.length( 4 );
  244. } );
  245. } );
  246. it( 'should show filtered results for matched text', () => {
  247. setData( model, '<paragraph>foo []</paragraph>' );
  248. model.change( writer => {
  249. writer.insertText( '#', doc.selection.getFirstPosition() );
  250. } );
  251. model.change( writer => {
  252. writer.insertText( '2', doc.selection.getFirstPosition() );
  253. } );
  254. return waitForDebounce()
  255. .then( () => {
  256. expect( panelView.isVisible ).to.be.true;
  257. expect( listView.items ).to.have.length( 1 );
  258. } );
  259. } );
  260. it( 'should hide panel if no matched items', () => {
  261. setData( model, '<paragraph>foo []</paragraph>' );
  262. model.change( writer => {
  263. writer.insertText( '#', doc.selection.getFirstPosition() );
  264. } );
  265. return waitForDebounce()
  266. .then( () => expect( panelView.isVisible ).to.be.true )
  267. .then( () => {
  268. model.change( writer => {
  269. writer.insertText( 'x', doc.selection.getFirstPosition() );
  270. } );
  271. } )
  272. .then( waitForDebounce )
  273. .then( () => {
  274. expect( panelView.isVisible ).to.be.false;
  275. expect( listView.items ).to.have.length( 0 );
  276. } );
  277. } );
  278. it( 'should hide panel when text was unmatched', () => {
  279. setData( model, '<paragraph>foo []</paragraph>' );
  280. model.change( writer => {
  281. writer.insertText( '#', doc.selection.getFirstPosition() );
  282. } );
  283. return waitForDebounce()
  284. .then( () => expect( panelView.isVisible ).to.be.true )
  285. .then( () => {
  286. model.change( writer => {
  287. const end = doc.selection.getFirstPosition();
  288. const start = end.getShiftedBy( -1 );
  289. writer.remove( writer.createRange( start, end ) );
  290. } );
  291. } )
  292. .then( waitForDebounce )
  293. .then( () => expect( panelView.isVisible ).to.be.false );
  294. } );
  295. } );
  296. } );
  297. describe( 'keys', () => {
  298. beforeEach( () => {
  299. return createClassicTestEditor( staticConfig );
  300. } );
  301. describe( 'arrows', () => {
  302. it( 'should cycle down on arrow down', () => {
  303. setData( model, '<paragraph>foo []</paragraph>' );
  304. model.change( writer => {
  305. writer.insertText( '@', doc.selection.getFirstPosition() );
  306. } );
  307. return waitForDebounce()
  308. .then( () => {
  309. const buttons = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  310. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  311. const keyEvtData = {
  312. keyCode: keyCodes.arrowdown,
  313. preventDefault: sinon.spy(),
  314. stopPropagation: sinon.spy()
  315. };
  316. fireKeyDownEvent( keyEvtData );
  317. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, true, false, false, false ] );
  318. fireKeyDownEvent( keyEvtData );
  319. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, true, false, false ] );
  320. fireKeyDownEvent( keyEvtData );
  321. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, false, true, false ] );
  322. fireKeyDownEvent( keyEvtData );
  323. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, false, false, true ] );
  324. fireKeyDownEvent( keyEvtData );
  325. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  326. } );
  327. } );
  328. it( 'should cycle up on arrow up', () => {
  329. setData( model, '<paragraph>foo []</paragraph>' );
  330. model.change( writer => {
  331. writer.insertText( '@', doc.selection.getFirstPosition() );
  332. } );
  333. return waitForDebounce()
  334. .then( () => {
  335. const buttons = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  336. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  337. const keyEvtData = {
  338. keyCode: keyCodes.arrowup,
  339. preventDefault: sinon.spy(),
  340. stopPropagation: sinon.spy()
  341. };
  342. fireKeyDownEvent( keyEvtData );
  343. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, false, false, true ] );
  344. fireKeyDownEvent( keyEvtData );
  345. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, false, true, false ] );
  346. fireKeyDownEvent( keyEvtData );
  347. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, true, false, false ] );
  348. fireKeyDownEvent( keyEvtData );
  349. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, true, false, false, false ] );
  350. fireKeyDownEvent( keyEvtData );
  351. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  352. } );
  353. } );
  354. } );
  355. describe( 'enter', () => {
  356. it( 'should execute selected button', () => {
  357. setData( model, '<paragraph>foo []</paragraph>' );
  358. model.change( writer => {
  359. writer.insertText( '@', doc.selection.getFirstPosition() );
  360. } );
  361. const command = editor.commands.get( 'mention' );
  362. const spy = testUtils.sinon.spy( command, 'execute' );
  363. return waitForDebounce()
  364. .then( () => {
  365. const buttons = [ ...listView.items ].map( listView => listView.children.get( 0 ) );
  366. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ true, false, false, false, false ] );
  367. fireKeyDownEvent( {
  368. keyCode: keyCodes.arrowup,
  369. preventDefault: sinon.spy(),
  370. stopPropagation: sinon.spy()
  371. } );
  372. expect( buttons.map( b => b.isOn ) ).to.deep.equal( [ false, false, false, false, true ] );
  373. fireKeyDownEvent( {
  374. keyCode: keyCodes.enter,
  375. preventDefault: sinon.spy(),
  376. stopPropagation: sinon.spy()
  377. } );
  378. sinon.assert.calledOnce( spy );
  379. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  380. expect( commandOptions ).to.have.property( 'mention', 'Ted' );
  381. expect( commandOptions ).to.have.property( 'marker', '@' );
  382. expect( commandOptions ).to.have.property( 'range' );
  383. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  384. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  385. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  386. } );
  387. } );
  388. } );
  389. describe.skip( 'tab', () => {} );
  390. } );
  391. describe( 'itemRenderer', () => {
  392. beforeEach( () => {
  393. const issues = [
  394. { id: '1002', title: 'Some bug in editor.' },
  395. { id: '1003', title: 'Introduce this feature.' },
  396. { id: '1004', title: 'Missing docs.' }
  397. ];
  398. return createClassicTestEditor( [
  399. {
  400. marker: '#',
  401. feed: feedText => {
  402. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  403. },
  404. itemRenderer: item => {
  405. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  406. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  407. return span;
  408. }
  409. }
  410. ] );
  411. } );
  412. it( 'should show panel for matched marker', () => {
  413. setData( model, '<paragraph>foo []</paragraph>' );
  414. model.change( writer => {
  415. writer.insertText( '#', doc.selection.getFirstPosition() );
  416. } );
  417. return waitForDebounce()
  418. .then( () => {
  419. expect( panelView.isVisible ).to.be.true;
  420. expect( listView.items ).to.have.length( 3 );
  421. } );
  422. } );
  423. } );
  424. describe( 'execute', () => {
  425. beforeEach( () => createClassicTestEditor( staticConfig ) );
  426. it( 'should call the mention command with proper options', () => {
  427. setData( model, '<paragraph>foo []</paragraph>' );
  428. model.change( writer => {
  429. writer.insertText( '@', doc.selection.getFirstPosition() );
  430. } );
  431. const command = editor.commands.get( 'mention' );
  432. const spy = testUtils.sinon.spy( command, 'execute' );
  433. return waitForDebounce()
  434. .then( () => {
  435. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  436. sinon.assert.calledOnce( spy );
  437. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  438. expect( commandOptions ).to.have.property( 'mention', 'Barney' );
  439. expect( commandOptions ).to.have.property( 'marker', '@' );
  440. expect( commandOptions ).to.have.property( 'range' );
  441. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  442. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  443. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  444. } );
  445. } );
  446. it( 'should hide panel on execute', () => {
  447. setData( model, '<paragraph>foo []</paragraph>' );
  448. model.change( writer => {
  449. writer.insertText( '@', doc.selection.getFirstPosition() );
  450. } );
  451. return waitForDebounce()
  452. .then( () => {
  453. listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  454. expect( panelView.isVisible ).to.be.false;
  455. } );
  456. } );
  457. } );
  458. function createClassicTestEditor( mentionConfig ) {
  459. return ClassicTestEditor
  460. .create( editorElement, {
  461. plugins: [ Paragraph, MentionEditing, MentionUI ],
  462. mention: mentionConfig
  463. } )
  464. .then( newEditor => {
  465. editor = newEditor;
  466. model = editor.model;
  467. doc = model.document;
  468. editingView = editor.editing.view;
  469. mentionUI = editor.plugins.get( MentionUI );
  470. panelView = mentionUI.panelView;
  471. mentionsView = mentionUI._mentionsView;
  472. listView = mentionsView.listView;
  473. editingView.attachDomRoot( editorElement );
  474. // Focus the engine.
  475. editingView.document.isFocused = true;
  476. editingView.getDomRoot().focus();
  477. // Remove all selection ranges from DOM before testing.
  478. window.getSelection().removeAllRanges();
  479. } );
  480. }
  481. function waitForDebounce() {
  482. return new Promise( resolve => {
  483. setTimeout( () => {
  484. resolve();
  485. }, 50 );
  486. } );
  487. }
  488. function fireKeyDownEvent( options ) {
  489. const eventInfo = new EventInfo( editingView.document, 'keydown' );
  490. const eventData = new DomEventData( editingView.document, {
  491. target: document.body
  492. }, options );
  493. editingView.document.fire( eventInfo, eventData );
  494. }
  495. function stubSelectionRects( rects ) {
  496. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  497. // Mock selection rect.
  498. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  499. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  500. sinon.stub( domRange, 'getClientRects' )
  501. .returns( rects );
  502. return domRange;
  503. } );
  504. }
  505. } );