2019-12-09 14:47:23 -03:00
|
|
|
import React from 'react';
|
2019-05-29 08:39:54 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-09 14:47:23 -03:00
|
|
|
import { commandsManager } from './../App.js';
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-05-29 08:39:54 -04:00
|
|
|
import './ToolContextMenu.css';
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-05-13 18:07:57 -04:00
|
|
|
const toolTypes = [
|
|
|
|
|
'Angle',
|
|
|
|
|
'Bidirectional',
|
|
|
|
|
'Length',
|
|
|
|
|
'FreehandMouse',
|
|
|
|
|
'EllipticalRoi',
|
|
|
|
|
'CircleRoi',
|
|
|
|
|
'RectangleRoi',
|
2019-05-29 08:39:54 -04:00
|
|
|
];
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
const ToolContextMenu = ({
|
|
|
|
|
onSetLabel,
|
|
|
|
|
onSetDescription,
|
|
|
|
|
isTouchEvent,
|
|
|
|
|
eventData,
|
|
|
|
|
onClose,
|
|
|
|
|
onDelete,
|
|
|
|
|
}) => {
|
|
|
|
|
const defaultDropdownItems = [
|
|
|
|
|
{
|
|
|
|
|
actionType: 'Delete',
|
|
|
|
|
action: ({ nearbyToolData, eventData }) =>
|
|
|
|
|
onDelete(nearbyToolData, eventData),
|
2019-05-02 17:12:06 -04:00
|
|
|
},
|
2019-12-09 14:47:23 -03:00
|
|
|
{
|
|
|
|
|
actionType: 'setLabel',
|
|
|
|
|
action: ({ nearbyToolData, eventData }) => {
|
|
|
|
|
const { tool: measurementData } = nearbyToolData;
|
|
|
|
|
onSetLabel(eventData, measurementData);
|
|
|
|
|
},
|
2019-05-02 17:12:06 -04:00
|
|
|
},
|
2019-12-09 14:47:23 -03:00
|
|
|
{
|
|
|
|
|
actionType: 'setDescription',
|
|
|
|
|
action: ({ nearbyToolData, eventData }) => {
|
|
|
|
|
const { tool: measurementData } = nearbyToolData;
|
|
|
|
|
onSetDescription(eventData, measurementData);
|
|
|
|
|
},
|
2019-05-02 17:12:06 -04:00
|
|
|
},
|
2019-12-09 14:47:23 -03:00
|
|
|
];
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
const getDropdownItems = (eventData, isTouchEvent = false) => {
|
|
|
|
|
const nearbyToolData = commandsManager.runCommand('getNearbyToolData', {
|
|
|
|
|
element: eventData.element,
|
|
|
|
|
canvasCoordinates: eventData.currentPoints.canvas,
|
|
|
|
|
availableToolTypes: toolTypes,
|
2019-05-29 08:39:54 -04:00
|
|
|
});
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
// Annotate tools for touch events already have a press handle to edit it, has a better UX for deleting it
|
|
|
|
|
if (
|
|
|
|
|
isTouchEvent &&
|
|
|
|
|
nearbyToolData &&
|
|
|
|
|
nearbyToolData.toolType === 'arrowAnnotate'
|
|
|
|
|
) {
|
|
|
|
|
return;
|
2019-05-02 17:12:06 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
let dropdownItems = [];
|
|
|
|
|
if (nearbyToolData) {
|
|
|
|
|
defaultDropdownItems.forEach(item => {
|
|
|
|
|
item.params = {
|
|
|
|
|
eventData,
|
|
|
|
|
nearbyToolData,
|
|
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
if (item.actionType === 'Delete') {
|
|
|
|
|
item.text = 'Delete measurement';
|
2019-05-02 17:12:06 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
if (item.actionType === 'setLabel') {
|
|
|
|
|
item.text = 'Relabel';
|
|
|
|
|
}
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
if (item.actionType === 'setDescription') {
|
|
|
|
|
item.text = `${
|
|
|
|
|
nearbyToolData.tool.description ? 'Edit' : 'Add'
|
|
|
|
|
} Description`;
|
|
|
|
|
}
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
dropdownItems.push(item);
|
|
|
|
|
});
|
2019-05-02 17:12:06 -04:00
|
|
|
}
|
2019-12-09 14:47:23 -03:00
|
|
|
|
|
|
|
|
return dropdownItems;
|
2019-05-29 08:39:54 -04:00
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
const itemOnClickHandler = (action, params, onClose) => {
|
|
|
|
|
action(params);
|
|
|
|
|
if (onClose) {
|
|
|
|
|
onClose();
|
2019-05-02 17:12:06 -04:00
|
|
|
}
|
2019-05-29 08:39:54 -04:00
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
const dropdownItems = getDropdownItems(eventData, isTouchEvent);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
dropdownItems.length &&
|
|
|
|
|
eventData && (
|
|
|
|
|
<div className="ToolContextMenu">
|
|
|
|
|
<ul className="bounded">
|
|
|
|
|
{dropdownItems.map(({ params, action, text, actionType }) => (
|
|
|
|
|
<li key={actionType}>
|
|
|
|
|
<button
|
|
|
|
|
className="form-action"
|
|
|
|
|
onClick={() => itemOnClickHandler(action, params, onClose)}
|
|
|
|
|
>
|
|
|
|
|
<span key={actionType}>{text}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
ToolContextMenu.propTypes = {
|
|
|
|
|
isTouchEvent: PropTypes.bool.isRequired,
|
|
|
|
|
eventData: PropTypes.object,
|
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-12-09 14:47:23 -03:00
|
|
|
ToolContextMenu.defaultProps = {
|
|
|
|
|
isTouchEvent: false,
|
|
|
|
|
};
|
2019-05-02 17:12:06 -04:00
|
|
|
|
2019-05-29 08:39:54 -04:00
|
|
|
export default ToolContextMenu;
|