1
0
mirror of https://github.com/flarum/core.git synced 2025-08-08 09:26:34 +02:00

chore: simplify checking of current composer

This commit is contained in:
Sami Mazouz
2024-10-25 14:48:16 +01:00
parent 6e5180dcfe
commit 53ac644516
5 changed files with 14 additions and 23 deletions

View File

@@ -21,10 +21,7 @@ export default class PostMention extends MentionableModel<Post, AtMentionFormat>
* match any username characters that have been typed. * match any username characters that have been typed.
*/ */
initialResults(): Post[] { initialResults(): Post[] {
const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); if (!app.composer.bodyMatches('flarum/forum/components/ReplyComposer') && !app.composer.bodyMatches('flarum/forum/components/EditPostComposer')) {
const ReplyComposer = flarum.reg.checkModule('core', 'forum/components/ReplyComposer');
if ((!ReplyComposer || !app.composer.bodyMatches(ReplyComposer)) && (!EditPostComposer || !app.composer.bodyMatches(EditPostComposer))) {
return []; return [];
} }

View File

@@ -26,9 +26,7 @@ export function insertMention(post, composer, quote) {
} }
export default function reply(post, quote) { export default function reply(post, quote) {
const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); if (app.composer.bodyMatches('flarum/forum/components/EditPostComposer') && app.composer.body.attrs.post.discussion() === post.discussion()) {
if (EditPostComposer && app.composer.bodyMatches(EditPostComposer) && app.composer.body.attrs.post.discussion() === post.discussion()) {
// If we're already editing a post in the discussion of post we're quoting, // If we're already editing a post in the discussion of post we're quoting,
// insert the mention directly. // insert the mention directly.
return insertMention(post, app.composer, quote); return insertMention(post, app.composer, quote);

View File

@@ -24,11 +24,7 @@ app.initializers.add('flarum-messages', () => {
); );
app.composer.composingMessageTo = function (dialog: Dialog) { app.composer.composingMessageTo = function (dialog: Dialog) {
const MessageComposer = flarum.reg.checkModule('flarum-messages', 'forum/components/MessageComposer'); return this.isVisible() && this.bodyMatches('flarum/messages/forum/components/MessageComposer', { dialog });
if (!MessageComposer) return false;
return this.isVisible() && this.bodyMatches(MessageComposer, { dialog });
}; };
extend(IndexSidebar.prototype, 'navItems', function (items) { extend(IndexSidebar.prototype, 'navItems', function (items) {

View File

@@ -107,11 +107,7 @@ export default class CommentPost extends Post {
} }
isEditing() { isEditing() {
const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); return app.composer.bodyMatches('flarum/forum/components/EditPostComposer', { post: this.attrs.post });
if (!EditPostComposer) return false;
return app.composer.bodyMatches(EditPostComposer, { post: this.attrs.post });
} }
elementAttrs() { elementAttrs() {

View File

@@ -171,7 +171,15 @@ class ComposerState {
* @param type The component class to check against. Subclasses are accepted as well. * @param type The component class to check against. Subclasses are accepted as well.
* @param data * @param data
*/ */
bodyMatches(type: object, data: any = {}): boolean { bodyMatches(type: object | string, data: any = {}): boolean {
if (typeof type === 'string') {
const [namespace, id] = flarum.reg.namespaceAndIdFromPath(type);
type = flarum.reg.checkModule(namespace, id);
if (!type) return false;
}
// Fail early when the body is of a different type // Fail early when the body is of a different type
if (!subclassOf(this.body.componentClass, type)) return false; if (!subclassOf(this.body.componentClass, type)) return false;
@@ -212,11 +220,7 @@ class ComposerState {
* @return {boolean} * @return {boolean}
*/ */
composingReplyTo(discussion: Discussion) { composingReplyTo(discussion: Discussion) {
const ReplyComposer = flarum.reg.checkModule('core', 'forum/components/ReplyComposer'); return this.isVisible() && this.bodyMatches('flarum/forum/components/ReplyComposer', { discussion });
if (!ReplyComposer) return false;
return this.isVisible() && this.bodyMatches(ReplyComposer, { discussion });
} }
/** /**