PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
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 3.10.0-beta.1, at js/services/settings/tabs.ts

59 lines 1.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
18 // Refresh the editor preview if we're viewing the editor section.
19 const refreshEditorPreview = (section: string) => {
20 if ('editor' === section) {
21 window.code_snippets_editor_preview?.codemirror.refresh()
22 }
23 }
24
25 // Update the http referer value so that any redirections lead back to this tab.
26 const updateHttpReferer = (section: string) => {
27 const httpReferer: HTMLInputElement | null = document.querySelector('input[name=_wp_http_referer]')
28 if (!httpReferer) {
29 console.error('could not find http referer')
30 return
31 }
32
33 const newReferer = httpReferer.value.replace(/(?<base>[&?]section=)[^&]+/, `$1${section}`)
34 httpReferer.value = newReferer + (newReferer === httpReferer.value ? `&section=${section}` : '')
35 }
36
37 export const handleSettingsTabs = () => {
38 const tabsWrapper = document.getElementById('settings-sections-tabs')
39 if (!tabsWrapper) {
40 console.error('Could not find snippets tabs')
41 return
42 }
43
44 const tabs = tabsWrapper.querySelectorAll('.snippet-type-link')
45
46 for (const tab of tabs) {
47 tab.addEventListener('click', event => {
48 event.preventDefault()
49 const section = tab.getAttribute('data-section')
50
51 if (section) {
52 selectTab(tabsWrapper, tab, section)
53 refreshEditorPreview(section)
54 updateHttpReferer(section)
55 }
56 })
57 }
58 }
59