-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: add useZoom button to control zooming of the canvas from outsi…
…de using the hook quicker
- Loading branch information
1 parent
5e98ffe
commit e1e96df
Showing
5 changed files
with
144 additions
and
92 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
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
105 changes: 105 additions & 0 deletions
105
packages/react-filerobot-image-editor/src/hooks/useZoom.js
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,105 @@ | ||
/** External Dependencies */ | ||
import { useState } from 'react'; | ||
|
||
/** Internal Dependencies */ | ||
import { ZOOM_CANVAS } from 'actions'; | ||
import getZoomFitFactor from 'utils/getZoomFitFactor'; | ||
import { DEFAULT_ZOOM_FACTOR, TOOLS_IDS } from 'utils/constants'; | ||
import toPrecisedFloat from 'utils/toPrecisedFloat'; | ||
import useStore from './useStore'; | ||
|
||
const MULTIPLY_ZOOM_FACTOR = 1.1; | ||
|
||
const useZoom = () => { | ||
const { | ||
dispatch, | ||
zoom = {}, | ||
toolId, | ||
feedback, | ||
shownImageDimensions, | ||
resize, | ||
originalSource, | ||
adjustments: { crop }, | ||
} = useStore(); | ||
const isBlockerError = feedback.duration === 0; | ||
const [zoomingMenuAnchorEl, setZoomingMenuAnchorEl] = useState(null); | ||
|
||
const saveZoom = (zoomFactor, isAbsoluteZoom, zoomCustomLabel) => { | ||
dispatch({ | ||
type: ZOOM_CANVAS, | ||
payload: { | ||
factor: zoomFactor, | ||
isAbsoluteZoom, | ||
customLabel: zoomCustomLabel, | ||
}, | ||
}); | ||
}; | ||
|
||
const zoomIn = () => { | ||
saveZoom(zoom.factor * MULTIPLY_ZOOM_FACTOR); | ||
}; | ||
|
||
const fitCanvas = () => { | ||
const usedAsOrgDimens = | ||
(resize.width && resize.height && resize) || | ||
(crop.width && crop.height && crop) || | ||
shownImageDimensions; | ||
const fitCanvasFactor = getZoomFitFactor( | ||
(crop.width && crop.height && crop) || shownImageDimensions, | ||
usedAsOrgDimens, | ||
); | ||
saveZoom(fitCanvasFactor || DEFAULT_ZOOM_FACTOR, true); | ||
}; | ||
|
||
const zoomOut = () => { | ||
saveZoom(zoom.factor / MULTIPLY_ZOOM_FACTOR); | ||
}; | ||
|
||
const toggleZoomingMenu = (event) => { | ||
setZoomingMenuAnchorEl(zoomingMenuAnchorEl || !event ? null : event.target); | ||
}; | ||
|
||
const applyZoomFactorPreset = (factor, zoomCustomLabel) => { | ||
if (factor === 'fit') { | ||
fitCanvas(); | ||
toggleZoomingMenu(); | ||
return; | ||
} | ||
|
||
const factorToAchieveSelected = | ||
resize.width || resize.height | ||
? factor | ||
: factor / shownImageDimensions.originalSourceInitialScale; | ||
saveZoom(factorToAchieveSelected, true, zoomCustomLabel); | ||
toggleZoomingMenu(); | ||
}; | ||
|
||
const getPreviewedZoomLevelLabel = () => { | ||
if (zoom.customLabel) { | ||
return zoom.customLabel; | ||
} | ||
|
||
const previewToRealImgFactor = | ||
originalSource && !resize.width && !resize.height | ||
? shownImageDimensions.originalSourceInitialScale * zoom.factor | ||
: zoom.factor; | ||
return `${toPrecisedFloat(previewToRealImgFactor * 100, 0) || '100'}%`; | ||
}; | ||
|
||
const isZoomDisabled = toolId === TOOLS_IDS.CROP || isBlockerError; | ||
|
||
return { | ||
applyZoomFactorPreset, | ||
getPreviewedZoomLevelLabel, | ||
isZoomDisabled, | ||
fitCanvas, | ||
zoomIn, | ||
zoomOut, | ||
saveZoom, | ||
toggleZoomingMenu, | ||
zoomingMenuAnchorEl, | ||
setZoomingMenuAnchorEl, | ||
}; | ||
}; | ||
|
||
export default useZoom; |