PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.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 / editor-preview.ts

editor-preview.ts in Code Snippets 3.10.1, at js/services/settings/editor-preview.ts

56 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import '../../entries/editor'
2
3 const parseSelect = (select: HTMLSelectElement) => select.options[select.selectedIndex].value
4 const parseCheckbox = (checkbox: HTMLInputElement) => checkbox.checked
5 const parseNumber = (input: HTMLInputElement) => parseInt(input.value, 10)
6
7 const initialiseCodeMirror = () => {
8 const { codeEditor } = window.wp
9 const textarea = document.getElementById('code_snippets_editor_preview')
10
11 if (textarea && codeEditor) {
12 window.code_snippets_editor_preview = codeEditor.initialize(textarea)
13 return window.code_snippets_editor_preview.codemirror
14 }
15
16 console.error('Could not initialise CodeMirror on textarea.', textarea)
17 return undefined
18 }
19
20 export const handleEditorPreviewUpdates = () => {
21 const editor = initialiseCodeMirror()
22 const editorSettings = window.code_snippets_editor_settings
23
24 for (const setting of editorSettings) {
25 const element = document.querySelector(`[name="code_snippets_settings[editor][${setting.name}]"]`)
26
27 element?.addEventListener('change', () => {
28 const opt = setting.codemirror
29
30 const value = (() => {
31 switch (setting.type) {
32 case 'select':
33 return parseSelect(<HTMLSelectElement> element)
34 case 'checkbox':
35 return parseCheckbox(<HTMLInputElement> element)
36 case 'number':
37 return parseNumber(<HTMLInputElement> element)
38 default:
39 return null
40 }
41 })()
42
43 if (null !== value) {
44 if ('font_size' === setting.name) {
45 const codeElement = document.querySelector('.CodeMirror-code')
46 if (codeElement && codeElement instanceof HTMLElement) {
47 codeElement.style.fontSize = `${value}px`
48 }
49 } else {
50 editor?.setOption(opt, value)
51 }
52 }
53 })
54 }
55 }
56