forked from jordanjay29/flarum-ext-summaries
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Excerpt component, export it
- Loading branch information
Showing
4 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Excerpt from './Excerpt'; | ||
|
||
export const components = { | ||
Excerpt, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters