Skip to content

Commit 72fd67b

Browse files
committed
feat: discussion excerpts support
1 parent 64f1d74 commit 72fd67b

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

extend.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Flarum\Extend;
1515
use Darkle\Fancybox\WrapImagesInGallery;
1616
use Darkle\Fancybox\DefineGalleryTemplate;
17+
use Darkle\Fancybox\AddExcerptToDiscussion;
1718

1819
return [
1920
(new Extend\Frontend('forum'))
@@ -22,5 +23,11 @@
2223

2324
(new Extend\Formatter)
2425
->configure(DefineGalleryTemplate::class)
26+
->configure(AddExcerptToDiscussion::class)
2527
->render(WrapImagesInGallery::class),
28+
29+
(new Extend\ApiSerializer(\Flarum\Api\Serializer\DiscussionSerializer::class))
30+
->attribute('excerpt', function ($serializer, $discussion) {
31+
return $discussion->firstPost ? $discussion->firstPost->formatContent() : null;
32+
}),
2633
];

js/dist/forum.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/dist/forum.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/package-lock.json

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

js/src/forum/index.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import app from 'flarum/forum/app';
22
import {extend} from 'flarum/common/extend';
33
import CommentPost from 'flarum/forum/components/CommentPost';
4+
import DiscussionListItem from 'flarum/forum/components/DiscussionListItem';
5+
import Discussion from 'flarum/common/models/Discussion';
6+
import Model from 'flarum/common/Model';
47

58
import {Carousel, Fancybox} from '@fancyapps/ui';
69

@@ -11,8 +14,7 @@ app.initializers.add('darkle/fancybox', () => {
1114
const postBody = this.$('.Post-body');
1215
if (postBody.length == 0) return;
1316

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
17+
postBody.children('.fancybox-gallery:not(.fancybox-ready)')
1618
.addClass('fancybox-ready')
1719
.each((_, gallery) => {
1820
new Carousel(gallery, {
@@ -71,4 +73,78 @@ app.initializers.add('darkle/fancybox', () => {
7173
});
7274
});
7375
});
76+
77+
Discussion.prototype.excerpt = Model.attribute<string>('excerpt');
78+
79+
extend(DiscussionListItem.prototype, 'infoItems', function(items) {
80+
const excerpt = this.attrs.discussion.excerpt();
81+
if (excerpt) {
82+
items.remove('excerpt');
83+
items.add('excerpt', m.trust(excerpt), -100);
84+
}
85+
});
86+
87+
extend(DiscussionListItem.prototype, 'oncreate', function() {
88+
const excerptBody = this.$('.item-excerpt');
89+
if (excerptBody.length == 0) return;
90+
91+
excerptBody.children('.fancybox-gallery:not(.fancybox-ready)')
92+
.addClass('fancybox-ready')
93+
.each((_, gallery) => {
94+
new Carousel(gallery, {
95+
Dots: false,
96+
infinite: false,
97+
dragFree: false,
98+
preload: 0,
99+
});
100+
});
101+
102+
excerptBody.find('a[data-fancybox]:not(.fancybox-ready)')
103+
.addClass('fancybox-ready')
104+
.each((_, el) => {
105+
const link = $(el);
106+
let isDragging = false;
107+
let startX: number, startY: number;
108+
109+
link
110+
.on('mousedown', (e) => {
111+
isDragging = false;
112+
startX = e.clientX;
113+
startY = e.clientY;
114+
})
115+
.on('mousemove', (e) => {
116+
if (Math.abs(e.clientX - startX) > 5 || Math.abs(e.clientY - startY) > 5) {
117+
isDragging = true;
118+
}
119+
})
120+
.on('click', (e) => {
121+
e.preventDefault();
122+
if (isDragging) return;
123+
const groupName = link.attr('data-fancybox');
124+
const carouselEl = link.closest('.fancybox-gallery');
125+
const group = (carouselEl.length > 0 ? carouselEl : excerptBody).find(`a[data-fancybox="${groupName}"]`).toArray();
126+
const startIndex = group.indexOf(el);
127+
128+
Fancybox.fromNodes(group, {
129+
Carousel: {
130+
infinite: false,
131+
preload: 0,
132+
},
133+
Toolbar: {
134+
display: {
135+
left: ['infobar'],
136+
middle: ['rotateCCW', 'rotateCW', 'flipX', 'flipY'],
137+
right: ['slideshow', 'fullscreen', 'close'],
138+
},
139+
},
140+
Images: {
141+
initialSize: 'fit' as 'fit',
142+
},
143+
dragToClose: true,
144+
Hash: false,
145+
startIndex,
146+
});
147+
});
148+
});
149+
});
74150
});

src/AddExcerptToDiscussion.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Darkle\Fancybox;
4+
5+
use s9e\TextFormatter\Configurator;
6+
7+
class AddExcerptToDiscussion
8+
{
9+
public function __invoke(Configurator $config)
10+
{
11+
if ($config->tags->exists('IMG')) {
12+
$tag = $config->tags->get('IMG');
13+
$originalTemplate = $tag->template;
14+
$tag->template = <<<XML
15+
<xsl:choose>
16+
<xsl:when test="ancestor::DISCUSSION-EXCERPT">
17+
<a data-fancybox="excerpt-gallery" href="{@src}">
18+
<img data-lazy-src="{@src}" alt="{@alt}" class="excerpt-image"/>
19+
</a>
20+
</xsl:when>
21+
<xsl:otherwise>
22+
{$originalTemplate}
23+
</xsl:otherwise>
24+
</xsl:choose>
25+
XML;
26+
}
27+
28+
if ($config->tags->exists('UPL-IMAGE-PREVIEW')) {
29+
$tag = $config->tags->get('UPL-IMAGE-PREVIEW');
30+
$originalTemplate = $tag->template;
31+
$tag->template = <<<XML
32+
<xsl:choose>
33+
<xsl:when test="ancestor::DISCUSSION-EXCERPT">
34+
<a data-fancybox="excerpt-gallery" href="{@url}">
35+
<img data-lazy-src="{@url}" alt="" class="excerpt-image"/>
36+
</a>
37+
</xsl:when>
38+
<xsl:otherwise>
39+
{$originalTemplate}
40+
</xsl:otherwise>
41+
</xsl:choose>
42+
XML;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)