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 <rafael.horvat@glowingblue.com>
This commit is contained in:
Gianni Guida 2021-11-23 21:44:46 +01:00 committed by GitHub
parent f611938707
commit 302d92aa47
3 changed files with 51 additions and 19 deletions

View File

@ -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
};

View File

@ -71,3 +71,9 @@ app.initializers.add('flarum-mentions', function() {
}); });
export * from './utils/textFormatter'; export * from './utils/textFormatter';
// Expose compat API
import mentionsCompat from './compat';
import { compat } from '@flarum/core/forum';
Object.assign(compat, mentionsCompat);

View File

@ -2,37 +2,40 @@ import DiscussionControls from 'flarum/forum/utils/DiscussionControls';
import EditPostComposer from 'flarum/forum/components/EditPostComposer'; import EditPostComposer from 'flarum/forum/components/EditPostComposer';
import getMentionText from './getMentionText'; import getMentionText from './getMentionText';
function insertMention(post, composer, quote) { export function insertMention(post, composer, quote) {
const user = post.user(); return new Promise((resolve) => {
const mention = getMentionText(user, post.id()) + ' '; const user = post.user();
const mention = getMentionText(user, post.id()) + ' ';
// If the composer is empty, then assume we're starting a new reply. // 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 // In which case we don't want the user to have to confirm if they
// close the composer straight away. // close the composer straight away.
if (!composer.fields.content()) { if (!composer.fields.content()) {
composer.body.attrs.originalContent = mention; composer.body.attrs.originalContent = mention;
} }
const cursorPosition = composer.editor.getSelectionRange()[0]; const cursorPosition = composer.editor.getSelectionRange()[0];
const preceding = composer.fields.content().slice(0, cursorPosition); const preceding = composer.fields.content().slice(0, cursorPosition);
const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length; const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length;
composer.editor.insertAtCursor( composer.editor.insertAtCursor(
Array(precedingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace Array(precedingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace
(quote ? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n' : mention), (quote ? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n' : mention),
false false
); );
return resolve(composer);
});
} }
export default function reply(post, quote) { export default function reply(post, quote) {
if (app.composer.bodyMatches(EditPostComposer) && app.composer.body.attrs.post.discussion() === post.discussion()) { 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, // If we're already editing a post in the discussion of post we're quoting,
// insert the mention directly. // insert the mention directly.
insertMention(post, app.composer, quote); return insertMention(post, app.composer, quote);
} else { } else {
// The default "Reply" action behavior will only open a new composer if // The default "Reply" action behavior will only open a new composer if
// necessary, but it will always be a ReplyComposer, hence the exceptional // necessary, but it will always be a ReplyComposer, hence the exceptional
// case above. // 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));
} }
} }