|
|
@ -55,146 +55,152 @@ type ToolButtonProps =
|
|
|
|
onPointerDown?(data: { pointerType: PointerType }): void;
|
|
|
|
onPointerDown?(data: { pointerType: PointerType }): void;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export const ToolButton = React.forwardRef((props: ToolButtonProps, ref) => {
|
|
|
|
export const ToolButton = React.forwardRef(
|
|
|
|
const { id: excalId } = useExcalidrawContainer();
|
|
|
|
(
|
|
|
|
const innerRef = React.useRef(null);
|
|
|
|
{
|
|
|
|
React.useImperativeHandle(ref, () => innerRef.current);
|
|
|
|
size = "medium",
|
|
|
|
const sizeCn = `ToolIcon_size_${props.size}`;
|
|
|
|
visible = true,
|
|
|
|
|
|
|
|
className = "",
|
|
|
|
|
|
|
|
...props
|
|
|
|
|
|
|
|
}: ToolButtonProps,
|
|
|
|
|
|
|
|
ref,
|
|
|
|
|
|
|
|
) => {
|
|
|
|
|
|
|
|
const { id: excalId } = useExcalidrawContainer();
|
|
|
|
|
|
|
|
const innerRef = React.useRef(null);
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => innerRef.current);
|
|
|
|
|
|
|
|
const sizeCn = `ToolIcon_size_${size}`;
|
|
|
|
|
|
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const isMountedRef = useRef(true);
|
|
|
|
const isMountedRef = useRef(true);
|
|
|
|
|
|
|
|
|
|
|
|
const onClick = async (event: React.MouseEvent) => {
|
|
|
|
const onClick = async (event: React.MouseEvent) => {
|
|
|
|
const ret = "onClick" in props && props.onClick?.(event);
|
|
|
|
const ret = "onClick" in props && props.onClick?.(event);
|
|
|
|
|
|
|
|
|
|
|
|
if (isPromiseLike(ret)) {
|
|
|
|
if (isPromiseLike(ret)) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
setIsLoading(true);
|
|
|
|
setIsLoading(true);
|
|
|
|
await ret;
|
|
|
|
await ret;
|
|
|
|
} catch (error: any) {
|
|
|
|
} catch (error: any) {
|
|
|
|
if (!(error instanceof AbortError)) {
|
|
|
|
if (!(error instanceof AbortError)) {
|
|
|
|
throw error;
|
|
|
|
throw error;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.warn(error);
|
|
|
|
console.warn(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (isMountedRef.current) {
|
|
|
|
if (isMountedRef.current) {
|
|
|
|
setIsLoading(false);
|
|
|
|
setIsLoading(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
isMountedRef.current = true;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
|
|
isMountedRef.current = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const lastPointerTypeRef = useRef<PointerType | null>(null);
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
isMountedRef.current = true;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
|
|
isMountedRef.current = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const lastPointerTypeRef = useRef<PointerType | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
props.type === "button" ||
|
|
|
|
|
|
|
|
props.type === "icon" ||
|
|
|
|
|
|
|
|
props.type === "submit"
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
const type = (props.type === "icon" ? "button" : props.type) as
|
|
|
|
|
|
|
|
| "button"
|
|
|
|
|
|
|
|
| "submit";
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
|
|
|
className={clsx(
|
|
|
|
|
|
|
|
"ToolIcon_type_button",
|
|
|
|
|
|
|
|
sizeCn,
|
|
|
|
|
|
|
|
className,
|
|
|
|
|
|
|
|
visible && !props.hidden
|
|
|
|
|
|
|
|
? "ToolIcon_type_button--show"
|
|
|
|
|
|
|
|
: "ToolIcon_type_button--hide",
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ToolIcon: !props.hidden,
|
|
|
|
|
|
|
|
"ToolIcon--selected": props.selected,
|
|
|
|
|
|
|
|
"ToolIcon--plain": props.type === "icon",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
style={props.style}
|
|
|
|
|
|
|
|
data-testid={props["data-testid"]}
|
|
|
|
|
|
|
|
hidden={props.hidden}
|
|
|
|
|
|
|
|
title={props.title}
|
|
|
|
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
|
|
|
|
type={type}
|
|
|
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
|
|
|
ref={innerRef}
|
|
|
|
|
|
|
|
disabled={isLoading || props.isLoading || !!props.disabled}
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
{(props.icon || props.label) && (
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
|
|
|
className="ToolIcon__icon"
|
|
|
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
|
|
|
aria-disabled={!!props.disabled}
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
{props.icon || props.label}
|
|
|
|
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
|
|
|
|
<span className="ToolIcon__keybinding">
|
|
|
|
|
|
|
|
{props.keyBindingLabel}
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
{props.isLoading && <Spinner />}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
{props.showAriaLabel && (
|
|
|
|
|
|
|
|
<div className="ToolIcon__label">
|
|
|
|
|
|
|
|
{props["aria-label"]} {isLoading && <Spinner />}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
{props.children}
|
|
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
props.type === "button" ||
|
|
|
|
|
|
|
|
props.type === "icon" ||
|
|
|
|
|
|
|
|
props.type === "submit"
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
const type = (props.type === "icon" ? "button" : props.type) as
|
|
|
|
|
|
|
|
| "button"
|
|
|
|
|
|
|
|
| "submit";
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
<label
|
|
|
|
className={clsx(
|
|
|
|
className={clsx("ToolIcon", className)}
|
|
|
|
"ToolIcon_type_button",
|
|
|
|
|
|
|
|
sizeCn,
|
|
|
|
|
|
|
|
props.className,
|
|
|
|
|
|
|
|
props.visible && !props.hidden
|
|
|
|
|
|
|
|
? "ToolIcon_type_button--show"
|
|
|
|
|
|
|
|
: "ToolIcon_type_button--hide",
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ToolIcon: !props.hidden,
|
|
|
|
|
|
|
|
"ToolIcon--selected": props.selected,
|
|
|
|
|
|
|
|
"ToolIcon--plain": props.type === "icon",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
style={props.style}
|
|
|
|
|
|
|
|
data-testid={props["data-testid"]}
|
|
|
|
|
|
|
|
hidden={props.hidden}
|
|
|
|
|
|
|
|
title={props.title}
|
|
|
|
title={props.title}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
onPointerDown={(event) => {
|
|
|
|
type={type}
|
|
|
|
lastPointerTypeRef.current = event.pointerType || null;
|
|
|
|
onClick={onClick}
|
|
|
|
props.onPointerDown?.({ pointerType: event.pointerType || null });
|
|
|
|
ref={innerRef}
|
|
|
|
}}
|
|
|
|
disabled={isLoading || props.isLoading || !!props.disabled}
|
|
|
|
onPointerUp={() => {
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
|
|
|
lastPointerTypeRef.current = null;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
{(props.icon || props.label) && (
|
|
|
|
<input
|
|
|
|
<div
|
|
|
|
className={`ToolIcon_type_radio ${sizeCn}`}
|
|
|
|
className="ToolIcon__icon"
|
|
|
|
type="radio"
|
|
|
|
aria-hidden="true"
|
|
|
|
name={props.name}
|
|
|
|
aria-disabled={!!props.disabled}
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
>
|
|
|
|
aria-keyshortcuts={props["aria-keyshortcuts"]}
|
|
|
|
{props.icon || props.label}
|
|
|
|
data-testid={props["data-testid"]}
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
id={`${excalId}-${props.id}`}
|
|
|
|
<span className="ToolIcon__keybinding">
|
|
|
|
onChange={() => {
|
|
|
|
{props.keyBindingLabel}
|
|
|
|
props.onChange?.({ pointerType: lastPointerTypeRef.current });
|
|
|
|
</span>
|
|
|
|
}}
|
|
|
|
)}
|
|
|
|
checked={props.checked}
|
|
|
|
{props.isLoading && <Spinner />}
|
|
|
|
ref={innerRef}
|
|
|
|
</div>
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<div className="ToolIcon__icon">
|
|
|
|
{props.showAriaLabel && (
|
|
|
|
{props.icon}
|
|
|
|
<div className="ToolIcon__label">
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
{props["aria-label"]} {isLoading && <Spinner />}
|
|
|
|
<span className="ToolIcon__keybinding">
|
|
|
|
</div>
|
|
|
|
{props.keyBindingLabel}
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
{props.children}
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
|
|
|
|
<label
|
|
|
|
|
|
|
|
className={clsx("ToolIcon", props.className)}
|
|
|
|
|
|
|
|
title={props.title}
|
|
|
|
|
|
|
|
onPointerDown={(event) => {
|
|
|
|
|
|
|
|
lastPointerTypeRef.current = event.pointerType || null;
|
|
|
|
|
|
|
|
props.onPointerDown?.({ pointerType: event.pointerType || null });
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
onPointerUp={() => {
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
|
|
|
lastPointerTypeRef.current = null;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
<input
|
|
|
|
|
|
|
|
className={`ToolIcon_type_radio ${sizeCn}`}
|
|
|
|
|
|
|
|
type="radio"
|
|
|
|
|
|
|
|
name={props.name}
|
|
|
|
|
|
|
|
aria-label={props["aria-label"]}
|
|
|
|
|
|
|
|
aria-keyshortcuts={props["aria-keyshortcuts"]}
|
|
|
|
|
|
|
|
data-testid={props["data-testid"]}
|
|
|
|
|
|
|
|
id={`${excalId}-${props.id}`}
|
|
|
|
|
|
|
|
onChange={() => {
|
|
|
|
|
|
|
|
props.onChange?.({ pointerType: lastPointerTypeRef.current });
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
checked={props.checked}
|
|
|
|
|
|
|
|
ref={innerRef}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="ToolIcon__icon">
|
|
|
|
|
|
|
|
{props.icon}
|
|
|
|
|
|
|
|
{props.keyBindingLabel && (
|
|
|
|
|
|
|
|
<span className="ToolIcon__keybinding">{props.keyBindingLabel}</span>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</label>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToolButton.defaultProps = {
|
|
|
|
|
|
|
|
visible: true,
|
|
|
|
|
|
|
|
className: "",
|
|
|
|
|
|
|
|
size: "medium",
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToolButton.displayName = "ToolButton";
|
|
|
|
ToolButton.displayName = "ToolButton";
|
|
|
|