mirror of
https://github.com/flarum/framework.git
synced 2025-04-25 06:04:04 +08:00
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:
parent
f611938707
commit
302d92aa47
23
extensions/mentions/js/src/forum/compat.js
Normal file
23
extensions/mentions/js/src/forum/compat.js
Normal 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
|
||||
};
|
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user