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 3 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 @@ -251,17 +251,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 @@ -277,13 +279,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 (strpos($plugin['demo'], 'mp4') === false): ?>
Copy link
Member

Choose a reason for hiding this comment

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

что находится в поле demo? ссылка? а почему называется demo? но это меньшая проблема.

мало же проверять просто вхождение mp4. надо проверять наличие .mp4 в конце строки

<img class="lazy-load__media js_lazy-media" src="<?= $plugin['demo'] ?>" alt="<?= $plugin['name'] ?>">
<? else: ?>
<video class="lazy-load__media js_lazy-media" autoplay loop muted playsinline>
<source src="<?= $plugin['demo'] ?>" type="video/mp4">
Copy link
Member

Choose a reason for hiding this comment

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

зачем ты сразу прописываешь src у элемента? идея в том, чтобы не задавать этот параметр сразу. тот же путь к источнику прописывается, например, в data-src, а потом с помощью js подставляется в параметр src. тем самым получается, что эти элементы не тормозят загрузку страницы

</video>
<? endif; ?>
</div>
</div>
<a href="<?= $plugin['url'] ?>" target="_blank">
<h3 class="editor-plugin__title">
Expand Down
13 changes: 13 additions & 0 deletions www/public/app/css/components/lazy-load.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.lazy-load {
background: #f7fafb;
border-radius: 3px;

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

&--loaded > &__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 @@ -136,5 +136,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;

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

/**
* Classes used in module
*/
static get classes() {
Copy link
Member

Choose a reason for hiding this comment

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

мы везде этот геттер вроде называем CSS()


return {
wrapper: 'js_lazy-load',
loaded: 'lazy-load--loaded',
item: 'js_lazy-media'
};

}

/**
* 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.classes.item}`);

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();

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

this.addLoadedClass(element);

};

}

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

const tempVideo = document.createElement('video');
const tempVideoSource = document.createElement('source');

tempVideoSource.src = element.querySelector('source').src;
tempVideo.appendChild(tempVideoSource);

tempVideo.onloadeddata = () => {

this.addLoadedClass(element);

};

}

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

element.closest(`.${LazyLoad.classes.wrapper}`).classList.add(LazyLoad.classes.loaded);
Copy link
Member

Choose a reason for hiding this comment

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

подключи линтер, плиз


}

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

Large diffs are not rendered by default.

6 changes: 3 additions & 3 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.