From b0e660eca00b2e8ad7c7824a56b03f2e9547e8c0 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sun, 12 May 2019 21:19:36 +0300 Subject: [PATCH] feat: basic UI labels configuration This can be used for translations later --- src/components/Fields/EnumValues.tsx | 11 ++++---- src/components/Fields/FieldDetails.tsx | 12 +++++---- src/components/Schema/Schema.tsx | 4 ++- src/services/Labels.ts | 37 ++++++++++++++++++++++++++ src/services/RedocNormalizedOptions.ts | 6 +++++ src/services/models/Schema.ts | 4 ++- 6 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/services/Labels.ts diff --git a/src/components/Fields/EnumValues.tsx b/src/components/Fields/EnumValues.tsx index 30472ff7..5fb76122 100644 --- a/src/components/Fields/EnumValues.tsx +++ b/src/components/Fields/EnumValues.tsx @@ -1,6 +1,8 @@ import * as React from 'react'; import { ExampleValue, FieldLabel } from '../../common-elements/fields'; +import { l } from '../../services/Labels'; + export interface EnumValuesProps { values: string[]; type: string; @@ -16,12 +18,11 @@ export class EnumValues extends React.PureComponent { return (
- {type === 'array' ? 'Items' : ''} {values.length === 1 ? 'Value' : 'Enum'}: - {' '} + {type === 'array' ? l('enumArray') : ''}{' '} + {values.length === 1 ? l('enumSingleValue') : l('enum')}: + {values.map((value, idx) => ( - - {JSON.stringify(value)} - + {JSON.stringify(value)} ))}
); diff --git a/src/components/Fields/FieldDetails.tsx b/src/components/Fields/FieldDetails.tsx index 32371688..04672726 100644 --- a/src/components/Fields/FieldDetails.tsx +++ b/src/components/Fields/FieldDetails.tsx @@ -19,6 +19,8 @@ import { FieldDetail } from './FieldDetail'; import { Badge } from '../../common-elements/'; +import { l } from '../../services/Labels'; + export class FieldDetails extends React.PureComponent { render() { const { showExamples, field, renderDiscriminatorSwitch } = this.props; @@ -40,18 +42,18 @@ export class FieldDetails extends React.PureComponent { )} {schema.title && ({schema.title}) } - {schema.nullable && Nullable } + {schema.nullable && {l('nullable')} } {schema.pattern && {schema.pattern}} - {schema.isCircular && Recursive } + {schema.isCircular && {l('recursive')} } {deprecated && (
- Deprecated + {l('deprecated')}
)} - + {!renderDiscriminatorSwitch && }{' '} - {showExamples && } + {showExamples && } {}
diff --git a/src/components/Schema/Schema.tsx b/src/components/Schema/Schema.tsx index 22e1eabc..f71c33a2 100644 --- a/src/components/Schema/Schema.tsx +++ b/src/components/Schema/Schema.tsx @@ -10,6 +10,8 @@ import { ArraySchema } from './ArraySchema'; import { ObjectSchema } from './ObjectSchema'; import { OneOfSchema } from './OneOfSchema'; +import { l } from '../../services/Labels'; + export interface SchemaOptions { showTitle?: boolean; skipReadOnly?: boolean; @@ -34,7 +36,7 @@ export class Schema extends React.Component> {
{schema.displayType} {schema.title && {schema.title} } - Recursive + {l('recursive')}
); } diff --git a/src/services/Labels.ts b/src/services/Labels.ts new file mode 100644 index 00000000..45365036 --- /dev/null +++ b/src/services/Labels.ts @@ -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; + +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; +} diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index a84e93c6..7c893f1b 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -2,6 +2,7 @@ import defaultTheme, { ResolvedThemeInterface, resolveTheme, ThemeInterface } fr import { querySelector } from '../utils/dom'; import { isNumeric, mergeObjects } from '../utils/helpers'; +import { LabelsConfigRaw, setRedocLabels } from './Labels'; import { MDXComponentMeta } from './MarkdownRenderer'; export interface RedocRawOptions { @@ -25,6 +26,8 @@ export interface RedocRawOptions { unstable_ignoreMimeParameters?: boolean; allowedMdComponents?: Dict; + + labels?: LabelsConfigRaw; } function argValueToBoolean(val?: string | boolean): boolean { @@ -136,6 +139,9 @@ export class RedocNormalizedOptions { 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.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname); this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses); diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 668cd86d..8440c37d 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -19,6 +19,8 @@ import { sortByRequired, } from '../../utils/'; +import { l } from '../Labels'; + // TODO: refactor this model, maybe use getters instead of copying all the values export class SchemaModel { pointer: string; @@ -148,7 +150,7 @@ export class SchemaModel { this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options); this.displayType = pluralizeType(this.items.displayType); 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.isPrimitive = this.items.isPrimitive; if (this.example === undefined && this.items.example !== undefined) {