widgettoolbar.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import env from '@ckeditor/ckeditor5-utils/src/env';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import WidgetToolbar from '../src/widgettoolbar';
  17. import Widget from '../src/widget';
  18. import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  19. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  20. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  21. describe( 'WidgetToolbar', () => {
  22. let editor, model, doc, toolbar, balloon, widgetToolbar, editorElement;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. editorElement = global.document.createElement( 'div' );
  26. global.document.body.appendChild( editorElement );
  27. return ClassicEditor
  28. .create( editorElement, {
  29. plugins: [ Paragraph, Image, FakeButton, WidgetToolbar, FakeWidget ],
  30. fake: {
  31. toolbar: [ 'fake_button' ]
  32. }
  33. } )
  34. .then( newEditor => {
  35. editor = newEditor;
  36. model = newEditor.model;
  37. doc = model.document;
  38. widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
  39. balloon = editor.plugins.get( 'ContextualBalloon' );
  40. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return editor.destroy();
  45. } );
  46. it( 'should be loaded', () => {
  47. expect( editor.plugins.get( WidgetToolbar ) ).to.be.instanceOf( WidgetToolbar );
  48. } );
  49. describe( 'add()', () => {
  50. it( 'should create a widget toolbar and add it to the collection', () => {
  51. widgetToolbar.add( 'fake', {
  52. toolbarItems: editor.config.get( 'fake.toolbar' ),
  53. isVisible: () => false,
  54. } );
  55. expect( widgetToolbar._toolbars.size ).to.equal( 1 );
  56. expect( widgetToolbar._toolbars.get( 'fake' ) ).to.be.an( 'object' );
  57. } );
  58. it( 'should throw when adding two times widget with the same id', () => {
  59. widgetToolbar.add( 'fake', {
  60. toolbarItems: editor.config.get( 'fake.toolbar' ),
  61. isVisible: () => false
  62. } );
  63. expect( () => {
  64. widgetToolbar.add( 'fake', {
  65. toolbarItems: editor.config.get( 'fake.toolbar' ),
  66. isVisible: () => false
  67. } );
  68. } ).to.throw( /widget-toolbar-duplicated/ );
  69. } );
  70. } );
  71. describe( 'remove', () => {
  72. it( 'should remove given widget toolbar', () => {
  73. widgetToolbar.add( 'fake', {
  74. toolbarItems: editor.config.get( 'fake.toolbar' ),
  75. isVisible: () => false
  76. } );
  77. widgetToolbar.remove( 'fake' );
  78. expect( widgetToolbar._toolbars.size ).to.equal( 0 );
  79. } );
  80. it( 'should throw an error if a toolbar does not exist', () => {
  81. widgetToolbar.add( 'foo', {
  82. toolbarItems: editor.config.get( 'fake.toolbar' ),
  83. isVisible: () => false
  84. } );
  85. expect( () => {
  86. widgetToolbar.remove( 'bar' );
  87. } ).to.throw( /widget-toolbar-does-not-exist/ );
  88. } );
  89. } );
  90. describe( 'has', () => {
  91. it( 'should return `true` when a toolbar with given id was added', () => {
  92. widgetToolbar.add( 'foo', {
  93. toolbarItems: editor.config.get( 'fake.toolbar' ),
  94. isVisible: () => false
  95. } );
  96. expect( widgetToolbar.has( 'foo' ) ).to.be.true;
  97. } );
  98. it( 'should return `false` when a toolbar with given id was not added', () => {
  99. widgetToolbar.add( 'foo', {
  100. toolbarItems: editor.config.get( 'fake.toolbar' ),
  101. isVisible: () => false
  102. } );
  103. expect( widgetToolbar.has( 'bar' ) ).to.be.false;
  104. } );
  105. } );
  106. describe( 'integration tests', () => {
  107. beforeEach( () => {
  108. editor.ui.focusTracker.isFocused = true;
  109. } );
  110. it( 'should show widget toolbar when the `isVisible` callback returns true', () => {
  111. widgetToolbar.add( 'fake', {
  112. toolbarItems: editor.config.get( 'fake.toolbar' ),
  113. isVisible: selection => {
  114. const el = selection.getSelectedElement();
  115. return el && isWidget( el );
  116. }
  117. } );
  118. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  119. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  120. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  121. } );
  122. it( 'should hide widget toolbar when the `isVisible` callback returns false', () => {
  123. widgetToolbar.add( 'fake', {
  124. toolbarItems: editor.config.get( 'fake.toolbar' ),
  125. isVisible: selection => {
  126. const el = selection.getSelectedElement();
  127. return el && isWidget( el );
  128. }
  129. } );
  130. setData( model, '[<paragraph>foo</paragraph>]<fake-widget></fake-widget>' );
  131. expect( balloon.visibleView ).to.equal( null );
  132. } );
  133. it( 'should hide widget toolbar when the `isVisible` callback returns false', () => {
  134. widgetToolbar.add( 'fake', {
  135. toolbarItems: editor.config.get( 'fake.toolbar' ),
  136. isVisible: selection => {
  137. const el = selection.getSelectedElement();
  138. return el && isWidget( el );
  139. }
  140. } );
  141. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  142. model.change( writer => {
  143. // Select the paragraph content.
  144. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  145. } );
  146. expect( balloon.visibleView ).to.equal( null );
  147. } );
  148. it( 'should update toolbar position when other widget is being selected', () => {
  149. widgetToolbar.add( 'fake', {
  150. toolbarItems: editor.config.get( 'fake.toolbar' ),
  151. isVisible: selection => {
  152. const el = selection.getSelectedElement();
  153. return el && isWidget( el );
  154. }
  155. } );
  156. setData( model, '[<fake-widget></fake-widget>]<fake-widget></fake-widget>' );
  157. model.change( writer => {
  158. // Select the second widget.
  159. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  160. } );
  161. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  162. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  163. } );
  164. it( 'should be able to show toolbar for elements inside the widget', () => {
  165. widgetToolbar.add( 'fake', {
  166. toolbarItems: editor.config.get( 'fake.toolbar' ),
  167. isVisible: selection => {
  168. const pos = selection.getFirstPosition();
  169. let node = pos.parent;
  170. while( node ) {
  171. if ( node.is( 'element' ) && isWidget( node ) ) {
  172. return true;
  173. }
  174. node = node.parent;
  175. }
  176. return false;
  177. }
  178. } );
  179. setData( model, '<fake-widget>[foo]</fake-widget>' );
  180. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  181. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  182. } );
  183. } );
  184. // Plugin that adds fake_button to editor's component factory.
  185. class FakeButton extends Plugin {
  186. init() {
  187. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  188. const view = new ButtonView( locale );
  189. view.set( {
  190. label: 'fake button'
  191. } );
  192. return view;
  193. } );
  194. }
  195. }
  196. // Simple widget plugin
  197. class FakeWidget extends Plugin {
  198. static get requires() {
  199. return [ Widget ];
  200. }
  201. init() {
  202. const editor = this.editor;
  203. const schema = editor.model.schema;
  204. schema.register( 'fake-widget', {
  205. isObject: true,
  206. isBlock: true,
  207. allowWhere: '$block',
  208. } );
  209. schema.extend( '$text', { allowIn: 'fake-widget' } );
  210. const conversion = editor.conversion;
  211. conversion.for( 'dataDowncast' ).add( downcastElementToElement( {
  212. model: 'fake-widget',
  213. view: ( modelElement, viewWriter ) => {
  214. const fakeWidget = viewWriter.createContainerElement( 'div' );
  215. return fakeWidget;
  216. }
  217. } ) );
  218. conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  219. model: 'fake-widget',
  220. view: ( modelElement, viewWriter ) => {
  221. const fakeWidget = viewWriter.createContainerElement( 'div' );
  222. return toWidget( fakeWidget, viewWriter, { label: 'fake-widget' } );
  223. }
  224. } ) );
  225. conversion.for( 'upcast' )
  226. .add( upcastElementToElement( {
  227. view: {
  228. name: 'div'
  229. },
  230. model: ( viewMedia, modelWriter ) => {
  231. return modelWriter.createElement( 'fake-widget' );
  232. }
  233. } ) )
  234. }
  235. }
  236. } );