8
0

widgettoolbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import WidgetToolbar from '../src/widgettoolbar';
  14. import Widget from '../src/widget';
  15. import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  16. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  17. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  18. import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  19. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  20. import View from '@ckeditor/ckeditor5-ui/src/view';
  21. describe( 'WidgetToolbar', () => {
  22. let editor, model, 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. widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
  38. balloon = editor.plugins.get( 'ContextualBalloon' );
  39. } );
  40. } );
  41. afterEach( () => {
  42. editorElement.remove();
  43. return editor.destroy();
  44. } );
  45. it( 'should be loaded', () => {
  46. expect( editor.plugins.get( WidgetToolbar ) ).to.be.instanceOf( WidgetToolbar );
  47. } );
  48. describe( 'add()', () => {
  49. it( 'should create a widget toolbar and add it to the collection', () => {
  50. widgetToolbar.add( 'fake', {
  51. toolbarItems: editor.config.get( 'fake.toolbar' ),
  52. isVisible: () => false,
  53. } );
  54. expect( widgetToolbar._toolbars.size ).to.equal( 1 );
  55. expect( widgetToolbar._toolbars.get( 'fake' ) ).to.be.an( 'object' );
  56. } );
  57. it( 'should throw when adding two times widget with the same id', () => {
  58. widgetToolbar.add( 'fake', {
  59. toolbarItems: editor.config.get( 'fake.toolbar' ),
  60. isVisible: () => false
  61. } );
  62. expect( () => {
  63. widgetToolbar.add( 'fake', {
  64. toolbarItems: editor.config.get( 'fake.toolbar' ),
  65. isVisible: () => false
  66. } );
  67. } ).to.throw( /widget-toolbar-duplicated/ );
  68. } );
  69. } );
  70. describe( 'remove', () => {
  71. it( 'should remove given widget toolbar', () => {
  72. widgetToolbar.add( 'fake', {
  73. toolbarItems: editor.config.get( 'fake.toolbar' ),
  74. isVisible: () => false
  75. } );
  76. widgetToolbar.remove( 'fake' );
  77. expect( widgetToolbar._toolbars.size ).to.equal( 0 );
  78. } );
  79. it( 'should throw an error if a toolbar does not exist', () => {
  80. widgetToolbar.add( 'foo', {
  81. toolbarItems: editor.config.get( 'fake.toolbar' ),
  82. isVisible: () => false
  83. } );
  84. expect( () => {
  85. widgetToolbar.remove( 'bar' );
  86. } ).to.throw( /widget-toolbar-does-not-exist/ );
  87. } );
  88. } );
  89. describe( 'has', () => {
  90. it( 'should return `true` when a toolbar with given id was added', () => {
  91. widgetToolbar.add( 'foo', {
  92. toolbarItems: editor.config.get( 'fake.toolbar' ),
  93. isVisible: () => false
  94. } );
  95. expect( widgetToolbar.has( 'foo' ) ).to.be.true;
  96. } );
  97. it( 'should return `false` when a toolbar with given id was not added', () => {
  98. widgetToolbar.add( 'foo', {
  99. toolbarItems: editor.config.get( 'fake.toolbar' ),
  100. isVisible: () => false
  101. } );
  102. expect( widgetToolbar.has( 'bar' ) ).to.be.false;
  103. } );
  104. } );
  105. describe( 'integration tests', () => {
  106. beforeEach( () => {
  107. editor.ui.focusTracker.isFocused = true;
  108. } );
  109. it( 'toolbar should be visible when the `isVisible` callback returns true', () => {
  110. widgetToolbar.add( 'fake', {
  111. toolbarItems: editor.config.get( 'fake.toolbar' ),
  112. isVisible: isWidgetSelected
  113. } );
  114. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  115. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  116. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  117. } );
  118. it( 'toolbar should be hidden when the `isVisible` callback returns false', () => {
  119. widgetToolbar.add( 'fake', {
  120. toolbarItems: editor.config.get( 'fake.toolbar' ),
  121. isVisible: isWidgetSelected
  122. } );
  123. setData( model, '[<paragraph>foo</paragraph>]<fake-widget></fake-widget>' );
  124. expect( balloon.visibleView ).to.equal( null );
  125. } );
  126. it( 'toolbar should be hidden when the `isVisible` callback returns false #2', () => {
  127. widgetToolbar.add( 'fake', {
  128. toolbarItems: editor.config.get( 'fake.toolbar' ),
  129. isVisible: isWidgetSelected
  130. } );
  131. setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
  132. model.change( writer => {
  133. // Select the <paragraph>foo</paragraph>.
  134. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  135. } );
  136. expect( balloon.visibleView ).to.equal( null );
  137. } );
  138. it( 'toolbar should update its position when other widget is selected', () => {
  139. widgetToolbar.add( 'fake', {
  140. toolbarItems: editor.config.get( 'fake.toolbar' ),
  141. isVisible: isWidgetSelected
  142. } );
  143. setData( model, '[<fake-widget></fake-widget>]<fake-widget></fake-widget>' );
  144. model.change( writer => {
  145. // Select the second widget.
  146. writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
  147. } );
  148. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  149. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  150. } );
  151. it( 'it should be possible to create a widget toolbar for content inside the widget', () => {
  152. widgetToolbar.add( 'fake', {
  153. toolbarItems: editor.config.get( 'fake.toolbar' ),
  154. isVisible: doesWidgetContainSelection
  155. } );
  156. setData( model, '<fake-widget>[foo]</fake-widget>' );
  157. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  158. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  159. } );
  160. it( 'toolbar should not engage when is in the balloon yet invisible', () => {
  161. widgetToolbar.add( 'fake', {
  162. toolbarItems: editor.config.get( 'fake.toolbar' ),
  163. isVisible: isWidgetSelected
  164. } );
  165. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  166. setData( model, '[<fake-widget></fake-widget>]' );
  167. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  168. const lastView = new View();
  169. lastView.element = document.createElement( 'div' );
  170. balloon.add( {
  171. view: lastView,
  172. position: {
  173. target: document.body
  174. }
  175. } );
  176. expect( balloon.visibleView ).to.equal( lastView );
  177. editor.ui.fire( 'update' );
  178. expect( balloon.visibleView ).to.equal( lastView );
  179. } );
  180. } );
  181. } );
  182. describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
  183. let clock, editor, model, balloon, balloonToolbar, widgetToolbar, editorElement;
  184. testUtils.createSinonSandbox();
  185. beforeEach( () => {
  186. editorElement = global.document.createElement( 'div' );
  187. global.document.body.appendChild( editorElement );
  188. clock = testUtils.sinon.useFakeTimers();
  189. return BalloonEditor
  190. .create( editorElement, {
  191. plugins: [ Paragraph, Image, FakeButton, WidgetToolbar, FakeWidget, Bold ],
  192. balloonToolbar: [ 'bold' ],
  193. fake: {
  194. toolbar: [ 'fake_button' ]
  195. }
  196. } )
  197. .then( newEditor => {
  198. editor = newEditor;
  199. model = newEditor.model;
  200. widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
  201. balloon = editor.plugins.get( 'ContextualBalloon' );
  202. balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  203. editor.editing.view.document.isFocused = true;
  204. } );
  205. } );
  206. afterEach( () => {
  207. editorElement.remove();
  208. return editor.destroy();
  209. } );
  210. it( 'balloon toolbar should be hidden when the widget is selected', () => {
  211. widgetToolbar.add( 'fake', {
  212. toolbarItems: editor.config.get( 'fake.toolbar' ),
  213. isVisible: isWidgetSelected,
  214. } );
  215. const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
  216. setData( model, '[<fake-widget></fake-widget>]<paragraph>foo</paragraph>' );
  217. clock.tick( 200 );
  218. expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
  219. } );
  220. it( 'balloon toolbar should be visible when the widget is not selected', () => {
  221. widgetToolbar.add( 'fake', {
  222. toolbarItems: editor.config.get( 'fake.toolbar' ),
  223. isVisible: isWidgetSelected
  224. } );
  225. setData( model, '<fake-widget></fake-widget><paragraph>[foo]</paragraph>' );
  226. clock.tick( 200 );
  227. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  228. } );
  229. } );
  230. function isWidgetSelected( selection ) {
  231. const viewElement = selection.getSelectedElement();
  232. return !!( viewElement && isWidget( viewElement ) );
  233. }
  234. function doesWidgetContainSelection( selection ) {
  235. const pos = selection.getFirstPosition();
  236. let node = pos.parent;
  237. while ( node ) {
  238. if ( node.is( 'element' ) && isWidget( node ) ) {
  239. return true;
  240. }
  241. node = node.parent;
  242. }
  243. return false;
  244. }
  245. // Plugin that adds fake_button to editor's component factory.
  246. class FakeButton extends Plugin {
  247. init() {
  248. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  249. const view = new ButtonView( locale );
  250. view.set( {
  251. label: 'fake button'
  252. } );
  253. return view;
  254. } );
  255. }
  256. }
  257. // Simple widget plugin
  258. // It registers `<fake-widget>` block in model and represents `div` in the view.
  259. // It allows having text inside self.
  260. class FakeWidget extends Plugin {
  261. static get requires() {
  262. return [ Widget ];
  263. }
  264. init() {
  265. const editor = this.editor;
  266. const schema = editor.model.schema;
  267. schema.register( 'fake-widget', {
  268. isObject: true,
  269. isBlock: true,
  270. allowWhere: '$block',
  271. } );
  272. schema.extend( '$text', { allowIn: 'fake-widget' } );
  273. const conversion = editor.conversion;
  274. conversion.for( 'dataDowncast' ).add( downcastElementToElement( {
  275. model: 'fake-widget',
  276. view: ( modelElement, viewWriter ) => {
  277. return viewWriter.createContainerElement( 'div' );
  278. }
  279. } ) );
  280. conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  281. model: 'fake-widget',
  282. view: ( modelElement, viewWriter ) => {
  283. const fakeWidget = viewWriter.createContainerElement( 'div' );
  284. return toWidget( fakeWidget, viewWriter, { label: 'fake-widget' } );
  285. }
  286. } ) );
  287. conversion.for( 'upcast' )
  288. .add( upcastElementToElement( {
  289. view: {
  290. name: 'div'
  291. },
  292. model: ( viewMedia, modelWriter ) => {
  293. return modelWriter.createElement( 'fake-widget' );
  294. }
  295. } ) )
  296. }
  297. }