Skip to content

Commit cdf6f38

Browse files
committed
fix for uncontrolled resizing
1 parent 053b589 commit cdf6f38

File tree

4 files changed

+80
-17
lines changed

4 files changed

+80
-17
lines changed

src/Annotator/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import generalReducer from "./reducers/general-reducer.js"
1717
import imageReducer from "./reducers/image-reducer.js"
1818
import videoReducer from "./reducers/video-reducer.js"
1919
import historyHandler from "./reducers/history-handler.js"
20-
import ShortcutsManager from "../ShortcutsManager"
2120

2221
import useEventCallback from "use-event-callback"
2322
import makeImmutable, { without } from "seamless-immutable"
@@ -132,15 +131,13 @@ export const Annotator = ({
132131

133132
return (
134133
<SettingsProvider>
135-
<ShortcutsManager dispatch={dispatch}>
136-
<MainLayout
137-
RegionEditLabel={RegionEditLabel}
138-
alwaysShowNextButton={Boolean(onNextImage)}
139-
alwaysShowPrevButton={Boolean(onPrevImage)}
140-
state={state}
141-
dispatch={dispatch}
142-
/>
143-
</ShortcutsManager>
134+
<MainLayout
135+
RegionEditLabel={RegionEditLabel}
136+
alwaysShowNextButton={Boolean(onNextImage)}
137+
alwaysShowPrevButton={Boolean(onPrevImage)}
138+
state={state}
139+
dispatch={dispatch}
140+
/>
144141
</SettingsProvider>
145142
)
146143
}

src/Annotator/index.story.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ storiesOf("Annotator", module)
5050
]}
5151
/>
5252
))
53+
.add("Fixed Size Container", () => (
54+
<div style={{ width: 500, height: 500 }}>
55+
<Annotator
56+
onExit={actionAddon("onExit")}
57+
middlewares={[
58+
(store) => (next) => (action) => {
59+
actionAddon(action.type)(action)
60+
return next(action)
61+
},
62+
]}
63+
labelImages
64+
regionClsList={["Alpha", "Beta", "Charlie", "Delta"]}
65+
regionTagList={["tag1", "tag2", "tag3"]}
66+
imageClsList={["Alpha", "Beta", "Charlie", "Delta"]}
67+
imageTagList={["tag1", "tag2", "tag3"]}
68+
images={[
69+
{
70+
src: exampleImage,
71+
name: "Seve's Desk",
72+
regions: testRegions,
73+
},
74+
{
75+
src: "https://loremflickr.com/100/100/cars?lock=1",
76+
name: "Frame 0036",
77+
},
78+
{
79+
src: "https://loremflickr.com/100/100/cars?lock=2",
80+
name: "Frame 0037",
81+
},
82+
{
83+
src: "https://loremflickr.com/100/100/cars?lock=3",
84+
name: "Frame 0038",
85+
},
86+
]}
87+
/>
88+
</div>
89+
))
5390
.add("Shrunk Annotator (Test Fullscreen)", () => (
5491
<div style={{ padding: 100 }}>
5592
<Annotator
@@ -565,7 +602,6 @@ storiesOf("Annotator", module)
565602
)
566603
})
567604
.add("Two on sample page w/ hotkeys", () => {
568-
console.log({ defaultKeyMap })
569605
return (
570606
<HotKeys keyMap={defaultKeyMap}>
571607
<div>

src/MainLayout/index.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ import SettingsDialog from "../SettingsDialog"
1717
import Fullscreen from "../Fullscreen"
1818
import getActiveImage from "../Annotator/reducers/get-active-image"
1919
import useImpliedVideoRegions from "./use-implied-video-regions"
20+
import { useDispatchHotkeyHandlers } from "../ShortcutsManager"
21+
import { withHotKeys } from "react-hotkeys"
2022

2123
const useStyles = makeStyles(styles)
2224

25+
const HotkeyDiv = withHotKeys(({ hotKeys, children, divRef, ...props }) => (
26+
<div {...{ ...hotKeys, ...props }} ref={divRef}>
27+
{children}
28+
</div>
29+
))
30+
2331
type Props = {
2432
state: MainLayoutState,
2533
RegionEditLabel?: Node,
@@ -69,6 +77,7 @@ export default ({
6977

7078
const isAVideoFrame = activeImage && activeImage.frameTime !== undefined
7179
const innerContainerRef = useRef()
80+
const hotkeyHandlers = useDispatchHotkeyHandlers({ dispatch })
7281

7382
let impliedVideoRegions = useImpliedVideoRegions(state)
7483

@@ -81,15 +90,17 @@ export default ({
8190
}
8291
}}
8392
>
84-
<div
85-
ref={innerContainerRef}
93+
<HotkeyDiv
8694
tabIndex={-1}
87-
onMouseOver={(e) => {
95+
divRef={innerContainerRef}
96+
onMouseDown={(e) => {
8897
if (innerContainerRef.current) innerContainerRef.current.focus()
8998
}}
90-
onMouseDown={(e) => {
99+
onMouseOver={(e) => {
91100
if (innerContainerRef.current) innerContainerRef.current.focus()
92101
}}
102+
allowChanges
103+
handlers={hotkeyHandlers}
93104
className={classnames(
94105
classes.container,
95106
state.fullScreen && "Fullscreen"
@@ -242,7 +253,7 @@ export default ({
242253
})
243254
}
244255
/>
245-
</div>
256+
</HotkeyDiv>
246257
</Fullscreen>
247258
)
248259
}

src/ShortcutsManager/index.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ export const defaultHotkeys = [
1616
id: "create_point",
1717
description: "Create a point",
1818
},
19+
{
20+
id: "create_bounding_box",
21+
description: "Create a bounding box",
22+
binding: "b",
23+
},
1924
{
2025
id: "pan_tool",
2126
description: "Select the Pan Tool",
27+
binding: "p",
2228
},
2329
{
2430
id: "create_polygon",
@@ -31,10 +37,12 @@ export const defaultHotkeys = [
3137
{
3238
id: "save_and_previous_sample",
3339
description: "Save and go to previous sample",
40+
binding: "a",
3441
},
3542
{
3643
id: "save_and_next_sample",
3744
description: "Save and go to next sample",
45+
binding: "d",
3846
},
3947
{
4048
id: "save_and_exit_sample",
@@ -48,7 +56,7 @@ export const defaultHotkeys = [
4856
export const defaultKeyMap = {}
4957
for (const { id, binding } of defaultHotkeys) defaultKeyMap[id] = binding
5058

51-
export default ({ children, dispatch }) => {
59+
export const useDispatchHotkeyHandlers = ({ dispatch }) => {
5260
const handlers = useMemo(
5361
() => ({
5462
select_tool: () => {
@@ -69,6 +77,12 @@ export default ({ children, dispatch }) => {
6977
selectedTool: "create-point",
7078
})
7179
},
80+
create_bounding_box: () => {
81+
dispatch({
82+
type: "SELECT_TOOL",
83+
selectedTool: "create-box",
84+
})
85+
},
7286
pan_tool: () => {
7387
dispatch({
7488
type: "SELECT_TOOL",
@@ -114,6 +128,11 @@ export default ({ children, dispatch }) => {
114128
}),
115129
[dispatch]
116130
)
131+
return handlers
132+
}
133+
134+
export default ({ children, dispatch }) => {
135+
const handlers = useDispatchHotkeyHandlers({ dispatch })
117136
return (
118137
<HotKeys allowChanges handlers={handlers}>
119138
{children}

0 commit comments

Comments
 (0)