mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-08 14:14:56 +03:00
feat: add sampleCollapseLevel option
This commit is contained in:
parent
753b013eee
commit
312c85a911
|
@ -242,6 +242,7 @@ You can use all of the following options with standalone version on <redoc> tag
|
||||||
* `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button.
|
* `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button.
|
||||||
* `disableSearch` - disable search indexing and search box
|
* `disableSearch` - disable search indexing and search box
|
||||||
* `onlyRequiredInSamples` - shows only required fields in request samples.
|
* `onlyRequiredInSamples` - shows only required fields in request samples.
|
||||||
|
* `sampleCollapseLevel` - set the collapse level in response samples. Special value 'all' expand all response. Default value = 2.
|
||||||
* `theme` - ReDoc theme. Not documented yet. For details check source code: [theme.ts](https://github.com/Redocly/redoc/blob/master/src/theme.ts)
|
* `theme` - ReDoc theme. Not documented yet. For details check source code: [theme.ts](https://github.com/Redocly/redoc/blob/master/src/theme.ts)
|
||||||
|
|
||||||
## Advanced usage of standalone version
|
## Advanced usage of standalone version
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { SampleControls } from '../../common-elements';
|
||||||
import { CopyButtonWrapper } from '../../common-elements/CopyButtonWrapper';
|
import { CopyButtonWrapper } from '../../common-elements/CopyButtonWrapper';
|
||||||
import { PrismDiv } from '../../common-elements/PrismDiv';
|
import { PrismDiv } from '../../common-elements/PrismDiv';
|
||||||
import { jsonToHTML } from '../../utils/jsonToHtml';
|
import { jsonToHTML } from '../../utils/jsonToHtml';
|
||||||
|
import { OptionsContext } from '../OptionsProvider';
|
||||||
import { jsonStyles } from './style';
|
import { jsonStyles } from './style';
|
||||||
|
|
||||||
export interface JsonProps {
|
export interface JsonProps {
|
||||||
|
@ -32,12 +33,18 @@ class Json extends React.PureComponent<JsonProps> {
|
||||||
<span onClick={this.expandAll}> Expand all </span>
|
<span onClick={this.expandAll}> Expand all </span>
|
||||||
<span onClick={this.collapseAll}> Collapse all </span>
|
<span onClick={this.collapseAll}> Collapse all </span>
|
||||||
</SampleControls>
|
</SampleControls>
|
||||||
<PrismDiv
|
<OptionsContext.Consumer>
|
||||||
className={this.props.className}
|
{options => (
|
||||||
// tslint:disable-next-line
|
<PrismDiv
|
||||||
ref={node => (this.node = node!)}
|
className={this.props.className}
|
||||||
dangerouslySetInnerHTML={{ __html: jsonToHTML(this.props.data) }}
|
// tslint:disable-next-line
|
||||||
/>
|
ref={node => (this.node = node!)}
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: jsonToHTML(this.props.data, options.sampleCollapseLevel),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</OptionsContext.Consumer>
|
||||||
</JsonViewerWrap>
|
</JsonViewerWrap>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ export interface RedocRawOptions {
|
||||||
onlyRequiredInSamples?: boolean | string;
|
onlyRequiredInSamples?: boolean | string;
|
||||||
showExtensions?: boolean | string | string[];
|
showExtensions?: boolean | string | string[];
|
||||||
hideSingleRequestSampleTab?: boolean | string;
|
hideSingleRequestSampleTab?: boolean | string;
|
||||||
|
sampleCollapseLevel?: number | string | 'all';
|
||||||
|
|
||||||
unstable_ignoreMimeParameters?: boolean;
|
unstable_ignoreMimeParameters?: boolean;
|
||||||
|
|
||||||
|
@ -110,6 +111,16 @@ export class RedocNormalizedOptions {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static normalizeSampleCollapseLevel(level?: number | string | 'all'): number {
|
||||||
|
if (level === 'all') {
|
||||||
|
return +Infinity;
|
||||||
|
}
|
||||||
|
if (!isNaN(Number(level))) {
|
||||||
|
return Math.ceil(Number(level));
|
||||||
|
}
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
theme: ResolvedThemeInterface;
|
theme: ResolvedThemeInterface;
|
||||||
scrollYOffset: () => number;
|
scrollYOffset: () => number;
|
||||||
hideHostname: boolean;
|
hideHostname: boolean;
|
||||||
|
@ -125,6 +136,7 @@ export class RedocNormalizedOptions {
|
||||||
onlyRequiredInSamples: boolean;
|
onlyRequiredInSamples: boolean;
|
||||||
showExtensions: boolean | string[];
|
showExtensions: boolean | string[];
|
||||||
hideSingleRequestSampleTab: boolean;
|
hideSingleRequestSampleTab: boolean;
|
||||||
|
sampleCollapseLevel: number;
|
||||||
|
|
||||||
/* tslint:disable-next-line */
|
/* tslint:disable-next-line */
|
||||||
unstable_ignoreMimeParameters: boolean;
|
unstable_ignoreMimeParameters: boolean;
|
||||||
|
@ -156,6 +168,9 @@ export class RedocNormalizedOptions {
|
||||||
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
|
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
|
||||||
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
|
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
|
||||||
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
|
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
|
||||||
|
this.sampleCollapseLevel = RedocNormalizedOptions.normalizeSampleCollapseLevel(
|
||||||
|
raw.sampleCollapseLevel,
|
||||||
|
);
|
||||||
|
|
||||||
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
|
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
let level = 1;
|
let level = 1;
|
||||||
const COLLAPSE_LEVEL = 2;
|
let collapseLevel;
|
||||||
|
|
||||||
export function jsonToHTML(json) {
|
export function jsonToHTML(json, maxCollapseLevel) {
|
||||||
level = 1;
|
level = 1;
|
||||||
|
collapseLevel = maxCollapseLevel;
|
||||||
let output = '';
|
let output = '';
|
||||||
output += '<div class="redoc-json">';
|
output += '<div class="redoc-json">';
|
||||||
output += valueToHTML(json);
|
output += valueToHTML(json);
|
||||||
|
@ -71,7 +72,7 @@ function valueToHTML(value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayToHTML(json) {
|
function arrayToHTML(json) {
|
||||||
const collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
|
const collapsed = level > collapseLevel ? 'collapsed' : '';
|
||||||
let output = `<div class="collapser"></div>${punctuation(
|
let output = `<div class="collapser"></div>${punctuation(
|
||||||
'[',
|
'[',
|
||||||
)}<span class="ellipsis"></span><ul class="array collapsible">`;
|
)}<span class="ellipsis"></span><ul class="array collapsible">`;
|
||||||
|
@ -94,7 +95,7 @@ function arrayToHTML(json) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function objectToHTML(json) {
|
function objectToHTML(json) {
|
||||||
const collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
|
const collapsed = level > collapseLevel ? 'collapsed' : '';
|
||||||
const keys = Object.keys(json);
|
const keys = Object.keys(json);
|
||||||
const length = keys.length;
|
const length = keys.length;
|
||||||
let output = `<div class="collapser"></div>${punctuation(
|
let output = `<div class="collapser"></div>${punctuation(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user