From 962034baabcc4b86f42397ed6cd629d5d818e6f5 Mon Sep 17 00:00:00 2001 From: Robert Hunt Date: Fri, 22 May 2020 12:44:35 +0100 Subject: [PATCH] Change behaviour so it injects existing content rather than needing to overwrite --- src/inject/inject.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/inject/inject.js b/src/inject/inject.js index 34e9ba7..c59392b 100644 --- a/src/inject/inject.js +++ b/src/inject/inject.js @@ -31,23 +31,27 @@ const semanticLabels = { const semanticCommentStructure = `**%label%decoration:** `; -const fillTextAreaValue = (textarea, value) => { +const fillTextAreaValue = (textarea, value, emptySubject = true) => { textarea.value = value; textarea.focus(); - textarea.setSelectionRange(textarea.value.length - 9, textarea.value.length); + + const length = textarea.value.length + + if (emptySubject) { + textarea.setSelectionRange(length - 9, length); + } }; const semanticButtonClickHandler = (e, { textarea, label, blocking }) => { e.preventDefault(); + const decoration = blocking ? "" : " (non-blocking)"; const semanticComment = semanticCommentStructure .replace("%label", label) - .replace("%decoration", blocking ? "" : " (non-blocking)"); - if (textarea.value && !textarea.value.endsWith(":** ")) { - if ( - window.confirm("Are you sure you want to replace the current comment?") - ) { - fillTextAreaValue(textarea, semanticComment); - } + .replace("%decoration", decoration); + const cleanedValue = textarea.value.replace(/\*\*\w+(\s\(non-blocking\))?:\*\*\s?/, ''); + + if (cleanedValue && cleanedValue !== "") { + fillTextAreaValue(textarea, semanticComment.replace(":** ", `:** ${cleanedValue}`), false) } else { fillTextAreaValue(textarea, semanticComment); }