|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" |
| 5 | + integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> |
| 6 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/6.3.0/jsoneditor.min.css" |
| 7 | + integrity="sha256-YSy983pvDw/pNyv/+uiHXKNh1LlveOgHY6XAXFclM/Y=" crossorigin="anonymous" /> |
| 8 | + <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" |
| 9 | + rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> |
| 10 | + <style> |
| 11 | + #d2sJson .jsoneditor { |
| 12 | + border: 0; |
| 13 | + } |
| 14 | + </style> |
| 15 | + <title>d2s - Example</title> |
| 16 | +</head> |
| 17 | +<body> |
| 18 | + <nav class="navbar navbar-expand-lg navbar-light bg-light"> |
| 19 | + <div class="octicon octicon-clippy navbar-brand"> |
| 20 | + <i class="fa fa-fw fa-github"></i> |
| 21 | + <a href="https://github.com/dschu012">dschu012</a> / <a class="font-weight-bold" href="https://github.com/dschu012/d2s">d2s</a> |
| 22 | + </div> |
| 23 | + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> |
| 24 | + <span class="navbar-toggler-icon"></span> |
| 25 | + </button> |
| 26 | + <div class="collapse navbar-collapse" id="navbarSupportedContent"> |
| 27 | + <ul class="navbar-nav"> |
| 28 | + <li class="nav-item active"> |
| 29 | + <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> |
| 30 | + </li> |
| 31 | + </ul> |
| 32 | + </div> |
| 33 | + </nav> |
| 34 | + <div class="container-fluid"> |
| 35 | + <div class="row"> |
| 36 | + <div class="col-2 d-none d-md-block"></div> |
| 37 | + <div class="col-8 mt-2"> |
| 38 | + <div class="card bg-light"> |
| 39 | + <div class="card-body"> |
| 40 | + <form id="d2sForm"> |
| 41 | + <fieldset> |
| 42 | + <div class="form-group"> |
| 43 | + <div class="input-group"> |
| 44 | + <div class="custom-file"> |
| 45 | + <input type="file" name="d2sFile" id="d2sFile"> |
| 46 | + <label class="custom-file-label" for="d2sFile">*.d2s</label> |
| 47 | + </div> |
| 48 | + <div class="input-group-append"> |
| 49 | + <span> </span> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + <div class="form-group mt-2"> |
| 54 | + <div id="d2sJson"></div> |
| 55 | + </div> |
| 56 | + </fieldset> |
| 57 | + <div id="errors"> |
| 58 | + </div> |
| 59 | + <button type="submit" class="btn btn-primary">Save</button> |
| 60 | + </form> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + |
| 67 | + <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" |
| 68 | + integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" |
| 69 | + crossorigin="anonymous"></script> |
| 70 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/6.3.0/jsoneditor.min.js" |
| 71 | + integrity="sha256-o2/L37SBn4ufKxtZAItcvBdidVsP170IKNcqfVd3ZF4=" |
| 72 | + crossorigin="anonymous"></script> |
| 73 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" |
| 74 | + integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" |
| 75 | + crossorigin="anonymous"></script> |
| 76 | + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" |
| 77 | + integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" |
| 78 | + crossorigin="anonymous"></script> |
| 79 | + <script src="../dist/constants.bundle.js"></script> |
| 80 | + <script src="../dist/d2s.bundle.js"></script> |
| 81 | + <script> |
| 82 | + //if we arent local, use github latest |
| 83 | + if (window.d2s === undefined) { |
| 84 | + let s = document.createElement('script'); |
| 85 | + s.src = "https://github.com/dschu012/d2s/releases/latest/download/constants.bundle.min.js"; |
| 86 | + document.querySelector('body').appendChild(s); |
| 87 | + s = document.createElement('script'); |
| 88 | + s.src = "https://github.com/dschu012/d2s/releases/latest/download/d2s.bundle.min.js"; |
| 89 | + document.querySelector('body').appendChild(s); |
| 90 | + } |
| 91 | + let editor = new JSONEditor(document.querySelector('#d2sJson'), { |
| 92 | + mode: 'form', |
| 93 | + mainMenuBar: false, |
| 94 | + navigationBar: false |
| 95 | + }) |
| 96 | + let errors = document.querySelector("#errors"); |
| 97 | + |
| 98 | + //read from input type=file |
| 99 | + let input = document.querySelector('#d2sFile'); |
| 100 | + input.onchange = function (e) { |
| 101 | + let reader = new FileReader(); |
| 102 | + reader.onload = function (re) { |
| 103 | + try { |
| 104 | + d2s.read(re.target.result, constants.constants).then((response) => { |
| 105 | + editor.set(response); |
| 106 | + //textarea.style.display = "block"; |
| 107 | + //textarea.value = JSON.stringify(response, null, 2); |
| 108 | + }); |
| 109 | + } catch (e) { |
| 110 | + errors.innerHTML = `<div class="alert alert-warning alert-dismissible fade show" role="alert"> |
| 111 | + <strong>Error: </strong>${e.message} |
| 112 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> |
| 113 | + </div>` |
| 114 | + } |
| 115 | + } |
| 116 | + reader.readAsArrayBuffer(this.files[0]); |
| 117 | + this.value = null; |
| 118 | + } |
| 119 | + |
| 120 | + //write from json data |
| 121 | + let form = document.querySelector("#d2sForm"); |
| 122 | + let link = document.createElement('a'); |
| 123 | + link.style.display = 'none'; |
| 124 | + document.body.appendChild(link); |
| 125 | + form.onsubmit = function (e) { |
| 126 | + e.preventDefault(); |
| 127 | + e.stopPropagation(); |
| 128 | + let j = editor.get(); |
| 129 | + try { |
| 130 | + d2s.write(j, constants.constants).then((response) => { |
| 131 | + let blob = new Blob([response], { type: "octet/stream" }); |
| 132 | + link.href = window.URL.createObjectURL(blob); |
| 133 | + link.download = j.header.name + '.d2s'; |
| 134 | + link.click(); |
| 135 | + }); |
| 136 | + } catch (e) { |
| 137 | + errors.innerHTML = `<div class="alert alert-warning alert-dismissible fade show" role="alert"> |
| 138 | + <strong>Error: </strong>${e.message} |
| 139 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> |
| 140 | + </div>` |
| 141 | + } |
| 142 | + } |
| 143 | + </script> |
| 144 | +</body> |
| 145 | +</html> |
0 commit comments