|
31 | 31 | }
|
32 | 32 |
|
33 | 33 | const LS = safeStorage(localStorage)
|
34 |
| - const SS = safeStorage(sessionStorage) |
35 | 34 |
|
36 | 35 | const prepareContentEditorObject = () => {
|
37 | 36 | Object.assign(ContentEditor, JSON.parse(_contentEditorContext))
|
|
901 | 900 | })
|
902 | 901 |
|
903 | 902 | 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( |
908 | 909 | ".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() |
911 | 915 | }
|
912 | 916 |
|
913 | 917 | const restoreEditorState = () => {
|
914 | 918 | const tabs = $(".tabs.regions .tab")
|
915 | 919 |
|
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 | + |
920 | 926 | for (const inline of qsa(
|
921 | 927 | ".order-machine .inline-related:not(.empty-form)",
|
922 | 928 | )) {
|
923 |
| - const collapsed = state.collapsed.includes( |
| 929 | + const wasCollapsed = collapsed.includes( |
924 | 930 | qs(".order-machine-ordering", inline).value,
|
925 | 931 | )
|
926 | 932 | /* XXX handle sections */
|
927 | 933 | inline.classList.toggle(
|
928 | 934 | "collapsed",
|
929 |
| - collapsed && !inline.querySelector(".errorlist"), |
| 935 | + wasCollapsed && !inline.querySelector(".errorlist"), |
930 | 936 | )
|
931 | 937 | }
|
932 | 938 |
|
933 |
| - const tab = tabs.filter(`[data-region="${state.region}"]`) |
| 939 | + const tab = tabs.filter(`[data-region="${region}"]`) |
934 | 940 | if (tab.length) {
|
935 | 941 | tab.click()
|
936 | 942 | } else {
|
|
939 | 945 |
|
940 | 946 | initializeCollapseAll()
|
941 | 947 |
|
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 | + } |
946 | 953 | } else {
|
947 | 954 | tabs.eq(0).click()
|
948 | 955 | initializeCollapseAll()
|
949 | 956 | }
|
950 | 957 | }
|
951 | 958 |
|
952 | 959 | $("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()}` |
955 | 962 | })
|
956 | 963 |
|
957 | 964 | setTimeout(restoreEditorState, 1)
|
|
0 commit comments