Skip to content

Commit

Permalink
feat: use Excerpt component, export it
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 22, 2024
1 parent 784cb6d commit 401c4c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
6 changes: 2 additions & 4 deletions js/src/forum/addSummaryExcerpt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ItemList from 'flarum/common/utils/ItemList';
import Tag from 'flarum/tags/models/Tag';
import Model from 'flarum/common/Model';
import type Mithril from 'mithril';
import Excerpt from './components/Excerpt';

export default function addSummaryExcerpt() {
if (app.initializers.has('flarum-tags')) {
Expand Down Expand Up @@ -47,11 +48,8 @@ export default function addSummaryExcerpt() {
return;
}

if (!excerptPost?.contentHtml?.()) return;
const content = richExcerpt ? m.trust(truncate(excerptPost.contentHtml(), excerptLength)) : truncate(excerptPost.contentPlain(), excerptLength);

if (excerptPost) {
const excerpt = <div>{content}</div>;
const excerpt = <Excerpt post={excerptPost} length={excerptLength} richExcerpt={richExcerpt} />;

items.add(onMobile ? 'excerptM' : 'excerpt', excerpt, -100);
}
Expand Down
40 changes: 40 additions & 0 deletions js/src/forum/components/Excerpt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Component, { ComponentAttrs } from 'flarum/common/Component';
import Post from 'flarum/common/models/Post';
import { truncate } from 'flarum/common/utils/string';
import type Mithril from 'mithril';

export interface ExcerptAttrs extends ComponentAttrs {
post: Post;
length: number;
richExcerpt: boolean;
}

export default class Excerpt extends Component<ExcerptAttrs> {
post!: Post;
length!: number;
richExcerpt!: boolean;

oninit(vnode: Mithril.Vnode<ExcerptAttrs, this>) {
super.oninit(vnode);

this.post = this.attrs.post;
this.length = this.attrs.length;
this.richExcerpt = this.attrs.richExcerpt;
}

view() {
return <div>{m.trust(this.getContent())}</div>;
}

getContent(): string {
return this.richExcerpt ? truncate(this.contentRich() ?? '', this.length) : truncate(this.contentPlain() ?? '', this.length);
}

contentRich() {
return this.post.contentHtml();
}

contentPlain() {
return this.post.contentPlain();
}
}
5 changes: 5 additions & 0 deletions js/src/forum/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Excerpt from './Excerpt';

export const components = {
Excerpt,
};
3 changes: 2 additions & 1 deletion js/src/forum/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import app from 'flarum/forum/app';

import addSummaryExcerpt from './addSummaryExcerpt';
import addUserPreference from './addUserPreference';

export * from './components';

app.initializers.add('ianm-synopsis', () => {
addSummaryExcerpt();
addUserPreference();
Expand Down

0 comments on commit 401c4c8

Please sign in to comment.