1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00

Extract Scroll Storage controller

This commit is contained in:
Yassine Guedidi 2025-03-02 13:43:18 +01:00
parent 019d252446
commit d515e11fe4
4 changed files with 22 additions and 50 deletions

View file

@ -0,0 +1,19 @@
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
static values = { entryId: Number };
connect() {
window.scrollTo({
top: window.innerHeight * localStorage[`wallabag.article.${this.entryIdValue}.percent`],
behavior: 'smooth',
});
}
saveScroll() {
const scrollPercent = window.scrollY / window.innerHeight;
const scrollPercentRounded = Math.round(scrollPercent * 100) / 100;
localStorage[`wallabag.article.${this.entryIdValue}.percent`] = scrollPercentRounded;
}
}

View file

@ -23,7 +23,7 @@ import '@fontsource/oswald';
/* Tools */
import {
savePercent, retrievePercent, initPreviewText,
initPreviewText,
} from './js/tools';
/* Import shortcuts */
@ -34,7 +34,7 @@ import './js/shortcuts/entry';
import './scss/index.scss';
/* ==========================================================================
Annotations & Remember position
Annotations
========================================================================== */
$(document).ready(() => {
@ -70,20 +70,6 @@ $(document).ready(() => {
app.start().then(() => {
app.annotations.load({ entry: x.entryId });
});
$(window).scroll(() => {
const scrollTop = $(window).scrollTop();
const docHeight = $(document).height();
const scrollPercent = (scrollTop) / (docHeight);
const scrollPercentRounded = Math.round(scrollPercent * 100) / 100;
savePercent(x.entryId, scrollPercentRounded);
});
retrievePercent(x.entryId);
$(window).resize(() => {
retrievePercent(x.entryId, true);
});
}
document.querySelectorAll('[data-handler=tag-rename]').forEach((item) => {

View file

@ -1,36 +1,5 @@
import $ from 'jquery';
function supportsLocalStorage() {
try {
return 'localStorage' in window && window.localStorage !== null;
} catch (e) {
return false;
}
}
function savePercent(id, percent) {
if (!supportsLocalStorage()) { return false; }
localStorage[`wallabag.article.${id}.percent`] = percent;
return true;
}
function retrievePercent(id, resized) {
if (!supportsLocalStorage()) { return false; }
const bheight = $(document).height();
const percent = localStorage[`wallabag.article.${id}.percent`];
const scroll = bheight * percent;
if (!resized) {
window.scrollTo({
top: scroll,
behavior: 'smooth',
});
}
return true;
}
function initPreviewText() {
// no display if preview_text not available
if ($('div').is('#preview-article')) {
@ -48,7 +17,5 @@ function initPreviewText() {
}
export {
savePercent,
retrievePercent,
initPreviewText,
};