HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux WebLive 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/wpamazingsecret/wp-content/plugins_/code-snippets/js/edit/tabs.ts
import { SnippetType } from '../types/snippet'
import { EditorConfiguration } from 'codemirror'

const EDITOR_MODES: Record<SnippetType, string> = {
	css: 'text/css',
	js: 'javascript',
	php: 'text/x-php',
	html: 'application/x-httpd-php'
}

const selectScope = (type: SnippetType, snippetForm: HTMLElement | null) => {
	const editor = window.code_snippets_editor?.codemirror
	const scope = snippetForm?.querySelector<HTMLInputElement>(`.${type}-scopes-list input:first-child`)

	if (scope) {
		scope.checked = true
	}

	editor?.setOption('lint' as keyof EditorConfiguration, 'php' === type || 'css' === type)
	if (type in EDITOR_MODES) {
		editor?.setOption('mode', EDITOR_MODES[type])
	}
}

const switchTab = (tabsContainer: HTMLElement, tab: Element) => {
	const prevActive = tabsContainer.querySelector('.nav-tab-active')
	prevActive?.setAttribute('href', '#')
	prevActive?.classList.remove('nav-tab-active')

	tab.classList.add('nav-tab-active')
	tab.removeAttribute('href')
}


export const handleSnippetTypeTabs = () => {
	const tabsContainer = document.getElementById('snippet-type-tabs')

	if (!tabsContainer) {
		return
	}

	const snippetForm = document.getElementById('snippet-form')
	const tabs = tabsContainer.querySelectorAll('.nav-tab')

	for (const tab of tabs) {
		tab.addEventListener('click', event => {
			if (tab.classList.contains('nav-tab-active') || tab.classList.contains('nav-tab-inactive')) return
			const type = tab.getAttribute('data-type') as SnippetType
			event.preventDefault()

			// Update the form styles to match the new type.
			snippetForm?.setAttribute('data-snippet-type', type)

			// Switch the active tab and change the snippet scope.
			switchTab(tabsContainer, tab)
			selectScope(type, snippetForm)
		})
	}

}