8 Commits

Author SHA1 Message Date
Caijinglong
e74dcfd54d Update file to docs 2019-08-20 11:03:49 +08:00
Caijinglong
82fe179b7f Update dist 2019-08-20 10:24:05 +08:00
Caijinglong
73dccc3453 remove log 2019-08-20 10:05:24 +08:00
Caijinglong
75df3b8fce Update input class name 2019-08-20 10:03:10 +08:00
Caijinglong
e3d864c92f 完成了 class name 的修改功能, 后续修改更友善的界面 2019-08-20 09:13:27 +08:00
Caijinglong
734a160387 update class name 2019-08-19 18:01:30 +08:00
Caijinglong
57517d26f9 format html code 2019-08-19 16:14:39 +08:00
Caijinglong
3358d0ea93 Merge pull request #2 from CaiJingLong/replace-factory-to-static-method
use static method
2019-08-19 15:50:31 +08:00
12 changed files with 3628 additions and 124 deletions

11
build.sh Executable file
View File

@@ -0,0 +1,11 @@
dist=docs
webdev build
rm -rf $dist
mkdir $dist
cp build/*.ico $dist
cp build/*.jpg $dist
cp build/*.html $dist
cp build/*.js $dist
cp build/*.css $dist
echo "copy path to $dist"

BIN
docs/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
docs/github_logo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

97
docs/index.html Normal file
View File

@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta lang="en" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="https://github.com/google/stagehand" />
<title>json2dart_for_json_serializable</title>
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script defer src="main.dart.js"></script>
</head>
<body>
<div class="lang">
<a href="index_ch.html">中文</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
</div>
<div>
<h1>
simple use
<a
href="https://pub.dartlang.org/packages/json_serializable"
target="_blank"
>json_serializable</a
>
</h1>
<div class="version">
<input
class="version"
type="radio"
value="0"
name="version"
id="v0"
checked="true"
/><label for="v0">v0.x.x</label>
<input
class="version"
type="radio"
value="1"
name="version"
id="v1"
/><label for="v1">v1.x.x</label>
</div>
<div>
<div class="title">
<span class="half_span">copy your json to left textarea</span>
<span class="half_span">
<div class="result_title">
dart class name
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">jsonKey annotation</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">use camelCase</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">use static</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea id="json" title="json" class="content_area"></textarea>
</span>
<span class="half_span">
<textarea
id="result"
title="result"
class="content_area"
></textarea>
<textarea
id="class_name"
title="class name"
class="content_area"
hidden
></textarea>
</span>
</div>
<div class="func">
<span class="half_span">
<button id="format">format</button>
</span>
<span class="half_span">
<button id="edit_class">edit class name</button>
<button id="copy">copy</button>
<button id="save">download</button>
</span>
</div>
</div>
</div>
</body>
</html>

96
docs/index_ch.html Normal file
View File

@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta lang="zh-CN" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="https://github.com/google/stagehand" />
<title>json2dart_for_json_serializable</title>
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script defer src="main.dart.js"></script>
</head>
<body>
<div class="lang">
<a href="index.html">English</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
</div>
<div>
<h1>
为了便利使用
<a href="https://pub.dartlang.org/packages/json_serializable"
>json_serializable</a
>
</h1>
<div class="version">
<input
class="version"
type="radio"
value="0"
name="version"
id="v0"
checked="true"
/><label for="v0">v0.x.x</label>
<input
class="version"
type="radio"
value="1"
name="version"
id="v1"
/><label for="v1">v1.x.x</label>
</div>
</div>
<div>
<div class="title">
<span class="half_span">将json粘贴至左边</span>
<span class="half_span">
<div class="result_title">
类名称
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">使用 JsonKey 注解</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">驼峰命名</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">使用静态方法</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea
id="json"
title="json字符串"
class="content_area"
></textarea>
</span>
<span class="half_span">
<textarea id="result" title="结果" class="content_area"></textarea>
<textarea
id="class_name"
title="class name"
class="content_area"
hidden
></textarea>
</span>
</div>
<div class="func">
<span class="half_span">
<button id="format">格式化</button>
</span>
<span class="half_span">
<button id="edit_class">编辑</button>
<button id="copy">复制</button>
<button id="save">下载</button>
</span>
</div>
</div>
</body>
</html>

3079
docs/main.dart.js Normal file

File diff suppressed because one or more lines are too long

74
docs/styles.css Normal file
View File

@@ -0,0 +1,74 @@
@import url(https://fonts.googleapis.com/css?family=Roboto);
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
}
h1 {
text-align: center;
}
.half_span {
width: 50%;
float: left;
text-align: center;
}
textarea {
width: 86%;
}
.content {
padding-top: 5vh;
}
.func .half_span {
height: 7rem;
padding-top: 1rem;
}
button {
width: 9rem;
height: 2rem;
font-size: 1rem;
}
#file_name {
font-size: 0.5rem;
padding-top: 5px;
color: mediumvioletred;
}
.lang {
padding-right: 1vw;
padding-top: 1vh;
text-align: right;
}
a {
color: blue;
text-decoration: none;
}
a:hover {
text-decoration-line: underline;
}
.version {
font-size: 10px;
text-align: center;
}
.content_area {
height: 60vh;
}
#class_name {
width: 86%;
background-color: rgba(255, 255, 0, 0.164);
height: 60vh;
}

View File

@@ -14,7 +14,7 @@ class Generator {
List<DefaultTemplate> templateList = [];
String makeDartCode() {
void refreshAllTemplates() {
var entityName = this.entityName ?? "Entity";
DefaultTemplate template;
if (version == Version.v1) {
@@ -23,7 +23,6 @@ class Generator {
template = DefaultTemplate(srcJson: jsonString, className: entityName);
}
StringBuffer resultSb = StringBuffer();
if (!template.isList) {
templateList.add(template);
refreshTemplate(template);
@@ -34,6 +33,11 @@ class Generator {
refreshTemplate(template);
}
}
String makeDartCode() {
StringBuffer resultSb = StringBuffer();
handleInputClassName();
resultSb.writeln(header);
templateList.forEach((template) {
@@ -42,21 +46,39 @@ class Generator {
return resultSb.toString();
}
void handleInputClassName() {
final text = eClassName.value;
final lines = text.split("\n");
for (var i = 0; i < templateList.length; i++) {
final template = templateList[i];
final line = lines[i].trim();
final inputKeyValue = line.split(":");
final inputName = inputKeyValue[1].trim();
template.className = inputName;
}
}
void refreshTemplate(DefaultTemplate template) {
var fieldList = template.fieldList;
fieldList.forEach((filed) {
if (filed is MapField) {
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.map), className: filed.typeString);
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.map), className: filed.typeString);
if (version == Version.v1) {
template = V1Template(srcJson: json.encode(filed.map), className: filed.typeString);
template = V1Template(
srcJson: json.encode(filed.map), className: filed.typeString);
}
templateList.add(template);
refreshTemplate(template);
} else if (filed is ListField) {
if (filed.childIsObject) {
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.list[0]), className: filed.typeName);
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.list[0]), className: filed.typeName);
if (version == Version.v1) {
template = V1Template(srcJson: json.encode(filed.list[0]), className: filed.typeName);
template = V1Template(
srcJson: json.encode(filed.list[0]), className: filed.typeName);
}
templateList.add(template);
refreshTemplate(template);
@@ -67,13 +89,23 @@ class Generator {
String get fileName => camelCase2UnderScoreCase(entityName);
static const String importString = "import 'package:json_annotation/json_annotation.dart';";
static const String importString =
"import 'package:json_annotation/json_annotation.dart';";
String get header => """$importString
part '$fileName.g.dart';
part '$fileName.g.dart';
""";
""";
String getClassNameText() {
final sb = StringBuffer();
for (final template in templateList) {
String text = "${template.className} : ${template.className}";
sb.writeln(text);
}
return sb.toString();
}
}
String camelCase2UnderScoreCase(String name) {

View File

@@ -28,10 +28,15 @@ enum Version { v0, v1 }
Version v = Version.v0;
TextAreaElement eResult;
TextAreaElement eClassName;
Element editButton;
void main() async {
isChinese = await _isChinese();
var dataHelper = CookieHelper();
TextAreaElement jsonInput = querySelector("#json");
eClassName = querySelector("#class_name");
eResult = querySelector("#result");
jsonInput.value = dataHelper.loadJsonString();
jsonInput.onInput.listen((event) {
@@ -163,6 +168,15 @@ void main() async {
saveLink.download = downloadFileName;
saveLink.click();
});
editButton = querySelector("#edit_class");
editButton.onClick.listen((event) {
showOrClassName();
});
eClassName.onInput.listen((event) {
refreshClassNameChange(eClassName.text);
});
}
Future<bool> _isChinese() async {
@@ -185,7 +199,7 @@ Future<bool> _isChinese() async {
}
bool isChinese = false;
Generator generator;
void refreshData() async {
TextAreaElement jsonInput = querySelector("#json");
var string = jsonInput.value;
@@ -208,7 +222,18 @@ void refreshData() async {
entityClassName = entityName;
}
var generator = Generator(string, entityClassName, v);
generator = Generator(string, entityClassName, v);
generator.refreshAllTemplates();
writeClassNameText(generator);
makeCode(generator);
}
void writeClassNameText(Generator generator) {
String names = generator.getClassNameText();
eClassName.text = names;
}
void makeCode(Generator generator) {
var dartCode = generator.makeDartCode();
var dartFileName = ("${generator.fileName}.dart");
downloadFileName = dartFileName;
@@ -219,10 +244,14 @@ void refreshData() async {
} else {
filePrefix = "your dart file name is:";
}
// print(filePrefix);
querySelector("#file_name").text = "$filePrefix $dartFileName";
final resultName = "$filePrefix $dartFileName";
writeToResult(resultName, dartCode);
}
result.value = dartCode;
void writeToResult(String resultName, String resultText) {
// print(filePrefix);
querySelector("#file_name").text = resultName;
eResult.value = resultText;
}
String formatJson(String jsonString) {
@@ -230,3 +259,26 @@ String formatJson(String jsonString) {
var prettyString = JsonEncoder.withIndent(" ").convert(map);
return prettyString;
}
void showOrClassName() {
final currentState = !eClassName.hidden;
eClassName.hidden = !eClassName.hidden;
eResult.hidden = !eResult.hidden;
editButton.text = currentState ? "name" : "result";
}
void refreshClassNameChange(String text) {
final value = generator.makeDartCode();
String filePrefix;
if (isChinese) {
filePrefix = "应该使用的文件名为:";
} else {
filePrefix = "your dart file name is:";
}
var dartFileName = ("${generator.fileName}.dart");
downloadFileName = dartFileName;
final resultName = "$filePrefix $dartFileName";
writeToResult(resultName, value);
}

View File

@@ -1,69 +1,97 @@
<!DOCTYPE html>
<html>
<head>
<head>
<meta charset="utf-8" />
<meta lang="en" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="https://github.com/google/stagehand" />
<title>json2dart_for_json_serializable</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script defer src="main.dart.js"></script>
</head>
</head>
<body>
<body>
<div class="lang">
<a href="index_ch.html">中文</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
<a href="index_ch.html">中文</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
</div>
<div>
<h1>simple use
<a href="https://pub.dartlang.org/packages/json_serializable" target="_blank">json_serializable</a>
</h1>
<div class="version">
<input class="version" type="radio" value="0" name="version" id="v0" checked="true">v0.x.x</input>
<input class="version" type="radio" value="1" name="version" id="v1">v1.x.x</input>
<h1>
simple use
<a
href="https://pub.dartlang.org/packages/json_serializable"
target="_blank"
>json_serializable</a
>
</h1>
<div class="version">
<input
class="version"
type="radio"
value="0"
name="version"
id="v0"
checked="true"
/><label for="v0">v0.x.x</label>
<input
class="version"
type="radio"
value="1"
name="version"
id="v1"
/><label for="v1">v1.x.x</label>
</div>
<div>
<div class="title">
<span class="half_span">copy your json to left textarea</span>
<span class="half_span">
<div class="result_title">
dart class name
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">jsonKey annotation</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">use camelCase</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">use static</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div>
<div class="title">
<span class="half_span">copy your json to left textarea</span>
<span class="half_span">
<div class="result_title">
dart class name
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">jsonKey annotation</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">use camelCase</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">use static</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea id="json" title="json" class="content_area"></textarea>
</span>
<span class="half_span">
<textarea id="result" title="result" class="content_area"></textarea>
</span>
</div>
<div class="func">
<span class="half_span">
<button id="format">format</button>
</span>
<span class="half_span">
<button id="copy">copy</button>
<button id="save">download</button>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea id="json" title="json" class="content_area"></textarea>
</span>
<span class="half_span">
<textarea
id="result"
title="result"
class="content_area"
></textarea>
<textarea
id="class_name"
title="class name"
class="content_area"
hidden
></textarea>
</span>
</div>
</body>
</html>
<div class="func">
<span class="half_span">
<button id="format">format</button>
</span>
<span class="half_span">
<button id="edit_class">edit class name</button>
<button id="copy">copy</button>
<button id="save">download</button>
</span>
</div>
</div>
</div>
</body>
</html>

View File

@@ -1,70 +1,96 @@
<!DOCTYPE html>
<html>
<head>
<head>
<meta charset="utf-8" />
<meta lang="zh-CN" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="https://github.com/google/stagehand" />
<title>json2dart_for_json_serializable</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="favicon.ico" />
<script defer src="main.dart.js"></script>
</head>
</head>
<body>
<body>
<div class="lang">
<a href="index.html">English</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
<a href="index.html">English</a>
<a href="https://github.com/caijinglong/json2dart">
<img src="github_logo.jpg" width="50px" />
</a>
</div>
<div>
<h1>为了便利使用
<a href="https://pub.dartlang.org/packages/json_serializable">json_serializable</a>
</h1>
<div class="version">
<input class="version" type="radio" value="0" name="version" id="v0" checked="true">v0.x.x</input>
<input class="version" type="radio" value="1" name="version" id="v1">v1.x.x</input>
</div>
<h1>
为了便利使用
<a href="https://pub.dartlang.org/packages/json_serializable"
>json_serializable</a
>
</h1>
<div class="version">
<input
class="version"
type="radio"
value="0"
name="version"
id="v0"
checked="true"
/><label for="v0">v0.x.x</label>
<input
class="version"
type="radio"
value="1"
name="version"
id="v1"
/><label for="v1">v1.x.x</label>
</div>
</div>
<div>
<div class="title">
<span class="half_span">将json粘贴至左边</span>
<span class="half_span">
<div class="result_title">
类名称
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">使用 JsonKey 注解</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">驼峰命名</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">使用静态方法</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea id="json" title="json字符串" class="content_area"></textarea>
</span>
<span class="half_span">
<textarea id="result" title="结果" class="content_area"></textarea>
</span>
</div>
<div class="func">
<span class="half_span">
<button id="format">格式化</button>
</span>
<span class="half_span">
<button id="copy">复制</button>
<button id="save">下载</button>
</span>
</div>
<div class="title">
<span class="half_span">将json粘贴至左边</span>
<span class="half_span">
<div class="result_title">
类名称
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">使用 JsonKey 注解</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">驼峰命名</span>
<input type="checkbox" id="use_static" />
<span id="useStaticLabel">使用静态方法</span>
</div>
<div id="file_name"></div>
</span>
</div>
<div class="content">
<span class="half_span">
<textarea
id="json"
title="json字符串"
class="content_area"
></textarea>
</span>
<span class="half_span">
<textarea id="result" title="结果" class="content_area"></textarea>
<textarea
id="class_name"
title="class name"
class="content_area"
hidden
></textarea>
</span>
</div>
<div class="func">
<span class="half_span">
<button id="format">格式化</button>
</span>
<span class="half_span">
<button id="edit_class">编辑</button>
<button id="copy">复制</button>
<button id="save">下载</button>
</span>
</div>
</div>
</body>
</html>
</body>
</html>

View File

@@ -6,7 +6,7 @@ body {
height: 100%;
margin: 0;
padding: 0;
font-family: "Roboto", sans-serif;
font-family: 'Roboto', sans-serif;
}
h1 {
@@ -21,7 +21,6 @@ h1 {
textarea {
width: 86%;
height: 70vh;
}
.content {
@@ -34,7 +33,7 @@ textarea {
}
button {
width: 6rem;
width: 9rem;
height: 2rem;
font-size: 1rem;
}
@@ -63,3 +62,13 @@ a:hover {
font-size: 10px;
text-align: center;
}
.content_area {
height: 60vh;
}
#class_name {
width: 86%;
background-color: rgba(255, 255, 0, 0.164);
height: 60vh;
}