| 
									
										
										
										
											2020-11-05 16:58:23 +00:00
										 |  |  | import CommandService from '../CommandService'; | 
					
						
							|  |  |  | import { stateUtils } from '../../reducer'; | 
					
						
							| 
									
										
										
										
											2021-04-07 01:51:24 +05:30
										 |  |  | import focusEditorIfEditorCommand from './focusEditorIfEditorCommand'; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const separatorItem = { type: 'separator' }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface ToolbarButtonInfo { | 
					
						
							| 
									
										
										
										
											2020-11-12 19:29:22 +00:00
										 |  |  | 	name: string; | 
					
						
							|  |  |  | 	tooltip: string; | 
					
						
							|  |  |  | 	iconName: string; | 
					
						
							|  |  |  | 	enabled: boolean; | 
					
						
							|  |  |  | 	onClick(): void; | 
					
						
							|  |  |  | 	title: string; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface ToolbarButtonCacheItem { | 
					
						
							| 
									
										
										
										
											2020-11-12 19:29:22 +00:00
										 |  |  | 	info: ToolbarButtonInfo; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface ToolbarButtonCache { | 
					
						
							| 
									
										
										
										
											2020-11-12 19:29:22 +00:00
										 |  |  | 	[key: string]: ToolbarButtonCacheItem; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class ToolbarButtonUtils { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | 	private service_: CommandService; | 
					
						
							|  |  |  | 	private toolbarButtonCache_: ToolbarButtonCache = {}; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 14:22:01 +00:00
										 |  |  | 	public constructor(service: CommandService) { | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 		this.service_ = service; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | 	private get service(): CommandService { | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 		return this.service_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | 	private commandToToolbarButton(commandName: string, whenClauseContext: any): ToolbarButtonInfo { | 
					
						
							| 
									
										
										
										
											2020-10-18 21:52:10 +01:00
										 |  |  | 		const newEnabled = this.service.isEnabled(commandName, whenClauseContext); | 
					
						
							|  |  |  | 		const newTitle = this.service.title(commandName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ( | 
					
						
							|  |  |  | 			this.toolbarButtonCache_[commandName] && | 
					
						
							|  |  |  | 			this.toolbarButtonCache_[commandName].info.enabled === newEnabled && | 
					
						
							|  |  |  | 			this.toolbarButtonCache_[commandName].info.title === newTitle | 
					
						
							|  |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 			return this.toolbarButtonCache_[commandName].info; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const command = this.service.commandByName(commandName, { runtimeMustBeRegistered: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const output = { | 
					
						
							|  |  |  | 			name: commandName, | 
					
						
							|  |  |  | 			tooltip: this.service.label(commandName), | 
					
						
							|  |  |  | 			iconName: command.declaration.iconName, | 
					
						
							| 
									
										
										
										
											2020-10-18 21:52:10 +01:00
										 |  |  | 			enabled: newEnabled, | 
					
						
							| 
									
										
										
										
											2020-12-02 03:36:00 -07:00
										 |  |  | 			onClick: async () => { | 
					
						
							| 
									
										
										
										
											2020-11-25 14:40:25 +00:00
										 |  |  | 				void this.service.execute(commandName); | 
					
						
							| 
									
										
										
										
											2021-04-07 01:51:24 +05:30
										 |  |  | 				void focusEditorIfEditorCommand(commandName, this.service); | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2020-10-18 21:52:10 +01:00
										 |  |  | 			title: newTitle, | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.toolbarButtonCache_[commandName] = { | 
					
						
							|  |  |  | 			info: output, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return this.toolbarButtonCache_[commandName].info; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This method ensures that if the provided commandNames and state hasn't changed
 | 
					
						
							|  |  |  | 	// the output also won't change. Invididual toolbarButtonInfo also won't changed
 | 
					
						
							|  |  |  | 	// if the state they use hasn't changed. This is to avoid useless renders of the toolbars.
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | 	public commandsToToolbarButtons(commandNames: string[], whenClauseContext: any): ToolbarButtonInfo[] { | 
					
						
							|  |  |  | 		const output: ToolbarButtonInfo[] = []; | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (const commandName of commandNames) { | 
					
						
							|  |  |  | 			if (commandName === '-') { | 
					
						
							|  |  |  | 				output.push(separatorItem as any); | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-18 21:52:10 +01:00
										 |  |  | 			output.push(this.commandToToolbarButton(commandName, whenClauseContext)); | 
					
						
							| 
									
										
										
										
											2020-10-09 18:35:46 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return stateUtils.selectArrayShallow({ array: output }, commandNames.join('_')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |