1
0
mirror of https://github.com/flarum/core.git synced 2025-10-10 14:34:30 +02:00
Files
php-flarum/js/forum/src/components/ReplyComposer.js
Toby Zerner 094345de85 Improve mobile composer behaviour
- Don't scroll to the bottom of the discussion when selecting "Reply"
  from the menu if the composer is in full screen mode (ie. on mobile).
  ref #1271

- After posting a reply, scroll to the end of the discussion

- Reduce the textarea height - previously it was 100vh, but this doesn't
  account for the height of the iOS keyboard, so I've just arbitrarily
  chosen 300px instead. There may be a better solution for this.
  ref #1269
2017-11-05 16:25:21 +10:30

117 lines
3.3 KiB
JavaScript

import ComposerBody from 'flarum/components/ComposerBody';
import Alert from 'flarum/components/Alert';
import Button from 'flarum/components/Button';
import icon from 'flarum/helpers/icon';
import extractText from 'flarum/utils/extractText';
function minimizeComposerIfFullScreen(e) {
if (app.composer.isFullScreen()) {
app.composer.minimize();
e.stopPropagation();
}
}
/**
* The `ReplyComposer` component displays the composer content for replying to a
* discussion.
*
* ### Props
*
* - All of the props of ComposerBody
* - `discussion`
*/
export default class ReplyComposer extends ComposerBody {
init() {
super.init();
this.editor.props.preview = e => {
minimizeComposerIfFullScreen(e);
m.route(app.route.discussion(this.props.discussion, 'reply'));
};
}
static initProps(props) {
super.initProps(props);
props.placeholder = props.placeholder || extractText(app.translator.trans('core.forum.composer_reply.body_placeholder'));
props.submitLabel = props.submitLabel || app.translator.trans('core.forum.composer_reply.submit_button');
props.confirmExit = props.confirmExit || extractText(app.translator.trans('core.forum.composer_reply.discard_confirmation'));
}
headerItems() {
const items = super.headerItems();
const discussion = this.props.discussion;
const routeAndMinimize = function(element, isInitialized) {
if (isInitialized) return;
$(element).on('click', minimizeComposerIfFullScreen);
m.route.apply(this, arguments);
};
items.add('title', (
<h3>
{icon('reply')} {' '}
<a href={app.route.discussion(discussion)} config={routeAndMinimize}>{discussion.title()}</a>
</h3>
));
return items;
}
/**
* Get the data to submit to the server when the reply is saved.
*
* @return {Object}
*/
data() {
return {
content: this.content(),
relationships: {discussion: this.props.discussion}
};
}
onsubmit() {
const discussion = this.props.discussion;
this.loading = true;
m.redraw();
const data = this.data();
app.store.createRecord('posts').save(data).then(
post => {
// If we're currently viewing the discussion which this reply was made
// in, then we can update the post stream and scroll to the post.
if (app.viewingDiscussion(discussion)) {
app.current.stream.update().then(() => app.current.stream.goToNumber(post.number()));
} else {
// Otherwise, we'll create an alert message to inform the user that
// their reply has been posted, containing a button which will
// transition to their new post when clicked.
let alert;
const viewButton = Button.component({
className: 'Button Button--link',
children: app.translator.trans('core.forum.composer_reply.view_button'),
onclick: () => {
m.route(app.route.post(post));
app.alerts.dismiss(alert);
}
});
app.alerts.show(
alert = new Alert({
type: 'success',
message: app.translator.trans('core.forum.composer_reply.posted_message'),
controls: [viewButton]
})
);
}
app.composer.hide();
},
this.loaded.bind(this)
);
}
}