Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active July 19, 2021 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosoccerboy5/eb896fef2a75cf4b1bb182aed65b79ae to your computer and use it in GitHub Desktop.
Save gosoccerboy5/eb896fef2a75cf4b1bb182aed65b79ae to your computer and use it in GitHub Desktop.
A recreation of trim-whitespace.html on my formatting-help repository.
import "dart:html";
final input = querySelector("#input") as TextAreaElement;
final target = querySelector("#output") as TextAreaElement;
final number = querySelector("#amount") as InputElement;
final trigger = querySelector("#trigger") as ButtonElement;
// Setup elements
void main() {
input.addEventListener("input", (Event event) {
number.value = RegExp(r"^\s*")
.firstMatch(input.value?.split("\n")[0] as String)?[0]
?.length
.toString(); // Guess the amount of spaces we want
});
input.focus(); // Focus on the textarea
number.value = "0"; // Initialize it
trigger.addEventListener("click", (Event event) {
final StringBuffer temp = new StringBuffer(); // We will expand on this
final int trimNumber;
try {
trimNumber = int.parse(number.value as String);
} on FormatException {
return; // If we reach here, the user has likely entered an invalid number
}
for (String currentLine in input.value!.split("\n")) {
if (currentLine != "") {
temp.write(currentLine.replaceFirst(
RegExp(r"^" + r"\s" * trimNumber), ""
// Match string-start, then designated number of spaces/tabs
));
}
temp.write("\n"); // Add a newline so it's not all mashed together
}
target.value = temp.toString();
// We have the result we want, then update the output textarea
});
}
<style>
textarea {
width: 500px;
height: 200px;
}
html {
color: white;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Google Sans", Roboto, Ubuntu, sans-serif;
tab-size: 4;
-moz-tab-size: 4;
-webkit-tab-size: 4;
}
</style>
<h1>Remove indentation wacky whitespace</h1>
<h2>When your JS or CSS is indented along with the HTML, it will show with awkward whitespace to the left of it.</h2>
<p>Paste it here to remove that.</p>
<textarea id="input"></textarea>
<br>
Trim <input type="number" id="amount"> off the beginning of each line
<br>
<button id="trigger">Trim!</button>
<br>
<textarea id="output"></textarea>
@gosoccerboy5
Copy link
Author

https://dartpad.dev/eb896fef2a75cf4b1bb182aed65b79ae?null_safety=true

Made with ❤️ by Dartpad, the official online IDE/code runner for Dart, which allows even HTML to be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment