chore: various small refactorings

This commit is contained in:
Roman Hotsiy 2018-08-16 12:36:51 +03:00
parent eae11682b8
commit 0cf7191144
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 20 additions and 22 deletions

View File

@ -9,7 +9,6 @@ import { ContentItemModel } from '../../services/MenuBuilder';
import { GroupModel, OperationModel } from '../../services/models'; import { GroupModel, OperationModel } from '../../services/models';
import { Operation } from '../Operation/Operation'; import { Operation } from '../Operation/Operation';
import { SecurityDefs } from '../SecuritySchemes/SecuritySchemes'; import { SecurityDefs } from '../SecuritySchemes/SecuritySchemes';
import { StoreConsumer } from '../StoreBuilder';
const DEFAULT_ALLOWED_COMPONENTS = { const DEFAULT_ALLOWED_COMPONENTS = {
'security-definitions': { 'security-definitions': {
@ -74,7 +73,7 @@ export class ContentItem extends React.Component<ContentItemProps> {
return ( return (
<> <>
<Section id={item.id} underlined={item.type === 'section'}> <Section id={item.id} underlined={item.type === 'operation'}>
{content} {content}
</Section> </Section>
{item.items && ( {item.items && (
@ -91,7 +90,8 @@ const middlePanelWrap = component => <MiddlePanel>{component}</MiddlePanel>;
export class SectionItem extends React.Component<ContentItemProps> { export class SectionItem extends React.Component<ContentItemProps> {
render() { render() {
const { name, description, level } = this.props.item as GroupModel; const { name, description, level } = this.props.item as GroupModel;
const components = this.props.allowedMdComponents; const { allowedMdComponents } = this.props;
const Header = level === 2 ? H2 : H1; const Header = level === 2 ? H2 : H1;
return ( return (
<> <>
@ -103,16 +103,11 @@ export class SectionItem extends React.Component<ContentItemProps> {
</Header> </Header>
</MiddlePanel> </MiddlePanel>
</Row> </Row>
<StoreConsumer> <AdvancedMarkdown
{store => ( allowedComponents={allowedMdComponents}
<AdvancedMarkdown source={description || ''}
source={description || ''} htmlWrap={middlePanelWrap}
allowedComponents={components} />
store={store}
htmlWrap={middlePanelWrap}
/>
)}
</StoreConsumer>
</> </>
); );
} }

View File

@ -4,16 +4,20 @@ import { AppStore, MarkdownRenderer, MDXComponentMeta } from '../../services';
import { BaseMarkdownProps } from './Markdown'; import { BaseMarkdownProps } from './Markdown';
import { SanitizedMarkdownHTML } from './SanitizedMdBlock'; import { SanitizedMarkdownHTML } from './SanitizedMdBlock';
import { StoreConsumer } from '../StoreBuilder';
export interface AdvancedMarkdownProps extends BaseMarkdownProps { export interface AdvancedMarkdownProps extends BaseMarkdownProps {
store?: AppStore;
allowedComponents: Dict<MDXComponentMeta>; allowedComponents: Dict<MDXComponentMeta>;
htmlWrap?: (part: JSX.Element) => JSX.Element; htmlWrap?: (part: JSX.Element) => JSX.Element;
} }
export class AdvancedMarkdown extends React.Component<AdvancedMarkdownProps> { export class AdvancedMarkdown extends React.Component<AdvancedMarkdownProps> {
render() { render() {
const { store, source, allowedComponents, htmlWrap = i => i } = this.props; return <StoreConsumer>{store => this.renderWithStore(store)}</StoreConsumer>;
}
renderWithStore(store?: AppStore) {
const { allowedComponents, source, htmlWrap = i => i } = this.props;
if (!store) { if (!store) {
throw new Error('When using componentes in markdown, store prop must be provided'); throw new Error('When using componentes in markdown, store prop must be provided');
} }

View File

@ -1,7 +1,7 @@
import * as DOMPurify from 'dompurify'; import * as DOMPurify from 'dompurify';
import * as React from 'react'; import * as React from 'react';
import { OptionsContext } from '../OptionsProvider'; import { OptionsConsumer } from '../OptionsProvider';
import { StylingMarkdownProps } from './Markdown'; import { StylingMarkdownProps } from './Markdown';
import { StyledMarkdownBlock } from './styled.elements'; import { StyledMarkdownBlock } from './styled.elements';
@ -15,7 +15,7 @@ export function SanitizedMarkdownHTML(
const Wrap = props.inline ? StyledMarkdownSpan : StyledMarkdownBlock; const Wrap = props.inline ? StyledMarkdownSpan : StyledMarkdownBlock;
return ( return (
<OptionsContext.Consumer> <OptionsConsumer>
{options => ( {options => (
<Wrap <Wrap
className={'redoc-markdown ' + (props.className || '')} className={'redoc-markdown ' + (props.className || '')}
@ -25,6 +25,6 @@ export function SanitizedMarkdownHTML(
{...props} {...props}
/> />
)} )}
</OptionsContext.Consumer> </OptionsConsumer>
); );
} }

View File

@ -74,7 +74,7 @@ export class MenuStore {
* @param scroll scroll service instance used by this menu * @param scroll scroll service instance used by this menu
*/ */
constructor(spec: SpecStore, public scroll: ScrollService) { constructor(spec: SpecStore, public scroll: ScrollService) {
this.items = spec.operationGroups; this.items = spec.contentItems;
this.flatItems = flattenByProp(this.items || [], 'items'); this.flatItems = flattenByProp(this.items || [], 'items');
this.flatItems.forEach((item, idx) => (item.absoluteIdx = idx)); this.flatItems.forEach((item, idx) => (item.absoluteIdx = idx));

View File

@ -13,7 +13,7 @@ export class SpecStore {
info: ApiInfoModel; info: ApiInfoModel;
externalDocs?: OpenAPIExternalDocumentation; externalDocs?: OpenAPIExternalDocumentation;
operationGroups: ContentItemModel[]; contentItems: ContentItemModel[];
securitySchemes: SecuritySchemesModel; securitySchemes: SecuritySchemesModel;
constructor( constructor(
@ -24,8 +24,7 @@ export class SpecStore {
this.parser = new OpenAPIParser(spec, specUrl, options); this.parser = new OpenAPIParser(spec, specUrl, options);
this.info = new ApiInfoModel(this.parser); this.info = new ApiInfoModel(this.parser);
this.externalDocs = this.parser.spec.externalDocs; this.externalDocs = this.parser.spec.externalDocs;
this.operationGroups = MenuBuilder.buildStructure(this.parser, this.options); this.contentItems = MenuBuilder.buildStructure(this.parser, this.options);
this.securitySchemes = new SecuritySchemesModel(this.parser); this.securitySchemes = new SecuritySchemesModel(this.parser);
} }
} }