Skip to content

Commit bd2a0ce

Browse files
committed
fixes #276 - in focused mode update example values with original, if the user have edited them
1 parent 35c28a5 commit bd2a0ce

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/components/api-request.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import marked from 'marked';
33
import Prism from 'prismjs';
44

55
import { unsafeHTML } from 'lit-html/directives/unsafe-html';
6+
import { live } from 'lit-html/directives/live';
67
import TableStyles from '@/styles/table-styles';
78
import FlexStyles from '@/styles/flex-styles';
89
import InputStyles from '@/styles/input-styles';
@@ -172,6 +173,24 @@ export default class ApiRequest extends LitElement {
172173
`;
173174
}
174175

176+
updated(changedProperties) {
177+
// In focused mode after navbar click, update the textareas(which contains examples) with the original values
178+
// as user may update the dom by editing the textarea's which will not trigger any futher dom-change events
179+
if (this.renderStyle === 'focused') {
180+
if (changedProperties.size === 1 && changedProperties.has('activeSchemaTab')) {
181+
// dont update example as only tabs is switched
182+
} else {
183+
const exampleTextAreaEls = [...this.shadowRoot.querySelectorAll('textarea[data-ptype="form-data"]')];
184+
exampleTextAreaEls.forEach((el) => {
185+
const origExampleEl = this.shadowRoot.querySelector(`textarea[data-pname='hidden-${el.dataset.pname}']`);
186+
if (origExampleEl) {
187+
el.value = origExampleEl.value;
188+
}
189+
});
190+
}
191+
}
192+
}
193+
175194
/* eslint-disable indent */
176195
inputParametersTemplate(paramType) {
177196
let title = '';
@@ -246,7 +265,7 @@ export default class ApiRequest extends LitElement {
246265
data-param-serialize-explode = "${paramExplode}"
247266
data-array = "true"
248267
placeholder= "add-multiple ⮐"
249-
.value = "${inputVal}"
268+
.value = "${live(inputVal)}"
250269
>
251270
</tag-input>`
252271
: paramSchema.type === 'object'
@@ -265,7 +284,7 @@ export default class ApiRequest extends LitElement {
265284
data-pname="${param.name}"
266285
data-ptype="${paramType}"
267286
data-array="false"
268-
value="${inputVal}"
287+
.value="${live(inputVal)}"
269288
/>`
270289
}
271290
</td>`
@@ -623,13 +642,15 @@ export default class ApiRequest extends LitElement {
623642
data-pname = "${fieldName}"
624643
spellcheck = "false"
625644
>${formdataPartExample[0].exampleValue}</textarea>
645+
<!-- This textarea(hidden) is to store the original example value, in focused mode on navbar change it is used to update the example text -->
646+
<textarea data-pname = "hidden-${fieldName}" data-ptype = "${mimeType.includes('form-urlencode') ? 'hidden-form-urlencode' : 'hidden-form-data'}" style="display:none">${formdataPartExample[0].exampleValue}</textarea>
626647
</div>`
627648
}
628649
</div>`
629650
: html`
630651
${this.allowTry === 'true' || fieldSchema.example
631652
? html`<input
632-
value = "${fieldSchema.example || ''}"
653+
.value = "${live(fieldSchema.example || '')}"
633654
spellcheck = "false"
634655
type = "${fieldSchema.format === 'binary' ? 'file' : fieldSchema.format === 'password' ? 'password' : 'text'}"
635656
style = "width:200px"

src/templates/navbar-template.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,19 @@ export default function navbarTemplate() {
7272
}
7373
return true;
7474
}).map((p) => html`
75-
<div class='nav-bar-path ${this.usePathInNavBar === 'true' ? 'small-font' : ''}' data-content-id='${p.method}-${p.path}' id='link-${p.method}-${p.path.replace(invalidCharsRegEx, '-')}' @click='${(e) => this.scrollToEl(e)}'>
76-
<span style = "${p.deprecated ? 'filter:opacity(0.5)' : ''}"> ${this.usePathInNavBar === 'true' ? html`<span class='mono-font'>${p.method.toUpperCase()} ${p.path}</span>` : p.summary} </span>
75+
<div
76+
class='nav-bar-path
77+
${this.usePathInNavBar === 'true' ? 'small-font' : ''}'
78+
data-content-id='${p.method}-${p.path}'
79+
id='link-${p.method}-${p.path.replace(invalidCharsRegEx, '-')}'
80+
@click = '${(e) => this.scrollToEl(e)}'
81+
>
82+
<span style = "${p.deprecated ? 'filter:opacity(0.5)' : ''}">
83+
${this.usePathInNavBar === 'true'
84+
? html`<span class='mono-font'>${p.method.toUpperCase()} ${p.path}</span>`
85+
: p.summary
86+
}
87+
</span>
7788
</div>`)}
7889
`)}
7990

0 commit comments

Comments
 (0)