OLD | NEW |
(Empty) | |
| 1 import 'dart:html'; |
| 2 |
| 3 import 'package:dart_style/dart_style.dart'; |
| 4 |
| 5 TextAreaElement before; |
| 6 TextAreaElement after; |
| 7 |
| 8 int width = 80; |
| 9 |
| 10 PreElement columnMarker = querySelector(".column-marker"); |
| 11 InputElement widthInput = querySelector("#width"); |
| 12 OutputElement widthOutput = querySelector("#width-output"); |
| 13 |
| 14 void main() { |
| 15 before = querySelector("#before") as TextAreaElement; |
| 16 after = querySelector("#after") as TextAreaElement; |
| 17 |
| 18 before.onKeyUp.listen((event) { |
| 19 reformat(); |
| 20 |
| 21 window.localStorage["code"] = before.value; |
| 22 }); |
| 23 |
| 24 var code = window.localStorage["code"]; |
| 25 if (code != null) before.text = code; |
| 26 |
| 27 if (window.localStorage.containsKey("width")) { |
| 28 setWidth(window.localStorage["width"]); |
| 29 } |
| 30 |
| 31 widthInput.onInput.listen((event) { |
| 32 setWidth(widthInput.value); |
| 33 window.localStorage["width"] = widthInput.value; |
| 34 }); |
| 35 |
| 36 reformat(); |
| 37 } |
| 38 |
| 39 void reformat() { |
| 40 var source = before.value; |
| 41 |
| 42 try { |
| 43 after.value = new DartFormatter(pageWidth: width).format(source); |
| 44 return; |
| 45 } on FormatterException { |
| 46 // Do nothing. |
| 47 } |
| 48 |
| 49 // Maybe it's a statement. |
| 50 try { |
| 51 after.value = new DartFormatter(pageWidth: width).formatStatement(source); |
| 52 } on FormatterException catch (err) { |
| 53 after.value = "Format failed:\n$err"; |
| 54 } |
| 55 } |
| 56 |
| 57 void setWidth(String widthString) { |
| 58 width = int.parse(widthString); |
| 59 |
| 60 widthInput.value = widthString; |
| 61 widthOutput.value = widthString; |
| 62 |
| 63 var pad = " " * width + "|"; |
| 64 columnMarker.innerHtml = "$pad $width columns" + "\n$pad" * 29; |
| 65 reformat(); |
| 66 } |
OLD | NEW |