Skip to content
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

feat: load all images on print #66

Merged
merged 3 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,61 @@ export function createImage (context, { providers, defaultProvider, presets, int
return image
}

function createObserver (options: object) {
const intersectOptions = {
function printObserver (onMatch) {
if (typeof window === 'undefined' || typeof window.matchMedia === 'undefined') {
return
}

const mediaQueryList = window.matchMedia('print')
mediaQueryList.addListener((query) => {
if (query.matches) {
onMatch()
}
})
}

function intersectionObserver (onMatch, options) {
const observer = (typeof IntersectionObserver !== 'undefined' ? new IntersectionObserver(onMatch, {
rootMargin: '50px',
...options
}
const observer = (typeof IntersectionObserver !== 'undefined' ? new IntersectionObserver(callback, intersectOptions) : {}) as IntersectionObserver
}) : {}) as IntersectionObserver

return observer
}

function createObserver (intersectionOptions: object) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for longer term it would be better to create observer directly in component instead of share runtime logic (createObserver(image))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move observer into component we are creating multiple query match and IntersectionOsberver. I'm not sure but it may reduce the performance

const callbackType = { intersect: 'onIntersect', print: 'onPrint' }
const elementCallbackMap = {}
function callback (entries, imgObserver) {
function intersectionCallback (entries, imgObserver) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const lazyImage = entry.target
const callback = elementCallbackMap[lazyImage.__unique]
if (typeof callback === 'function') {
callback()
callback(callbackType.intersect)
}
delete elementCallbackMap[lazyImage.__unique]
imgObserver.unobserve(lazyImage)
}
})
}

printObserver(() => {
Object.values(elementCallbackMap).forEach((callback: any) => callback(callbackType.print))
})

const intersectObserver = intersectionObserver(intersectionCallback, intersectionOptions)

return {
add (target, component, unique) {
// add unique id to recognize target
target.__unique = unique || target.id || target.__vue__._uid
elementCallbackMap[target.__unique] = component
observer.observe(target)
intersectObserver.observe(target)
},
remove (target) {
delete elementCallbackMap[target.__unique]
observer.unobserve(target)
intersectObserver.unobserve(target)
}
}
}
21 changes: 13 additions & 8 deletions src/runtime/nuxt-image-mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,13 @@ export default {

if (this.lazy) {
this.$img.$observer.remove(this.$el)
this.$img.$observer.add(this.$el, () => {
// OK, element is visible, Hoooray
this.loadOriginalImage()
})
this.$img.$observer.add(this.$el, this.onObserverEvent)
}
}
},
mounted () {
if (this.lazy) {
this.$img.$observer.add(this.$el, () => {
// OK, element is visible, Hoooray
this.loadOriginalImage()
})
this.$img.$observer.add(this.$el, this.onObserverEvent)
}
},
beforeDestroy () {
Expand Down Expand Up @@ -229,6 +223,17 @@ export default {
// hanlde onLoad event of original image element
onImageLoaded () {
this.lazyState = LazyState.LOADED
},
onObserverEvent (type) {
if (type === 'onIntersect') {
// OK, element is visible, Hoooray
this.loadOriginalImage()
return
}
if (type === 'onPrint') {
this.$img.$observer.remove(this.$el)
this.lazyState = LazyState.LOADED
}
}
}
}
3 changes: 2 additions & 1 deletion test/fixture/utils/componet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export function testComponent (Component, props) {
function $img () {
return src
}
const eventType = { intersect: 'onIntersect' }
$img.$observer = {
add (_, callback) {
observerAdded += 1
becomeVisible = callback
becomeVisible = () => callback(eventType.intersect)
},
remove () {
observerDestroyed += 1
Expand Down