|
| 1 | +// Create the initial formatter element |
| 2 | +var formatter; |
| 3 | + |
| 4 | +// Everything is customizable here. |
| 5 | +// (local images don't work yet, but who cares. |
| 6 | +// this is the internet.) |
| 7 | +var tags = [ |
| 8 | + { |
| 9 | + "name": "bold", |
| 10 | + "tag": "b", |
| 11 | + "src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/bold.svg", |
| 12 | + "fillers": ["[b]", "[/b]"], |
| 13 | + "formatter": function(part1, part2) { |
| 14 | + return "<b>" + part2 + "</b>"; |
| 15 | + } |
| 16 | + }, |
| 17 | + { |
| 18 | + "name": "italics", |
| 19 | + "tag": "i", |
| 20 | + "src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/italic.svg", |
| 21 | + "fillers": ["[i]", "[/i]"], |
| 22 | + "formatter": function(part1, part2) { |
| 23 | + return "<i>" + part2 + "</i>"; |
| 24 | + } |
| 25 | + }, |
| 26 | + { |
| 27 | + "name": "underline", |
| 28 | + "tag": "u", |
| 29 | + "src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/underline.svg", |
| 30 | + "fillers": ["[u]", "[/u]"], |
| 31 | + "formatter": function(part1, part2) { |
| 32 | + return "<u>" + part2 + "</u>"; |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + "name": "color", |
| 37 | + "tag": "color", |
| 38 | + "src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/font-color.svg", |
| 39 | + "fillers": ["[color=red]", "[/color]"], |
| 40 | + "formatter": function(part1, part2) { |
| 41 | + return "<span style='color:" + part1 + "'>" + part2 + "</span>"; |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + "name": "link", |
| 46 | + "tag": "link", |
| 47 | + "src": "https://raw.githubusercontent.com/Remix-Design/RemixIcon/master/icons/Editor/link.svg", |
| 48 | + "fillers": ["[link=", "]Link[/link]"], |
| 49 | + "formatter": function(part1, part2) { |
| 50 | + return "<a href='" + part1 + "' target='_newtab'>" + part2 + "</a>"; |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + "name": "easteregg", |
| 55 | + "tag": "easteregg", |
| 56 | + "dontshow": true, |
| 57 | + "fillers": ["[easteregg]"], |
| 58 | + "formatter": function(part1, part2) { |
| 59 | + return "<i>Yes? No. Or is it?</i>"; |
| 60 | + } |
| 61 | + } |
| 62 | +] |
| 63 | + |
| 64 | +// Firstly, initialize the formatter, and its icons |
| 65 | +window.onload = function() { |
| 66 | + document.getElementsByName("content")[0].placeholder = "Click here to activate ScratchFormat"; |
| 67 | + |
| 68 | + formatter = document.createElement("div"); |
| 69 | + formatter.id = "formatter"; |
| 70 | + for (var t = 0; t < tags.length; t++) { |
| 71 | + if (tags[t].dontshow) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + var icon = document.createElement("img"); |
| 76 | + icon.src = tags[t].src; |
| 77 | + icon.fillers = tags[t].fillers; |
| 78 | + |
| 79 | + // This may look janky, but with Chrome extensions, |
| 80 | + // Everything is jank. Basically I have to set custom |
| 81 | + // properties to the element in order to get data without |
| 82 | + // having functions, which would require some "injection" |
| 83 | + // garbage. |
| 84 | + icon.onclick = function(event) { |
| 85 | + var textarea = event.target.parentElement.parentElement.children[1]; |
| 86 | + var fillers = event.target.fillers; |
| 87 | + |
| 88 | + // Grab the selected text |
| 89 | + var selection = textarea.value.substring( |
| 90 | + textarea.selectionStart, |
| 91 | + textarea.selectionEnd |
| 92 | + ); |
| 93 | + |
| 94 | + // Generate new text, if just 1 filler, ex [br], don't attempt |
| 95 | + // to use second part. |
| 96 | + var newText = textarea.value.substring(0, textarea.selectionStart) |
| 97 | + if (fillers.length > 1) { |
| 98 | + newText += fillers[0] + selection + fillers[1]; |
| 99 | + } else { |
| 100 | + newText += fillers[0]; |
| 101 | + } |
| 102 | + |
| 103 | + newText += textarea.value.substring(textarea.selectionEnd); |
| 104 | + |
| 105 | + textarea.value = newText; |
| 106 | + textarea.focus(); |
| 107 | + } |
| 108 | + |
| 109 | + formatter.appendChild(icon); |
| 110 | + } |
| 111 | + |
| 112 | + // Move formatter if user clicks on textarea. |
| 113 | + document.body.onclick = function(event) { |
| 114 | + if (event.target.name == "content") { |
| 115 | + event.target.parentElement.prepend(formatter); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Initial background formatting loop. |
| 120 | + setInterval(function() { |
| 121 | + format(); |
| 122 | + }, 300); |
| 123 | +} |
| 124 | + |
| 125 | +var oldComments = 0; |
| 126 | +function format() { |
| 127 | + // Quit if we already formatted those comments. |
| 128 | + // Checks for last vs new length. |
| 129 | + var comments = document.getElementsByClassName("content"); |
| 130 | + if (oldComments == comments.length) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + oldComments = comments.length |
| 135 | + |
| 136 | + for (var c = 0; c < comments.length; c++) { |
| 137 | + comments[c].style.whiteSpace = "pre"; |
| 138 | + comments[c].innerHTML = parse(comments[c].innerHTML); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Custom regex parser. Easy to maintain. |
| 143 | +function parse(text) { |
| 144 | + var startBracket = "[\\(|\\[]"; |
| 145 | + var endBracket = "[\\)|\\]]"; |
| 146 | + |
| 147 | + for (var t = 0; t < tags.length; t++) { |
| 148 | + |
| 149 | + // First part of tag |
| 150 | + var regex = ""; |
| 151 | + regex += startBracket; |
| 152 | + regex += tags[t].tag; |
| 153 | + regex += "(=(.*))*"; |
| 154 | + regex += endBracket; |
| 155 | + |
| 156 | + // If just 1 tag (Ex [br]) |
| 157 | + if (tags[t].fillers.length > 1) { |
| 158 | + regex += "(.*)"; |
| 159 | + |
| 160 | + // Second part of tag |
| 161 | + regex += startBracket; |
| 162 | + regex += "\/"; |
| 163 | + regex += tags[t].tag; |
| 164 | + regex += endBracket; |
| 165 | + } |
| 166 | + |
| 167 | + regex = new RegExp(regex, "gm"); |
| 168 | + text = text.replace(regex, tags[t].formatter("$2", "$3")); |
| 169 | + } |
| 170 | + |
| 171 | + text = text.replace(/^(\n| )+/gm, ""); |
| 172 | + |
| 173 | + return text; |
| 174 | +} |
0 commit comments