Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mainly added the function of file download #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions code.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ $(function () {

//snake to camel
const snakeToCamel = (str) => str.replace(
/([-_][a-zA-Z])/g,
(group) => group.toUpperCase()
.replace('-', '')
.replace('_', '')
/([-_][a-zA-Z])/g,
(group) => group.toUpperCase()
.replace('-', '')
.replace('_', '')
);

//去除重复元素
Expand Down Expand Up @@ -459,6 +459,9 @@ $(function () {
checkBoxBinding('faultToleranceCheckBox', false);
checkBoxBinding('forceStringCheckBox', false);
checkBoxBinding('origJsonCheckBox', false);
// 默认允许为空
$('#nullSafeCheckBox').prop('checked', true);


$('#usingJsonKeyCheckBox').on('change', function () {
$('#jsonKeyPrivateCheckBox').prop('disabled', !(this.checked));
Expand All @@ -476,7 +479,45 @@ $(function () {
}

$('#copyFileBtn').click(function () {
copyToClipboard(resultDartCode);
let fileName = $('#fileNameTextField').val();
if(fileName === undefined || fileName === ''){
alert('Please enter the class name');
}else{
copyToClipboard(resultDartCode);
}
});


function downloadFile(filename, text) {
// 创建一个Blob实例,类型为纯文本
var blob = new Blob([text], { type: 'text/plain' });

// 创建一个指向Blob的URL
var url = URL.createObjectURL(blob);

// 创建一个a标签
var a = document.createElement('a');

// 设置a标签属性
a.href = url;
a.download = filename;

// 模拟a标签点击,触发下载
document.body.appendChild(a);
a.click();

// 清理临时DOM和对象URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
}

$('#downloadFileBtn').click(function () {
let fileName = $('#fileNameTextField').val();
if(fileName === undefined || fileName === ''){
alert('Please enter the class name');
}else{
downloadFile(fileName, resultDartCode);
}
});

})();
Expand Down
207 changes: 102 additions & 105 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,125 +7,122 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico?">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="styles/default.css">
<link href="jsoneditor.min.css" rel="stylesheet">

<title>Json To Dart Model</title>
</head>

<body>
<div class="divContainer divContainer mainContent">
<div class="divContainer header">
<!-- 标题 -->
<h1>Json To Dart Model Code Generator</h1>
<a href="https://github.com/ashamp/jsonToDartModel/tree/gh-pages" target="_blank">view source</a>
</div>
<div class="divContainer info">
<!-- 提示信息 -->
<div class="divContainer divContainer mainContent">
<div class="divContainer header">
<!-- 标题 -->
<h1>Json To Dart Model Code Generator</h1>
<a href="https://github.com/ashamp/jsonToDartModel/tree/gh-pages" target="_blank">view source</a>
</div>
<div class="divContainer info">
<!-- 提示信息 -->

</div>
<div class="divContainer mainContainer">
<!-- 主容器 -->
<div class="divContainer leftContainer">
<!-- 左容器 -->
<div class="divContainer jsonContainer">
<!-- json容器 -->
<div class="origJsonContainer">
<!-- 原始json -->
<!-- <textarea name="origJsonTextarea" id="origJsonTextarea"></textarea> -->
<div id="origJsonContainer" style="height: 100%"></div>
</div>
<div class="divContainer mainContainer">
<!-- 主容器 -->
<div class="divContainer leftContainer">
<!-- 左容器 -->
<div class="divContainer jsonContainer">
<!-- json容器 -->
<div class="origJsonContainer">
<!-- 原始json -->
<!-- <textarea name="origJsonTextarea" id="origJsonTextarea"></textarea> -->
<div id="origJsonContainer" style="height: 100%"></div>
</div>
</div>
<div class="divContainer inputsContainer">
<!-- Input容器 -->
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>JsonKey:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="usingJsonKeyCheckBox" id="usingJsonKeyCheckBox"> Json key string</p>
<p>&nbsp; | &nbsp;</p>
<p><input type="checkbox" name="jsonKeyPrivateCheckBox" id="jsonKeyPrivateCheckBox"> Private</p>
</div>
</div>
<div class="divContainer inputsContainer">
<!-- Input容器 -->
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>JsonKey:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="usingJsonKeyCheckBox" id="usingJsonKeyCheckBox"> Json key string</p>
<p>&nbsp; | &nbsp;</p>
<p><input type="checkbox" name="jsonKeyPrivateCheckBox" id="jsonKeyPrivateCheckBox"> Private</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Camel Case:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="camelCheckBox" id="camelCheckBox"> Convert snake case to camel case</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Null safe:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="nullSafeCheckBox" id="nullSafeCheckBox"> Enable null safe</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Fault tolerance:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="faultToleranceCheckBox" id="faultToleranceCheckBox"> Fault tolerance for JSON with mismatched data types</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Force String Type:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="forceStringCheckBox" id="forceStringCheckBox"> Convert all props to String type (Except bool)</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Store Original Json:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="origJsonCheckBox" id="origJsonCheckBox"> Enable store original Json</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Root Class Name:</p>
</div>
<div class="divContainer inputContainer">
<input type="text" name="className" id="classNameTextField" placeholder="Typecally you can using interface name">
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>File Name:</p>
</div>
<div class="divContainer inputContainer">
<input type="text" name="className" disabled id="fileNameTextField" placeholder="Auto generated snake case file name">
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Camel Case:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="camelCheckBox" id="camelCheckBox" > Convert snake case to camel case</p>
</div>
</div>
</div>
<div class="divContainer rightContainer">
<!-- 右容器 -->
<div class="divContainer objcHeader">
<!-- 头文件 -->
<pre><code id="dartCode" class="objectivec"></code></pre>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Null safe:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="nullSafeCheckBox" id="nullSafeCheckBox" > Enable null safe</p>
</div>
</div>
<div class="divContainer copyBtnContaner">
<button id="copyFileBtn" class="copyBtn">Copy</button>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Fault tolerance:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="faultToleranceCheckBox" id="faultToleranceCheckBox"> Fault tolerance for JSON with mismatched data types</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Force String Type:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="forceStringCheckBox" id="forceStringCheckBox"> Convert all props to String type (Except bool)</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Store Original Json:</p>
</div>
<div class="divContainer inputContainer">
<p><input type="checkbox" name="origJsonCheckBox" id="origJsonCheckBox"> Enable store original Json</p>
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>Root Class Name:</p>
</div>
<div class="divContainer inputContainer">
<input type="text" name="className" id="classNameTextField" placeholder="Typecally you can using interface name">
</div>
</div>
<div class="divContainer textInputContainer">
<div class="divContainer inputTitle">
<p>File Name:</p>
</div>
<div class="divContainer inputContainer">
<input type="text" name="className" disabled id="fileNameTextField" placeholder="Auto generated snake case file name">
</div>
</div>
</div>
</div>
<div class="divContainer rightContainer">
<!-- 右容器 -->
<div class="divContainer objcHeader">
<!-- 头文件 -->
<pre><code id="dartCode" class="objectivec"></code></pre>
</div>
<div class="divContainer copyBtnContaner">
<button id="copyFileBtn" class="copyBtn">Copy</button>
<button id="downloadFileBtn" class="copyBtn">Download</button>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/9.5.3/jsoneditor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="highlight.pack.js"></script>
<script src="code.js"></script>
<div style="display:none">
<script type="text/javascript" src="https://s4.cnzz.com/z_stat.php?id=1277869668&web_id=1277869668"></script>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/9.7.0/jsoneditor.min.js" integrity="sha512-T9isxS19cp5TyUWFgUh67kjuTcafB/OyIxav2G1whJda6cmiGidGtqZUl1Iep+Hs6tS8kvl5c+MT4xfWuUUv0w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/9.7.0/jsoneditor.css" integrity="sha512-+dwBKGKkHTEGq+HPC9veK/RMCoS7iHZPy337aQsm2iiz0nSm5yp6WkbK7+UjlTTlCADz9hliigkfmq9fhf29Wg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<link rel="stylesheet" href="styles/default.css">
<script src="highlight.pack.js"></script>
<script src="code.js"></script>
</body>

</html>