Skip to content

Commit 28acf11

Browse files
committed
client: Replace fancybox & drop jQuery
The jQuery removal was long coming. Since the next version of Fancybox is unfree, let’s switch to something else.
1 parent b7afbd4 commit 28acf11

9 files changed

Lines changed: 46 additions & 67 deletions

File tree

assets/js/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import 'regenerator-runtime/runtime';
2-
import './jquery';
32
import selfoss from './selfoss-base';
43
import './selfoss-db-online';
54
import './selfoss-db-offline';
65
import './selfoss-db';
7-
import '@fancyapps/fancybox';
86

97
selfoss.init();
108

assets/js/jquery.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

assets/js/selfoss-base.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var selfoss = {
3131
*/
3232
entriesPage: null,
3333

34+
/**
35+
* Whether lightbox is open.
36+
*/
37+
lightboxActive: new ValueListenable(false),
38+
3439
windowLoaded: new Promise((resolve) => {
3540
window.addEventListener('load', () => resolve());
3641
}),
@@ -358,23 +363,6 @@ var selfoss = {
358363
},
359364

360365

361-
/**
362-
* Setup fancyBox image viewer
363-
* @param content element
364-
* @param int
365-
*/
366-
setupFancyBox: function(content, id) {
367-
// Close existing fancyBoxes
368-
$.fancybox.close();
369-
let images = content.querySelectorAll('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"], a[href$=".jpg:large"], a[href$=".jpeg:large"], a[href$=".png:large"], a[href$=".gif:large"]');
370-
images.forEach((el) => {
371-
el.setAttribute('data-fancybox', 'gallery-' + id);
372-
$(el).off('click');
373-
});
374-
images.forEach((el) => el.setAttribute('data-type', 'image'));
375-
},
376-
377-
378366
handleAjaxError: function(error, tryOffline = true) {
379367
if (!(error instanceof HttpError || error instanceof TimeoutError)) {
380368
return Promise.reject(error);

assets/js/shortcuts.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,13 @@ function entrynav(direction) {
114114
nextprev(direction, open);
115115
}
116116

117-
/**
118-
* Check whether keyboard shortcuts should be active
119-
*/
120-
function lightboxActive() {
121-
var fancyboxInactive = !$.fancybox.getInstance();
122-
123-
return fancyboxInactive;
124-
}
125-
126117
/**
127118
* Decorates an event handler so that it only runs
128119
* when not interacting with an input field or lightbox.
129120
*/
130121
function ignoreWhenInteracting(handler) {
131122
return (event) => {
132-
if (!lightboxActive()) {
123+
if (selfoss.lightboxActive.value) {
133124
return;
134125
}
135126

assets/js/templates/App.jsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
33
import nullable from 'prop-types-nullable';
44
import {
@@ -200,11 +200,6 @@ function PureApp({
200200

201201
const _ = React.useContext(LocalizationContext);
202202

203-
useEffect(() => {
204-
// Initialize FancyBox globally
205-
$.fancybox.defaults.hash = false;
206-
}, []);
207-
208203
return (
209204
<React.StrictMode>
210205
<Message message={globalMessage} />

assets/js/templates/Item.jsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ import * as icons from '../icons';
1212
import { ConfigurationContext } from '../helpers/configuration';
1313
import { LocalizationContext } from '../helpers/i18n';
1414
import { useSharers } from '../sharers';
15+
import SimpleLightbox from 'simplelightbox/dist/simple-lightbox.esm';
16+
17+
function setupLightbox(element) {
18+
let images = element.querySelectorAll('a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"], a[href$=".jpg:large"], a[href$=".jpeg:large"], a[href$=".png:large"], a[href$=".gif:large"]');
19+
20+
if (images.length === 0) {
21+
return;
22+
}
23+
const gallery = new SimpleLightbox(
24+
images,
25+
{
26+
loop: false,
27+
// react-router already handles this.
28+
history: false,
29+
},
30+
);
31+
32+
gallery.on('show.simplelightbox', function() {
33+
selfoss.lightboxActive.update(true);
34+
});
35+
36+
gallery.on('close.simplelightbox', function() {
37+
selfoss.lightboxActive.update(false);
38+
});
39+
}
1540

1641
function stopPropagation(event) {
1742
event.stopPropagation();
@@ -276,7 +301,7 @@ export default function Item({ currentTime, item, selected, expanded, setNavExpa
276301
} else {
277302
if (firstExpansion) {
278303
// setup fancyBox image viewer
279-
selfoss.setupFancyBox(contentBlock.current, item.id);
304+
setupLightbox(contentBlock.current);
280305
}
281306

282307
// scroll to article header

assets/package-lock.json

Lines changed: 11 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"dependencies": {
3-
"@fancyapps/fancybox": "^3.1.20",
43
"@floating-ui/react-dom": "^1.0.0",
54
"@floating-ui/react-dom-interactions": "^0.9.3",
65
"@fortawesome/free-brands-svg-icons": "^6.2.0",
@@ -17,7 +16,6 @@
1716
"focus-trap": "^7.0.0",
1817
"form-urlencoded": "^6.0.0",
1918
"html-escaper": "^3.0.0",
20-
"jquery": "^3.5.1",
2119
"lodash.pick": "^4.4.0",
2220
"prop-types": "^15.7.2",
2321
"prop-types-nullable": "^1.0.1",
@@ -28,6 +26,7 @@
2826
"react-router-dom": "^5.2.0",
2927
"reset-css": "^5.0.1",
3028
"rooks": "^7.1.1",
29+
"simplelightbox": "^2.10.3",
3130
"tinykeys": "^1.1.1",
3231
"unreset-css": "^1.0.1",
3332
"use-state-with-deps": "^1.1.1"

assets/styles/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@use 'sass:color';
22

3-
@import 'npm:@fancyapps/fancybox/dist/jquery.fancybox.css';
3+
@import 'npm:simplelightbox/src/simple-lightbox';
44
@import 'npm:reset-css/sass/reset';
55
@import 'mixins/visually-hidden';
66

0 commit comments

Comments
 (0)