2020-01-25 14:52:03 -03:00
|
|
|
import "./ToolIcon.scss";
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2020-10-19 17:14:28 +03:00
|
|
|
import clsx from "clsx";
|
2021-06-10 02:46:56 +05:30
|
|
|
import { useExcalidrawContainer } from "./App";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { AbortError } from "../errors";
|
|
|
|
import Spinner from "./Spinner";
|
|
|
|
import { PointerType } from "../element/types";
|
2020-01-25 14:52:03 -03:00
|
|
|
|
2021-07-15 18:48:03 +02:00
|
|
|
export type ToolButtonSize = "small" | "medium";
|
2020-01-25 14:52:03 -03:00
|
|
|
|
|
|
|
type ToolButtonBaseProps = {
|
2020-01-29 02:30:03 +02:00
|
|
|
icon?: React.ReactNode;
|
2020-01-25 14:52:03 -03:00
|
|
|
"aria-label": string;
|
|
|
|
"aria-keyshortcuts"?: string;
|
2020-04-07 14:39:06 +03:00
|
|
|
"data-testid"?: string;
|
2020-01-29 02:30:03 +02:00
|
|
|
label?: string;
|
2020-01-25 14:52:03 -03:00
|
|
|
title?: string;
|
|
|
|
name?: string;
|
|
|
|
id?: string;
|
2021-07-15 18:48:03 +02:00
|
|
|
size?: ToolButtonSize;
|
2020-02-01 17:31:28 +00:00
|
|
|
keyBindingLabel?: string;
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel?: boolean;
|
2020-06-12 18:35:04 +02:00
|
|
|
hidden?: boolean;
|
2020-02-21 14:34:18 -05:00
|
|
|
visible?: boolean;
|
2020-03-01 14:39:03 -05:00
|
|
|
selected?: boolean;
|
2020-03-11 19:42:18 +01:00
|
|
|
className?: string;
|
2021-11-17 23:53:43 +05:30
|
|
|
isLoading?: boolean;
|
2020-01-25 14:52:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
type ToolButtonProps =
|
2020-03-11 22:48:27 +01:00
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "button";
|
|
|
|
children?: React.ReactNode;
|
2021-10-21 22:05:48 +02:00
|
|
|
onClick?(event: React.MouseEvent): void;
|
2020-03-11 22:48:27 +01:00
|
|
|
})
|
2021-11-17 23:53:43 +05:30
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "submit";
|
|
|
|
children?: React.ReactNode;
|
|
|
|
onClick?(event: React.MouseEvent): void;
|
|
|
|
})
|
2021-05-25 21:37:14 +02:00
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "icon";
|
|
|
|
children?: React.ReactNode;
|
|
|
|
onClick?(): void;
|
|
|
|
})
|
2020-01-25 14:52:03 -03:00
|
|
|
| (ToolButtonBaseProps & {
|
|
|
|
type: "radio";
|
|
|
|
checked: boolean;
|
2021-10-21 22:05:48 +02:00
|
|
|
onChange?(data: { pointerType: PointerType | null }): void;
|
2020-01-25 14:52:03 -03:00
|
|
|
});
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const ToolButton = React.forwardRef((props: ToolButtonProps, ref) => {
|
2021-06-10 02:46:56 +05:30
|
|
|
const { id: excalId } = useExcalidrawContainer();
|
2020-01-30 10:40:49 +01:00
|
|
|
const innerRef = React.useRef(null);
|
2020-01-25 19:37:58 -03:00
|
|
|
React.useImperativeHandle(ref, () => innerRef.current);
|
2021-07-15 18:48:03 +02:00
|
|
|
const sizeCn = `ToolIcon_size_${props.size}`;
|
2020-01-25 14:52:03 -03:00
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
const isMountedRef = useRef(true);
|
|
|
|
|
|
|
|
const onClick = async (event: React.MouseEvent) => {
|
|
|
|
const ret = "onClick" in props && props.onClick?.(event);
|
|
|
|
|
|
|
|
if (ret && "then" in ret) {
|
|
|
|
try {
|
|
|
|
setIsLoading(true);
|
|
|
|
await ret;
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2021-10-21 22:05:48 +02:00
|
|
|
if (!(error instanceof AbortError)) {
|
|
|
|
throw error;
|
2021-11-19 10:54:23 +01:00
|
|
|
} else {
|
|
|
|
console.warn(error);
|
2021-10-21 22:05:48 +02:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (isMountedRef.current) {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
isMountedRef.current = false;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
const lastPointerTypeRef = useRef<PointerType | null>(null);
|
|
|
|
|
2021-11-17 23:53:43 +05:30
|
|
|
if (
|
|
|
|
props.type === "button" ||
|
|
|
|
props.type === "icon" ||
|
|
|
|
props.type === "submit"
|
|
|
|
) {
|
|
|
|
const type = (props.type === "icon" ? "button" : props.type) as
|
|
|
|
| "button"
|
|
|
|
| "submit";
|
2020-01-25 14:52:03 -03:00
|
|
|
return (
|
|
|
|
<button
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx(
|
|
|
|
"ToolIcon_type_button",
|
|
|
|
sizeCn,
|
|
|
|
props.className,
|
2020-06-12 18:35:04 +02:00
|
|
|
props.visible && !props.hidden
|
2020-04-26 11:45:35 +02:00
|
|
|
? "ToolIcon_type_button--show"
|
2020-10-19 17:14:28 +03:00
|
|
|
: "ToolIcon_type_button--hide",
|
|
|
|
{
|
|
|
|
ToolIcon: !props.hidden,
|
|
|
|
"ToolIcon--selected": props.selected,
|
2021-05-25 21:37:14 +02:00
|
|
|
"ToolIcon--plain": props.type === "icon",
|
2020-10-19 17:14:28 +03:00
|
|
|
},
|
|
|
|
)}
|
2021-03-20 16:08:03 +05:30
|
|
|
data-testid={props["data-testid"]}
|
2020-06-12 18:35:04 +02:00
|
|
|
hidden={props.hidden}
|
2020-01-25 14:52:03 -03:00
|
|
|
title={props.title}
|
|
|
|
aria-label={props["aria-label"]}
|
2021-11-17 23:53:43 +05:30
|
|
|
type={type}
|
2021-10-21 22:05:48 +02:00
|
|
|
onClick={onClick}
|
2020-01-30 10:40:49 +01:00
|
|
|
ref={innerRef}
|
2021-11-17 23:53:43 +05:30
|
|
|
disabled={isLoading || props.isLoading}
|
2020-01-25 14:52:03 -03:00
|
|
|
>
|
2021-05-25 21:37:14 +02:00
|
|
|
{(props.icon || props.label) && (
|
|
|
|
<div className="ToolIcon__icon" aria-hidden="true">
|
|
|
|
{props.icon || props.label}
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
<span className="ToolIcon__keybinding">
|
|
|
|
{props.keyBindingLabel}
|
|
|
|
</span>
|
|
|
|
)}
|
2021-11-17 23:53:43 +05:30
|
|
|
{props.isLoading && <Spinner />}
|
2021-05-25 21:37:14 +02:00
|
|
|
</div>
|
|
|
|
)}
|
2020-02-20 18:44:38 -05:00
|
|
|
{props.showAriaLabel && (
|
2021-10-21 22:05:48 +02:00
|
|
|
<div className="ToolIcon__label">
|
|
|
|
{props["aria-label"]} {isLoading && <Spinner />}
|
|
|
|
</div>
|
2020-02-20 18:44:38 -05:00
|
|
|
)}
|
2020-03-11 22:48:27 +01:00
|
|
|
{props.children}
|
2020-01-25 14:52:03 -03:00
|
|
|
</button>
|
|
|
|
);
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2020-01-25 14:52:03 -03:00
|
|
|
|
|
|
|
return (
|
2021-10-21 22:05:48 +02:00
|
|
|
<label
|
|
|
|
className={clsx("ToolIcon", props.className)}
|
|
|
|
title={props.title}
|
|
|
|
onPointerDown={(event) => {
|
|
|
|
lastPointerTypeRef.current = event.pointerType || null;
|
|
|
|
}}
|
|
|
|
onPointerUp={() => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
lastPointerTypeRef.current = null;
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
2020-01-25 14:52:03 -03:00
|
|
|
<input
|
|
|
|
className={`ToolIcon_type_radio ${sizeCn}`}
|
|
|
|
type="radio"
|
|
|
|
name={props.name}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
aria-keyshortcuts={props["aria-keyshortcuts"]}
|
2020-04-07 14:39:06 +03:00
|
|
|
data-testid={props["data-testid"]}
|
2021-06-10 02:46:56 +05:30
|
|
|
id={`${excalId}-${props.id}`}
|
2021-10-21 22:05:48 +02:00
|
|
|
onChange={() => {
|
|
|
|
props.onChange?.({ pointerType: lastPointerTypeRef.current });
|
|
|
|
}}
|
2020-01-25 14:52:03 -03:00
|
|
|
checked={props.checked}
|
2020-01-30 10:40:49 +01:00
|
|
|
ref={innerRef}
|
2020-01-25 14:52:03 -03:00
|
|
|
/>
|
2020-02-01 17:31:28 +00:00
|
|
|
<div className="ToolIcon__icon">
|
|
|
|
{props.icon}
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
<span className="ToolIcon__keybinding">{props.keyBindingLabel}</span>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-01-25 14:52:03 -03:00
|
|
|
</label>
|
|
|
|
);
|
2020-01-25 19:37:58 -03:00
|
|
|
});
|
2020-04-29 20:43:29 +05:30
|
|
|
|
|
|
|
ToolButton.defaultProps = {
|
|
|
|
visible: true,
|
|
|
|
className: "",
|
2021-07-15 18:48:03 +02:00
|
|
|
size: "medium",
|
2020-04-29 20:43:29 +05:30
|
|
|
};
|