PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / services / settings / tabs.ts

tabs.ts in Code Snippets trunk, at js/services/settings/tabs.ts

88 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { updateQueryParams } from '../../utils/urls'
2
3 const selectTab = (tabsWrapper: Element, tab: Element, section: string) => {
4 // Swap the active tab class from the previously active tab to the current one.
5 tabsWrapper.querySelector('.active-type')?.classList.remove('active-type')
6 tab.classList.add('active-type')
7 updateQueryParams({ section })
8
9 // Update the current active tab attribute so that only the active tab is displayed.
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 }
20 }
21
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()
28 }
29
30 // Update the http referer value so that any redirections lead back to this tab.
31 const updateHttpReferer = (section: string) => {
32 const httpReferer: HTMLInputElement | null = document.querySelector('input[name=_wp_http_referer]')
33 if (!httpReferer) {
34 console.error('could not find http referer')
35 return
36 }
37
38 const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`)
39 httpReferer.value = newReferer + (newReferer === httpReferer.value ? `&section=${section}` : '')
40 }
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
62 export const handleSettingsTabs = () => {
63 const tabsWrapper = document.getElementById('settings-sections-tabs')
64 if (!tabsWrapper) {
65 console.error('Could not find snippets tabs')
66 return
67 }
68
69 setupHorizontalOverflow(tabsWrapper)
70
71 const tabs = tabsWrapper.querySelectorAll('.snippet-type-link')
72
73 for (const tab of tabs) {
74 tab.addEventListener('click', event => {
75 const section = tab.getAttribute('data-section')
76
77 if (!section || !document.querySelector(`.settings-section.${section}-settings`)) {
78 return
79 }
80
81 event.preventDefault()
82 selectTab(tabsWrapper, tab, section)
83 refreshEditorPreview()
84 updateHttpReferer(section)
85 })
86 }
87 }
88