Skip to content

Commit 950d31a

Browse files
authored
Merge pull request #2809 from roottool/zoom-in-and-out-image#1448
Zoom in and out image #1448
2 parents 543c31c + 5e87ec2 commit 950d31a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

browser/components/MarkdownPreview.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,81 @@ export default class MarkdownPreview extends React.Component {
832832
)
833833
}
834834
)
835+
836+
const markdownPreviewIframe = document.querySelector('.MarkdownPreview')
837+
const rect = markdownPreviewIframe.getBoundingClientRect()
838+
const imgList = markdownPreviewIframe.contentWindow.document.body.querySelectorAll('img')
839+
for (const img of imgList) {
840+
img.onclick = () => {
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 zoomImgTop = (document.body.clientHeight - zoomImgHeight) / 2
849+
const zoomImgLeft = (document.body.clientWidth - zoomImgWidth) / 2
850+
const originalImgTop = img.y + rect.top
851+
const originalImgLeft = img.x + rect.left
852+
const originalImgRect = {
853+
top: `${originalImgTop}px`,
854+
left: `${originalImgLeft}px`,
855+
width: `${img.width}px`,
856+
height: `${img.height}px`
857+
}
858+
const zoomInImgRect = {
859+
top: `${baseOnWidth ? zoomImgTop : 0}px`,
860+
left: `${baseOnWidth ? 0 : zoomImgLeft}px`,
861+
width: `${zoomImgWidth}px`,
862+
height: `${zoomImgHeight}px`
863+
}
864+
const animationSpeed = 300
865+
866+
const zoomImg = document.createElement('img')
867+
zoomImg.src = img.src
868+
zoomImg.style = `
869+
position: absolute;
870+
top: ${baseOnWidth ? zoomImgTop : 0}px;
871+
left: ${baseOnWidth ? 0 : zoomImgLeft}px;
872+
width: ${zoomImgWidth};
873+
height: ${zoomImgHeight}px;
874+
`
875+
zoomImg.animate([
876+
originalImgRect,
877+
zoomInImgRect
878+
], animationSpeed)
879+
880+
const overlay = document.createElement('div')
881+
overlay.style = `
882+
background-color: rgba(0,0,0,0.5);
883+
cursor: zoom-out;
884+
position: absolute;
885+
top: 0;
886+
left: 0;
887+
width: 100%;
888+
height: ${document.body.clientHeight}px;
889+
z-index: 100;
890+
`
891+
overlay.onclick = () => {
892+
zoomImg.style = `
893+
position: absolute;
894+
top: ${originalImgTop}px;
895+
left: ${originalImgLeft}px;
896+
width: ${img.width}px;
897+
height: ${img.height}px;
898+
`
899+
const zoomOutImgAnimation = zoomImg.animate([
900+
zoomInImgRect,
901+
originalImgRect
902+
], animationSpeed)
903+
zoomOutImgAnimation.onfinish = () => overlay.remove()
904+
}
905+
906+
overlay.appendChild(zoomImg)
907+
document.body.appendChild(overlay)
908+
}
909+
}
835910
}
836911

837912
focus () {

browser/components/markdown.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ p
165165
white-space pre-line
166166
word-wrap break-word
167167
img
168+
cursor zoom-in
168169
max-width 100%
169170
strong, b
170171
font-weight bold

0 commit comments

Comments
 (0)