mirror of
https://github.com/Redocly/redoc.git
synced 2025-07-02 10:13:04 +03:00
feat: basic UI labels configuration
This can be used for translations later
This commit is contained in:
parent
fdcac30829
commit
b0e660eca0
|
@ -1,6 +1,8 @@
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { ExampleValue, FieldLabel } from '../../common-elements/fields';
|
import { ExampleValue, FieldLabel } from '../../common-elements/fields';
|
||||||
|
|
||||||
|
import { l } from '../../services/Labels';
|
||||||
|
|
||||||
export interface EnumValuesProps {
|
export interface EnumValuesProps {
|
||||||
values: string[];
|
values: string[];
|
||||||
type: string;
|
type: string;
|
||||||
|
@ -16,12 +18,11 @@ export class EnumValues extends React.PureComponent<EnumValuesProps> {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<FieldLabel>
|
<FieldLabel>
|
||||||
{type === 'array' ? 'Items' : ''} {values.length === 1 ? 'Value' : 'Enum'}:
|
{type === 'array' ? l('enumArray') : ''}{' '}
|
||||||
</FieldLabel>{' '}
|
{values.length === 1 ? l('enumSingleValue') : l('enum')}:
|
||||||
|
</FieldLabel>
|
||||||
{values.map((value, idx) => (
|
{values.map((value, idx) => (
|
||||||
<ExampleValue key={idx}>
|
<ExampleValue key={idx}>{JSON.stringify(value)}</ExampleValue>
|
||||||
{JSON.stringify(value)}
|
|
||||||
</ExampleValue>
|
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -19,6 +19,8 @@ import { FieldDetail } from './FieldDetail';
|
||||||
|
|
||||||
import { Badge } from '../../common-elements/';
|
import { Badge } from '../../common-elements/';
|
||||||
|
|
||||||
|
import { l } from '../../services/Labels';
|
||||||
|
|
||||||
export class FieldDetails extends React.PureComponent<FieldProps> {
|
export class FieldDetails extends React.PureComponent<FieldProps> {
|
||||||
render() {
|
render() {
|
||||||
const { showExamples, field, renderDiscriminatorSwitch } = this.props;
|
const { showExamples, field, renderDiscriminatorSwitch } = this.props;
|
||||||
|
@ -40,18 +42,18 @@ export class FieldDetails extends React.PureComponent<FieldProps> {
|
||||||
)}
|
)}
|
||||||
{schema.title && <TypeTitle> ({schema.title}) </TypeTitle>}
|
{schema.title && <TypeTitle> ({schema.title}) </TypeTitle>}
|
||||||
<ConstraintsView constraints={schema.constraints} />
|
<ConstraintsView constraints={schema.constraints} />
|
||||||
{schema.nullable && <NullableLabel> Nullable </NullableLabel>}
|
{schema.nullable && <NullableLabel> {l('nullable')} </NullableLabel>}
|
||||||
{schema.pattern && <PatternLabel>{schema.pattern}</PatternLabel>}
|
{schema.pattern && <PatternLabel>{schema.pattern}</PatternLabel>}
|
||||||
{schema.isCircular && <RecursiveLabel> Recursive </RecursiveLabel>}
|
{schema.isCircular && <RecursiveLabel> {l('recursive')} </RecursiveLabel>}
|
||||||
</div>
|
</div>
|
||||||
{deprecated && (
|
{deprecated && (
|
||||||
<div>
|
<div>
|
||||||
<Badge type="warning"> Deprecated </Badge>
|
<Badge type="warning"> {l('deprecated')} </Badge>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<FieldDetail label={'Default:'} value={schema.default} />
|
<FieldDetail label={l('default') + ':'} value={schema.default} />
|
||||||
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
|
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
|
||||||
{showExamples && <FieldDetail label={'Example:'} value={example} />}
|
{showExamples && <FieldDetail label={l('example') + ':'} value={example} />}
|
||||||
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}
|
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}
|
||||||
<div>
|
<div>
|
||||||
<Markdown compact={true} source={description} />
|
<Markdown compact={true} source={description} />
|
||||||
|
|
|
@ -10,6 +10,8 @@ import { ArraySchema } from './ArraySchema';
|
||||||
import { ObjectSchema } from './ObjectSchema';
|
import { ObjectSchema } from './ObjectSchema';
|
||||||
import { OneOfSchema } from './OneOfSchema';
|
import { OneOfSchema } from './OneOfSchema';
|
||||||
|
|
||||||
|
import { l } from '../../services/Labels';
|
||||||
|
|
||||||
export interface SchemaOptions {
|
export interface SchemaOptions {
|
||||||
showTitle?: boolean;
|
showTitle?: boolean;
|
||||||
skipReadOnly?: boolean;
|
skipReadOnly?: boolean;
|
||||||
|
@ -34,7 +36,7 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
|
||||||
<div>
|
<div>
|
||||||
<TypeName>{schema.displayType}</TypeName>
|
<TypeName>{schema.displayType}</TypeName>
|
||||||
{schema.title && <TypeTitle> {schema.title} </TypeTitle>}
|
{schema.title && <TypeTitle> {schema.title} </TypeTitle>}
|
||||||
<RecursiveLabel> Recursive </RecursiveLabel>
|
<RecursiveLabel> {l('recursive')} </RecursiveLabel>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
37
src/services/Labels.ts
Normal file
37
src/services/Labels.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
export interface LabelsConfig {
|
||||||
|
enum: string;
|
||||||
|
enumSingleValue: string;
|
||||||
|
enumArray: string;
|
||||||
|
default: string;
|
||||||
|
deprecated: string;
|
||||||
|
example: string;
|
||||||
|
nullable: string;
|
||||||
|
recursive: string;
|
||||||
|
arrayOf: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LabelsConfigRaw = Partial<LabelsConfig>;
|
||||||
|
|
||||||
|
const labels: LabelsConfig = {
|
||||||
|
enum: 'Enum',
|
||||||
|
enumSingleValue: 'Value',
|
||||||
|
enumArray: 'Items',
|
||||||
|
default: 'Default',
|
||||||
|
deprecated: 'Deprecated',
|
||||||
|
example: 'Example',
|
||||||
|
nullable: 'Nullable',
|
||||||
|
recursive: 'Recursive',
|
||||||
|
arrayOf: 'Array of ',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function setRedocLabels(_labels?: LabelsConfigRaw) {
|
||||||
|
Object.assign(labels, _labels);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function l(key: keyof LabelsConfig, idx?: number): string {
|
||||||
|
const label = labels[key];
|
||||||
|
if (idx !== undefined) {
|
||||||
|
return label[idx];
|
||||||
|
}
|
||||||
|
return label;
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ import defaultTheme, { ResolvedThemeInterface, resolveTheme, ThemeInterface } fr
|
||||||
import { querySelector } from '../utils/dom';
|
import { querySelector } from '../utils/dom';
|
||||||
import { isNumeric, mergeObjects } from '../utils/helpers';
|
import { isNumeric, mergeObjects } from '../utils/helpers';
|
||||||
|
|
||||||
|
import { LabelsConfigRaw, setRedocLabels } from './Labels';
|
||||||
import { MDXComponentMeta } from './MarkdownRenderer';
|
import { MDXComponentMeta } from './MarkdownRenderer';
|
||||||
|
|
||||||
export interface RedocRawOptions {
|
export interface RedocRawOptions {
|
||||||
|
@ -25,6 +26,8 @@ export interface RedocRawOptions {
|
||||||
unstable_ignoreMimeParameters?: boolean;
|
unstable_ignoreMimeParameters?: boolean;
|
||||||
|
|
||||||
allowedMdComponents?: Dict<MDXComponentMeta>;
|
allowedMdComponents?: Dict<MDXComponentMeta>;
|
||||||
|
|
||||||
|
labels?: LabelsConfigRaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
function argValueToBoolean(val?: string | boolean): boolean {
|
function argValueToBoolean(val?: string | boolean): boolean {
|
||||||
|
@ -136,6 +139,9 @@ export class RedocNormalizedOptions {
|
||||||
|
|
||||||
this.theme.extensionsHook = hook as any;
|
this.theme.extensionsHook = hook as any;
|
||||||
|
|
||||||
|
// do not support dynamic labels changes. Labels should be configured before
|
||||||
|
setRedocLabels(raw.labels);
|
||||||
|
|
||||||
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
|
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
|
||||||
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
|
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
|
||||||
this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses);
|
this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses);
|
||||||
|
|
|
@ -19,6 +19,8 @@ import {
|
||||||
sortByRequired,
|
sortByRequired,
|
||||||
} from '../../utils/';
|
} from '../../utils/';
|
||||||
|
|
||||||
|
import { l } from '../Labels';
|
||||||
|
|
||||||
// TODO: refactor this model, maybe use getters instead of copying all the values
|
// TODO: refactor this model, maybe use getters instead of copying all the values
|
||||||
export class SchemaModel {
|
export class SchemaModel {
|
||||||
pointer: string;
|
pointer: string;
|
||||||
|
@ -148,7 +150,7 @@ export class SchemaModel {
|
||||||
this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options);
|
this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options);
|
||||||
this.displayType = pluralizeType(this.items.displayType);
|
this.displayType = pluralizeType(this.items.displayType);
|
||||||
this.displayFormat = this.items.format;
|
this.displayFormat = this.items.format;
|
||||||
this.typePrefix = this.items.typePrefix + 'Array of ';
|
this.typePrefix = this.items.typePrefix + l('arrayOf');
|
||||||
this.title = this.title || this.items.title;
|
this.title = this.title || this.items.title;
|
||||||
this.isPrimitive = this.items.isPrimitive;
|
this.isPrimitive = this.items.isPrimitive;
|
||||||
if (this.example === undefined && this.items.example !== undefined) {
|
if (this.example === undefined && this.items.example !== undefined) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user