| @@ -1,23 +1,36 @@ | ||
| 1 | +import { updateQueryParams } from '../../utils/urls' | |
| 2 | + | |
| 1 | 3 | const selectTab = (tabsWrapper: Element, tab: Element, section: string) => { |
| 2 | 4 | // Swap the active tab class from the previously active tab to the current one. |
| 3 | - tabsWrapper.querySelector('.nav-tab-active')?.classList.remove('nav-tab-active') | |
| 4 | - tab.classList.add('nav-tab-active') | |
| 5 | + tabsWrapper.querySelector('.active-type')?.classList.remove('active-type') | |
| 6 | + tab.classList.add('active-type') | |
| 7 | + updateQueryParams({ section }) | |
| 5 | 8 | |
| 6 | 9 | // Update the current active tab attribute so that only the active tab is displayed. |
| 7 | 10 | tabsWrapper.closest('.wrap')?.setAttribute('data-active-tab', section) |
| 11 | + | |
| 12 | + //Hide all cloud messages - this is a bit of a hack, but it works make better **TODO** | |
| 13 | + document.querySelectorAll('.cloud-message').forEach(element => { | |
| 14 | + element.classList.add('hidden') | |
| 15 | + }) | |
| 16 | + | |
| 17 | + if ('version-switch' !== section) { | |
| 18 | + document.getElementById('version-switch-warning')?.classList.add('hidden') | |
| 19 | + } | |
| 8 | 20 | } |
| 9 | 21 | |
| 10 | -// Refresh the editor preview if we're viewing the editor section. | |
| 11 | -const refreshEditorPreview = (section: string) => { | |
| 12 | - if ('editor' === section) { | |
| 13 | - window.code_snippets_editor_preview?.codemirror.refresh() | |
| 14 | - } | |
| 22 | +// Refresh the editor preview whenever tabs change. CodeMirror measures itself | |
| 23 | +// as zero-height while its container is hidden, so it has to be told to | |
| 24 | +// remeasure once the section holding it becomes visible. Keying this to a | |
| 25 | +// specific section name meant the preview rendered blank if that name changed. | |
| 26 | +const refreshEditorPreview = () => { | |
| 27 | + window.code_snippets_editor_preview?.codemirror.refresh() | |
| 15 | 28 | } |
| 16 | 29 | |
| 17 | 30 | // Update the http referer value so that any redirections lead back to this tab. |
| 18 | 31 | const updateHttpReferer = (section: string) => { |
| 19 | - const httpReferer = document.querySelector<HTMLInputElement>('input[name=_wp_http_referer]') | |
| 32 | + const httpReferer: HTMLInputElement | null = document.querySelector('input[name=_wp_http_referer]') | |
| 20 | 33 | if (!httpReferer) { |
| 21 | 34 | console.error('could not find http referer') |
| 22 | 35 | return |
| 23 | 36 | } |
| @@ -25,8 +38,28 @@ | ||
| 25 | 38 | const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`) |
| 26 | 39 | httpReferer.value = newReferer + (newReferer === httpReferer.value ? `§ion=${section}` : '') |
| 27 | 40 | } |
| 28 | 41 | |
| 42 | +const setupHorizontalOverflow = (tabs: HTMLElement) => { | |
| 43 | + const wrapper = tabs.closest<HTMLElement>('.snippet-type-nav-wrapper') | |
| 44 | + if (!wrapper) { | |
| 45 | + return | |
| 46 | + } | |
| 47 | + | |
| 48 | + const updateFades = () => { | |
| 49 | + const remainingScroll = tabs.scrollWidth - tabs.clientWidth - tabs.scrollLeft | |
| 50 | + wrapper.classList.toggle('has-scroll-start', 1 < tabs.scrollLeft) | |
| 51 | + wrapper.classList.toggle('has-scroll-end', 1 < remainingScroll) | |
| 52 | + } | |
| 53 | + | |
| 54 | + tabs.addEventListener('scroll', updateFades, { passive: true }) | |
| 55 | + | |
| 56 | + const observer = new ResizeObserver(updateFades) | |
| 57 | + observer.observe(tabs) | |
| 58 | + observer.observe(tabs.firstElementChild ?? tabs) | |
| 59 | + updateFades() | |
| 60 | +} | |
| 61 | + | |
| 29 | 62 | export const handleSettingsTabs = () => { |
| 30 | 63 | const tabsWrapper = document.getElementById('settings-sections-tabs') |
| 31 | 64 | if (!tabsWrapper) { |
| 32 | 65 | console.error('Could not find snippets tabs') |
| @@ -32,19 +65,23 @@ | ||
| 32 | 65 | console.error('Could not find snippets tabs') |
| 33 | 66 | return |
| 34 | 67 | } |
| 35 | 68 | |
| 36 | - const tabs = tabsWrapper.querySelectorAll('.nav-tab') | |
| 69 | + setupHorizontalOverflow(tabsWrapper) | |
| 37 | 70 | |
| 71 | + const tabs = tabsWrapper.querySelectorAll('.snippet-type-link') | |
| 72 | + | |
| 38 | 73 | for (const tab of tabs) { |
| 39 | 74 | tab.addEventListener('click', event => { |
| 40 | - event.preventDefault() | |
| 41 | 75 | const section = tab.getAttribute('data-section') |
| 42 | 76 | |
| 43 | - if (section) { | |
| 44 | - selectTab(tabsWrapper, tab, section) | |
| 45 | - refreshEditorPreview(section) | |
| 46 | - updateHttpReferer(section) | |
| 77 | + if (!section || !document.querySelector(`.settings-section.${section}-settings`)) { | |
| 78 | + return | |
| 47 | 79 | } |
| 80 | + | |
| 81 | + event.preventDefault() | |
| 82 | + selectTab(tabsWrapper, tab, section) | |
| 83 | + refreshEditorPreview() | |
| 84 | + updateHttpReferer(section) | |
| 48 | 85 | }) |
| 49 | 86 | } |
| 50 | 87 | } |