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 / editor.ts

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

38 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import '../editor'
2
3 export const loadSnippetCodeEditor = () => {
4 const { codeEditor } = window.wp
5
6 const textarea = document.getElementById('snippet_code')
7
8 if (!textarea) {
9 console.error('Could not initialise CodeMirror on textarea.', textarea)
10 return
11 }
12
13 const editor = codeEditor.initialize(textarea)
14 window.code_snippets_editor = editor
15
16 const extraKeys = editor.codemirror.getOption('extraKeys')
17 const controlKey = window.navigator.platform.match('Mac') ? 'Cmd' : 'Ctrl'
18 const saveSnippet = () => document.getElementById('save_snippet')?.click()
19
20 editor.codemirror.setOption('extraKeys', {
21 ...'object' === typeof extraKeys ? extraKeys : {},
22 [`${controlKey}-S`]: saveSnippet,
23 [`${controlKey}-Enter`]: saveSnippet,
24 })
25
26 if (window.navigator.platform.match('Mac')) {
27 const helpText = document.querySelector('.editor-help-text')
28 if (helpText) {
29 helpText.className += ' platform-mac'
30 }
31 }
32
33 const directionControl = document.getElementById('snippet-code-direction') as HTMLSelectElement | null
34 directionControl?.addEventListener('change', () => {
35 window.code_snippets_editor?.codemirror.setOption('direction', 'rtl' === directionControl.value ? 'rtl' : 'ltr')
36 })
37 }
38