From cf2f7d3a2fbb23f8acee981bfbb9b90e44fb289a Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 27 Jul 2014 11:29:10 -0700 Subject: [PATCH] fix resize-compensating scrolling --- src/Images/ImageExpand.coffee | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Images/ImageExpand.coffee b/src/Images/ImageExpand.coffee index 7c16d50c0..f9e9ca007 100755 --- a/src/Images/ImageExpand.coffee +++ b/src/Images/ImageExpand.coffee @@ -114,11 +114,20 @@ ImageExpand = contract: (post) -> {file} = post + + {bottom} = post.nodes.root.getBoundingClientRect() + oldHeight = d.body.clientHeight + $.rmClass post.nodes.root, 'expanded-image' $.rmClass file.thumb, 'expanding' $.rm file.videoControls if file.videoControls for x in ['isExpanding', 'isExpanded', 'videoControls', 'wasPlaying', 'scrollIntoView'] delete file[x] + + # Scroll to keep our place in the thread when images are contracted above us. + if doc.contains(post.nodes.root) and bottom <= 0 + window.scrollBy 0, d.body.clientHeight - oldHeight + if el = file.fullImage $.off el, 'error', ImageExpand.error if file.isVideo @@ -161,32 +170,30 @@ ImageExpand = ImageExpand.setupVideoCB post $.asap (-> if isVideo then el.readyState >= el.HAVE_CURRENT_DATA else el.naturalHeight), -> - if post.nodes.root.parentNode - {bottom} = post.nodes.root.getBoundingClientRect() - $.queueTask -> ImageExpand.completeExpand post, bottom - else - # Image might start/finish loading before the post is inserted; don't scroll. - ImageExpand.completeExpand post + ImageExpand.completeExpand post - completeExpand: (post, oldBottom) -> + completeExpand: (post) -> {file} = post return unless file.isExpanding # contracted before the image loaded + {bottom} = post.nodes.root.getBoundingClientRect() + oldHeight = d.body.clientHeight + $.addClass post.nodes.root, 'expanded-image' $.rmClass file.thumb, 'expanding' file.isExpanded = true delete file.isExpanding # Scroll to keep our place in the thread when images are expanded above us. - if oldBottom? and oldBottom <= 0 - window.scrollBy 0, post.nodes.root.getBoundingClientRect().bottom - oldBottom + if doc.contains(post.nodes.root) and bottom <= 0 + window.scrollBy 0, d.body.clientHeight - oldHeight # Scroll to display full image. if file.scrollIntoView delete file.scrollIntoView imageBottom = Header.getBottomOf file.fullImage - {height} = file.fullImage.getBoundingClientRect() - if imageBottom + height >= 0 and imageBottom < 0 + imageHeight = file.fullImage.getBoundingClientRect().height + if imageBottom + imageHeight >= 0 and imageBottom < 0 window.scrollBy 0, Math.min(-imageBottom, Header.getTopOf file.fullImage) if file.isVideo