-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] lazy load image attachments (#10608)
[NEW] Lazy load image attachments
- Loading branch information
Showing
18 changed files
with
135 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,3 +190,4 @@ rocketchat:version-check | |
|
||
rocketchat:search | ||
chatpal:search | ||
rocketchat:lazy-load |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import _ from 'underscore'; | ||
import './lazyloadImage'; | ||
export const fixCordova = function(url) { | ||
if (url && url.indexOf('data:image') === 0) { | ||
return url; | ||
} | ||
if (Meteor.isCordova && (url && url[0] === '/')) { | ||
url = Meteor.absoluteUrl().replace(/\/$/, '') + url; | ||
const query = `rc_uid=${ Meteor.userId() }&rc_token=${ Meteor._localStorage.getItem( | ||
'Meteor.loginToken' | ||
) }`; | ||
if (url.indexOf('?') === -1) { | ||
url = `${ url }?${ query }`; | ||
} else { | ||
url = `${ url }&${ query }`; | ||
} | ||
} | ||
if (Meteor.settings['public'].sandstorm || url.match(/^(https?:)?\/\//i)) { | ||
return url; | ||
} else if (navigator.userAgent.indexOf('Electron') > -1) { | ||
return __meteor_runtime_config__.ROOT_URL_PATH_PREFIX + url; | ||
} else { | ||
return Meteor.absoluteUrl().replace(/\/$/, '') + url; | ||
} | ||
}; | ||
|
||
const loadImage = el => { | ||
const img = new Image(); | ||
const src = el.getAttribute('data-src'); | ||
el.className = el.className.replace('lazy-img', ''); | ||
img.onload = function() { | ||
el.src = src; | ||
el.removeAttribute('data-src'); | ||
}; | ||
img.src = fixCordova(src); | ||
}; | ||
|
||
const isVisible = el => { | ||
requestAnimationFrame(() => { | ||
const rect = el.getBoundingClientRect(); | ||
if (rect.top >= -100 && rect.left >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight)) { | ||
return loadImage(el); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
window.addEventListener('resize', window.lazyloadtick); | ||
|
||
export const lazyloadtick = _.debounce(() => { | ||
[...document.querySelectorAll('.lazy-img[data-src]')] | ||
|
||
.forEach(isVisible); | ||
}, 500); | ||
|
||
window.lazyloadtick = lazyloadtick; | ||
|
||
export const addImage = el => isVisible(el); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<template name="lazyloadImage"> | ||
<img data-src="{{src}}" src="{{lazy}}" height="{{height}}" class="{{class}}" | ||
data-title="{{title}}" data-description="{{description}}"> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import './lazyloadImage.html'; | ||
import { addImage, fixCordova } from './'; | ||
|
||
Template.lazyloadImage.helpers({ | ||
lazy() { | ||
const { preview, src, placeholder } = this; | ||
|
||
if (!preview && !placeholder) { | ||
return fixCordova(src); | ||
} | ||
return `data:image/png;base64,${ preview || 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8+/u3PQAJJAM0dIyWdgAAAABJRU5ErkJggg==' }`; | ||
} | ||
}); | ||
|
||
Template.lazyloadImage.onCreated(function() { | ||
const element = Template.instance().firstNode; | ||
if (!element) { | ||
return; | ||
} | ||
addImage(element); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Package.describe({ | ||
name: 'rocketchat:lazy-load', | ||
version: '0.0.1', | ||
summary: 'Lazy load image', | ||
git: '' | ||
}); | ||
|
||
Package.onUse(function(api) { | ||
api.use([ | ||
'ecmascript', | ||
'templating', | ||
'rocketchat:lib' | ||
]); | ||
|
||
api.mainModule('client/index.js', 'client'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3167,8 +3167,6 @@ | |
max-height: 200px; | ||
|
||
cursor: pointer; | ||
|
||
opacity: 0; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
<template name="avatar"> | ||
<div class="avatar"> | ||
<div class="avatar-image" style="{{imageUrl}}"></div> | ||
{{# if lazy}} | ||
{{> lazyloadImage src=src class="avatar-image lazy-img" placeholder=true}} | ||
{{else}} | ||
<img src="{{src}}" class="avatar-image"/> | ||
{{/if}} | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters