Skip to content

Commit

Permalink
fix: metadata contextmenu position (#6643)
Browse files Browse the repository at this point in the history
Co-authored-by: 杨国璇 <[email protected]>
  • Loading branch information
YangGuoXuan-0503 and 杨国璇 authored Aug 26, 2024
1 parent 31fdf97 commit 4dea019
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 17 deletions.
2 changes: 2 additions & 0 deletions frontend/src/components/dir-view-mode/dir-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const DirViews = ({ userPerm, repoID, currentPath, currentRepoInfo }) => {
updateEnableMetadata(value);
}, [updateEnableMetadata]);

if (!enableMetadataManagement) return null;

return (
<>
<TreeSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
display: block;
opacity: 1;
box-shadow: 0 0 5px #ccc;
position: fixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const ContextMenu = ({
onClearSelected,
onCopySelected,
updateRecords,
getTableContentRect,
getTableCanvasContainerRect,
}) => {
const menuRef = useRef(null);
const [visible, setVisible] = useState(false);
Expand Down Expand Up @@ -220,41 +222,33 @@ const ContextMenu = ({
setVisible(false);
}, [onOpenFileInNewTab, onOpenParentFolder, onCopySelected, onClearSelected, generateSummary]);

const getMenuPosition = (x = 0, y = 0) => {
const getMenuPosition = useCallback((x = 0, y = 0) => {
let menuStyles = {
top: y,
left: x
};
if (!menuRef.current) return menuStyles;

const { innerWidth, innerHeight } = window;
const rect = menuRef.current.getBoundingClientRect();
const tableCanvasContainerRect = getTableCanvasContainerRect();
const tableContentRect = getTableContentRect();
const { right: innerWidth, bottom: innerHeight } = tableContentRect;
menuStyles.top = menuStyles.top - tableCanvasContainerRect.top;
menuStyles.left = menuStyles.left - tableCanvasContainerRect.left;

// Calculate the offset of the parent components
const parentRect = menuRef.current.parentElement.getBoundingClientRect();
const offsetX = parentRect.left;
const offsetY = parentRect.top;

// Adjust the position based on the offset
menuStyles.top = y - offsetY;
menuStyles.left = x - offsetX;

const metadataResultFooterHeight = 32;
const contentHeight = innerHeight - metadataResultFooterHeight;
if (y + rect.height > contentHeight) {
if (y + rect.height > innerHeight - 10) {
menuStyles.top -= rect.height;
}
if (x + rect.width > innerWidth) {
menuStyles.left -= rect.width;
}
if (menuStyles.top < 0) {
menuStyles.top = rect.height < contentHeight ? (contentHeight - rect.height) / 2 : 0;
menuStyles.top = rect.bottom > innerHeight ? (innerHeight - 10 - rect.height) / 2 : 0;
}
if (menuStyles.left < 0) {
menuStyles.left = rect.width < innerWidth ? (innerWidth - rect.width) / 2 : 0;
}
return menuStyles;
};
}, [getTableContentRect, getTableCanvasContainerRect]);

useEffect(() => {
const handleShow = (event) => {
Expand All @@ -272,6 +266,7 @@ const ContextMenu = ({
return () => {
document.removeEventListener('contextmenu', handleShow);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
Expand Down Expand Up @@ -313,6 +308,7 @@ ContextMenu.propTypes = {
selectedRange: PropTypes.object,
selectedPosition: PropTypes.object,
recordMetrics: PropTypes.object,
getTableContentRect: PropTypes.func,
recordGetterByIndex: PropTypes.func,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ class RecordsBody extends Component {
gridUtils={this.props.gridUtils}
getCopiedRecordsAndColumnsFromRange={this.props.getCopiedRecordsAndColumnsFromRange}
modifyColumnData={this.props.modifyColumnData}
getTableCanvasContainerRect={this.props.getTableCanvasContainerRect}
/>
<div className="sf-metadata-result-table" style={{ width: this.props.totalWidth + SEQUENCE_COLUMN_WIDTH }} ref={this.setResultRef}>
{this.renderRecords()}
Expand Down Expand Up @@ -608,6 +609,7 @@ RecordsBody.propTypes = {
openDownloadFilesDialog: PropTypes.func,
cacheDownloadFilesProps: PropTypes.func,
onCellContextMenu: PropTypes.func,
getTableCanvasContainerRect: PropTypes.func,
};

export default RecordsBody;
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ class GroupBody extends Component {
gridUtils={this.props.gridUtils}
getCopiedRecordsAndColumnsFromRange={this.props.getCopiedRecordsAndColumnsFromRange}
modifyColumnData={this.props.modifyColumnData}
getTableCanvasContainerRect={this.props.getTableCanvasContainerRect}
/>
<div className="sf-metadata-result-table" ref={this.setResultRef}>
{this.renderGroups()}
Expand Down Expand Up @@ -972,6 +973,7 @@ GroupBody.propTypes = {
openDownloadFilesDialog: PropTypes.func,
cacheDownloadFilesProps: PropTypes.func,
onCellContextMenu: PropTypes.func,
getTableCanvasContainerRect: PropTypes.func,
};

export default GroupBody;
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ class Records extends Component {
}
};

getTableCanvasContainerRect = () => {
return this.resultContainerRef.getBoundingClientRect();
};

renderRecordsBody = ({ containerWidth }) => {
const { isGroupView, recordGetterByIndex, updateRecords } = this.props;
const { recordMetrics, columnMetrics, colOverScanStartIdx, colOverScanEndIdx } = this.state;
Expand All @@ -629,6 +633,7 @@ class Records extends Component {
hasSelectedCell: this.hasSelectedCell,
cacheScrollTop: this.storeScrollTop,
onCellContextMenu: this.onCellContextMenu,
getTableCanvasContainerRect: this.getTableCanvasContainerRect,
};
if (this.props.isGroupView) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,8 @@ class InteractionMasks extends React.Component {
selectedRange: !isSelectedSingleCell ? selectedRange : null,
onClearSelected: this.handleSelectCellsDelete,
onCopySelected: this.onCopySelected,
getTableContentRect: this.props.getTableContentRect,
getTableCanvasContainerRect: this.props.getTableCanvasContainerRect
})}
</div>
);
Expand Down Expand Up @@ -1178,6 +1180,7 @@ InteractionMasks.propTypes = {
gridUtils: PropTypes.object,
getCopiedRecordsAndColumnsFromRange: PropTypes.func,
onCommit: PropTypes.func,
getTableCanvasContainerRect: PropTypes.func,
};

export default InteractionMasks;

0 comments on commit 4dea019

Please sign in to comment.