Skip to content

Commit cd0cd82

Browse files
committed
refactor: simplify note editing logic
1 parent d7e1802 commit cd0cd82

1 file changed

Lines changed: 33 additions & 45 deletions

File tree

‎github-user-notes.user.js‎

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,39 @@
3030
return username;
3131
}
3232

33-
function getNote(username) {
34-
return GM_getValue(`${KEY_PREFIX}${username}`, "");
35-
}
36-
function setNote(username, note) {
37-
const key = `${KEY_PREFIX}${username}`;
38-
note ? GM_setValue(key, note) : GM_deleteValue(key);
39-
}
33+
function addNotesBlock() {
34+
if (!location.pathname.match(/^\/[^/]+\/?$/)) return;
35+
if (document.querySelector(".github-user-notes")) return;
4036

41-
function createNotesBlock(username) {
42-
const note = getNote(username);
37+
const profileArea = document.querySelector(".js-profile-editable-area.d-flex.flex-column.d-md-block");
38+
if (!profileArea) return;
39+
40+
const username = getUsername();
41+
if (!username) return;
42+
43+
const KEY = `${KEY_PREFIX}${username}`;
44+
const savedNote = GM_getValue(KEY, "");
4345

4446
const containerEl = document.createElement("div");
4547
containerEl.classList.add("github-user-notes", "border-top", "color-border-muted", "pt-3", "mt-3");
4648

4749
const titleEl = document.createElement("h2");
4850
titleEl.classList.add("h4", "mb-2");
4951
titleEl.textContent = "Personal note";
50-
titleEl.hidden = !note;
52+
titleEl.hidden = !savedNote;
5153

5254
const textEl = document.createElement("div");
5355
textEl.style.whiteSpace = "pre-wrap";
54-
textEl.textContent = note;
56+
textEl.textContent = savedNote;
5557

5658
const textareaEl = document.createElement("textarea");
5759
textareaEl.classList.add("form-control", "FormControl-input", "FormControl-textarea");
5860
textareaEl.id = "github-user-notes-textarea";
59-
textareaEl.style.resize = "vertical";
60-
textareaEl.style.minHeight = "2lh";
6161
textareaEl.maxLength = MAX_NOTE_LENGTH;
6262
textareaEl.placeholder = "Enter your note here…";
63-
textareaEl.value = note;
63+
textareaEl.value = savedNote;
64+
textareaEl.style.resize = "vertical";
65+
textareaEl.style.minHeight = "2lh";
6466
textareaEl.hidden = true;
6567

6668
const captionEl = document.createElement("span");
@@ -71,49 +73,35 @@
7173
const buttonEl = document.createElement("button");
7274
buttonEl.type = "button";
7375
buttonEl.classList.add("btn", "btn-block", "mt-2");
74-
buttonEl.textContent = note ? "Edit note" : "Add note";
76+
buttonEl.textContent = savedNote ? "Edit note" : "Add note";
7577

76-
containerEl.append(titleEl, textEl, textareaEl, captionEl, buttonEl);
78+
function setEditState(isEditing) {
79+
const hasNote = !!textEl.textContent.trim();
80+
textareaEl.hidden = !isEditing;
81+
captionEl.hidden = !isEditing;
82+
textEl.hidden = isEditing;
83+
titleEl.hidden = isEditing || !hasNote;
84+
buttonEl.textContent = isEditing ? "Save note" : hasNote ? "Edit note" : "Add note";
85+
}
7786

7887
buttonEl.addEventListener("click", () => {
7988
if (textareaEl.hidden) {
80-
textEl.hidden = true;
81-
textareaEl.hidden = false;
82-
captionEl.hidden = false;
83-
buttonEl.textContent = "Save note";
89+
setEditState(true);
8490
textareaEl.focus();
8591
return;
8692
}
8793

88-
const note = textareaEl.value.trim();
89-
setNote(username, note);
90-
textareaEl.value = note;
91-
textEl.textContent = note;
92-
textareaEl.hidden = true;
93-
captionEl.hidden = true;
94-
textEl.hidden = false;
95-
titleEl.hidden = !note;
96-
buttonEl.textContent = note ? "Edit note" : "Add note";
94+
const newNote = textareaEl.value.trim();
95+
newNote ? GM_setValue(KEY, newNote) : GM_deleteValue(KEY);
96+
textareaEl.value = newNote;
97+
textEl.textContent = newNote;
98+
setEditState(false);
9799
});
98100

99-
return containerEl;
100-
}
101-
102-
function addNotesBlock() {
103-
if (!location.pathname.match(/^\/[^/]+\/?$/)) return;
104-
if (document.querySelector(".github-user-notes")) return;
105-
106-
const profileArea = document.querySelector(".js-profile-editable-area.d-flex.flex-column.d-md-block");
107-
if (!profileArea) return;
108-
109-
const username = getUsername();
110-
if (!username) return;
111-
112-
const notesBlock = createNotesBlock(username);
113-
profileArea.after(notesBlock);
101+
containerEl.append(titleEl, textEl, textareaEl, captionEl, buttonEl);
102+
profileArea.after(containerEl);
114103
}
115104

116105
addNotesBlock();
117-
118106
document.addEventListener("turbo:load", addNotesBlock);
119107
})();

0 commit comments

Comments
 (0)