Last active
August 27, 2021 20:41
-
-
Save gosoccerboy5/aec0c9059054c13e64ca4cdc2a587be8 to your computer and use it in GitHub Desktop.
A recreation of tabs-to-spaces.html on my formatting-help repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:html"; | |
final input = querySelector("#input") as TextAreaElement; | |
final output = querySelector("#output") as TextAreaElement; | |
final radio = querySelector("#tabs2spaces") as RadioButtonInputElement; | |
final button = querySelector("#button") as ButtonElement; | |
final spacesPerTabs = querySelector("#spacesPerTabs") as InputElement; | |
// Setup elements | |
void main() { | |
spacesPerTabs.value = "4"; | |
input.focus(); | |
// Init | |
void Function(Event) update = (event) { | |
if (radio.checked as bool) { | |
// We want tabs to spaces | |
output.value = input.value?.replaceAllMapped( | |
RegExp(r"^\t*", multiLine: true), | |
// Regex matches string-start/newline, then multiple tabs | |
(n) => " " * (n[0]!.length * int.tryParse(spacesPerTabs.value ?? "4") ?? 4) | |
// Check number of tabs, and multiply that by wanted num of spaces | |
); | |
} else { | |
// We want spaces to tabs | |
output.value = input.value?.replaceAllMapped( | |
RegExp("^ *", multiLine: true), // Newline/string-start, then spaces | |
(n) => "\t"*(n[0]!.length/int.parse(spacesPerTabs.value ?? "4")).floor() | |
// Divide that by spaces per tab, and use that many tabs | |
); | |
} | |
}; | |
input.onInput.listen(update); | |
button.onClick.listen(update); | |
spacesPerTabs.onInput.listen(update); | |
radio.onChange.listen(update); | |
(querySelector("#spaces2tabs") as RadioButtonInputElement) | |
.onChange.listen(update); | |
// Add event listeners to all the elements that receive input | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Replacing tabs with spaces, or vice versa</h1> | |
<h2>Do you ever get frustrated when someone uses tabs and you don't like tabs, | |
<br>or someone insists on using spaces while you prefer tabs?</h2> | |
<p>Then this is for you!</p> | |
<textarea id="input" placeholder="Input here..."></textarea> | |
<br> | |
<button id="button">Switch!</button> | |
<div> | |
<input type="radio" name="tabsorspaces" value="tabs2spaces" id="tabs2spaces" checked> | |
<label for="tabs2spaces">Tabs to spaces</label> | |
</div> | |
<div> | |
<input type="radio" name="tabsorspaces" value="spaces2tabs" id="spaces2tabs"> | |
<label for="spaces2tabs">Spaces to tabs</label> | |
</div> | |
Spaces per tab: | |
<input type="number" id="spacesPerTabs"> | |
<br> | |
<textarea id="output" placeholder="Output here."></textarea> | |
<style> | |
html { | |
font-family: sans-serif; | |
tab-size: 4; | |
-moz-tab-size: 4; | |
-webkit-tab-size: 4; | |
margin: 10px; | |
color: white; | |
} | |
textarea { | |
width: 500px; | |
height: 200px; | |
} | |
h1, h2 { | |
font-weight: 400; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dartpad.dev/aec0c9059054c13e64ca4cdc2a587be8?null_safety=true
Made with ❤️ by Dartpad, the official online IDE/code runner for Dart, which allows even HTML to be used.