-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Zoom in and out image #1448 #2809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f566b56
72df418
ea5970a
294bf74
33fb030
1683d63
5e87ec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -832,6 +832,82 @@ export default class MarkdownPreview extends React.Component { | |
| ) | ||
| } | ||
| ) | ||
|
|
||
| document.querySelectorAll('iframe').forEach(item => { | ||
| const rect = item.getBoundingClientRect() | ||
| const imgList = item.contentWindow.document.body.querySelectorAll('img') | ||
| for (const img of imgList) { | ||
| img.onclick = () => { | ||
| const widthMagnification = document.body.clientWidth / img.width | ||
| const heightMagnification = document.body.clientHeight / img.height | ||
| const baseOnWidth = widthMagnification < heightMagnification | ||
| const magnification = baseOnWidth ? widthMagnification : heightMagnification | ||
|
|
||
| const zoomImgWidth = img.width * magnification | ||
| const zoomImgHeight = img.height * magnification | ||
| const zoomImgTop = document.body.clientHeight / 2 - zoomImgHeight / 2 | ||
|
||
| const zoomImgLeft = document.body.clientWidth / 2 - zoomImgWidth / 2 | ||
| const originalImgTop = img.y + rect.top | ||
| const originalImgLeft = img.x + rect.left | ||
| const originalImgRect = { | ||
| top: `${originalImgTop}px`, | ||
| left: `${originalImgLeft}px`, | ||
| width: `${img.width}px`, | ||
| height: `${img.height}px` | ||
| } | ||
| const zoomInImgRect = { | ||
| top: `${baseOnWidth ? zoomImgTop : 0}px`, | ||
| left: `${baseOnWidth ? 0 : zoomImgLeft}px`, | ||
| width: `${zoomImgWidth}px`, | ||
| height: `${zoomImgHeight}px` | ||
| } | ||
| const animationSpeed = 300 | ||
|
|
||
| const zoomImg = document.createElement('img') | ||
| zoomImg.src = img.src | ||
| zoomImg.style = ` | ||
| position: absolute; | ||
| top: ${baseOnWidth ? zoomImgTop : 0}px; | ||
| left: ${baseOnWidth ? 0 : zoomImgLeft}px; | ||
| width: ${zoomImgWidth}; | ||
| height: ${zoomImgHeight}px; | ||
| ` | ||
| zoomImg.animate([ | ||
| originalImgRect, | ||
| zoomInImgRect | ||
| ], animationSpeed) | ||
|
|
||
| const overlay = document.createElement('div') | ||
| overlay.style = ` | ||
| background-color: rgba(0,0,0,0.5); | ||
| cursor: zoom-out; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: ${document.body.clientHeight}px; | ||
| z-index: 100; | ||
| ` | ||
| overlay.onclick = () => { | ||
| zoomImg.style = ` | ||
| position: absolute; | ||
| top: ${originalImgTop}px; | ||
| left: ${originalImgLeft}px; | ||
| width: ${img.width}px; | ||
| height: ${img.height}px; | ||
| ` | ||
| const zoomOutImgAnimation = zoomImg.animate([ | ||
| zoomInImgRect, | ||
| originalImgRect | ||
| ], animationSpeed) | ||
| zoomOutImgAnimation.onfinish = () => overlay.remove() | ||
| } | ||
|
|
||
| overlay.appendChild(zoomImg) | ||
| document.body.appendChild(overlay) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| focus () { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to select all iframes like this? The markdown preview only contains 1 iframe, then why did you have to find all iframes and loop through them? Can you explain this to me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explanation
The code is my mistake.
Because when I tried to catch image elements into iframe, I used the code as it is without thinking.
Suggestion
I have a suggestion to resolve it.

document.querySelector()searches for an element from entire HTML.I considered the possibility of adding new iframes in Boostnote.
So I use a classname of a markdown preview iframe.
It has a pros and cons.
Pros
MarkdownPreviewused only here.Cons
MarkdownPreview, a classname usingdocument.querySelector()will need to change too.Before
After
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can just go for the 2nd method, I don't think there's any reason to change the
MarkdownPreviewclass name. In case we were going to change it, a simple search & replace for theMarkdownPreviewclass wouldn't be hard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was worried about changing only in HTML.
I want to modify from before code to after code because I feel relieved by your reply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can just go with the after code 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I rewrote to after code.
Please check the code again.