-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
312 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
cvat-ui/src/components/annotation-page/standard-workspace/canvas-context-menu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import ObjectItemContainer from 'containers/annotation-page/standard-workspace/objects-side-bar/object-item'; | ||
|
||
interface Props { | ||
activatedStateID: number | null; | ||
visible: boolean; | ||
left: number; | ||
top: number; | ||
} | ||
|
||
export default function CanvasContextMenu(props: Props): JSX.Element | null { | ||
const { | ||
activatedStateID, | ||
visible, | ||
left, | ||
top, | ||
} = props; | ||
|
||
if (!visible || activatedStateID === null) { | ||
return null; | ||
} | ||
|
||
return ReactDOM.createPortal( | ||
<div className='cvat-canvas-context-menu' style={{ top, left }}> | ||
<ObjectItemContainer clientID={activatedStateID} /> | ||
</div>, | ||
window.document.body, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
cvat-ui/src/containers/annotation-page/standard-workspace/canvas-context-menu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import React from 'react'; | ||
|
||
import { connect } from 'react-redux'; | ||
import { CombinedState } from 'reducers/interfaces'; | ||
|
||
import CanvasContextMenuComponent from 'components/annotation-page/standard-workspace/canvas-context-menu'; | ||
|
||
interface StateToProps { | ||
activatedStateID: number | null; | ||
visible: boolean; | ||
top: number; | ||
left: number; | ||
collapsed: boolean | undefined; | ||
} | ||
|
||
function mapStateToProps(state: CombinedState): StateToProps { | ||
const { | ||
annotation: { | ||
annotations: { | ||
activatedStateID, | ||
collapsed, | ||
}, | ||
canvas: { | ||
contextMenu: { | ||
visible, | ||
top, | ||
left, | ||
}, | ||
}, | ||
}, | ||
} = state; | ||
|
||
return { | ||
activatedStateID, | ||
collapsed: activatedStateID !== null ? collapsed[activatedStateID] : undefined, | ||
visible, | ||
left, | ||
top, | ||
}; | ||
} | ||
|
||
type Props = StateToProps; | ||
|
||
interface State { | ||
latestLeft: number; | ||
latestTop: number; | ||
left: number; | ||
top: number; | ||
} | ||
|
||
class CanvasContextMenuContainer extends React.PureComponent<Props, State> { | ||
private initialized: HTMLDivElement | null; | ||
private dragging: boolean; | ||
private dragInitPosX: number; | ||
private dragInitPosY: number; | ||
public constructor(props: Props) { | ||
super(props); | ||
|
||
this.initialized = null; | ||
this.dragging = false; | ||
this.dragInitPosX = 0; | ||
this.dragInitPosY = 0; | ||
this.state = { | ||
latestLeft: 0, | ||
latestTop: 0, | ||
left: 0, | ||
top: 0, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromProps(props: Props, state: State): State | null { | ||
if (props.left === state.latestLeft | ||
&& props.top === state.latestTop) { | ||
return null; | ||
} | ||
|
||
return { | ||
...state, | ||
latestLeft: props.left, | ||
latestTop: props.top, | ||
top: props.top, | ||
left: props.left, | ||
}; | ||
} | ||
|
||
public componentDidMount(): void { | ||
this.updatePositionIfOutOfScreen(); | ||
window.addEventListener('mousemove', this.moveContextMenu); | ||
} | ||
|
||
public componentDidUpdate(prevProps: Props): void { | ||
const { collapsed } = this.props; | ||
|
||
const [element] = window.document.getElementsByClassName('cvat-canvas-context-menu'); | ||
if (collapsed !== prevProps.collapsed && element) { | ||
element.addEventListener('transitionend', () => { | ||
this.updatePositionIfOutOfScreen(); | ||
}, { once: true }); | ||
} else if (element) { | ||
this.updatePositionIfOutOfScreen(); | ||
} | ||
|
||
if (element && (!this.initialized || this.initialized !== element)) { | ||
this.initialized = element as HTMLDivElement; | ||
|
||
this.initialized.addEventListener('mousedown', (e: MouseEvent): any => { | ||
this.dragging = true; | ||
this.dragInitPosX = e.clientX; | ||
this.dragInitPosY = e.clientY; | ||
}); | ||
|
||
this.initialized.addEventListener('mouseup', () => { | ||
this.dragging = false; | ||
}); | ||
} | ||
} | ||
|
||
public componentWillUnmount(): void { | ||
window.removeEventListener('mousemove', this.moveContextMenu); | ||
} | ||
|
||
private moveContextMenu = (e: MouseEvent): void => { | ||
if (this.dragging) { | ||
this.setState((state) => { | ||
const value = { | ||
left: state.left + e.clientX - this.dragInitPosX, | ||
top: state.top + e.clientY - this.dragInitPosY, | ||
}; | ||
|
||
this.dragInitPosX = e.clientX; | ||
this.dragInitPosY = e.clientY; | ||
|
||
return value; | ||
}); | ||
|
||
e.preventDefault(); | ||
} | ||
}; | ||
|
||
private updatePositionIfOutOfScreen(): void { | ||
const { | ||
top, | ||
left, | ||
} = this.state; | ||
|
||
const { | ||
innerWidth, | ||
innerHeight, | ||
} = window; | ||
|
||
const [element] = window.document.getElementsByClassName('cvat-canvas-context-menu'); | ||
if (element) { | ||
const height = element.clientHeight; | ||
const width = element.clientWidth; | ||
|
||
if (top + height > innerHeight || left + width > innerWidth) { | ||
this.setState({ | ||
top: top - Math.max(top + height - innerHeight, 0), | ||
left: left - Math.max(left + width - innerWidth, 0), | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public render(): JSX.Element { | ||
const { | ||
left, | ||
top, | ||
} = this.state; | ||
|
||
const { | ||
visible, | ||
activatedStateID, | ||
} = this.props; | ||
|
||
return ( | ||
<CanvasContextMenuComponent | ||
left={left} | ||
top={top} | ||
visible={visible} | ||
activatedStateID={activatedStateID} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
)(CanvasContextMenuContainer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.