2019-07-28 22:12:46 +08:00
|
|
|
<!DOCTYPE HTML>
|
2020-09-23 16:23:38 +08:00
|
|
|
<html lang="en">
|
2019-07-28 22:12:46 +08:00
|
|
|
<head>
|
2020-09-23 16:23:38 +08:00
|
|
|
<meta charset="utf-8">
|
|
|
|
|
2019-12-28 05:31:01 +08:00
|
|
|
<title>JSONEditor | Preview mode load and save</title>
|
2019-07-28 22:12:46 +08:00
|
|
|
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
|
|
|
|
|
|
|
<script src="https://bgrins.github.io/filereader.js/filereader.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
html, body {
|
|
|
|
font: 11pt sans-serif;
|
|
|
|
}
|
|
|
|
#jsoneditor {
|
|
|
|
width: 500px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-12-28 05:31:01 +08:00
|
|
|
<h1>Load and save JSON documents in Preview mode</h1>
|
2019-07-28 22:12:46 +08:00
|
|
|
<p>
|
|
|
|
This examples uses HTML5 to load/save local files.
|
|
|
|
Powered by <a href="http://bgrins.github.io/filereader.js/">FileReader.js</a> and
|
|
|
|
<a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a>.<br>
|
|
|
|
Only supported on modern browsers (Chrome, FireFox, IE10+, Safari 6.1+, Opera 15+).
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Load a JSON document: <input type="file" id="loadDocument" value="Load"/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Save a JSON document: <input type="button" id="saveDocument" value="Save" />
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
// create the editor
|
2019-12-28 05:31:01 +08:00
|
|
|
var editor = new JSONEditor(document.getElementById('jsoneditor'), {
|
|
|
|
mode: 'preview'
|
|
|
|
});
|
2019-07-28 22:12:46 +08:00
|
|
|
|
|
|
|
// Load a JSON document
|
|
|
|
FileReaderJS.setupInput(document.getElementById('loadDocument'), {
|
|
|
|
readAsDefault: 'Text',
|
|
|
|
on: {
|
|
|
|
load: function (event, file) {
|
|
|
|
editor.setText(event.target.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Save a JSON document
|
|
|
|
document.getElementById('saveDocument').onclick = function () {
|
|
|
|
// Save Dialog
|
|
|
|
fname = window.prompt("Save as...");
|
|
|
|
|
|
|
|
// Check json extension in file name
|
|
|
|
if(fname.indexOf(".")==-1){
|
|
|
|
fname = fname + ".json";
|
|
|
|
}else{
|
|
|
|
if(fname.split('.').pop().toLowerCase() == "json"){
|
|
|
|
// Nothing to do
|
|
|
|
}else{
|
|
|
|
fname = fname.split('.')[0] + ".json";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var blob = new Blob([editor.getText()], {type: 'application/json;charset=utf-8'});
|
|
|
|
saveAs(blob, fname);
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
|