Skip to content

Commit 2d53777

Browse files
authored
Merge pull request #14 from YUCLing/some-refactors
Frontend refactors
2 parents 8bd71aa + 624b0ae commit 2d53777

File tree

8 files changed

+1973
-4244
lines changed

8 files changed

+1973
-4244
lines changed

js/forum.js renamed to js/forum.ts

File renamed without changes.

js/package-lock.json

+1,867-4,113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
"webpack-cli": "^5.1.4"
1212
},
1313
"devDependencies": {
14-
"css-loader": "^7.1.2",
14+
"copy-webpack-plugin": "^12.0.2",
15+
"flarum-tsconfig": "^1.0.3",
1516
"prettier": "^3.3.3",
16-
"style-loader": "^4.0.0",
1717
"webpack-merge": "^6.0.1"
1818
},
1919
"scripts": {
20-
"dev": "npm run build:css && webpack --mode development --watch",
21-
"build": "npm run build:css && webpack --mode production",
20+
"dev": "webpack --mode development --watch",
21+
"build": "webpack --mode production",
2222
"format": "prettier --write src",
23-
"format-check": "prettier --check src",
24-
"build:css": "cp ./node_modules/@fancyapps/ui/dist/fancybox/fancybox.css ../less/fancybox.css && cp ./node_modules/@fancyapps/ui/dist/carousel/carousel.css ../less/carousel.css"
23+
"format-check": "prettier --check src"
2524
}
2625
}

js/src/forum/index.js

-115
This file was deleted.

js/src/forum/index.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import app from 'flarum/forum/app';
2+
import {extend} from 'flarum/common/extend';
3+
import CommentPost from 'flarum/forum/components/CommentPost';
4+
5+
import {Carousel, Fancybox} from '@fancyapps/ui';
6+
7+
app.initializers.add('darkle/fancybox', () => {
8+
extend(CommentPost.prototype, 'refreshContent', function () {
9+
if (this.isEditing()) return;
10+
11+
const postBody = this.$('.Post-body');
12+
if (postBody.length == 0) return;
13+
14+
// Initialize Carousel for each gallery
15+
postBody.children('.fancybox-gallery:not(.fancybox-ready)') // The galleries should be only at the first level of Post-body
16+
.addClass('fancybox-ready')
17+
.each((_, gallery) => {
18+
new Carousel(gallery, {
19+
Dots: false,
20+
infinite: false,
21+
dragFree: false,
22+
preload: 0,
23+
});
24+
});
25+
26+
postBody.find('a[data-fancybox]:not(.fancybox-ready)')
27+
.addClass('fancybox-ready')
28+
.each((_, el) => {
29+
const link = $(el);
30+
let isDragging = false;
31+
let startX: number, startY: number;
32+
33+
link
34+
.on('mousedown', (e) => {
35+
isDragging = false;
36+
startX = e.clientX;
37+
startY = e.clientY;
38+
})
39+
.on('mousemove', (e) => {
40+
if (Math.abs(e.clientX - startX) > 5 || Math.abs(e.clientY - startY) > 5) {
41+
isDragging = true;
42+
}
43+
})
44+
.on('click', (e) => {
45+
e.preventDefault();
46+
if (isDragging) return;
47+
const groupName = link.attr('data-fancybox');
48+
const carouselEl = link.closest('.fancybox-gallery');
49+
const group = (carouselEl.length > 0 ? carouselEl : postBody).find(`a[data-fancybox="${groupName}"]`).toArray();
50+
const startIndex = group.indexOf(el);
51+
52+
Fancybox.fromNodes(group, {
53+
Carousel: {
54+
infinite: false,
55+
preload: 0,
56+
},
57+
Toolbar: {
58+
display: {
59+
left: ['infobar'],
60+
middle: ['rotateCCW', 'rotateCW', 'flipX', 'flipY'],
61+
right: ['slideshow', 'fullscreen', 'close'],
62+
},
63+
},
64+
Images: {
65+
initialSize: 'fit' as 'fit',
66+
},
67+
dragToClose: true,
68+
Hash: false,
69+
startIndex,
70+
});
71+
});
72+
});
73+
});
74+
});

js/tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use Flarum's tsconfig as a starting point
3+
"extends": "flarum-tsconfig",
4+
// This will match all .ts, .tsx, .d.ts, .js, .jsx files in your `src` folder
5+
// and also tells your Typescript server to read core's global typings for
6+
// access to `dayjs` and `$` in the global namespace.
7+
"include": ["src/**/*", "../vendor/flarum/core/js/dist-typings/@types/**/*"],
8+
"compilerOptions": {
9+
// This will output typings to `dist-typings`
10+
"declarationDir": "./dist-typings",
11+
"paths": {
12+
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"]
13+
}
14+
}
15+
}

js/webpack.config.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const config = require('flarum-webpack-config');
22
const { merge } = require('webpack-merge');
33

4+
const CopyPlugin = require('copy-webpack-plugin');
5+
46
const customConfig = {
5-
module: {
6-
rules: [
7-
{
8-
test: /\.css$/i,
9-
use: ['style-loader', 'css-loader'],
10-
},
11-
],
12-
},
7+
plugins: [
8+
new CopyPlugin({
9+
patterns: [
10+
{ from: './node_modules/@fancyapps/ui/dist/fancybox/fancybox.css', to: '../../less' },
11+
{ from: './node_modules/@fancyapps/ui/dist/carousel/carousel.css', to: '../../less' }
12+
]
13+
})
14+
]
1315
};
1416

1517
module.exports = merge(config(), customConfig);

src/DefineGalleryTemplate.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __invoke(Configurator $config)
2121
<xsl:choose>
2222
<xsl:when test="parent::FANCYBOX-GALLERY-ITEM">
2323
<a data-fancybox="gallery" href="{@src}">
24-
<img src="{@src}" alt="{@alt}" loading="lazy"/>
24+
<img data-lazy-src="{@src}" alt="{@alt}" loading="lazy"/>
2525
</a>
2626
</xsl:when>
2727
<xsl:otherwise>
@@ -39,7 +39,7 @@ public function __invoke(Configurator $config)
3939
<xsl:choose>
4040
<xsl:when test="parent::FANCYBOX-GALLERY-ITEM">
4141
<a data-fancybox="gallery" href="{@url}">
42-
<img src="{@url}" alt="" loading="lazy"/>
42+
<img data-lazy-src="{@url}" alt="" loading="lazy"/>
4343
</a>
4444
</xsl:when>
4545
<xsl:otherwise>

0 commit comments

Comments
 (0)