Skip to content

Commit ea5970a

Browse files
committed
change a magnification by image size #1448
1 parent 72df418 commit ea5970a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

browser/components/MarkdownPreview.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -838,30 +838,39 @@ export default class MarkdownPreview extends React.Component {
838838
const imgList = item.contentWindow.document.body.querySelectorAll('img')
839839
for (const img of imgList) {
840840
img.onclick = () => {
841-
const magnification = document.body.clientWidth / img.width
842-
const top = document.body.clientHeight / 2 - img.height * magnification / 2
843-
const animationSpeed = 300
841+
const widthMagnification = document.body.clientWidth / img.width
842+
const heightMagnification = document.body.clientHeight / img.height
843+
const baseOnWidth = widthMagnification < heightMagnification
844+
const magnification = baseOnWidth ? widthMagnification : heightMagnification
845+
846+
const zoomImgWidth = img.width * magnification
847+
const zoomImgHeight = img.height * magnification
848+
const zoomInImgTop = document.body.clientHeight / 2 - zoomImgHeight / 2
849+
const zoomInImgLeft = document.body.clientWidth / 2 - zoomImgWidth / 2
850+
const originalImgTop = img.y + rect.top
851+
const originalImgLeft = img.x + rect.left
844852
const originalImgRect = {
845-
top: `${img.y + rect.top}px`,
846-
left: `${img.x + rect.left}px`,
853+
top: `${originalImgTop}px`,
854+
left: `${originalImgLeft}px`,
847855
width: `${img.width}px`,
848856
height: `${img.height}px`
849857
}
850858
const zoomInImgRect = {
851-
top: `${top}px`,
852-
left: 0,
853-
width: `100%`,
854-
height: `${img.height * magnification}px`
859+
top: `${baseOnWidth ? zoomInImgTop : 0}px`,
860+
left: `${baseOnWidth ? 0 : zoomInImgLeft}px`,
861+
width: `${zoomImgWidth}px`,
862+
height: `${zoomImgHeight}px`
855863
}
864+
const animationSpeed = 300
856865

857866
const zoomImg = document.createElement('img')
858867
zoomImg.src = img.src
859868
zoomImg.style = `
860869
position: absolute;
861-
top: ${top}px;
862-
left: 0;
863-
width: 100%;
864-
height: ${img.height * magnification}px;
870+
top: ${baseOnWidth ? zoomInImgTop : 0}px;
871+
left: ${baseOnWidth ? 0 : zoomInImgLeft}px;
872+
width: ${zoomImgWidth};
873+
height: ${zoomImgHeight}px;
865874
`
866875
zoomImg.animate([
867876
originalImgRect,
@@ -882,8 +891,8 @@ export default class MarkdownPreview extends React.Component {
882891
overlay.onclick = () => {
883892
zoomImg.style = `
884893
position: absolute;
885-
top: ${img.y + rect.top}px;
886-
left: ${img.x + rect.left}px;
894+
top: ${originalImgTop}px;
895+
left: ${originalImgLeft}px;
887896
width: ${img.width}px;
888897
height: ${img.height}px;
889898
`

0 commit comments

Comments
 (0)