From 53ac644516035324594f84c3c71c59448744b913 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 25 Oct 2024 14:48:16 +0100 Subject: [PATCH] chore: simplify checking of current composer --- .../js/src/forum/mentionables/PostMention.tsx | 5 +---- extensions/mentions/js/src/forum/utils/reply.js | 4 +--- extensions/messages/js/src/forum/index.tsx | 6 +----- .../core/js/src/forum/components/CommentPost.js | 6 +----- .../core/js/src/forum/states/ComposerState.tsx | 16 ++++++++++------ 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/extensions/mentions/js/src/forum/mentionables/PostMention.tsx b/extensions/mentions/js/src/forum/mentionables/PostMention.tsx index d64541780..92f949282 100644 --- a/extensions/mentions/js/src/forum/mentionables/PostMention.tsx +++ b/extensions/mentions/js/src/forum/mentionables/PostMention.tsx @@ -21,10 +21,7 @@ export default class PostMention extends MentionableModel * match any username characters that have been typed. */ initialResults(): Post[] { - const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); - const ReplyComposer = flarum.reg.checkModule('core', 'forum/components/ReplyComposer'); - - if ((!ReplyComposer || !app.composer.bodyMatches(ReplyComposer)) && (!EditPostComposer || !app.composer.bodyMatches(EditPostComposer))) { + if (!app.composer.bodyMatches('flarum/forum/components/ReplyComposer') && !app.composer.bodyMatches('flarum/forum/components/EditPostComposer')) { return []; } diff --git a/extensions/mentions/js/src/forum/utils/reply.js b/extensions/mentions/js/src/forum/utils/reply.js index 816ed83ef..5c2aa24b4 100644 --- a/extensions/mentions/js/src/forum/utils/reply.js +++ b/extensions/mentions/js/src/forum/utils/reply.js @@ -26,9 +26,7 @@ export function insertMention(post, composer, quote) { } export default function reply(post, quote) { - const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); - - if (EditPostComposer && app.composer.bodyMatches(EditPostComposer) && app.composer.body.attrs.post.discussion() === post.discussion()) { + if (app.composer.bodyMatches('flarum/forum/components/EditPostComposer') && app.composer.body.attrs.post.discussion() === post.discussion()) { // If we're already editing a post in the discussion of post we're quoting, // insert the mention directly. return insertMention(post, app.composer, quote); diff --git a/extensions/messages/js/src/forum/index.tsx b/extensions/messages/js/src/forum/index.tsx index 3ea6c0228..35e735948 100644 --- a/extensions/messages/js/src/forum/index.tsx +++ b/extensions/messages/js/src/forum/index.tsx @@ -24,11 +24,7 @@ app.initializers.add('flarum-messages', () => { ); app.composer.composingMessageTo = function (dialog: Dialog) { - const MessageComposer = flarum.reg.checkModule('flarum-messages', 'forum/components/MessageComposer'); - - if (!MessageComposer) return false; - - return this.isVisible() && this.bodyMatches(MessageComposer, { dialog }); + return this.isVisible() && this.bodyMatches('flarum/messages/forum/components/MessageComposer', { dialog }); }; extend(IndexSidebar.prototype, 'navItems', function (items) { diff --git a/framework/core/js/src/forum/components/CommentPost.js b/framework/core/js/src/forum/components/CommentPost.js index 498f531eb..ab1724579 100644 --- a/framework/core/js/src/forum/components/CommentPost.js +++ b/framework/core/js/src/forum/components/CommentPost.js @@ -107,11 +107,7 @@ export default class CommentPost extends Post { } isEditing() { - const EditPostComposer = flarum.reg.checkModule('core', 'forum/components/EditPostComposer'); - - if (!EditPostComposer) return false; - - return app.composer.bodyMatches(EditPostComposer, { post: this.attrs.post }); + return app.composer.bodyMatches('flarum/forum/components/EditPostComposer', { post: this.attrs.post }); } elementAttrs() { diff --git a/framework/core/js/src/forum/states/ComposerState.tsx b/framework/core/js/src/forum/states/ComposerState.tsx index a9e523348..2897daf8d 100644 --- a/framework/core/js/src/forum/states/ComposerState.tsx +++ b/framework/core/js/src/forum/states/ComposerState.tsx @@ -171,7 +171,15 @@ class ComposerState { * @param type The component class to check against. Subclasses are accepted as well. * @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 if (!subclassOf(this.body.componentClass, type)) return false; @@ -212,11 +220,7 @@ class ComposerState { * @return {boolean} */ composingReplyTo(discussion: Discussion) { - const ReplyComposer = flarum.reg.checkModule('core', 'forum/components/ReplyComposer'); - - if (!ReplyComposer) return false; - - return this.isVisible() && this.bodyMatches(ReplyComposer, { discussion }); + return this.isVisible() && this.bodyMatches('flarum/forum/components/ReplyComposer', { discussion }); } /**