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

Add lazyLoad module #540

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions www/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"XMLHttpRequest": true,
"ActiveXObject": true,
"FileReader": true,
"Element": true

"Element": true,
"Image" : true
}
}
36 changes: 20 additions & 16 deletions www/application/views/templates/landings/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,19 @@
<div class="editor-landing__plugins-description">
Plugins can represent any Blocks: Quotes, Galleries, Polls, Embeds, Tables — anything you need. Also they can implement Inline Tools such as Marker, Term, Comments etc.
</div>
<div class="editor-landing__plugins-filter" data-module="pluginsFilter">
<div class="editor-landing__plugins-filter" data-module="pluginsFilter lazyLoad">
khaydarov marked this conversation as resolved.
Show resolved Hide resolved
<textarea name="module-settings" hidden>
{
"inlineFilterButtonClass" : ".js-inline-tools-filter",
"blockFilterButtonClass" : ".js-block-tools-filter",
"allToolsFilterButtonClass" : ".js-all-tools-filter",
"blockToolsClass" : ".js-block-tool",
"inlineToolsClass" : ".js-inline-tool"
}
[
{
"inlineFilterButtonClass" : ".js-inline-tools-filter",
"blockFilterButtonClass" : ".js-block-tools-filter",
"allToolsFilterButtonClass" : ".js-all-tools-filter",
"blockToolsClass" : ".js-block-tool",
"inlineToolsClass" : ".js-inline-tool"
},
{}
]
</textarea>

<span class="editor-landing__plugins-filter-button js-block-tools-filter">
<? include(DOCROOT . '/public/app/landings/editor/svg/plus-icon.svg'); ?>
Blocks
Expand All @@ -292,13 +294,15 @@
<? foreach ( $plugins as $plugin ): ?>
<div class="editor-plugin clearfix <?= $plugin['type'] === 'Block' ? 'js-block-tool' : 'js-inline-tool' ?>">
<div class="editor-plugin__demo">
<? if (strpos($plugin['demo'], 'mp4') === false): ?>
<img src="<?= $plugin['demo'] ?>" alt="<?= $plugin['name'] ?>">
<? else: ?>
<video autoplay loop muted playsinline>
<source src="<?= $plugin['demo'] ?>" type="video/mp4">
</video>
<? endif; ?>
<div class="lazy-load js_lazy-load">
<? if (substr($plugin['demo'], -strlen('.mp4')) === '.mp4'): ?>
<video data-lazy-src="<?= $plugin['demo'] ?>" class="lazy-load__media" autoplay loop muted playsinline>
<source type="video/mp4">
</video>
<? else: ?>
<img class="lazy-load__media" data-lazy-src="<?= $plugin['demo'] ?>" alt="<?= $plugin['name'] ?>">
<? endif; ?>
</div>
</div>
<a href="<?= $plugin['url'] ?>" target="_blank">
<h3 class="editor-plugin__title">
Expand Down
23 changes: 23 additions & 0 deletions www/public/app/css/components/lazy-load.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.lazy-load {
background: #f7fafb;
border-radius: 3px;
width: 150px;

@media (--mobile) {
width: 75px;
}

&__media {
opacity: 0;
transition: opacity 0.6s ease;
}

&--loaded {
width: auto;
background: transparent;

> ^&__media {
opacity: 1;
}
}
}
1 change: 1 addition & 0 deletions www/public/app/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@import './components/video-overlay.css';
@import './components/follow-telegram.css';
@import './components/join.css';
@import './components/lazy-load.css';

/**
* Pages
Expand Down
3 changes: 3 additions & 0 deletions www/public/app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ codex.editorLanding = new EditorLanding();
import ArticleCreate from './modules/articleCreate';
codex.articleCreate = new ArticleCreate();

import LazyLoad from './modules/lazyLoad';
codex.lazyLoad = new LazyLoad();

module.exports = codex;

108 changes: 108 additions & 0 deletions www/public/app/js/modules/lazyLoad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Module for lazy-loading images & videos
*/
export default class LazyLoad {

/**
* Classes used in module
*/
static get CSS() {

return {
wrapper: 'js_lazy-load',
loaded: 'lazy-load--loaded',
itemAttribute: 'data-lazy-src'
};

}

/**
* Initialize module
*/
init() {

this.loadMedia();

}

/**
* Find media targets on page and apply to them various lazy-loading technique depending on type: image or video
*/
loadMedia() {

const items = document.querySelectorAll(`[${LazyLoad.CSS.itemAttribute}]`);

items.forEach((item) => {

switch (item.tagName) {

case 'IMG':
this.loadImage(item);
break;
case 'VIDEO':
this.loadVideo(item);
break;

}

});

}

/**
* Images lazy-loading technique
* @param {HTMLElement} element - target image
*/
loadImage(element) {

/**
* Create temporary image with real image src.
* When temporary image is loaded, reveal real image
*/
const tempImage = new Image();
const imageSrc = element.getAttribute(LazyLoad.CSS.itemAttribute);

tempImage.src = imageSrc;
tempImage.onload = () => {

element.src = imageSrc;
this.addLoadedClass(element);

};

}

/**
* Videos lazy-loading technique
* @param {HTMLElement} element - target video
*/
loadVideo(element) {

const tempVideo = document.createElement('video');
const tempVideoSource = document.createElement('source');
const videoSrc = element.getAttribute(LazyLoad.CSS.itemAttribute);

tempVideoSource.src = videoSrc;
tempVideo.appendChild(tempVideoSource);

tempVideo.onloadeddata = () => {

element.querySelector('source').src = videoSrc;
this.addLoadedClass(element);
element.load();

};

}

/**
* Add loaded class when element is ready
* @param {HTMLElement} element - media target
*/
addLoadedClass(element) {

element.closest(`.${LazyLoad.CSS.wrapper}`).classList.add(LazyLoad.CSS.loaded);

}

}
2 changes: 1 addition & 1 deletion www/public/build/codex.bundle.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions www/public/build/codex.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion www/public/build/editor.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion www/public/build/hawk.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion www/public/build/reactions.bundle.js

Large diffs are not rendered by default.