PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.1
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 3.0.1 All 64 releases
code-snippets / js / settings / tabs.ts

tabs.ts in Code Snippets 3.2.1, at js/settings/tabs.ts

51 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const selectTab = (tabsWrapper: Element, tab: Element, section: string) => {
2 // 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
6 // Update the current active tab attribute so that only the active tab is displayed.
7 tabsWrapper.closest('.wrap')?.setAttribute('data-active-tab', section)
8 }
9
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 }
15 }
16
17 // Update the http referer value so that any redirections lead back to this tab.
18 const updateHttpReferer = (section: string) => {
19 const httpReferer = document.querySelector<HTMLInputElement>('input[name=_wp_http_referer]')
20 if (!httpReferer) {
21 console.error('could not find http referer')
22 return
23 }
24
25 const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`)
26 httpReferer.value = newReferer + (newReferer === httpReferer.value ? `&section=${section}` : '')
27 }
28
29 export const handleSettingsTabs = () => {
30 const tabsWrapper = document.getElementById('settings-sections-tabs')
31 if (!tabsWrapper) {
32 console.error('Could not find snippets tabs')
33 return
34 }
35
36 const tabs = tabsWrapper.querySelectorAll('.nav-tab') ?? []
37
38 for (const tab of tabs) {
39 tab.addEventListener('click', event => {
40 event.preventDefault()
41 const section = tab.getAttribute('data-section')
42
43 if (section) {
44 selectTab(tabsWrapper, tab, section)
45 refreshEditorPreview(section)
46 updateHttpReferer(section)
47 }
48 })
49 }
50 }
51