diff --git a/web/src/compose_ui.js b/web/src/compose_ui.js index 63575d0bcc..526967583a 100644 --- a/web/src/compose_ui.js +++ b/web/src/compose_ui.js @@ -189,7 +189,9 @@ export function replace_syntax(old_syntax, new_syntax, $textarea = $("#compose-t // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Description // for details. + const old_text = $textarea.val(); replace($textarea[0], old_syntax, () => new_syntax, "after-replacement"); + const new_text = $textarea.val(); // When replacing content in a textarea, we need to move the cursor // to preserve its logical position if and only if the content we @@ -207,6 +209,9 @@ export function replace_syntax(old_syntax, new_syntax, $textarea = $("#compose-t // Otherwise we simply restore it to it's original position $textarea.caret(prev_caret); } + + // Return if anything was actually replaced. + return old_text !== new_text; } export function compute_placeholder_text(opts) { diff --git a/web/src/upload.js b/web/src/upload.js index 310efe231f..32c02f595a 100644 --- a/web/src/upload.js +++ b/web/src/upload.js @@ -341,12 +341,17 @@ export function setup_upload(config) { const split_url = url.split("/"); const filename = split_url.at(-1); const filename_url = "[" + filename + "](" + url + ")"; - compose_ui.replace_syntax( + const $text_area = get_item("textarea", config); + const replacement_successful = compose_ui.replace_syntax( get_translated_status(file), filename_url, - get_item("textarea", config), + $text_area, ); - compose_ui.autosize_textarea(get_item("textarea", config)); + if (!replacement_successful) { + compose_ui.insert_syntax_and_focus(filename_url, $text_area); + } + + compose_ui.autosize_textarea($text_area); // The uploaded files should be removed since uppy doesn't allow files in the store // to be re-uploaded again. diff --git a/web/tests/upload.test.js b/web/tests/upload.test.js index 11e6abd947..4aca4fabb3 100644 --- a/web/tests/upload.test.js +++ b/web/tests/upload.test.js @@ -492,6 +492,7 @@ test("copy_paste", ({override, override_rewire}) => { test("uppy_events", ({override_rewire, mock_template}) => { $("#compose_banners .upload_banner .moving_bar").css = () => {}; $("#compose_banners .upload_banner").length = 0; + override_rewire(compose_ui, "smart_insert_inline", () => {}); const callbacks = {}; let state = {};