|
30 | 30 | return username; |
31 | 31 | } |
32 | 32 |
|
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; |
40 | 36 |
|
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, ""); |
43 | 45 |
|
44 | 46 | const containerEl = document.createElement("div"); |
45 | 47 | containerEl.classList.add("github-user-notes", "border-top", "color-border-muted", "pt-3", "mt-3"); |
46 | 48 |
|
47 | 49 | const titleEl = document.createElement("h2"); |
48 | 50 | titleEl.classList.add("h4", "mb-2"); |
49 | 51 | titleEl.textContent = "Personal note"; |
50 | | - titleEl.hidden = !note; |
| 52 | + titleEl.hidden = !savedNote; |
51 | 53 |
|
52 | 54 | const textEl = document.createElement("div"); |
53 | 55 | textEl.style.whiteSpace = "pre-wrap"; |
54 | | - textEl.textContent = note; |
| 56 | + textEl.textContent = savedNote; |
55 | 57 |
|
56 | 58 | const textareaEl = document.createElement("textarea"); |
57 | 59 | textareaEl.classList.add("form-control", "FormControl-input", "FormControl-textarea"); |
58 | 60 | textareaEl.id = "github-user-notes-textarea"; |
59 | | - textareaEl.style.resize = "vertical"; |
60 | | - textareaEl.style.minHeight = "2lh"; |
61 | 61 | textareaEl.maxLength = MAX_NOTE_LENGTH; |
62 | 62 | textareaEl.placeholder = "Enter your note here…"; |
63 | | - textareaEl.value = note; |
| 63 | + textareaEl.value = savedNote; |
| 64 | + textareaEl.style.resize = "vertical"; |
| 65 | + textareaEl.style.minHeight = "2lh"; |
64 | 66 | textareaEl.hidden = true; |
65 | 67 |
|
66 | 68 | const captionEl = document.createElement("span"); |
|
71 | 73 | const buttonEl = document.createElement("button"); |
72 | 74 | buttonEl.type = "button"; |
73 | 75 | buttonEl.classList.add("btn", "btn-block", "mt-2"); |
74 | | - buttonEl.textContent = note ? "Edit note" : "Add note"; |
| 76 | + buttonEl.textContent = savedNote ? "Edit note" : "Add note"; |
75 | 77 |
|
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 | + } |
77 | 86 |
|
78 | 87 | buttonEl.addEventListener("click", () => { |
79 | 88 | if (textareaEl.hidden) { |
80 | | - textEl.hidden = true; |
81 | | - textareaEl.hidden = false; |
82 | | - captionEl.hidden = false; |
83 | | - buttonEl.textContent = "Save note"; |
| 89 | + setEditState(true); |
84 | 90 | textareaEl.focus(); |
85 | 91 | return; |
86 | 92 | } |
87 | 93 |
|
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); |
97 | 99 | }); |
98 | 100 |
|
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); |
114 | 103 | } |
115 | 104 |
|
116 | 105 | addNotesBlock(); |
117 | | - |
118 | 106 | document.addEventListener("turbo:load", addNotesBlock); |
119 | 107 | })(); |
0 commit comments