From 302d92aa475a5e247db311a215493cd48faa8177 Mon Sep 17 00:00:00 2001 From: Gianni Guida <53989450+gianniguida@users.noreply.github.com> Date: Tue, 23 Nov 2021 21:44:46 +0100 Subject: [PATCH] feat: added compat exports and extensibility (#76) * Added extensibility * Corrected object export * Exported the `insertMention` util * Return a `Promise` in the `reply` util (for extensibility) * Removed initialization utils Co-authored-by: Rafael Horvat --- extensions/mentions/js/src/forum/compat.js | 23 +++++++++++ extensions/mentions/js/src/forum/index.js | 6 +++ .../mentions/js/src/forum/utils/reply.js | 41 ++++++++++--------- 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 extensions/mentions/js/src/forum/compat.js diff --git a/extensions/mentions/js/src/forum/compat.js b/extensions/mentions/js/src/forum/compat.js new file mode 100644 index 000000000..6167d98f2 --- /dev/null +++ b/extensions/mentions/js/src/forum/compat.js @@ -0,0 +1,23 @@ +import MentionsUserPage from './components/MentionsUserPage'; +import PostMentionedNotification from './components/PostMentionedNotification'; +import UserMentionedNotification from './components/UserMentionedNotification'; +import AutocompleteDropdown from './fragments/AutocompleteDropdown'; +import PostQuoteButton from './fragments/PostQuoteButton'; +import getCleanDisplayName from './utils/getCleanDisplayName'; +import getMentionText from './utils/getMentionText'; +import * as reply from './utils/reply'; +import selectedText from './utils/selectedText'; +import * as textFormatter from './utils/textFormatter'; + +export default { + 'mentions/components/MentionsUserPage': MentionsUserPage, + 'mentions/components/PostMentionedNotification': PostMentionedNotification, + 'mentions/components/UserMentionedNotification': UserMentionedNotification, + 'mentions/fragments/AutocompleteDropdown': AutocompleteDropdown, + 'mentions/fragments/PostQuoteButton': PostQuoteButton, + 'mentions/utils/getCleanDisplayName': getCleanDisplayName, + 'mentions/utils/getMentionText': getMentionText, + 'mentions/utils/reply': reply, + 'mentions/utils/selectedText': selectedText, + 'mentions/utils/textFormatter': textFormatter +}; \ No newline at end of file diff --git a/extensions/mentions/js/src/forum/index.js b/extensions/mentions/js/src/forum/index.js index 555a651e7..7a0629fc5 100644 --- a/extensions/mentions/js/src/forum/index.js +++ b/extensions/mentions/js/src/forum/index.js @@ -71,3 +71,9 @@ app.initializers.add('flarum-mentions', function() { }); export * from './utils/textFormatter'; + +// Expose compat API +import mentionsCompat from './compat'; +import { compat } from '@flarum/core/forum'; + +Object.assign(compat, mentionsCompat); diff --git a/extensions/mentions/js/src/forum/utils/reply.js b/extensions/mentions/js/src/forum/utils/reply.js index 68cc8132d..b146a4141 100644 --- a/extensions/mentions/js/src/forum/utils/reply.js +++ b/extensions/mentions/js/src/forum/utils/reply.js @@ -2,37 +2,40 @@ import DiscussionControls from 'flarum/forum/utils/DiscussionControls'; import EditPostComposer from 'flarum/forum/components/EditPostComposer'; import getMentionText from './getMentionText'; -function insertMention(post, composer, quote) { - const user = post.user(); - const mention = getMentionText(user, post.id()) + ' '; +export function insertMention(post, composer, quote) { + return new Promise((resolve) => { + const user = post.user(); + const mention = getMentionText(user, post.id()) + ' '; - // If the composer is empty, then assume we're starting a new reply. - // In which case we don't want the user to have to confirm if they - // close the composer straight away. - if (!composer.fields.content()) { - composer.body.attrs.originalContent = mention; - } + // If the composer is empty, then assume we're starting a new reply. + // In which case we don't want the user to have to confirm if they + // close the composer straight away. + if (!composer.fields.content()) { + composer.body.attrs.originalContent = mention; + } - const cursorPosition = composer.editor.getSelectionRange()[0]; - const preceding = composer.fields.content().slice(0, cursorPosition); - const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length; + const cursorPosition = composer.editor.getSelectionRange()[0]; + const preceding = composer.fields.content().slice(0, cursorPosition); + const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length; - composer.editor.insertAtCursor( - Array(precedingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace - (quote ? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n' : mention), - false - ); + composer.editor.insertAtCursor( + Array(precedingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace + (quote ? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n' : mention), + false + ); + return resolve(composer); + }); } export default function reply(post, quote) { if (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, // insert the mention directly. - insertMention(post, app.composer, quote); + return insertMention(post, app.composer, quote); } else { // The default "Reply" action behavior will only open a new composer if // necessary, but it will always be a ReplyComposer, hence the exceptional // case above. - DiscussionControls.replyAction.call(post.discussion()).then((composer) => insertMention(post, composer, quote)); + return DiscussionControls.replyAction.call(post.discussion()).then((composer) => insertMention(post, composer, quote)); } }