|
| 1 | +export default async function ({ feature, console }) { |
| 2 | + let currentSearch = null; |
| 3 | + |
| 4 | + let colors = await ( |
| 5 | + await fetch(feature.self.getResource("tag-colors")) |
| 6 | + ).json(); |
| 7 | + |
| 8 | + ScratchTools.waitForElements(".scratchCommentTextarea", function (text) { |
| 9 | + updateTags(text); |
| 10 | + if (text.dataset.steTracker) return; |
| 11 | + text.dataset.steTracker = "tracking"; |
| 12 | + text.addEventListener("input", function () { |
| 13 | + updateTags(text); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + let ul = await ScratchTools.waitForElement( |
| 18 | + "ul[class^='react-tabs_react-tabs__tab-list_']" |
| 19 | + ); |
| 20 | + if (!ul.querySelector(".ste-clear-search")) { |
| 21 | + let clearSearch = document.createElement("div"); |
| 22 | + clearSearch.className = "ste-clear-search"; |
| 23 | + clearSearch.textContent = feature.msg("clear-search"); |
| 24 | + clearSearch.style.display = "none"; |
| 25 | + ul.appendChild(clearSearch); |
| 26 | + |
| 27 | + clearSearch.addEventListener("click", function () { |
| 28 | + currentSearch = null; |
| 29 | + clearSearch.style.display = "none"; |
| 30 | + document.querySelectorAll(".blocklyDraggable").forEach(function (el) { |
| 31 | + el.classList.remove("ste-hide-search"); |
| 32 | + if (el.parentElement.firstChild.tagName === "line") { |
| 33 | + el.parentElement.classList.remove("ste-hide-search") |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + ScratchTools.waitForElements(".blocklyDraggable", function (el) { |
| 40 | + if (!currentSearch) return; |
| 41 | + if ( |
| 42 | + [...el.querySelectorAll("textarea")].find((el) => |
| 43 | + el.value?.match(/#\w+/g)?.includes(currentSearch) |
| 44 | + ) |
| 45 | + ) { |
| 46 | + el.classList.remove("ste-hide-search"); |
| 47 | + if (el.parentElement.firstChild.tagName === "line") { |
| 48 | + el.parentElement.classList.remove("ste-hide-search") |
| 49 | + } |
| 50 | + } else { |
| 51 | + el.classList.add("ste-hide-search"); |
| 52 | + if (el.parentElement.firstChild.tagName === "line") { |
| 53 | + el.parentElement.classList.add("ste-hide-search") |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + function updateTags(text) { |
| 59 | + if (text.value.match(/#\w+/g)) { |
| 60 | + if (currentSearch) { |
| 61 | + if (text.value.match(/#\w+/g).includes(currentSearch)) { |
| 62 | + text.parentElement.parentElement.parentElement.classList.remove( |
| 63 | + "ste-hide-search" |
| 64 | + ); |
| 65 | + } else { |
| 66 | + text.parentElement.parentElement.parentElement.classList.add( |
| 67 | + "ste-hide-search" |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + if (text.parentElement.querySelector(".ste-comment-tags")) { |
| 72 | + text.parentElement.querySelector(".ste-comment-tags").innerHTML = ""; |
| 73 | + text.value.match(/#\w+/g).forEach(function (tag) { |
| 74 | + let span = document.createElement("span"); |
| 75 | + span.textContent = tag; |
| 76 | + span.style.backgroundColor = |
| 77 | + colors[tag.toLowerCase().split("")[1]]?.bg || "#434445"; |
| 78 | + span.style.color = |
| 79 | + colors[tag.toLowerCase().split("")[1]]?.text || "white"; |
| 80 | + text.parentElement |
| 81 | + .querySelector(".ste-comment-tags") |
| 82 | + .appendChild(span); |
| 83 | + |
| 84 | + span.addEventListener("click", function () { |
| 85 | + currentSearch = tag; |
| 86 | + document.querySelector(".ste-clear-search").style.display = null; |
| 87 | + document |
| 88 | + .querySelectorAll(".blocklyDraggable") |
| 89 | + .forEach(function (el) { |
| 90 | + if ( |
| 91 | + [...el.querySelectorAll("textarea")].find((el) => |
| 92 | + el.value?.match(/#\w+/g)?.includes(tag) |
| 93 | + ) |
| 94 | + ) { |
| 95 | + el.classList.remove("ste-hide-search"); |
| 96 | + if (el.parentElement.firstChild.tagName === "line") { |
| 97 | + el.parentElement.classList.remove("ste-hide-search") |
| 98 | + } |
| 99 | + } else { |
| 100 | + el.classList.add("ste-hide-search"); |
| 101 | + if (el.parentElement.firstChild.tagName === "line") { |
| 102 | + el.parentElement.classList.add("ste-hide-search") |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + } else { |
| 109 | + let div = document.createElement("div"); |
| 110 | + div.className = "ste-comment-tags"; |
| 111 | + text.parentElement.appendChild(div); |
| 112 | + text.value.match(/#\w+/g).forEach(function (tag) { |
| 113 | + let span = document.createElement("span"); |
| 114 | + span.textContent = tag; |
| 115 | + span.style.backgroundColor = |
| 116 | + colors[tag.toLowerCase().split("")[1]]?.bg || "#434445"; |
| 117 | + span.style.color = |
| 118 | + colors[tag.toLowerCase().split("")[1]]?.text || "white"; |
| 119 | + div.appendChild(span); |
| 120 | + |
| 121 | + span.addEventListener("click", function () { |
| 122 | + currentSearch = tag; |
| 123 | + document.querySelector(".ste-clear-search").style.display = null; |
| 124 | + document |
| 125 | + .querySelectorAll(".blocklyDraggable") |
| 126 | + .forEach(function (el) { |
| 127 | + if ( |
| 128 | + [...el.querySelectorAll("textarea")].find((el) => |
| 129 | + el.value?.match(/#\w+/g)?.includes(tag) |
| 130 | + ) |
| 131 | + ) { |
| 132 | + el.classList.remove("ste-hide-search"); |
| 133 | + if (el.parentElement.firstChild.tagName === "line") { |
| 134 | + el.parentElement.classList.remove("ste-hide-search") |
| 135 | + } |
| 136 | + } else { |
| 137 | + el.classList.add("ste-hide-search"); |
| 138 | + if (el.parentElement.firstChild.tagName === "line") { |
| 139 | + el.parentElement.classList.add("ste-hide-search") |
| 140 | + } |
| 141 | + } |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + } else { |
| 147 | + text.parentElement.querySelector(".ste-comment-tags")?.remove(); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments