@@ -33,12 +33,14 @@ import { IMenuManagerService, MenuManagerService } from '../../menu/menu-manager
3333import { MenuManagerPosition , RibbonPosition } from '../../menu/types' ;
3434import { DesktopRibbonService , IRibbonService } from '../ribbon.service' ;
3535
36- function createService ( ) {
36+ function createService ( univerInstanceService ?: { focused$ : BehaviorSubject < string | null > } ) {
3737 const injector = new Injector ( ) ;
3838 injector . add ( [ IConfigService , { useClass : ConfigService } ] ) ;
3939 injector . add ( [ IContextService , { useClass : ContextService } ] ) ;
4040 injector . add ( [ ILogService , { useClass : DesktopLogService } ] ) ;
41- injector . add ( [ IUniverInstanceService , { useClass : UniverInstanceService } ] ) ;
41+ injector . add ( univerInstanceService
42+ ? [ IUniverInstanceService , univerInstanceService as never ]
43+ : [ IUniverInstanceService , { useClass : UniverInstanceService } ] ) ;
4244 injector . add ( [ IMenuManagerService , { useClass : MenuManagerService } ] ) ;
4345 injector . add ( [ IRibbonService , { useClass : DesktopRibbonService } ] ) ;
4446 return {
@@ -149,4 +151,70 @@ describe('DesktopRibbonService', () => {
149151 ribbonSub . unsubscribe ( ) ;
150152 activeSub . unsubscribe ( ) ;
151153 } ) ;
154+
155+ it ( 'does not republish the ribbon when hidden states emit unchanged values' , ( ) => {
156+ const { service, menuManagerService } = createService ( ) ;
157+ const hidden$ = new BehaviorSubject ( false ) ;
158+ const ribbons : IMenuSchema [ ] [ ] = [ ] ;
159+ const ribbonSub = service . ribbon$ . subscribe ( ( ribbon ) => ribbons . push ( ribbon ) ) ;
160+
161+ menuManagerService . appendRootMenu ( {
162+ [ MenuManagerPosition . RIBBON ] : {
163+ [ RibbonPosition . START ] : {
164+ order : 0 ,
165+ [ `${ RibbonPosition . START } .stable-group` ] : {
166+ order : 0 ,
167+ stableCommand : {
168+ order : 0 ,
169+ menuItemFactory : ( ) => ( { id : 'stable-command' , type : MenuItemType . BUTTON , hidden$ } ) ,
170+ } ,
171+ } ,
172+ } ,
173+ } ,
174+ } as MenuSchemaType ) ;
175+
176+ const publishCountAfterInitialHiddenState = ribbons . length ;
177+ hidden$ . next ( false ) ;
178+
179+ expect ( ribbons . length ) . toBe ( publishCountAfterInitialHiddenState ) ;
180+
181+ hidden$ . next ( true ) ;
182+
183+ expect ( ribbons . length ) . toBe ( publishCountAfterInitialHiddenState + 1 ) ;
184+
185+ ribbonSub . unsubscribe ( ) ;
186+ } ) ;
187+
188+ it ( 'does not republish the ribbon when focus emits the same unit id' , ( ) => {
189+ const focused$ = new BehaviorSubject < string | null > ( 'unit-1' ) ;
190+ const { service, menuManagerService } = createService ( { focused$ } ) ;
191+ const ribbons : IMenuSchema [ ] [ ] = [ ] ;
192+ const ribbonSub = service . ribbon$ . subscribe ( ( ribbon ) => ribbons . push ( ribbon ) ) ;
193+
194+ menuManagerService . appendRootMenu ( {
195+ [ MenuManagerPosition . RIBBON ] : {
196+ [ RibbonPosition . START ] : {
197+ order : 0 ,
198+ [ `${ RibbonPosition . START } .focus-group` ] : {
199+ order : 0 ,
200+ focusCommand : {
201+ order : 0 ,
202+ menuItemFactory : ( ) => ( { id : 'focus-command' , type : MenuItemType . BUTTON } ) ,
203+ } ,
204+ } ,
205+ } ,
206+ } ,
207+ } as MenuSchemaType ) ;
208+
209+ const publishCountAfterMenuLoad = ribbons . length ;
210+ focused$ . next ( 'unit-1' ) ;
211+
212+ expect ( ribbons . length ) . toBe ( publishCountAfterMenuLoad ) ;
213+
214+ focused$ . next ( 'unit-2' ) ;
215+
216+ expect ( ribbons . length ) . toBe ( publishCountAfterMenuLoad + 1 ) ;
217+
218+ ribbonSub . unsubscribe ( ) ;
219+ } ) ;
152220} ) ;
0 commit comments