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>`;
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 i = document.createElement("i");
i.classList.add("fa");
@ -40,22 +62,13 @@ function buttonGenerator(textarea, parent, label, blocking) {
i.classList.add("blocking");
}
button.appendChild(i);
button.addEventListener("click", function (e) {
e.preventDefault();
textarea.value = semanticCommentStructure
.replace("%label", label)
.replace("%decoration", blocking ? "" : " (non-blocking)");
textarea.focus();
textarea.setSelectionRange(
textarea.value.length - 9,
textarea.value.length
);
});
button.addEventListener("click", (e) =>
semanticButtonClickHandler(e, { textarea, label, blocking })
);
parent.appendChild(button);
}
};
function buttonPairGenerator(textarea, parent, label) {
const buttonPairGenerator = (textarea, parent, label) => {
const buttonContainer = document.createElement("div");
buttonContainer.classList.add("buttonContainer");
buttonGenerator(textarea, buttonContainer, label, false);
@ -63,9 +76,9 @@ function buttonPairGenerator(textarea, parent, label) {
buttonGenerator(textarea, buttonContainer, label, true);
}
parent.appendChild(buttonContainer);
}
};
function addSemanticButton(element) {
const addSemanticButton = (element) => {
const parent = element.closest("div");
const container = document.createElement("div");
container.id = "semanticButtonContainer";
@ -74,10 +87,10 @@ function addSemanticButton(element) {
buttonPairGenerator(element, container, label);
});
parent.appendChild(container);
}
};
function main() {
document.addEventListener("click", function (e) {
const main = () => {
document.addEventListener("click", (e) => {
if (
e.target.id === "note_note" &&
!e.target.dataset.semanticButtonInitialized
@ -86,10 +99,10 @@ function main() {
addSemanticButton(e.target);
}
});
}
};
chrome.extension.sendMessage({}, function (response) {
const readyStateCheckInterval = setInterval(function () {
chrome.extension.sendMessage({}, (response) => {
const readyStateCheckInterval = setInterval(() => {
if (document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
main();