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 / edit / tabs.ts

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

61 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { SnippetType } from '../types/snippet'
2 import { EditorConfiguration } from 'codemirror'
3
4 const EDITOR_MODES: Record<SnippetType, string> = {
5 css: 'text/css',
6 js: 'javascript',
7 php: 'text/x-php',
8 html: 'application/x-httpd-php'
9 }
10
11 const selectScope = (type: SnippetType, snippetForm: HTMLElement | null) => {
12 const editor = window.code_snippets_editor?.codemirror
13 const scope = snippetForm?.querySelector<HTMLInputElement>(`.${type}-scopes-list input:first-child`)
14
15 if (scope) {
16 scope.checked = true
17 }
18
19 editor?.setOption('lint' as keyof EditorConfiguration, 'php' === type || 'css' === type)
20 if (type in EDITOR_MODES) {
21 editor?.setOption('mode', EDITOR_MODES[type])
22 }
23 }
24
25 const switchTab = (tabsContainer: HTMLElement, tab: Element) => {
26 const prevActive = tabsContainer.querySelector('.nav-tab-active')
27 prevActive?.setAttribute('href', '#')
28 prevActive?.classList.remove('nav-tab-active')
29
30 tab.classList.add('nav-tab-active')
31 tab.removeAttribute('href')
32 }
33
34
35 export const handleSnippetTypeTabs = () => {
36 const tabsContainer = document.getElementById('snippet-type-tabs')
37
38 if (!tabsContainer) {
39 return
40 }
41
42 const snippetForm = document.getElementById('snippet-form')
43 const tabs = tabsContainer.querySelectorAll('.nav-tab')
44
45 for (const tab of tabs) {
46 tab.addEventListener('click', event => {
47 if (tab.classList.contains('nav-tab-active') || tab.classList.contains('nav-tab-inactive')) return
48 const type = tab.getAttribute('data-type') as SnippetType
49 event.preventDefault()
50
51 // Update the form styles to match the new type.
52 snippetForm?.setAttribute('data-snippet-type', type)
53
54 // Switch the active tab and change the snippet scope.
55 switchTab(tabsContainer, tab)
56 selectScope(type, snippetForm)
57 })
58 }
59
60 }
61