Skip to content

Commit d4fe5f5

Browse files
[1.x] [extensibility] refactor(core, flags): improve & use extensibility of CommentPost & Post (#4047)
* refactor(core): improve extensibility of `CommentPost` * refactor(core): rename method to more appropriate name * refactor(core): further improve extensibility of `CommentPost` * refactor(core): improve extensibility of `Post` * refactor(flags): use new extensibility for flagged posts
1 parent 256c184 commit d4fe5f5

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

extensions/flags/js/src/forum/addFlagsToPosts.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,25 @@ export default function () {
7575
return items;
7676
};
7777

78-
extend(Post.prototype, 'content', function (vdom) {
78+
extend(Post.prototype, 'viewItems', function (items) {
7979
const post = this.attrs.post;
8080
const flags = post.flags();
8181

8282
if (!flags.length) return;
8383

8484
if (post.isHidden()) this.revealContent = true;
8585

86-
vdom.unshift(
86+
items.add(
87+
'flagged',
8788
<div className="Post-flagged">
8889
<div className="Post-flagged-flags">
8990
{flags.map((flag) => (
9091
<div className="Post-flagged-flag">{this.flagReason(flag)}</div>
9192
))}
9293
</div>
9394
<div className="Post-flagged-actions">{this.flagActionItems().toArray()}</div>
94-
</div>
95+
</div>,
96+
110
9597
);
9698
});
9799

framework/core/js/src/forum/components/CommentPost.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,35 @@ export default class CommentPost extends Post {
4747
}
4848

4949
content() {
50-
return super.content().concat([
50+
return super.content().concat(this.contentItems().toArray());
51+
}
52+
53+
contentItems() {
54+
const items = new ItemList();
55+
56+
items.add(
57+
'header',
5158
<header className="Post-header">
5259
<ul>{listItems(this.headerItems().toArray())}</ul>
5360
</header>,
54-
<div className="Post-body">
55-
{this.isEditing() ? <ComposerPostPreview className="Post-preview" composer={app.composer} /> : m.trust(this.attrs.post.contentHtml())}
56-
</div>,
57-
]);
61+
100
62+
);
63+
64+
items.add('body', <div className="Post-body">{this.bodyItems().toArray()}</div>, 90);
65+
66+
return items;
67+
}
68+
69+
bodyItems() {
70+
const items = new ItemList();
71+
72+
items.add(
73+
'content',
74+
this.isEditing() ? <ComposerPostPreview className="Post-preview" composer={app.composer} /> : m.trust(this.attrs.post.contentHtml()),
75+
100
76+
);
77+
78+
return items;
5879
}
5980

6081
refreshContent() {

framework/core/js/src/forum/components/Post.tsx

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Component, { ComponentAttrs } from '../../common/Component';
33
import SubtreeRetainer from '../../common/utils/SubtreeRetainer';
44
import Dropdown from '../../common/components/Dropdown';
55
import PostControls from '../utils/PostControls';
6-
import listItems from '../../common/helpers/listItems';
6+
import listItems, { ModdedChildrenWithItemName } from '../../common/helpers/listItems';
77
import ItemList from '../../common/utils/ItemList';
88
import type PostModel from '../../common/models/Post';
99
import LoadingIndicator from '../../common/components/LoadingIndicator';
@@ -55,34 +55,46 @@ export default abstract class Post<CustomAttrs extends IPostAttrs = IPostAttrs>
5555

5656
return (
5757
<article {...attrs}>
58-
<div>
59-
{this.loading ? <LoadingIndicator /> : this.content()}
60-
<aside className="Post-actions">
61-
<ul>
62-
{listItems(this.actionItems().toArray())}
63-
{!!controls.length && (
64-
<li>
65-
<Dropdown
66-
className="Post-controls"
67-
buttonClassName="Button Button--icon Button--flat"
68-
menuClassName="Dropdown-menu--right"
69-
icon="fas fa-ellipsis-h"
70-
onshow={() => this.$('.Post-controls').addClass('open')}
71-
onhide={() => this.$('.Post-controls').removeClass('open')}
72-
accessibleToggleLabel={app.translator.trans('core.forum.post_controls.toggle_dropdown_accessible_label')}
73-
>
74-
{controls}
75-
</Dropdown>
76-
</li>
77-
)}
78-
</ul>
79-
</aside>
80-
<footer className="Post-footer">{footerItems.length ? <ul>{listItems(footerItems)}</ul> : null}</footer>
81-
</div>
58+
<div>{this.viewItems(controls, footerItems).toArray()}</div>
8259
</article>
8360
);
8461
}
8562

63+
viewItems(controls: Mithril.Children[], footerItems: ModdedChildrenWithItemName[]): ItemList<Mithril.Children> {
64+
const items = new ItemList<Mithril.Children>();
65+
66+
items.add('content', this.loading ? <LoadingIndicator /> : this.content(), 100);
67+
68+
items.add(
69+
'actions',
70+
<aside className="Post-actions">
71+
<ul>
72+
{listItems(this.actionItems().toArray())}
73+
{!!controls.length && (
74+
<li>
75+
<Dropdown
76+
className="Post-controls"
77+
buttonClassName="Button Button--icon Button--flat"
78+
menuClassName="Dropdown-menu--right"
79+
icon="fas fa-ellipsis-h"
80+
onshow={() => this.$('.Post-controls').addClass('open')}
81+
onhide={() => this.$('.Post-controls').removeClass('open')}
82+
accessibleToggleLabel={app.translator.trans('core.forum.post_controls.toggle_dropdown_accessible_label')}
83+
>
84+
{controls}
85+
</Dropdown>
86+
</li>
87+
)}
88+
</ul>
89+
</aside>,
90+
90
91+
);
92+
93+
items.add('footer', <footer className="Post-footer">{footerItems.length > 0 ? <ul>{listItems(footerItems)}</ul> : <ul></ul>}</footer>, 80);
94+
95+
return items;
96+
}
97+
8698
onbeforeupdate(vnode: Mithril.VnodeDOM<CustomAttrs, this>) {
8799
super.onbeforeupdate(vnode);
88100

@@ -147,7 +159,7 @@ export default abstract class Post<CustomAttrs extends IPostAttrs = IPostAttrs>
147159
/**
148160
* Build an item list for the post's footer.
149161
*/
150-
footerItems(): ItemList<Mithril.Children> {
151-
return new ItemList();
162+
footerItems(): ItemList<ModdedChildrenWithItemName> {
163+
return new ItemList<ModdedChildrenWithItemName>();
152164
}
153165
}

0 commit comments

Comments
 (0)