Skip to content

Commit

Permalink
fix: scrollToView not working properly if images exist
Browse files Browse the repository at this point in the history
Closes: #351
  • Loading branch information
Kang Cheng committed Dec 23, 2018
1 parent 6ac7bac commit 3d0bfd6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {isMobile} from '../util/env'
import * as dom from '../util/dom'
import image from '../util/image'
import Tweezer from 'tweezer.js'

const nav = {}
Expand Down Expand Up @@ -123,11 +124,34 @@ export function scrollActiveSidebar(router) {
})
}

export let pausedScrollToView

export function proceedScrollToView() {
if (pausedScrollToView) {
const copy = pausedScrollToView.slice()
pausedScrollToView = undefined
scrollIntoView(...copy)
}

image.unsubscribe(proceedScrollToView)
}

export function scrollIntoView(path, id) {
if (!id) {
return
}

if (pausedScrollToView) {
pausedScrollToView = [path, id]
return
}

if (!image.isAllImagesComplete()) {
pausedScrollToView = [path, id]
image.subscribe(proceedScrollToView)
return
}

const section = dom.find('#' + id)
section && scrollTo(section)

Expand Down
4 changes: 3 additions & 1 deletion src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ export class Compiler {
url = getPath(contentBase, getParentPath(router.getCurrentPath()), href)
}

return `<img src="${url}"data-origin="${href}" alt="${text}"${attrs}>`
window.Docsify.util.image.increaseLoadingImageCount()

return `<img src="${url}" data-origin="${href}" alt="${text}" onload="javascript:Docsify.util.image.onLoad(this)" onerror="javascript:Docsify.util.image.onError(this)"${attrs}>`
}

renderer.origin = origin
Expand Down
62 changes: 62 additions & 0 deletions src/core/util/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export default (function () {
let loadingImageCount = 0
const subscribers = []

function subscribe(cb) {
subscribers.push(cb)
}

function unsubscribe(cb) {
const index = subscribers.indexOf(cb)

if (index !== -1) {
subscribers.splice(index, 1)
}
}

function notifyAllImagesComplete() {
for (let i = 0; i < subscribers.length; i++) {
subscribers[i]()
}
}

function increaseLoadingImageCount() {
loadingImageCount += 1
}

function decreaseLoadingImageCount() {
loadingImageCount -= 1

if (loadingImageCount === 0) {
notifyAllImagesComplete()
}
}

function cleanElement(ele) {
ele.removeAttribute('onload')
ele.removeAttribute('onerror')
}

function onLoad(ele) {
cleanElement(ele)
decreaseLoadingImageCount()
}

function onError(ele) {
cleanElement(ele)
decreaseLoadingImageCount()
}

function isAllImagesComplete() {
return loadingImageCount === 0
}

return {
subscribe,
unsubscribe,
increaseLoadingImageCount,
onLoad,
onError,
isAllImagesComplete
}
})()
1 change: 1 addition & 0 deletions src/core/util/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './core'
export * from './env'
export * from '../router/util'
export {default as image} from './image'

0 comments on commit 3d0bfd6

Please sign in to comment.