Merge branch 'change-behaviour-to-keep-existing-content' into 'master'

Change behaviour so it injects existing content rather than needing to overwrite

See merge request conventionalcomments/conventional-comments-button!4
This commit is contained in:
Nicolò Maria Mezzopera 2020-05-22 12:32:23 +00:00
commit f564bc1cdc

@ -31,23 +31,27 @@ const semanticLabels = {
const semanticCommentStructure = `**%label%decoration:** <subject>`;
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(":** <subject>")) {
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 !== "<subject>") {
fillTextAreaValue(textarea, semanticComment.replace(":** <subject>", `:** ${cleanedValue}`), false)
} else {
fillTextAreaValue(textarea, semanticComment);
}