|
| 1 | +const SCROLL_KEY = 'root::cms::scroll'; |
| 2 | +const OPEN_KEY = 'root::cms::open'; |
| 3 | + |
| 4 | +/* |
| 5 | + * Save the scroll position and open field editors prior to reloading the page. |
| 6 | + */ |
| 7 | +function saveUiState() { |
| 8 | + const side = document.querySelector( |
| 9 | + '.DocumentPage__side' |
| 10 | + ) as HTMLElement | null; |
| 11 | + if (side) { |
| 12 | + sessionStorage.setItem(SCROLL_KEY, String(side.scrollTop)); |
| 13 | + } |
| 14 | + const openSummaries = Array.from( |
| 15 | + document.querySelectorAll( |
| 16 | + '.DocEditor__ObjectFieldDrawer__drawer[open], .DocEditor__ArrayField__item[open] summary' |
| 17 | + ) |
| 18 | + ) |
| 19 | + .map((el) => el.id) |
| 20 | + .filter(Boolean); |
| 21 | + if (openSummaries.length > 0) { |
| 22 | + sessionStorage.setItem(OPEN_KEY, JSON.stringify(openSummaries)); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/* |
| 27 | + * Restore the scroll position and open field editors when page is loaded. |
| 28 | + */ |
| 29 | +function restoreUiState() { |
| 30 | + // Restore open editors. |
| 31 | + const open = sessionStorage.getItem(OPEN_KEY); |
| 32 | + if (open) { |
| 33 | + try { |
| 34 | + const ids = JSON.parse(open) as string[]; |
| 35 | + ids.forEach((id) => { |
| 36 | + const summary = document.getElementById(id); |
| 37 | + if (summary) { |
| 38 | + const details = summary.closest('details'); |
| 39 | + if (details) { |
| 40 | + details.open = true; |
| 41 | + } |
| 42 | + } |
| 43 | + }); |
| 44 | + } catch { |
| 45 | + /* ignore */ |
| 46 | + } |
| 47 | + } |
| 48 | + // Restore scroll position. |
| 49 | + const side = document.querySelector( |
| 50 | + '.DocumentPage__side' |
| 51 | + ) as HTMLElement | null; |
| 52 | + const scroll = sessionStorage.getItem(SCROLL_KEY); |
| 53 | + if (side && scroll) { |
| 54 | + side.scrollTop = parseInt(scroll, 10); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** Returns whether the UI state should be saved and restored. */ |
| 59 | +function testPreserveUiState() { |
| 60 | + // Currently, this is mainly used for development. Specifically, when the page is HMR'ed, we |
| 61 | + // want to preserve the scroll position and open editors. |
| 62 | + return window.location.hostname === 'localhost'; |
| 63 | +} |
| 64 | + |
| 65 | +/** Preserves the UI state across page reloads. */ |
| 66 | +export function preserveUiState(): () => void { |
| 67 | + // Do nothing if the UI state should not be preserved. |
| 68 | + if (!testPreserveUiState()) { |
| 69 | + return () => {}; |
| 70 | + } |
| 71 | + // Restore the UI state once the data is loaded. |
| 72 | + restoreUiState(); |
| 73 | + const beforeUnloadHandler = () => { |
| 74 | + // Save the scroll position and open field editors prior to reloading the page. |
| 75 | + saveUiState(); |
| 76 | + }; |
| 77 | + // Before the page is unloaded, save the ui state. |
| 78 | + window.addEventListener('beforeunload', beforeUnloadHandler); |
| 79 | + // Return a callback that removes the event listener. |
| 80 | + return () => { |
| 81 | + window.removeEventListener('beforeunload', beforeUnloadHandler); |
| 82 | + }; |
| 83 | +} |
0 commit comments