Don't scroll on m.route.set() to different post on same page.

This removes some messy logic, and the potential for glitches. This system worked well with Mithril 0.2 where we could listen in before (and prevent) page unload, but since that's not possible in Mithril 2, the implementation of the replacement was done in `onbeforeupdate`, which might be called while the page route is being updated, glitching out the page. Instead, extensions should check if they are already on the discussion page for the post they are linking to, and if so, use `app.current.get('stream').goToNumber(TARGET)`.

Please note that this does NOT affect going directly to posts from external links (or page reload), OR from other pages via m.route.set.
This commit is contained in:
Alexander Skvortsov 2020-10-08 11:34:56 -04:00
parent 656409794c
commit 641330ce52

View File

@ -99,28 +99,12 @@ export default class DiscussionPage extends Page {
onbeforeupdate(vnode) { onbeforeupdate(vnode) {
super.onbeforeupdate(vnode); super.onbeforeupdate(vnode);
if (m.route.get() !== this.prevRoute) { const idParam = m.route.param('id');
if (m.route.get() !== this.prevRoute && this.discussion && (!idParam || idParam.split('-')[0] !== this.discussion.id())) {
this.prevRoute = m.route.get(); this.prevRoute = m.route.get();
// If we have routed to the same discussion as we were viewing previously, this.onNewRoute();
// cancel the unloading of this controller and instead prompt the post this.oninit(vnode);
// stream to jump to the new 'near' param.
if (this.discussion) {
const idParam = m.route.param('id');
if (idParam && idParam.split('-')[0] === this.discussion.id()) {
const near = m.route.param('near') || '1';
if (near !== String(this.near)) {
this.stream.goToNumber(near);
}
this.near = near;
} else {
this.onNewRoute();
this.oninit(vnode);
}
}
} }
} }
@ -190,15 +174,19 @@ export default class DiscussionPage extends Page {
.slice(0, 20); .slice(0, 20);
} }
const startNumber = m.route.param('near') || (includedPosts[0] && includedPosts[0].number());
// Set up the post stream for this discussion, along with the first page of // Set up the post stream for this discussion, along with the first page of
// posts we want to display. Tell the stream to scroll down and highlight // posts we want to display. Tell the stream to scroll down and highlight
// the specific post that was routed to. // the specific post that was routed to.
this.stream = new PostStreamState(discussion, includedPosts); this.stream = new PostStreamState(discussion, includedPosts);
this.stream.goToNumber(m.route.param('near') || (includedPosts[0] && includedPosts[0].number()), true).then(() => { this.stream.goToNumber(startNumber, true).then(() => {
this.discussion = discussion; this.discussion = discussion;
app.current.set('discussion', discussion); app.current.set('discussion', discussion);
app.current.set('stream', this.stream); app.current.set('stream', this.stream);
this.positionChanged(startNumber);
}); });
} }