diff --git a/app/static/scripts/editor.js b/app/static/scripts/editor.js index e9ac0c9..a1f916b 100644 --- a/app/static/scripts/editor.js +++ b/app/static/scripts/editor.js @@ -96,42 +96,42 @@ function editor_act_on_lines(event, fn) ta.selectionEnd = end; } -/* - * Create a modal used to get input from the user when generating some M⬇ -*/ -function editor_create_modal(event, content = document.createElement("div"), fn = {}) { - // The anchor is two levels above because the event.srcElement/event.target - // are the path inside the button. - const anchor = event.currentTarget; +function editor_clear_modals(event, close = true) { + // Stop the propagation of the event + event.stopPropagation() - const modal = document.createElement("div"); - const contentDiv = document.createElement("div"); - const btnDiv = document.createElement("div"); - const validateBtn = document.createElement("button"); - const cancelBtn = document.createElement("button"); + // Reset all modal inputs + document.getElementById('img-alt-input').value = ''; + document.getElementById('img-link-input').value = ''; + document.getElementById('link-desc-input').value = ''; + document.getElementById('link-desc-input').value = ''; - modal.classList.add("modal"); + // Close all modal if requested + if (!close) { return } + const modals = document.getElementsByClassName('modal'); + for (const i of modals) {i.style.display = 'none'}; +} - validateBtn.onclick = function() { - modal.remove() +function editor_insert_link(event, link, text = "", image = false) +{ + const [editor, button, ta] = editor_event_source(event); + ta.focus(); + let indexStart = ta.selectionStart; + let indexEnd = ta.selectionEnd; - fn(event, content); - }; - validateBtn.textContent = "Valider"; - validateBtn.classList = ["bg-ok"]; + editor_clear_modals(event); - cancelBtn.onclick = function() { - modal.remove() - }; - cancelBtn.textContent = "Annuler"; - cancelBtn.classList = ["bg-error"]; + let [start, end] = editor_replace_range(ta, indexStart, indexEnd, + `[${text.length === 0 ? ta.value.substring(indexStart, indexEnd) : text}]${image ? "!" : ""}(${link})`); - contentDiv.appendChild(content); - modal.appendChild(contentDiv); - btnDiv.appendChild(validateBtn); - btnDiv.appendChild(cancelBtn); - modal.appendChild(btnDiv); - anchor.appendChild(modal); + /* Restore selection */ + if(indexStart != indexEnd) { + ta.selectionStart = start; + ta.selectionEnd = end; + } + else { + ta.selectionStart = ta.selectionEnd = start + 1; + } } function editor_set_title(event, level, diff) diff --git a/app/templates/widgets/editor.html b/app/templates/widgets/editor.html index 165f9bb..d9798bb 100644 --- a/app/templates/widgets/editor.html +++ b/app/templates/widgets/editor.html @@ -103,30 +103,47 @@ --> - - +
Aide