| 1 |
export const handleFormSubmitValidation = () => { |
| 2 |
const form = document.getElementById('snippet-form') |
| 3 |
const editor = window.code_snippets_editor?.codemirror |
| 4 |
const strings = window.code_snippets_edit_i18n |
| 5 |
const snippetName = document.querySelector('input[name=snippet_name]') as HTMLInputElement |
| 6 |
|
| 7 |
if (!form || !editor || !snippetName) { |
| 8 |
return |
| 9 |
} |
| 10 |
|
| 11 |
form.addEventListener('submit', (event: SubmitEvent) => { |
| 12 |
const missingTitle = '' === snippetName.value.trim() |
| 13 |
const missingCode = '' === editor.getValue().trim() |
| 14 |
|
| 15 |
const message = missingTitle ? |
| 16 |
missingCode ? strings.missing_title_code : strings.missing_title : |
| 17 |
missingCode ? strings.missing_code : '' |
| 18 |
|
| 19 |
if (event?.submitter?.id.startsWith('save_snippet') && message && !confirm(message)) { |
| 20 |
event.preventDefault() |
| 21 |
} |
| 22 |
}) |
| 23 |
} |
| 24 |
|