Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1838 - Create custom top toolbar buttons #5436

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/ketcher-core/src/application/ketcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,8 @@ export class Ketcher {
this.structService = structService;
this._indigo = new Indigo(structService);
}

public sendCustomAction(name: string) {
this.eventBus.emit('CUSTOM_BUTTON_PRESSED', name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="24"
viewBox="0 0 24 24"
Expand All @@ -38,7 +38,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand All @@ -62,7 +62,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down Expand Up @@ -103,7 +103,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down Expand Up @@ -140,7 +140,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down Expand Up @@ -182,7 +182,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="24"
viewBox="0 0 24 24"
Expand All @@ -204,7 +204,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand All @@ -228,7 +228,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down Expand Up @@ -269,7 +269,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down Expand Up @@ -306,7 +306,7 @@ Object {
title=""
>
<svg
class="css-uwwqev"
class="css-2ntgcm"
fill="none"
height="48"
viewBox="0 0 48 48"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,14 @@
* limitations under the License.
***************************************************************************/

import { StyledButton, StyledIcon } from './styles';
import { StyledIcon } from './styles';
import { IIconButtonProps } from './types';
import { IconButtonBase } from './IconButtonBase';

export const IconButton = ({
onClick,
iconName,
shortcut,
title,
className,
isActive = false,
isHidden = false,
disabled = false,
testId,
}: IIconButtonProps) => {
const combinedTitle = shortcut ? `${title} (${shortcut})` : title;

if (isHidden) {
return null;
}

export const IconButton = ({ iconName, ...props }: IIconButtonProps) => {
return (
<StyledButton
className={className}
title={combinedTitle}
onClick={onClick}
disabled={disabled}
isActive={isActive}
data-testid={testId}
>
<IconButtonBase {...props}>
<StyledIcon name={iconName} />
</StyledButton>
</IconButtonBase>
);
};

export default IconButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/****************************************************************************
* Copyright 2021 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

import { StyledButton } from './styles';
import { IIconButtonBaseProps } from './types';

export const IconButtonBase = ({
onClick,
shortcut,
title,
className,
isActive = false,
isHidden = false,
disabled = false,
testId,
children,
}: IIconButtonBaseProps) => {
const combinedTitle = shortcut ? `${title} (${shortcut})` : title;

if (isHidden) {
return null;
}

return (
<StyledButton
className={className}
title={combinedTitle}
onClick={onClick}
disabled={disabled}
isActive={isActive}
data-testid={testId}
>
{children}
</StyledButton>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/****************************************************************************
* Copyright 2021 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

import { IIconButtonCustomIconProps } from './types';
import { IconButtonBase } from './IconButtonBase';
import { StyledCustomIcon } from './styles';

export const IconButtonCustomIcon = ({
link,
...props
}: IIconButtonCustomIconProps) => {
return (
<IconButtonBase {...props}>
<StyledCustomIcon src={link} alt={props.title} />
</IconButtonBase>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
***************************************************************************/

export { IconButton } from './IconButton';
export { IconButtonCustomIcon } from './IconButtonCustomIcon';
17 changes: 13 additions & 4 deletions packages/ketcher-react/src/components/Buttons/IconButton/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import styled from '@emotion/styled';
import Icon from '../../Icon/Icon';
import { style } from 'src/components/styles';
import { IStyledButtonProps } from './types';
import { css } from '@emotion/react';

export const StyledButton = styled('button', {
shouldForwardProp: (prop) => prop !== 'isActive',
Expand Down Expand Up @@ -53,7 +54,15 @@ export const StyledButton = styled('button', {
},
}));

export const StyledIcon = styled(Icon)({
width: '100%',
height: '100%',
});
const IconStyles = css`
width: 100%;
height: 100%;
`;

export const StyledIcon = styled(Icon)`
${IconStyles};
`;

export const StyledCustomIcon = styled('img')`
${IconStyles};
`;
19 changes: 17 additions & 2 deletions packages/ketcher-react/src/components/Buttons/IconButton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
***************************************************************************/

import { IconName } from '../../Icon/types';
import { ReactNode } from 'react';

export interface IIconButtonProps {
iconName: IconName;
export interface IIconButtonBaseProps {
onClick: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
title?: string;
className?: string;
Expand All @@ -26,6 +26,21 @@ export interface IIconButtonProps {
isActive?: boolean;
shortcut?: string;
testId?: string;
children: ReactNode;
}

type IIconButtonBasePropsWithoutChildren = Omit<
IIconButtonBaseProps,
'children'
>;

export interface IIconButtonProps extends IIconButtonBasePropsWithoutChildren {
iconName: IconName;
}

export interface IIconButtonCustomIconProps
extends IIconButtonBasePropsWithoutChildren {
link: string;
}

export interface IStyledButtonProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface CustomButton {
id: string;
imageLink: string;
title: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { initApp } from '../../ui';
import { Root } from 'react-dom/client';
import { IndigoProvider } from 'src/script/providers';
import { STRUCT_SERVICE_INITIALIZED_EVENT } from '../../../constants';
import { CustomButton } from './CustomButtons';

class KetcherBuilder {
private structService: StructService | null;
Expand Down Expand Up @@ -97,6 +98,7 @@ class KetcherBuilder {
errorHandler: (message: string) => void,
buttons?: ButtonsConfig,
togglerComponent?: JSX.Element,
customButtons?: Array<CustomButton>,
): Promise<{
setKetcher: (ketcher: Ketcher) => void;
ketcherId: string;
Expand All @@ -122,6 +124,7 @@ class KetcherBuilder {
version: process.env.VERSION || '',
buildDate: process.env.BUILD_DATE || '',
buildNumber: process.env.BUILD_NUMBER || '',
customButtons: customButtons || [],
},
structService!,
resolve,
Expand Down
4 changes: 4 additions & 0 deletions packages/ketcher-react/src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import { Root } from 'react-dom/client';
import { ButtonsConfig, KetcherBuilder } from './builders';

import { StructServiceProvider } from 'ketcher-core';
import { CustomButton } from './builders/ketcher/CustomButtons';

interface Config {
element: HTMLDivElement | null;
appRoot: Root;
staticResourcesUrl: string;
structServiceProvider: StructServiceProvider;
buttons?: ButtonsConfig;
customButtons?: Array<CustomButton>;
errorHandler: (message: string) => void;
togglerComponent?: JSX.Element;
}
Expand All @@ -37,6 +39,7 @@ async function buildKetcherAsync({
buttons,
errorHandler,
togglerComponent,
customButtons,
}: Config) {
const builder = new KetcherBuilder();

Expand All @@ -50,6 +53,7 @@ async function buildKetcherAsync({
errorHandler,
buttons,
togglerComponent,
customButtons,
);

const ketcher = builder.build();
Expand Down
8 changes: 6 additions & 2 deletions packages/ketcher-react/src/script/ui/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ function getRootReducer(setEditor) {
}

export default function (options, server, setEditor) {
const { buttons = {}, ...restOptions } = options;
const { buttons = {}, customButtons, ...restOptions } = options;

// TODO: redux localStorage here
const initState = {
actionState: null,
editor: null,
modal: null,
options: Object.assign(initOptionsState, { app: restOptions, buttons }),
options: Object.assign(initOptionsState, {
app: restOptions,
buttons,
customButtons,
}),
server: server || Promise.reject(new Error('Standalone mode!')),
templates: initTmplsState,
};
Expand Down
Loading
Loading