Skip to content

Commit

Permalink
Maintain scroll position by URL
Browse files Browse the repository at this point in the history
If there were 3+ tabs opened to the same element, the localStorage value would only apply to one of them, and not necessarily the right one.
  • Loading branch information
brandonkelly committed May 21, 2024
1 parent 4f0d222 commit f3ab6ea
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fixed a bug where element queries’ `count()` methods were factoring in the `limit` param when searching with `orderBy` set to `score`. ([#15001](https://github.com/craftcms/cms/issues/15001))
- Fixed a bug where soft-deleted elements that belonged to a revision could be deleted by garbage collection. ([#14995](https://github.com/craftcms/cms/pull/14995))
- Fixed a bug where soft-deleted structure data associated with elements that belonged to a revision could be deleted by garbage collection. ([#14995](https://github.com/craftcms/cms/pull/14995))
- Fixed a bug where element edit pages’ scroll positions weren’t always retained when automatically refreshed.

## 4.9.4 - 2024-05-17

Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js.map

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/web/assets/cp/src/js/CP.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,25 @@ Craft.CP = Garnish.Base.extend(
}

// Should we match the previous scroll position?
let scrollY = Craft.getLocalStorage('scrollY');
if (typeof scrollY !== 'undefined') {
Craft.removeLocalStorage('scrollY');
let scrollY;
const location = document.location;
const params = new URLSearchParams(location.search);
if (params.has('scrollY')) {
scrollY = params.get('scrollY');
params.delete('scrollY');
Craft.setUrl(
Craft.getUrl(
`${location.origin}${location.pathname}${location.hash}`,
params.toString()
)
);
} else {
scrollY = Craft.getLocalStorage('scrollY');
if (scrollY !== undefined) {
Craft.removeLocalStorage('scrollY');
}
}
if (scrollY !== undefined) {
Garnish.$doc.ready(() => {
Garnish.requestAnimationFrame(() => {
window.scrollTo(0, scrollY);
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/src/js/Craft.js
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ $.extend(Craft, {
* Retrieves a value from localStorage if it exists.
*
* @param {string} key
* @param {*} defaultValue
* @param {*} [defaultValue]
*/
getLocalStorage: function (key, defaultValue) {
key = 'Craft-' + Craft.systemUid + '.' + key;
Expand Down
12 changes: 10 additions & 2 deletions src/web/assets/cp/src/js/ElementEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ Craft.ElementEditor = Garnish.Base.extend(
ev.data.id === this.settings.canonicalId &&
!this.settings.draftId)
) {
Craft.setLocalStorage('scrollY', window.scrollY);
Craft.setUrl(
Craft.getUrl(document.location.href, {
scrollY: window.scrollY,
})
);
window.location.reload();
} else if (
ev.data.event === 'deleteDraft' &&
Expand All @@ -232,7 +236,11 @@ Craft.ElementEditor = Garnish.Base.extend(
if (url.href !== document.location.href) {
window.location.href = url;
} else {
Craft.setLocalStorage('scrollY', window.scrollY);
Craft.setUrl(
Craft.getUrl(document.location.href, {
scrollY: window.scrollY,
})
);
window.location.reload();
}
}
Expand Down

0 comments on commit f3ab6ea

Please sign in to comment.