瀏覽代碼

Add single view mode switch to contextual balloon panel.

Maciej Gołaszewski 6 年之前
父節點
當前提交
3790ddd5e3

+ 41 - 12
packages/ckeditor5-ui/src/panel/balloon/contextualballoon.js

@@ -138,6 +138,8 @@ export default class ContextualBalloon extends Plugin {
 		 */
 		this.set( '_numberOfStacks', 0 );
 
+		this.set( '_singleViewMode', false );
+
 		/**
 		 * Rotator view embedded in the contextual balloon.
 		 * Displays currently visible view in the balloon and provides navigation for switching stacks.
@@ -176,6 +178,8 @@ export default class ContextualBalloon extends Plugin {
 	 * @param {module:utils/dom/position~Options} [data.position] Positioning options.
 	 * @param {String} [data.balloonClassName] Additional CSS class added to the {@link #view balloon} when visible.
 	 * @param {Boolean} [data.withArrow=true] Whether the {@link #view balloon} should be rendered with an arrow.
+	 * @param {Boolean} [data.preventOtherStacks=false] ... TODO
+	 * @param {Boolean} [singleViewMode=false]
 	 */
 	add( data ) {
 		if ( this.hasView( data.view ) ) {
@@ -195,7 +199,7 @@ export default class ContextualBalloon extends Plugin {
 			this._viewToStack.set( data.view, this._idToStack.get( stackId ) );
 			this._numberOfStacks = this._idToStack.size;
 
-			if ( !this._visibleStack ) {
+			if ( !this._visibleStack || data.singleViewMode ) {
 				this.showStack( stackId );
 			}
 
@@ -204,6 +208,10 @@ export default class ContextualBalloon extends Plugin {
 
 		const stack = this._idToStack.get( stackId );
 
+		if ( data.singleViewMode ) {
+			this.showStack( stackId );
+		}
+
 		// Add new view to the stack.
 		stack.set( data.view, data );
 		this._viewToStack.set( data.view, stack );
@@ -234,6 +242,10 @@ export default class ContextualBalloon extends Plugin {
 
 		const stack = this._viewToStack.get( view );
 
+		if ( this._singleViewMode == view ) {
+			this._singleViewMode = false;
+		}
+
 		// When visible view will be removed we need to show a preceding view or next stack
 		// if a view is the only view in the stack.
 		if ( this.visibleView === view ) {
@@ -281,6 +293,7 @@ export default class ContextualBalloon extends Plugin {
 	 * @param {String} id
 	 */
 	showStack( id ) {
+		this.visibleStack = id;
 		const stack = this._idToStack.get( id );
 
 		if ( !stack ) {
@@ -369,21 +382,31 @@ export default class ContextualBalloon extends Plugin {
 		this.view.content.add( view );
 
 		// Hide navigation when there is only a one stack.
-		view.bind( 'isNavigationVisible' ).to( this, '_numberOfStacks', value => value > 1 );
+		view.bind( 'isNavigationVisible' ).to( this, '_numberOfStacks', this, '_singleViewMode', ( value, isSingleViewMode ) => {
+			if ( this.visibleView == isSingleViewMode ) {
+				return false;
+			}
+
+			return value > 1;
+		} );
 
 		// Update balloon position after toggling navigation.
 		view.on( 'change:isNavigationVisible', () => ( this.updatePosition() ), { priority: 'low' } );
 
 		// Show stacks counter.
-		view.bind( 'counter' ).to( this, 'visibleView', this, '_numberOfStacks', ( visibleView, numberOfStacks ) => {
-			if ( numberOfStacks < 2 ) {
-				return '';
-			}
+		view.bind( 'counter' ).to(
+			this, 'visibleView',
+			this, '_numberOfStacks',
+			this, '_singleViewMode',
+			( visibleView, numberOfStacks, isSingleViewMode ) => {
+				if ( numberOfStacks < 2 || isSingleViewMode ) {
+					return '';
+				}
 
-			const current = Array.from( this._idToStack.values() ).indexOf( this._visibleStack ) + 1;
+				const current = Array.from( this._idToStack.values() ).indexOf( this._visibleStack ) + 1;
 
-			return `${ current } ${ t( 'of' ) } ${ numberOfStacks }`;
-		} );
+				return `${ current } ${ t( 'of' ) } ${ numberOfStacks }`;
+			} );
 
 		view.buttonNextView.on( 'execute', () => {
 			// When current view has a focus then move focus to the editable before removing it,
@@ -414,8 +437,10 @@ export default class ContextualBalloon extends Plugin {
 	_createFakePanelsView() {
 		const view = new FakePanelsView( this.editor.locale, this.view );
 
-		view.bind( 'numberOfPanels' ).to( this, '_numberOfStacks', number => {
-			return number < 2 ? 0 : Math.min( number - 1, 2 );
+		view.bind( 'numberOfPanels' ).to( this, '_numberOfStacks', this, '_singleViewMode', ( number, isSingleViewMode ) => {
+			const hide = isSingleViewMode || number < 2;
+
+			return hide ? 0 : Math.min( number - 1, 2 );
 		} );
 
 		view.listenTo( this.view, 'change:top', () => view.updatePosition() );
@@ -436,7 +461,7 @@ export default class ContextualBalloon extends Plugin {
 	 * @param {String} [data.balloonClassName=''] Additional class name which will be added to the {@link #view balloon}.
 	 * @param {Boolean} [data.withArrow=true] Whether the {@link #view balloon} should be rendered with an arrow.
 	 */
-	_showView( { view, balloonClassName = '', withArrow = true } ) {
+	_showView( { view, balloonClassName = '', withArrow = true, singleViewMode = false } ) {
 		this.view.class = balloonClassName;
 		this.view.withArrow = withArrow;
 
@@ -444,6 +469,10 @@ export default class ContextualBalloon extends Plugin {
 		this.visibleView = view;
 		this.view.pin( this._getBalloonPosition() );
 		this._fakePanelsView.updatePosition();
+
+		if ( singleViewMode ) {
+			this._singleViewMode = view;
+		}
 	}
 
 	/**

+ 162 - 0
packages/ckeditor5-ui/tests/panel/balloon/contextualballoon.js

@@ -967,5 +967,167 @@ describe( 'ContextualBalloon', () => {
 			expect( fakePanelsView.element.style.width ).to.equal( '30px' );
 			expect( fakePanelsView.element.style.height ).to.equal( '40px' );
 		} );
+
+		describe( 'singleViewMode', () => {
+			it( 'should not display navigation when there is more than one stack', () => {
+				const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second',
+					singleViewMode: true
+				} );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+			} );
+
+			it( 'should hide display navigation after adding view', () => {
+				const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second'
+				} );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.false;
+
+				balloon.add( {
+					view: viewC,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+			} );
+
+			it( 'should display navigation after removing a view', () => {
+				const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second'
+				} );
+
+				balloon.add( {
+					view: viewC,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+
+				balloon.remove( viewC );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.false;
+			} );
+
+			it( 'should not update the counter when adding a view', () => {
+				const counterElement = rotatorView.element.querySelector( '.ck-balloon-rotator__counter' );
+
+				expect( counterElement.textContent ).to.equal( '' );
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second',
+					singleViewMode: true
+				} );
+
+				expect( counterElement.textContent ).to.equal( '' );
+			} );
+
+			it( 'should not show fake panels when more than one stack is added to the balloon (max to 2 panels)', () => {
+				const fakePanelsView = editor.ui.view.body.last;
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second'
+				} );
+
+				expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
+				expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
+
+				balloon.add( {
+					view: viewC,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.be.true;
+				expect( fakePanelsView.element.childElementCount ).to.equal( 0 );
+
+				balloon.remove( viewC );
+
+				expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
+				expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
+
+				balloon.remove( viewB );
+			} );
+
+			it( 'should switch visible view when adding a view to new stack', () => {
+				const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second'
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewA );
+
+				balloon.add( {
+					view: viewC,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewC );
+
+				const viewD = new View();
+
+				balloon.add( {
+					view: viewD,
+					stackId: 'fifth',
+					singleViewMode: true
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewD );
+			} );
+
+			it( 'should switch visible view when adding a view to the same stack', () => {
+				const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
+
+				expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
+
+				balloon.add( {
+					view: viewB,
+					stackId: 'second'
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewA );
+
+				balloon.add( {
+					view: viewC,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewC );
+
+				const viewD = new View();
+
+				balloon.add( {
+					view: viewD,
+					stackId: 'third',
+					singleViewMode: true
+				} );
+
+				expect( balloon.visibleView ).to.equal( viewD );
+			} );
+		} );
 	} );
 } );