1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/components/NoteEditor/MarkdownToolbar/buttons/usePluginButtons.ts
Henry Heino 55cafb8891
Android: Add support for Markdown editor plugins (#10086)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-03-11 15:02:15 +00:00

39 lines
1.1 KiB
TypeScript

import { useMemo } from 'react';
import { ButtonSpec } from '../types';
import { ButtonRowProps } from '../types';
import { PluginStates, utils as pluginUtils } from '@joplin/lib/services/plugins/reducer';
import CommandService from '@joplin/lib/services/CommandService';
interface PluginButtonsRowProps extends ButtonRowProps {
pluginStates: PluginStates;
}
const usePluginButtons = (props: PluginButtonsRowProps) => {
return useMemo(() => {
const pluginButtons: ButtonSpec[] = [];
const pluginCommands =
pluginUtils
.commandNamesFromViews(props.pluginStates, 'editorToolbar')
// Remove separators
.filter(name => name !== '-');
const commandService = CommandService.instance();
for (const commandName of pluginCommands) {
const command = commandService.commandByName(commandName, { runtimeMustBeRegistered: true });
pluginButtons.push({
description: commandService.description(commandName),
icon: command.declaration.iconName ?? 'fas fa-cog',
onPress: async () => {
void commandService.execute(commandName);
},
});
}
return pluginButtons;
}, [props.pluginStates]);
};
export default usePluginButtons;