Skip to content

Commit 5e87ec2

Browse files
committed
Specified MarkdownPreview class
1 parent 1683d63 commit 5e87ec2

File tree

1 file changed

+68
-69
lines changed

1 file changed

+68
-69
lines changed

browser/components/MarkdownPreview.js

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -833,81 +833,80 @@ export default class MarkdownPreview extends React.Component {
833833
}
834834
)
835835

836-
document.querySelectorAll('iframe').forEach(item => {
837-
const rect = item.getBoundingClientRect()
838-
const imgList = item.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
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 = () => {
868892
zoomImg.style = `
869893
position: absolute;
870-
top: ${baseOnWidth ? zoomImgTop : 0}px;
871-
left: ${baseOnWidth ? 0 : zoomImgLeft}px;
872-
width: ${zoomImgWidth};
873-
height: ${zoomImgHeight}px;
894+
top: ${originalImgTop}px;
895+
left: ${originalImgLeft}px;
896+
width: ${img.width}px;
897+
height: ${img.height}px;
874898
`
875-
zoomImg.animate([
876-
originalImgRect,
877-
zoomInImgRect
899+
const zoomOutImgAnimation = zoomImg.animate([
900+
zoomInImgRect,
901+
originalImgRect
878902
], 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)
903+
zoomOutImgAnimation.onfinish = () => overlay.remove()
908904
}
905+
906+
overlay.appendChild(zoomImg)
907+
document.body.appendChild(overlay)
909908
}
910-
})
909+
}
911910
}
912911

913912
focus () {

0 commit comments

Comments
 (0)