mentionui.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document */
  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 MentionUI from '../src/mentionui';
  12. import MentionEditing from '../src/mentionediting';
  13. import MentionsView from '../src/ui/mentionsview';
  14. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. describe( 'BalloonToolbar', () => {
  16. let editor, model, doc, editingView, mentionUI, editorElement;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ Paragraph, MentionEditing, MentionUI ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. doc = model.document;
  29. editingView = editor.editing.view;
  30. mentionUI = editor.plugins.get( MentionUI );
  31. editingView.attachDomRoot( editorElement );
  32. // Focus the engine.
  33. editingView.document.isFocused = true;
  34. editingView.getDomRoot().focus();
  35. // Remove all selection ranges from DOM before testing.
  36. window.getSelection().removeAllRanges();
  37. } );
  38. } );
  39. afterEach( () => {
  40. sinon.restore();
  41. editorElement.remove();
  42. return editor.destroy();
  43. } );
  44. it( 'should create a plugin instance', () => {
  45. expect( mentionUI ).to.instanceOf( Plugin );
  46. expect( mentionUI ).to.instanceOf( MentionUI );
  47. } );
  48. describe( 'pluginName', () => {
  49. it( 'should return plugin by its name', () => {
  50. expect( editor.plugins.get( 'MentionUI' ) ).to.equal( mentionUI );
  51. } );
  52. } );
  53. describe( 'child views', () => {
  54. describe( 'panelView', () => {
  55. it( 'should create a view instance', () => {
  56. expect( mentionUI.panelView ).to.instanceof( BalloonPanelView );
  57. } );
  58. it( 'should be added to the ui.view.body collection', () => {
  59. expect( Array.from( editor.ui.view.body ) ).to.include( mentionUI.panelView );
  60. } );
  61. it( 'should have disabled arrow', () => {
  62. expect( mentionUI.panelView.withArrow ).to.be.false;
  63. } );
  64. it( 'should have added MentionView as a child', () => {
  65. expect( mentionUI.panelView.content.get( 0 ) ).to.be.instanceof( MentionsView );
  66. } );
  67. } );
  68. } );
  69. describe( 'triggers', () => {
  70. it( 'should show panel for matched marker', () => {
  71. setData( model, '<paragraph>foo []</paragraph>' );
  72. model.change( writer => {
  73. writer.insertText( '@', doc.selection.getFirstPosition() );
  74. } );
  75. expect( mentionUI.panelView.isVisible ).to.be.true;
  76. expect( mentionUI._mentionsView.listView.items ).to.have.length( 3 );
  77. } );
  78. it( 'should show filtered results for matched text', () => {
  79. setData( model, '<paragraph>foo []</paragraph>' );
  80. model.change( writer => {
  81. writer.insertText( '@', doc.selection.getFirstPosition() );
  82. } );
  83. model.change( writer => {
  84. writer.insertText( 'f', doc.selection.getFirstPosition() );
  85. } );
  86. expect( mentionUI.panelView.isVisible ).to.be.true;
  87. expect( mentionUI._mentionsView.listView.items ).to.have.length( 1 );
  88. } );
  89. it( 'should hide panel if no matched items', () => {
  90. setData( model, '<paragraph>foo []</paragraph>' );
  91. model.change( writer => {
  92. writer.insertText( '@', doc.selection.getFirstPosition() );
  93. } );
  94. expect( mentionUI.panelView.isVisible ).to.be.true;
  95. model.change( writer => {
  96. writer.insertText( 'x', doc.selection.getFirstPosition() );
  97. } );
  98. expect( mentionUI.panelView.isVisible ).to.be.false;
  99. expect( mentionUI._mentionsView.listView.items ).to.have.length( 0 );
  100. } );
  101. it( 'should hide panel when text was unmatched', () => {
  102. setData( model, '<paragraph>foo []</paragraph>' );
  103. model.change( writer => {
  104. writer.insertText( '@', doc.selection.getFirstPosition() );
  105. } );
  106. expect( mentionUI.panelView.isVisible ).to.be.true;
  107. model.change( writer => {
  108. const end = doc.selection.getFirstPosition();
  109. const start = end.getShiftedBy( -1 );
  110. writer.remove( writer.createRange( start, end ) );
  111. } );
  112. expect( mentionUI.panelView.isVisible ).to.be.false;
  113. } );
  114. } );
  115. } );