Skip to content

Commit ae662ca

Browse files
committed
Save the editor state in the hash so that we can set the region explicitly
1 parent 35e5b3a commit ae662ca

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Next version
99
modern browsers.
1010
- Made the content editor region tabs stick to the top of the browser window
1111
when scrolling down.
12+
- Saved the editor state in the URL instead of using session storage because
13+
this allows loading a specific region explicitly.
1214

1315

1416
7.4 (2025-07-02)

content_editor/static/content_editor/content_editor.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
}
3232

3333
const LS = safeStorage(localStorage)
34-
const SS = safeStorage(sessionStorage)
3534

3635
const prepareContentEditorObject = () => {
3736
Object.assign(ContentEditor, JSON.parse(_contentEditorContext))
@@ -901,36 +900,43 @@
901900
})
902901

903902
const saveEditorState = () => {
904-
SS.set(location.pathname, {
905-
region: ContentEditor.currentRegion,
906-
scrollY: window.scrollY,
907-
collapsed: qsa(
903+
const u = new URLSearchParams()
904+
u.append("region", ContentEditor.currentRegion)
905+
u.append("scrollY", Math.floor(window.scrollY))
906+
u.append(
907+
"collapsed",
908+
qsa(
908909
".order-machine .inline-related.collapsed:not(.empty-form) .order-machine-ordering",
909-
).map((input) => input.value),
910-
})
910+
)
911+
.map((input) => input.value)
912+
.join(","),
913+
)
914+
return u.toString()
911915
}
912916

913917
const restoreEditorState = () => {
914918
const tabs = $(".tabs.regions .tab")
915919

916-
const state = location.hash.includes("restore")
917-
? SS.get(location.pathname)
918-
: null
919-
if (state) {
920+
const u = new URLSearchParams(location.hash.replace(/^#/, ""))
921+
if (u.size) {
922+
const region = u.get("region")
923+
const scrollY = u.get("scrollY") || 0
924+
const collapsed = (u.get("collapsed") || "").split(",")
925+
920926
for (const inline of qsa(
921927
".order-machine .inline-related:not(.empty-form)",
922928
)) {
923-
const collapsed = state.collapsed.includes(
929+
const wasCollapsed = collapsed.includes(
924930
qs(".order-machine-ordering", inline).value,
925931
)
926932
/* XXX handle sections */
927933
inline.classList.toggle(
928934
"collapsed",
929-
collapsed && !inline.querySelector(".errorlist"),
935+
wasCollapsed && !inline.querySelector(".errorlist"),
930936
)
931937
}
932938

933-
const tab = tabs.filter(`[data-region="${state.region}"]`)
939+
const tab = tabs.filter(`[data-region="${region}"]`)
934940
if (tab.length) {
935941
tab.click()
936942
} else {
@@ -939,19 +945,20 @@
939945

940946
initializeCollapseAll()
941947

942-
setTimeout(() => {
943-
window.history.replaceState(null, "", ".")
944-
window.scrollTo(0, state.scrollY)
945-
}, 200)
948+
if (scrollY) {
949+
setTimeout(() => {
950+
window.scrollTo(0, scrollY)
951+
}, 200)
952+
}
946953
} else {
947954
tabs.eq(0).click()
948955
initializeCollapseAll()
949956
}
950957
}
951958

952959
$("form").submit(function () {
953-
this.action = `${this.action.split("#")[0]}#restore`
954-
saveEditorState()
960+
// Use the hash because it's still there after the save-and-continue redirect
961+
this.action = `${this.action.split("#")[0]}#${saveEditorState()}`
955962
})
956963

957964
setTimeout(restoreEditorState, 1)

0 commit comments

Comments
 (0)