Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sampleCollapseLevel option #937

Merged
merged 5 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* `disableSearch` - disable search indexing and search box
* `onlyRequiredInSamples` - shows only required fields in request samples.
* `jsonSampleExpandLevel` - set the default expand level for JSON payload samples (responses and request body). Special value 'all' expands all levels. The default value is `2`.
* `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
Expand Down
19 changes: 13 additions & 6 deletions src/components/JsonViewer/JsonViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SampleControls } from '../../common-elements';
import { CopyButtonWrapper } from '../../common-elements/CopyButtonWrapper';
import { PrismDiv } from '../../common-elements/PrismDiv';
import { jsonToHTML } from '../../utils/jsonToHtml';
import { OptionsContext } from '../OptionsProvider';
import { jsonStyles } from './style';

export interface JsonProps {
Expand Down Expand Up @@ -32,12 +33,18 @@ class Json extends React.PureComponent<JsonProps> {
<span onClick={this.expandAll}> Expand all </span>
<span onClick={this.collapseAll}> Collapse all </span>
</SampleControls>
<PrismDiv
className={this.props.className}
// tslint:disable-next-line
ref={node => (this.node = node!)}
dangerouslySetInnerHTML={{ __html: jsonToHTML(this.props.data) }}
/>
<OptionsContext.Consumer>
{options => (
<PrismDiv
className={this.props.className}
// tslint:disable-next-line
ref={node => (this.node = node!)}
dangerouslySetInnerHTML={{
__html: jsonToHTML(this.props.data, options.jsonSampleExpandLevel),
}}
/>
)}
</OptionsContext.Consumer>
</JsonViewerWrap>
);

Expand Down
15 changes: 15 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface RedocRawOptions {
onlyRequiredInSamples?: boolean | string;
showExtensions?: boolean | string | string[];
hideSingleRequestSampleTab?: boolean | string;
jsonSampleExpandLevel?: number | string | 'all';

unstable_ignoreMimeParameters?: boolean;

Expand Down Expand Up @@ -110,6 +111,16 @@ export class RedocNormalizedOptions {
return value;
}

private static normalizeSampleCollapseLevel(level?: number | string | 'all'): number {
alex-karo marked this conversation as resolved.
Show resolved Hide resolved
if (level === 'all') {
return +Infinity;
}
if (!isNaN(Number(level))) {
return Math.ceil(Number(level));
}
return 2;
}

theme: ResolvedThemeInterface;
scrollYOffset: () => number;
hideHostname: boolean;
Expand All @@ -125,6 +136,7 @@ export class RedocNormalizedOptions {
onlyRequiredInSamples: boolean;
showExtensions: boolean | string[];
hideSingleRequestSampleTab: boolean;
jsonSampleExpandLevel: number;

/* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean;
Expand Down Expand Up @@ -156,6 +168,9 @@ export class RedocNormalizedOptions {
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
this.jsonSampleExpandLevel = RedocNormalizedOptions.normalizeSampleCollapseLevel(
alex-karo marked this conversation as resolved.
Show resolved Hide resolved
raw.jsonSampleExpandLevel,
);

this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

Expand Down
23 changes: 11 additions & 12 deletions src/utils/jsonToHtml.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
let level = 1;
const COLLAPSE_LEVEL = 2;

export function jsonToHTML(json) {
export function jsonToHTML(json, maxCollapseLevel) {
level = 1;
let output = '';
output += '<div class="redoc-json">';
output += valueToHTML(json);
output += valueToHTML(json, maxCollapseLevel);
output += '</div>';
return output;
}
Expand Down Expand Up @@ -33,20 +32,20 @@ function punctuation(val) {
return '<span class="token punctuation">' + val + '</span>';
}

function valueToHTML(value) {
function valueToHTML(value, maxCollapseLevel: number) {
const valueType = typeof value;
let output = '';
if (value === undefined || value === null) {
output += decorateWithSpan('null', 'token keyword');
} else if (value && value.constructor === Array) {
level++;
output += arrayToHTML(value);
output += arrayToHTML(value, maxCollapseLevel);
level--;
} else if (value && value.constructor === Date) {
output += decorateWithSpan('"' + value.toISOString() + '"', 'token string');
} else if (valueType === 'object') {
level++;
output += objectToHTML(value);
output += objectToHTML(value, maxCollapseLevel);
level--;
} else if (valueType === 'number') {
output += decorateWithSpan(value, 'token number');
Expand All @@ -70,8 +69,8 @@ function valueToHTML(value) {
return output;
}

function arrayToHTML(json) {
const collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
function arrayToHTML(json, maxCollapseLevel: number) {
const collapsed = level > maxCollapseLevel ? 'collapsed' : '';
let output = `<div class="collapser"></div>${punctuation(
'[',
)}<span class="ellipsis"></span><ul class="array collapsible">`;
Expand All @@ -80,7 +79,7 @@ function arrayToHTML(json) {
for (let i = 0; i < length; i++) {
hasContents = true;
output += '<li><div class="hoverable ' + collapsed + '">';
output += valueToHTML(json[i]);
output += valueToHTML(json[i], maxCollapseLevel);
if (i < length - 1) {
output += ',';
}
Expand All @@ -93,8 +92,8 @@ function arrayToHTML(json) {
return output;
}

function objectToHTML(json) {
const collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
function objectToHTML(json, maxCollapseLevel: number) {
const collapsed = level > maxCollapseLevel ? 'collapsed' : '';
const keys = Object.keys(json);
const length = keys.length;
let output = `<div class="collapser"></div>${punctuation(
Expand All @@ -106,7 +105,7 @@ function objectToHTML(json) {
hasContents = true;
output += '<li><div class="hoverable ' + collapsed + '">';
output += '<span class="property token string">"' + htmlEncode(key) + '"</span>: ';
output += valueToHTML(json[key]);
output += valueToHTML(json[key], maxCollapseLevel);
if (i < length - 1) {
output += punctuation(',');
}
Expand Down