Skip to content

Commit 34ad51f

Browse files
committed
Added functionality to load local json file
1 parent 28dced2 commit 34ad51f

File tree

8 files changed

+72
-44
lines changed

8 files changed

+72
-44
lines changed

dist/rapidoc-min.js

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rapidoc-min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,5 @@ Style the element using standard `css`. Apply borders, padding, width, height et
8080
</body>
8181
```
8282

83-
84-
8583
- [Change Attributes using JavaScript](example6.html)
8684

docs/example6.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
}
4848
else if(attr==="spec-url"){
4949
arr = [
50-
"https://api.apis.guru/v2/specs/amazonaws.com/acm/2015-12-08/swagger.json"
50+
"https://api.apis.guru/v2/specs/amazonaws.com/acm/2015-12-08/swagger.json"
5151
,"https://api.apis.guru/v2/specs/bitbucket.org/2.0/swagger.json",
5252
,"https://api.apis.guru/v2/specs/appveyor.com/0.20181229.0/swagger.json"
5353
,"https://raw.githubusercontent.com/APIs-guru/unofficial_openapi_specs/master/github.com/v3/swagger.yaml"

docs/rapidoc-min.js

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rapidoc.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class RapiDoc extends LitElement {
148148
</div>
149149
<div style="margin: 0px 8px;display:flex">
150150
<input id="spec-url" type="text" class="large header-input" placeholder="Spec URL" value="${this.specUrl}" @change="${this.onSepcUrlChange}">
151+
<input id="spec-file" type="file" style="display:none" value="${this.specFile}" @change="${this.onSepcFileChange}" >
152+
<button class="m-btn" style="margin-left:10px;" @click="${this.onFileLoadClick}"> LOCAL JSON FILE </button>
151153
</div>
152154
<div style="flex:1"></div>
153155
</div>`}
@@ -196,10 +198,8 @@ class RapiDoc extends LitElement {
196198

197199
static get properties() {
198200
return {
199-
specUrl: {
200-
type: String,
201-
attribute: 'spec-url',
202-
},
201+
specUrl : { type: String, attribute: 'spec-url',},
202+
specFile: { type: String, attribute: false,},
203203
headingText : {type: String, attribute: 'heading-text' },
204204
headerColor : {type: String, attribute: 'header-color' },
205205
primaryColor: {type: String, attribute: 'primary-color'},
@@ -216,21 +216,42 @@ class RapiDoc extends LitElement {
216216
}
217217
attributeChangedCallback(name, oldVal, newVal) {
218218
if (name=='spec-url'){
219+
console.log("url changed")
219220
if (oldVal !== newVal){
220221
this.loadSpec(newVal);
221222
}
222223
}
223224
super.attributeChangedCallback(name, oldVal, newVal);
224225
}
225226

226-
onOpenSpecClicked(e) {
227-
this.setAttribute('spec-url', this.shadowRoot.getElementById('spec-url').value);
227+
onSepcUrlChange(e){
228+
this.setAttribute('spec-url', this.shadowRoot.getElementById('spec-url').value);
228229
}
229230

230-
onSepcUrlChange(e){
231-
if (this.specUrl){
232-
this.setAttribute('spec-url', this.shadowRoot.getElementById('spec-url').value);
231+
onSepcFileChange(e){
232+
let me = this;
233+
this.setAttribute('spec-file', this.shadowRoot.getElementById('spec-file').value);
234+
let specFile = e.target.files[0];
235+
let reader = new FileReader();
236+
reader.onload = function(e) {
237+
try{
238+
let specObj = JSON.parse(reader.result);
239+
me.loadSpec(specObj);
240+
me.shadowRoot.getElementById('spec-url').value="";
241+
}
242+
catch{
243+
alert("Unable to read or parse json");
244+
console.log("Unable to read or parse json")
245+
}
246+
233247
}
248+
// Read the Text file
249+
reader.readAsText(specFile);
250+
251+
}
252+
253+
onFileLoadClick(){
254+
this.shadowRoot.getElementById('spec-file').click();
234255
}
235256

236257
loadSpec(specUrl) {
@@ -247,6 +268,7 @@ class RapiDoc extends LitElement {
247268
})
248269
.catch(function(err) {
249270
me.loading=false;
271+
alert("The API Spec is invalid or not readable ");
250272
console.error('Onoes! The API is invalid. ' + err.message);
251273
});
252274
}

src/styles/font-styles.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export default html`
66
.regular-font{font-family:var(--font-regular);}
77
.mono-font{font-family:var(--font-mono);}
88
9-
h1{ font-family:var(--font-regular); font-size:26px; font-weight:200; letter-spacing:normal; }
10-
h2{ font-family:var(--font-regular); font-size:24px; font-weight:200; letter-spacing:normal; }
11-
h3{ font-family:var(--font-regular); font-size:22px; font-weight:200; letter-spacing:normal; }
12-
h4{ font-family:var(--font-regular); font-size:18px; font-weight:200; letter-spacing:normal; }
13-
h5{ font-family:var(--font-regular); font-size:16px; font-weight:200; letter-spacing:normal; }
9+
h1{ font-family:var(--font-regular); font-size:20px; font-weight:200; letter-spacing:normal; }
10+
h2{ font-family:var(--font-regular); font-size:18px; font-weight:200; letter-spacing:normal; }
11+
h3{ font-family:var(--font-regular); font-size:16px; font-weight:200; letter-spacing:normal; }
12+
h4{ font-family:var(--font-regular); font-size:15px; font-weight:200; letter-spacing:normal; }
13+
h5{ font-family:var(--font-regular); font-size:14px; font-weight:200; letter-spacing:normal; }
1414
h6{ font-family:var(--font-regular); font-size:14px; font-weight:200; letter-spacing:normal; }
1515
1616
h1,h2,h3,h4,h5,h5{
@@ -51,9 +51,9 @@ export default html`
5151
}
5252
5353
.m-sub-title.m-3 {
54-
font-size: 12px;
55-
font-weight:bold;
56-
margin: 12px 0 4px 0;
54+
font-size: 12px;
55+
font-weight:bold;
56+
margin: 12px 0 4px 0;
5757
}
5858
5959
/* Markdown */
@@ -64,7 +64,8 @@ export default html`
6464
margin-top:0;
6565
}
6666
.m-markdown p{
67-
line-height: 16px;
67+
line-height:16px;
68+
font-size:14px;
6869
}
6970
.m-markdown code{
7071
background-color: rgba(0, 0, 0, 0.02);

src/utils/parse-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export default function ProcessSpec(specUrl){
160160
return Promise.resolve(parsedSpec);
161161
})
162162
.catch(function(err) {
163+
alert(err);
163164
console.error(err);
164165
});
165166

0 commit comments

Comments
 (0)