8
0
Просмотр исходного кода

Implemented ImageBalloon#remove and ImageBalloon#clear methods.

Aleksander Nowodzinski 8 лет назад
Родитель
Сommit
dbdbe426cc

+ 30 - 0
packages/ckeditor5-image/src/image/ui/imageballoon.js

@@ -31,6 +31,14 @@ export default class ImageBalloon extends ContextualBalloon {
 	init() {
 		super.init();
 
+		/**
+		 * Stack of the image package views injected into the balloon.
+		 *
+		 * @private
+		 * @member {Map} #_imageStack
+		 */
+		this._imageStack = new Set();
+
 		// Attach the life cycle actions.
 		this._handleEditingRender();
 		this._handleFocusChange();
@@ -53,6 +61,28 @@ export default class ImageBalloon extends ContextualBalloon {
 		} else {
 			super.add( data );
 		}
+
+		this._imageStack.add( data.view );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	remove( view ) {
+		super.remove( view );
+
+		this._imageStack.delete( view );
+	}
+
+	/**
+	 * Removes all views from the balloon, and also hides it.
+	 */
+	clear() {
+		for ( const view of this._imageStack ) {
+			this.remove( view );
+		}
+
+		this._imageStack.clear();
 	}
 
 	/**

+ 33 - 0
packages/ckeditor5-image/tests/image/ui/imageballoon.js

@@ -86,6 +86,39 @@ describe( 'ImageBalloon', () => {
 		} );
 	} );
 
+	describe( 'remove()', () => {
+		it( 'should remove a view', () => {
+			const spy = testUtils.sinon.stub( ContextualBalloon.prototype, 'remove' );
+			const view = {};
+
+			plugin.remove( view );
+			sinon.assert.calledOnce( spy );
+		} );
+	} );
+
+	describe( 'clear()', () => {
+		it( 'should remove all remaining views', () => {
+			setData( doc, '[<image src=""></image>]' );
+
+			testUtils.sinon.stub( ContextualBalloon.prototype, 'add' );
+			const removeSpy = testUtils.sinon.stub( ContextualBalloon.prototype, 'remove' );
+
+			const positionDataA = { view: {} };
+			const positionDataB = { view: {} };
+			const positionDataC = { view: {} };
+
+			plugin.add( positionDataA );
+			plugin.add( positionDataB );
+			plugin.add( positionDataC );
+
+			plugin.remove( positionDataB.view );
+			plugin.clear();
+			sinon.assert.calledThrice( removeSpy );
+			sinon.assert.calledWithExactly( removeSpy.firstCall, positionDataC.view );
+			sinon.assert.calledWithExactly( removeSpy.secondCall, positionDataA.view );
+		} );
+	} );
+
 	describe( 'editing view #render event handling', () => {
 		let updatePositionSpy;