8
0

mentionui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import MentionUI from '../src/mentionui';
  13. import MentionEditing from '../src/mentionediting';
  14. import MentionsView from '../src/ui/mentionsview';
  15. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. describe( 'BalloonToolbar', () => {
  17. let editor, model, doc, editingView, mentionUI, editorElement;
  18. const staticConfig = [
  19. { feed: [ 'Barney', 'Lily', 'Marshall', 'Robin', 'Ted' ] }
  20. ];
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. } );
  26. afterEach( () => {
  27. sinon.restore();
  28. editorElement.remove();
  29. return editor.destroy();
  30. } );
  31. it( 'should create a plugin instance', () => {
  32. return createClassicTestEditor().then( () => {
  33. expect( mentionUI ).to.instanceOf( Plugin );
  34. expect( mentionUI ).to.instanceOf( MentionUI );
  35. } );
  36. } );
  37. describe( 'pluginName', () => {
  38. it( 'should return plugin by its name', () => {
  39. return createClassicTestEditor().then( () => {
  40. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  41. } );
  42. } );
  43. } );
  44. describe( 'child views', () => {
  45. beforeEach( () => createClassicTestEditor() );
  46. describe( 'panelView', () => {
  47. it( 'should create a view instance', () => {
  48. expect( mentionUI.panelView ).to.instanceof( BalloonPanelView );
  49. } );
  50. it( 'should be added to the ui.view.body collection', () => {
  51. expect( Array.from( editor.ui.view.body ) ).to.include( mentionUI.panelView );
  52. } );
  53. it( 'should have disabled arrow', () => {
  54. expect( mentionUI.panelView.withArrow ).to.be.false;
  55. } );
  56. it( 'should have added MentionView as a child', () => {
  57. expect( mentionUI.panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  58. } );
  59. } );
  60. } );
  61. describe( 'typing integration', () => {
  62. describe( 'static list with default trigger', () => {
  63. beforeEach( () => {
  64. return createClassicTestEditor( staticConfig );
  65. } );
  66. it( 'should show panel for matched marker', () => {
  67. setData( model, '<paragraph>foo []</paragraph>' );
  68. model.change( writer => {
  69. writer.insertText( '@', doc.selection.getFirstPosition() );
  70. } );
  71. return waitForDebounce()
  72. .then( () => {
  73. expect( mentionUI.panelView.isVisible ).to.be.true;
  74. expect( mentionUI._mentionsView.listView.items ).to.have.length( 5 );
  75. } );
  76. } );
  77. it( 'should show filtered results for matched text', () => {
  78. setData( model, '<paragraph>foo []</paragraph>' );
  79. model.change( writer => {
  80. writer.insertText( '@', doc.selection.getFirstPosition() );
  81. } );
  82. model.change( writer => {
  83. writer.insertText( 'T', doc.selection.getFirstPosition() );
  84. } );
  85. return waitForDebounce()
  86. .then( () => {
  87. expect( mentionUI.panelView.isVisible ).to.be.true;
  88. expect( mentionUI._mentionsView.listView.items ).to.have.length( 1 );
  89. } );
  90. } );
  91. it( 'should hide panel if no matched items', () => {
  92. setData( model, '<paragraph>foo []</paragraph>' );
  93. model.change( writer => {
  94. writer.insertText( '@', doc.selection.getFirstPosition() );
  95. } );
  96. return waitForDebounce()
  97. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  98. .then( () => {
  99. model.change( writer => {
  100. writer.insertText( 'x', doc.selection.getFirstPosition() );
  101. } );
  102. } )
  103. .then( waitForDebounce )
  104. .then( () => {
  105. expect( mentionUI.panelView.isVisible ).to.be.false;
  106. expect( mentionUI._mentionsView.listView.items ).to.have.length( 0 );
  107. } );
  108. } );
  109. it( 'should hide panel when text was unmatched', () => {
  110. setData( model, '<paragraph>foo []</paragraph>' );
  111. model.change( writer => {
  112. writer.insertText( '@', doc.selection.getFirstPosition() );
  113. } );
  114. return waitForDebounce()
  115. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  116. .then( () => {
  117. model.change( writer => {
  118. const end = doc.selection.getFirstPosition();
  119. const start = end.getShiftedBy( -1 );
  120. writer.remove( writer.createRange( start, end ) );
  121. } );
  122. } )
  123. .then( waitForDebounce )
  124. .then( () => expect( mentionUI.panelView.isVisible ).to.be.false );
  125. } );
  126. } );
  127. describe( 'asynchronous list with custom trigger', () => {
  128. beforeEach( () => {
  129. const issuesNumbers = [ '100', '101', '102', '103' ];
  130. return createClassicTestEditor( [
  131. {
  132. marker: '#',
  133. feed: feedText => {
  134. return new Promise( resolve => {
  135. setTimeout( () => {
  136. resolve( issuesNumbers.filter( number => number.includes( feedText ) ) );
  137. }, 20 );
  138. } );
  139. }
  140. }
  141. ] );
  142. } );
  143. it( 'should show panel for matched marker', () => {
  144. setData( model, '<paragraph>foo []</paragraph>' );
  145. model.change( writer => {
  146. writer.insertText( '#', doc.selection.getFirstPosition() );
  147. } );
  148. return waitForDebounce()
  149. .then( () => {
  150. expect( mentionUI.panelView.isVisible ).to.be.true;
  151. expect( mentionUI._mentionsView.listView.items ).to.have.length( 4 );
  152. } );
  153. } );
  154. it( 'should show filtered results for matched text', () => {
  155. setData( model, '<paragraph>foo []</paragraph>' );
  156. model.change( writer => {
  157. writer.insertText( '#', doc.selection.getFirstPosition() );
  158. } );
  159. model.change( writer => {
  160. writer.insertText( '2', doc.selection.getFirstPosition() );
  161. } );
  162. return waitForDebounce()
  163. .then( () => {
  164. expect( mentionUI.panelView.isVisible ).to.be.true;
  165. expect( mentionUI._mentionsView.listView.items ).to.have.length( 1 );
  166. } );
  167. } );
  168. it( 'should hide panel if no matched items', () => {
  169. setData( model, '<paragraph>foo []</paragraph>' );
  170. model.change( writer => {
  171. writer.insertText( '#', doc.selection.getFirstPosition() );
  172. } );
  173. return waitForDebounce()
  174. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  175. .then( () => {
  176. model.change( writer => {
  177. writer.insertText( 'x', doc.selection.getFirstPosition() );
  178. } );
  179. } )
  180. .then( waitForDebounce )
  181. .then( () => {
  182. expect( mentionUI.panelView.isVisible ).to.be.false;
  183. expect( mentionUI._mentionsView.listView.items ).to.have.length( 0 );
  184. } );
  185. } );
  186. it( 'should hide panel when text was unmatched', () => {
  187. setData( model, '<paragraph>foo []</paragraph>' );
  188. model.change( writer => {
  189. writer.insertText( '#', doc.selection.getFirstPosition() );
  190. } );
  191. return waitForDebounce()
  192. .then( () => expect( mentionUI.panelView.isVisible ).to.be.true )
  193. .then( () => {
  194. model.change( writer => {
  195. const end = doc.selection.getFirstPosition();
  196. const start = end.getShiftedBy( -1 );
  197. writer.remove( writer.createRange( start, end ) );
  198. } );
  199. } )
  200. .then( waitForDebounce )
  201. .then( () => expect( mentionUI.panelView.isVisible ).to.be.false );
  202. } );
  203. } );
  204. } );
  205. describe( 'itemRenderer', () => {
  206. beforeEach( () => {
  207. const issues = [
  208. { id: '1002', title: 'Some bug in editor.' },
  209. { id: '1003', title: 'Introduce this feature.' },
  210. { id: '1004', title: 'Missing docs.' }
  211. ];
  212. return createClassicTestEditor( [
  213. {
  214. marker: '#',
  215. feed: feedText => {
  216. return Promise.resolve( issues.filter( issue => issue.id.includes( feedText ) ) );
  217. },
  218. itemRenderer: item => {
  219. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  220. span.innerHTML = `<span id="${ item.id }">@${ item.title }</span>`;
  221. return span;
  222. }
  223. }
  224. ] );
  225. } );
  226. it( 'should show panel for matched marker', () => {
  227. setData( model, '<paragraph>foo []</paragraph>' );
  228. model.change( writer => {
  229. writer.insertText( '#', doc.selection.getFirstPosition() );
  230. } );
  231. return waitForDebounce()
  232. .then( () => {
  233. expect( mentionUI.panelView.isVisible ).to.be.true;
  234. expect( mentionUI._mentionsView.listView.items ).to.have.length( 3 );
  235. } );
  236. } );
  237. } );
  238. describe( 'execute', () => {
  239. beforeEach( () => createClassicTestEditor( staticConfig ) );
  240. it( 'should call the mention command with proper options', () => {
  241. setData( model, '<paragraph>foo []</paragraph>' );
  242. model.change( writer => {
  243. writer.insertText( '@', doc.selection.getFirstPosition() );
  244. } );
  245. const command = editor.commands.get( 'mention' );
  246. const spy = testUtils.sinon.spy( command, 'execute' );
  247. return waitForDebounce()
  248. .then( () => {
  249. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  250. sinon.assert.calledOnce( spy );
  251. const commandOptions = spy.getCall( 0 ).args[ 0 ];
  252. expect( commandOptions ).to.have.property( 'mention', 'Barney' );
  253. expect( commandOptions ).to.have.property( 'marker', '@' );
  254. expect( commandOptions ).to.have.property( 'range' );
  255. const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
  256. const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
  257. expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
  258. } );
  259. } );
  260. it( 'should hide panel on execute', () => {
  261. setData( model, '<paragraph>foo []</paragraph>' );
  262. model.change( writer => {
  263. writer.insertText( '@', doc.selection.getFirstPosition() );
  264. } );
  265. return waitForDebounce()
  266. .then( () => {
  267. mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
  268. expect( mentionUI.panelView.isVisible ).to.be.false;
  269. } );
  270. } );
  271. } );
  272. function createClassicTestEditor( mentionConfig ) {
  273. return ClassicTestEditor
  274. .create( editorElement, {
  275. plugins: [ Paragraph, MentionEditing, MentionUI ],
  276. mention: mentionConfig
  277. } )
  278. .then( newEditor => {
  279. editor = newEditor;
  280. model = editor.model;
  281. doc = model.document;
  282. editingView = editor.editing.view;
  283. mentionUI = editor.plugins.get( MentionUI );
  284. editingView.attachDomRoot( editorElement );
  285. // Focus the engine.
  286. editingView.document.isFocused = true;
  287. editingView.getDomRoot().focus();
  288. // Remove all selection ranges from DOM before testing.
  289. window.getSelection().removeAllRanges();
  290. } );
  291. }
  292. function waitForDebounce() {
  293. return new Promise( resolve => {
  294. setTimeout( () => {
  295. resolve();
  296. }, 50 );
  297. } );
  298. }
  299. } );