FEATURE: Save scroll position on bookmarks page (#15296)

Clicking on a bookmark and then back will preserve the scrolling
position.
This commit is contained in:
Bianca Nenciu 2021-12-15 15:27:09 +02:00 committed by GitHub
parent 0719531bd3
commit a09b6fe114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 13 deletions

View File

@ -1,17 +1,53 @@
import Component from "@ember/component";
import { action } from "@ember/object";
import { schedule } from "@ember/runloop";
import bootbox from "bootbox";
import discourseDebounce from "discourse-common/lib/debounce";
import { openBookmarkModal } from "discourse/controllers/bookmark";
import {
openLinkInNewTab,
shouldOpenInNewTab,
} from "discourse/lib/click-track";
import Component from "@ember/component";
import Scrolling from "discourse/mixins/scrolling";
import I18n from "I18n";
import { Promise } from "rsvp";
import { action } from "@ember/object";
import bootbox from "bootbox";
import { openBookmarkModal } from "discourse/controllers/bookmark";
export default Component.extend({
export default Component.extend(Scrolling, {
classNames: ["bookmark-list-wrapper"],
didInsertElement() {
this._super(...arguments);
this.bindScrolling();
this.scrollToLastPosition();
},
willDestroyElement() {
this._super(...arguments);
this.unbindScrolling();
},
scrollToLastPosition() {
let scrollTo = this.session.bookmarkListScrollPosition;
if (scrollTo && scrollTo >= 0) {
schedule("afterRender", () => {
discourseDebounce(
this,
function () {
if (this.element && !this.isDestroying && !this.isDestroyed) {
window.scrollTo(0, scrollTo + 1);
}
},
0
);
});
}
},
scrolled() {
this._super(...arguments);
this.session.set("bookmarkListScrollPosition", window.scrollY);
},
@action
removeBookmark(bookmark) {
return new Promise((resolve, reject) => {

View File

@ -1,12 +1,12 @@
import Controller, { inject as controller } from "@ember/controller";
import EmberObject, { action, computed } from "@ember/object";
import { equal, notEmpty } from "@ember/object/computed";
import { iconHTML } from "discourse-common/lib/icon-library";
import discourseComputed from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
import Bookmark from "discourse/models/bookmark";
import I18n from "I18n";
import { Promise } from "rsvp";
import EmberObject, { action, computed } from "@ember/object";
import discourseComputed from "discourse-common/utils/decorators";
import { equal, notEmpty } from "@ember/object/computed";
import { ajax } from "discourse/lib/ajax";
export default Controller.extend({
queryParams: ["q"],
@ -102,6 +102,7 @@ export default Controller.extend({
if (response.bookmarks) {
const bookmarkModels = response.bookmarks.map(this.transform);
this.model.bookmarks.pushObjects(bookmarkModels);
this.session.set("bookmarksModel", this.model);
}
},

View File

@ -1,6 +1,7 @@
import DiscourseRoute from "discourse/routes/discourse";
import { ajax } from "discourse/lib/ajax";
import { action } from "@ember/object";
import { ajax } from "discourse/lib/ajax";
import DiscourseRoute from "discourse/routes/discourse";
import { Promise } from "rsvp";
export default DiscourseRoute.extend({
queryParams: {
@ -8,9 +9,18 @@ export default DiscourseRoute.extend({
q: { refreshModel: true },
},
model(params) {
model(params, transition) {
const controller = this.controllerFor("user-activity-bookmarks");
if (this.isPoppedState(transition) && this.session.bookmarksModel) {
return Promise.resolve(this.session.bookmarksModel);
}
this.session.setProperties({
bookmarksModel: null,
bookmarkListScrollPosition: null,
});
return this._loadBookmarks(params)
.then((response) => {
if (!response.user_bookmark_list) {
@ -22,7 +32,9 @@ export default DiscourseRoute.extend({
);
const loadMoreUrl = response.user_bookmark_list.more_bookmarks_url;
return { bookmarks, loadMoreUrl };
const model = { bookmarks, loadMoreUrl };
this.session.set("bookmarksModel", model);
return model;
})
.catch(() => controller.set("permissionDenied", true));
},