2022-10-12 18:22:22 +00:00
|
|
|
import * as React from "react";
|
|
|
|
import styled from "styled-components";
|
|
|
|
import Icon, { ActionType } from "./icon";
|
|
|
|
|
2022-10-27 11:07:09 +00:00
|
|
|
import MediaContextMenu from "../../../../public/images/vertical-dots.react.svg";
|
|
|
|
|
2022-10-12 18:22:22 +00:00
|
|
|
const ToolbarItem = styled.li`
|
|
|
|
height: 48px;
|
|
|
|
width: 48px;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
cursor: pointer;
|
|
|
|
${(props) =>
|
|
|
|
props.noHover ? "background-color: none;" : "background-color: #4d4d4d;"}
|
|
|
|
}
|
|
|
|
|
|
|
|
svg {
|
|
|
|
path,
|
|
|
|
rect {
|
2022-10-27 15:04:16 +00:00
|
|
|
${(props) => (props.percent !== 25 ? "fill: #fff;" : "fill: #BEBEBE;")}
|
2022-10-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
export const defaultToolbars = [
|
|
|
|
{
|
|
|
|
key: "zoomIn",
|
|
|
|
actionType: ActionType.zoomIn,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "zoomOut",
|
|
|
|
actionType: ActionType.zoomOut,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "prev",
|
|
|
|
actionType: ActionType.prev,
|
|
|
|
noHover: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "reset",
|
|
|
|
actionType: ActionType.reset,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "next",
|
|
|
|
actionType: ActionType.next,
|
|
|
|
noHover: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "rotateLeft",
|
|
|
|
actionType: ActionType.rotateLeft,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "rotateRight",
|
|
|
|
actionType: ActionType.rotateRight,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "scaleX",
|
|
|
|
actionType: ActionType.scaleX,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "scaleY",
|
|
|
|
actionType: ActionType.scaleY,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "download",
|
|
|
|
actionType: ActionType.download,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
function deleteToolbarFromKey(toolbars, keys) {
|
|
|
|
const targetToolbar = toolbars.filter((item) => keys.indexOf(item.key) < 0);
|
|
|
|
|
|
|
|
return targetToolbar;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ViewerToolbar(props) {
|
|
|
|
function handleAction(config) {
|
2022-10-27 15:04:16 +00:00
|
|
|
if (config.key === "percent") return props.onPercentClick();
|
2022-10-12 18:22:22 +00:00
|
|
|
props.onAction(config);
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:07:09 +00:00
|
|
|
const [isOpen, setIsOpen] = React.useState(false);
|
|
|
|
const iconRef = React.useRef(null);
|
|
|
|
|
2022-10-12 18:22:22 +00:00
|
|
|
function renderAction(config) {
|
|
|
|
let content = null;
|
|
|
|
if (typeof ActionType[config.actionType] !== "undefined") {
|
|
|
|
content = <Icon type={config.actionType} />;
|
|
|
|
}
|
|
|
|
if (config.render) {
|
|
|
|
content = config.render;
|
|
|
|
}
|
|
|
|
|
2022-10-27 15:04:16 +00:00
|
|
|
if (config.key === "percent") {
|
|
|
|
content = (
|
|
|
|
<div
|
|
|
|
className="iconContainer zoomPercent"
|
|
|
|
style={{ width: "auto", color: "#fff", userSelect: "none" }}
|
|
|
|
>
|
|
|
|
{`${props.percent}%`}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:07:09 +00:00
|
|
|
if (config.key === "context-menu") {
|
|
|
|
const contextMenu = props.generateContextMenu(isOpen);
|
|
|
|
return (
|
|
|
|
<ToolbarItem
|
|
|
|
ref={iconRef}
|
|
|
|
style={{ position: "relative" }}
|
|
|
|
noHover={config.noHover}
|
|
|
|
key={config.key}
|
|
|
|
className={`${props.prefixCls}-btn`}
|
|
|
|
onClick={() => setIsOpen((open) => !open)}
|
|
|
|
data-key={config.key}
|
|
|
|
>
|
|
|
|
<div className="context" style={{ height: "16px" }}>
|
|
|
|
<MediaContextMenu size="scale" />
|
|
|
|
</div>
|
|
|
|
{contextMenu}
|
|
|
|
</ToolbarItem>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-12 18:22:22 +00:00
|
|
|
return (
|
|
|
|
<ToolbarItem
|
2022-10-27 15:04:16 +00:00
|
|
|
percent={config.percent ? props.percent : 100}
|
2022-10-12 18:22:22 +00:00
|
|
|
noHover={config.noHover}
|
|
|
|
key={config.key}
|
|
|
|
className={`${props.prefixCls}-btn`}
|
|
|
|
onClick={() => {
|
|
|
|
handleAction(config);
|
|
|
|
}}
|
|
|
|
data-key={config.key}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</ToolbarItem>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let attributeNode = props.attribute ? (
|
|
|
|
<p className={`${props.prefixCls}-attribute`}>
|
|
|
|
{props.alt && `${props.alt}`}
|
|
|
|
{props.noImgDetails || (
|
|
|
|
<span className={`${props.prefixCls}-img-details`}>
|
|
|
|
{`(${props.width} x ${props.height})`}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{props.showTotal && (
|
|
|
|
<span className={`${props.prefixCls}-showTotal`}>
|
|
|
|
{`${props.activeIndex + 1} ${props.totalName} ${props.count}`}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
) : null;
|
|
|
|
let toolbars = props.toolbars;
|
|
|
|
if (!props.zoomable) {
|
|
|
|
toolbars = deleteToolbarFromKey(toolbars, ["zoomIn", "zoomOut"]);
|
|
|
|
}
|
|
|
|
if (!props.changeable) {
|
|
|
|
toolbars = deleteToolbarFromKey(toolbars, ["prev", "next"]);
|
|
|
|
}
|
|
|
|
if (!props.rotatable) {
|
|
|
|
toolbars = deleteToolbarFromKey(toolbars, ["rotateLeft", "rotateRight"]);
|
|
|
|
}
|
|
|
|
if (!props.scalable) {
|
|
|
|
toolbars = deleteToolbarFromKey(toolbars, ["scaleX", "scaleY"]);
|
|
|
|
}
|
|
|
|
if (!props.downloadable) {
|
|
|
|
toolbars = deleteToolbarFromKey(toolbars, ["download"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* {attributeNode} */}
|
|
|
|
<ul className={`${props.prefixCls}-toolbar`}>
|
|
|
|
{toolbars.map((item) => {
|
|
|
|
return renderAction(item);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|