| 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 |
|