Button logic improvements

- prevent button to override written text
- refactor to use arrow functions
This commit is contained in:
Nicolò Maria Mezzopera 2020-05-22 10:34:42 +02:00
parent 6151c90d18
commit e16445bd03
No known key found for this signature in database
GPG Key ID: 4FE1B71C61DFFB1F

@ -31,7 +31,29 @@ const semanticLabels = {
const semanticCommentStructure = `**%label%decoration:** <subject>`; const semanticCommentStructure = `**%label%decoration:** <subject>`;
function buttonGenerator(textarea, parent, label, blocking) { const fillTextAreaValue = (textarea, value) => {
textarea.value = value;
textarea.focus();
textarea.setSelectionRange(textarea.value.length - 9, textarea.value.length);
};
const semanticButtonClickHandler = (e, { textarea, label, blocking }) => {
e.preventDefault();
const semanticComment = semanticCommentStructure
.replace("%label", label)
.replace("%decoration", blocking ? "" : " (non-blocking)");
if (textarea.value && !textarea.value.includes(":** <subject>")) {
if (
window.confirm("Are you sure you want to replace the current comment?")
) {
fillTextAreaValue(textarea, semanticComment);
}
} else {
fillTextAreaValue(textarea, semanticComment);
}
};
const buttonGenerator = (textarea, parent, label, blocking) => {
const button = document.createElement("button"); const button = document.createElement("button");
const i = document.createElement("i"); const i = document.createElement("i");
i.classList.add("fa"); i.classList.add("fa");
@ -40,22 +62,13 @@ function buttonGenerator(textarea, parent, label, blocking) {
i.classList.add("blocking"); i.classList.add("blocking");
} }
button.appendChild(i); button.appendChild(i);
button.addEventListener("click", function (e) { button.addEventListener("click", (e) =>
e.preventDefault(); semanticButtonClickHandler(e, { textarea, label, blocking })
textarea.value = semanticCommentStructure );
.replace("%label", label)
.replace("%decoration", blocking ? "" : " (non-blocking)");
textarea.focus();
textarea.setSelectionRange(
textarea.value.length - 9,
textarea.value.length
);
});
parent.appendChild(button); parent.appendChild(button);
} };
function buttonPairGenerator(textarea, parent, label) { const buttonPairGenerator = (textarea, parent, label) => {
const buttonContainer = document.createElement("div"); const buttonContainer = document.createElement("div");
buttonContainer.classList.add("buttonContainer"); buttonContainer.classList.add("buttonContainer");
buttonGenerator(textarea, buttonContainer, label, false); buttonGenerator(textarea, buttonContainer, label, false);
@ -63,9 +76,9 @@ function buttonPairGenerator(textarea, parent, label) {
buttonGenerator(textarea, buttonContainer, label, true); buttonGenerator(textarea, buttonContainer, label, true);
} }
parent.appendChild(buttonContainer); parent.appendChild(buttonContainer);
} };
function addSemanticButton(element) { const addSemanticButton = (element) => {
const parent = element.closest("div"); const parent = element.closest("div");
const container = document.createElement("div"); const container = document.createElement("div");
container.id = "semanticButtonContainer"; container.id = "semanticButtonContainer";
@ -74,10 +87,10 @@ function addSemanticButton(element) {
buttonPairGenerator(element, container, label); buttonPairGenerator(element, container, label);
}); });
parent.appendChild(container); parent.appendChild(container);
} };
function main() { const main = () => {
document.addEventListener("click", function (e) { document.addEventListener("click", (e) => {
if ( if (
e.target.id === "note_note" && e.target.id === "note_note" &&
!e.target.dataset.semanticButtonInitialized !e.target.dataset.semanticButtonInitialized
@ -86,10 +99,10 @@ function main() {
addSemanticButton(e.target); addSemanticButton(e.target);
} }
}); });
} };
chrome.extension.sendMessage({}, function (response) { chrome.extension.sendMessage({}, (response) => {
const readyStateCheckInterval = setInterval(function () { const readyStateCheckInterval = setInterval(() => {
if (document.readyState === "complete") { if (document.readyState === "complete") {
clearInterval(readyStateCheckInterval); clearInterval(readyStateCheckInterval);
main(); main();