Parcourir la source

Changed WidgetToolbar to WidgetToolbarRepository.

Maciej Bukowski il y a 7 ans
Parent
commit
1f7fd272d0

+ 18 - 15
packages/ckeditor5-widget/src/widgettoolbar.js → packages/ckeditor5-widget/src/widgettoolbarrepository.js

@@ -7,32 +7,33 @@ import { isWidget } from './utils';
 const defaultBalloonClassName = 'ck-toolbar-container';
 
 /**
- * Widget toolbar plugin. It ease the process of creating widget toolbars by handling the whole rendering process and providing concise API.
+ * Widget toolbar repository plugin. A central point for creating widget toolbars. This plugin handles the whole
+ * toolbar rendering process and exposes concise API.
  *
- * Creating toolbar for the widget bases on the {@link ~add()} method. TODO
+ * Creating toolbar for the widget bases on the {@link ~register()} method.
  *
- * This plugin added to the plugin list directly or indirectly prevents showing up
+ * This plugin adds to the plugin list directly or indirectly prevents showing up
  * the {@link module:ui/toolbar/balloontoolbar~BalloonToolbar} toolbar and the widget toolbar at the same time.
  *
  * Usage example comes from {@link module:image/imagetoolbar~ImageToolbar}:
  *
  * 		class ImageToolbar extends Plugin {
  *			static get requires() {
- *				return [ WidgetToolbar ];
+ *				return [ WidgetToolbarRepository ];
  *			}
  *
  *			afterInit() {
  *				const editor = this.editor;
- *				const widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
+ *				const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  *
- *				widgetToolbar.add( {
+ *				widgetToolbarRepository.add( {
  *					toolbarItems: editor.config.get( 'image.toolbar' )
  *					isVisible: isImageWidgetSelected
  *				} );
  *			}
  *		}
  */
-export default class WidgetToolbar extends Plugin {
+export default class WidgetToolbarRepository extends Plugin {
 	/**
 	 * @inheritDoc
 	 */
@@ -44,7 +45,7 @@ export default class WidgetToolbar extends Plugin {
 	 * @inheritDoc
 	 */
 	static get pluginName() {
-		return 'WidgetToolbar';
+		return 'WidgetToolbarRepository';
 	}
 
 	/**
@@ -87,11 +88,11 @@ export default class WidgetToolbar extends Plugin {
 	}
 
 	/**
-	 * Adds toolbar to the WidgetToolbar's collection. It renders it in the `ContextualBalloon` based on the value of the invoked
+	 * Registers toolbar in the WidgetToolbarRepository. It renders it in the `ContextualBalloon` based on the value of the invoked
 	 * `isVisible` function. Toolbar items are gathered from `toolbarItems` array.
 	 * The balloon's CSS class is by default `ck-toolbar-container` and may be override with the `balloonClassName` option.
 	 *
-	 * Note: This method should be called in the `module:core/plugin/Plugin~afterInit` to make sure that plugins for toolbar items
+	 * Note: This method should be called in the {@link module:core/plugin/Plugin~afterInit} to make sure that plugins for toolbar items
 	 * will be already loaded and available in the UI component factory.
 	 *
 	 * @param {String} toolbarId An id for the toolbar. Used to
@@ -100,7 +101,7 @@ export default class WidgetToolbar extends Plugin {
 	 * @param {Function} options.isVisible Callback which specifies when the toolbar should be visible for the widget.
 	 * @param {String} [options.balloonClassName] CSS class for the widget balloon.
 	 */
-	add( toolbarId, { toolbarItems, isVisible, balloonClassName = defaultBalloonClassName } ) {
+	register( toolbarId, { toolbarItems, isVisible, balloonClassName = defaultBalloonClassName } ) {
 		const editor = this.editor;
 		const toolbarView = new ToolbarView();
 
@@ -111,6 +112,7 @@ export default class WidgetToolbar extends Plugin {
 			 * Toolbar with the given id was already added.
 			 *
 			 * @error widget-toolbar-duplicated
+			 * @param toolbarId Toolbar id.
 			 */
 			throw new Error( 'widget-toolbar-duplicated: Toolbar with the given id was already added.', { toolbarId } );
 		}
@@ -123,11 +125,11 @@ export default class WidgetToolbar extends Plugin {
 	}
 
 	/**
-	 * Removes toolbar of the given toolbarId.
+	 * Removes toolbar with the given toolbarId.
 	 *
 	 * @param {String} toolbarId Toolbar identificator.
 	 */
-	remove( toolbarId ) {
+	deregister( toolbarId ) {
 		const toolbar = this._toolbars.get( toolbarId );
 
 		if ( !toolbar ) {
@@ -135,6 +137,7 @@ export default class WidgetToolbar extends Plugin {
 			 * Toolbar with the given id was already added.
 			 *
 			 * @error widget-toolbar-does-not-exist
+			 * @param toolbarId Toolbar id.
 			 */
 			throw new Error( 'widget-toolbar-does-not-exist', { toolbarId } );
 		}
@@ -144,11 +147,11 @@ export default class WidgetToolbar extends Plugin {
 	}
 
 	/**
-	 * Returns `true` when a toolbar with the given id is present in the toolbar collection.
+	 * Returns `true` when a toolbar with the given id is present in the widget toolbar repository.
 	 *
 	 * @param {String} toolbarId Toolbar identificator.
 	 */
-	has( toolbarId ) {
+	isRegistered( toolbarId ) {
 		return this._toolbars.has( toolbarId );
 	}
 

+ 39 - 39
packages/ckeditor5-widget/tests/widgettoolbar.js → packages/ckeditor5-widget/tests/widgettoolbarrepository.js

@@ -11,7 +11,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
 import Widget from '../src/widget';
-import WidgetToolbar from '../src/widgettoolbar';
+import WidgetToolbarRepository from '../src/widgettoolbarrepository';
 import { isWidget, toWidget } from '../src/utils';
 import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
@@ -22,7 +22,7 @@ import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'WidgetToolbar', () => {
-	let editor, model, balloon, widgetToolbar, editorElement;
+	let editor, model, balloon, widgetToolbarRepository, editorElement;
 
 	testUtils.createSinonSandbox();
 
@@ -32,7 +32,7 @@ describe( 'WidgetToolbar', () => {
 
 		return ClassicTestEditor
 			.create( editorElement, {
-				plugins: [ Paragraph, FakeButton, WidgetToolbar, FakeWidget ],
+				plugins: [ Paragraph, FakeButton, WidgetToolbarRepository, FakeWidget ],
 				fake: {
 					toolbar: [ 'fake_button' ]
 				}
@@ -40,7 +40,7 @@ describe( 'WidgetToolbar', () => {
 			.then( newEditor => {
 				editor = newEditor;
 				model = newEditor.model;
-				widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
+				widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
 				balloon = editor.plugins.get( 'ContextualBalloon' );
 			} );
 	} );
@@ -52,28 +52,28 @@ describe( 'WidgetToolbar', () => {
 	} );
 
 	it( 'should be loaded', () => {
-		expect( editor.plugins.get( WidgetToolbar ) ).to.be.instanceOf( WidgetToolbar );
+		expect( editor.plugins.get( WidgetToolbarRepository ) ).to.be.instanceOf( WidgetToolbarRepository );
 	} );
 
-	describe( 'add()', () => {
+	describe( 'register()', () => {
 		it( 'should create a widget toolbar and add it to the collection', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false,
 			} );
 
-			expect( widgetToolbar._toolbars.size ).to.equal( 1 );
-			expect( widgetToolbar._toolbars.get( 'fake' ) ).to.be.an( 'object' );
+			expect( widgetToolbarRepository._toolbars.size ).to.equal( 1 );
+			expect( widgetToolbarRepository._toolbars.get( 'fake' ) ).to.be.an( 'object' );
 		} );
 
 		it( 'should throw when adding two times widget with the same id', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false
 			} );
 
 			expect( () => {
-				widgetToolbar.add( 'fake', {
+				widgetToolbarRepository.register( 'fake', {
 					toolbarItems: editor.config.get( 'fake.toolbar' ),
 					isVisible: () => false
 				} );
@@ -81,47 +81,47 @@ describe( 'WidgetToolbar', () => {
 		} );
 	} );
 
-	describe( 'remove', () => {
+	describe( 'deregister', () => {
 		it( 'should remove given widget toolbar', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false
 			} );
 
-			widgetToolbar.remove( 'fake' );
+			widgetToolbarRepository.deregister( 'fake' );
 
-			expect( widgetToolbar._toolbars.size ).to.equal( 0 );
+			expect( widgetToolbarRepository._toolbars.size ).to.equal( 0 );
 		} );
 
 		it( 'should throw an error if a toolbar does not exist', () => {
-			widgetToolbar.add( 'foo', {
+			widgetToolbarRepository.register( 'foo', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false
 			} );
 
 			expect( () => {
-				widgetToolbar.remove( 'bar' );
+				widgetToolbarRepository.deregister( 'bar' );
 			} ).to.throw( /widget-toolbar-does-not-exist/ );
 		} );
 	} );
 
-	describe( 'has', () => {
+	describe( 'isRegistered', () => {
 		it( 'should return `true` when a toolbar with given id was added', () => {
-			widgetToolbar.add( 'foo', {
+			widgetToolbarRepository.register( 'foo', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false
 			} );
 
-			expect( widgetToolbar.has( 'foo' ) ).to.be.true;
+			expect( widgetToolbarRepository.isRegistered( 'foo' ) ).to.be.true;
 		} );
 
 		it( 'should return `false` when a toolbar with given id was not added', () => {
-			widgetToolbar.add( 'foo', {
+			widgetToolbarRepository.register( 'foo', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: () => false
 			} );
 
-			expect( widgetToolbar.has( 'bar' ) ).to.be.false;
+			expect( widgetToolbarRepository.isRegistered( 'bar' ) ).to.be.false;
 		} );
 	} );
 
@@ -131,20 +131,20 @@ describe( 'WidgetToolbar', () => {
 		} );
 
 		it( 'toolbar should be visible when the `isVisible` callback returns true', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetSelected
 			} );
 
 			setData( model, '<paragraph>foo</paragraph>[<fake-widget></fake-widget>]' );
 
-			const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
+			const fakeWidgetToolbarView = widgetToolbarRepository._toolbars.get( 'fake' ).view;
 
 			expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
 		} );
 
 		it( 'toolbar should be hidden when the `isVisible` callback returns false', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetSelected
 			} );
@@ -155,7 +155,7 @@ describe( 'WidgetToolbar', () => {
 		} );
 
 		it( 'toolbar should be hidden when the `isVisible` callback returns false #2', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetSelected
 			} );
@@ -171,7 +171,7 @@ describe( 'WidgetToolbar', () => {
 		} );
 
 		it( 'toolbar should update its position when other widget is selected', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetSelected
 			} );
@@ -183,31 +183,31 @@ describe( 'WidgetToolbar', () => {
 				writer.setSelection( model.document.getRoot().getChild( 1 ), 'on' );
 			} );
 
-			const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
+			const fakeWidgetToolbarView = widgetToolbarRepository._toolbars.get( 'fake' ).view;
 
 			expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
 		} );
 
 		it( 'it should be possible to create a widget toolbar for content inside the widget', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetContentSelected
 			} );
 
 			setData( model, '<fake-widget>[foo]</fake-widget>' );
 
-			const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
+			const fakeWidgetToolbarView = widgetToolbarRepository._toolbars.get( 'fake' ).view;
 
 			expect( balloon.visibleView ).to.equal( fakeWidgetToolbarView );
 		} );
 
 		it( 'toolbar should not engage when is in the balloon yet invisible', () => {
-			widgetToolbar.add( 'fake', {
+			widgetToolbarRepository.register( 'fake', {
 				toolbarItems: editor.config.get( 'fake.toolbar' ),
 				isVisible: isFakeWidgetSelected
 			} );
 
-			const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
+			const fakeWidgetToolbarView = widgetToolbarRepository._toolbars.get( 'fake' ).view;
 
 			setData( model, '[<fake-widget></fake-widget>]' );
 
@@ -232,8 +232,8 @@ describe( 'WidgetToolbar', () => {
 	} );
 } );
 
-describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
-	let clock, editor, model, balloon, balloonToolbar, widgetToolbar, editorElement;
+describe( 'WidgetToolbarRepository - integration with the BalloonToolbar', () => {
+	let clock, editor, model, balloon, balloonToolbar, widgetToolbarRepository, editorElement;
 
 	testUtils.createSinonSandbox();
 
@@ -244,7 +244,7 @@ describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
 
 		return BalloonEditor
 			.create( editorElement, {
-				plugins: [ Paragraph, FakeButton, WidgetToolbar, FakeWidget, Bold ],
+				plugins: [ Paragraph, FakeButton, WidgetToolbarRepository, FakeWidget, Bold ],
 				balloonToolbar: [ 'bold' ],
 				fake: {
 					toolbar: [ 'fake_button' ]
@@ -253,7 +253,7 @@ describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
 			.then( newEditor => {
 				editor = newEditor;
 				model = newEditor.model;
-				widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
+				widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
 				balloon = editor.plugins.get( 'ContextualBalloon' );
 				balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
 
@@ -268,12 +268,12 @@ describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
 	} );
 
 	it( 'balloon toolbar should be hidden when the widget is selected', () => {
-		widgetToolbar.add( 'fake', {
+		widgetToolbarRepository.register( 'fake', {
 			toolbarItems: editor.config.get( 'fake.toolbar' ),
 			isVisible: isFakeWidgetSelected,
 		} );
 
-		const fakeWidgetToolbarView = widgetToolbar._toolbars.get( 'fake' ).view;
+		const fakeWidgetToolbarView = widgetToolbarRepository._toolbars.get( 'fake' ).view;
 
 		setData( model, '[<fake-widget></fake-widget>]<paragraph>foo</paragraph>' );
 
@@ -283,7 +283,7 @@ describe( 'WidgetToolbar - integration with the BalloonToolbar', () => {
 	} );
 
 	it( 'balloon toolbar should be visible when the widget is not selected', () => {
-		widgetToolbar.add( 'fake', {
+		widgetToolbarRepository.register( 'fake', {
 			toolbarItems: editor.config.get( 'fake.toolbar' ),
 			isVisible: isFakeWidgetSelected
 		} );