From 60d95475ebcfc868e54f90350fcca09e45b976ff Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 6 Mar 2018 16:45:25 +0100 Subject: Move DocsInfo out of the components folder --- packages/react-docs/src/ts/components/docs_info.ts | 120 --------------------- .../react-docs/src/ts/components/documentation.tsx | 2 +- .../src/ts/components/event_definition.tsx | 2 +- .../react-docs/src/ts/components/interface.tsx | 2 +- .../react-docs/src/ts/components/method_block.tsx | 2 +- .../src/ts/components/method_signature.tsx | 2 +- packages/react-docs/src/ts/components/type.tsx | 2 +- .../src/ts/components/type_definition.tsx | 2 +- packages/react-docs/src/ts/docs_info.ts | 120 +++++++++++++++++++++ packages/react-docs/src/ts/index.ts | 2 +- packages/react-docs/src/ts/utils/typedoc_utils.ts | 2 +- 11 files changed, 129 insertions(+), 129 deletions(-) delete mode 100644 packages/react-docs/src/ts/components/docs_info.ts create mode 100644 packages/react-docs/src/ts/docs_info.ts (limited to 'packages/react-docs/src') diff --git a/packages/react-docs/src/ts/components/docs_info.ts b/packages/react-docs/src/ts/components/docs_info.ts deleted file mode 100644 index 509bba89e..000000000 --- a/packages/react-docs/src/ts/components/docs_info.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { MenuSubsectionsBySection } from '@0xproject/react-shared'; -import compareVersions = require('compare-versions'); -import * as _ from 'lodash'; - -import { - ContractsByVersionByNetworkId, - DocAgnosticFormat, - DocsInfoConfig, - DocsMenu, - DoxityDocObj, - SectionsMap, - SupportedDocJson, - TypeDocNode, -} from '../types'; -import { doxityUtils } from '../utils/doxity_utils'; -import { typeDocUtils } from '../utils/typedoc_utils'; - -export class DocsInfo { - public id: string; - public type: SupportedDocJson; - public displayName: string; - public packageUrl: string; - public menu: DocsMenu; - public sections: SectionsMap; - public sectionNameToMarkdown: { [sectionName: string]: string }; - public contractsByVersionByNetworkId?: ContractsByVersionByNetworkId; - private _docsInfo: DocsInfoConfig; - constructor(config: DocsInfoConfig) { - this.id = config.id; - this.type = config.type; - this.displayName = config.displayName; - this.packageUrl = config.packageUrl; - this.sections = config.sections; - this.sectionNameToMarkdown = config.sectionNameToMarkdown; - this.contractsByVersionByNetworkId = config.contractsByVersionByNetworkId; - this._docsInfo = config; - } - public isPublicType(typeName: string): boolean { - if (_.isUndefined(this._docsInfo.publicTypes)) { - return false; - } - const isPublic = _.includes(this._docsInfo.publicTypes, typeName); - return isPublic; - } - public getModulePathsIfExists(sectionName: string): string[] { - const modulePathsIfExists = this._docsInfo.sectionNameToModulePath[sectionName]; - return modulePathsIfExists; - } - public getMenu(selectedVersion?: string): { [section: string]: string[] } { - if (_.isUndefined(selectedVersion) || _.isUndefined(this._docsInfo.menuSubsectionToVersionWhenIntroduced)) { - return this._docsInfo.menu; - } - - const finalMenu = _.cloneDeep(this._docsInfo.menu); - if (_.isUndefined(finalMenu.contracts)) { - return finalMenu; - } - - // TODO: refactor to include more sections then simply the `contracts` section - finalMenu.contracts = _.filter(finalMenu.contracts, (contractName: string) => { - const versionIntroducedIfExists = this._docsInfo.menuSubsectionToVersionWhenIntroduced[contractName]; - if (!_.isUndefined(versionIntroducedIfExists)) { - const existsInSelectedVersion = compareVersions(selectedVersion, versionIntroducedIfExists) >= 0; - return existsInSelectedVersion; - } else { - return true; - } - }); - return finalMenu; - } - public getMenuSubsectionsBySection(docAgnosticFormat?: DocAgnosticFormat): MenuSubsectionsBySection { - const menuSubsectionsBySection = {} as MenuSubsectionsBySection; - if (_.isUndefined(docAgnosticFormat)) { - return menuSubsectionsBySection; - } - - const docSections = _.keys(this.sections); - _.each(docSections, sectionName => { - const docSection = docAgnosticFormat[sectionName]; - if (_.isUndefined(docSection)) { - return; // no-op - } - - if (!_.isUndefined(this.sections.types) && sectionName === this.sections.types) { - const sortedTypesNames = _.sortBy(docSection.types, 'name'); - const typeNames = _.map(sortedTypesNames, t => t.name); - menuSubsectionsBySection[sectionName] = typeNames; - } else { - let eventNames: string[] = []; - if (!_.isUndefined(docSection.events)) { - const sortedEventNames = _.sortBy(docSection.events, 'name'); - eventNames = _.map(sortedEventNames, m => m.name); - } - const sortedMethodNames = _.sortBy(docSection.methods, 'name'); - const methodNames = _.map(sortedMethodNames, m => m.name); - menuSubsectionsBySection[sectionName] = [...methodNames, ...eventNames]; - } - }); - return menuSubsectionsBySection; - } - public getTypeDefinitionsByName(docAgnosticFormat: DocAgnosticFormat) { - if (_.isUndefined(this.sections.types)) { - return {}; - } - - const typeDocSection = docAgnosticFormat[this.sections.types]; - const typeDefinitionByName = _.keyBy(typeDocSection.types, 'name'); - return typeDefinitionByName; - } - public isVisibleConstructor(sectionName: string): boolean { - return _.includes(this._docsInfo.visibleConstructors, sectionName); - } - public convertToDocAgnosticFormat(docObj: DoxityDocObj | TypeDocNode): DocAgnosticFormat { - if (this.type === SupportedDocJson.Doxity) { - return doxityUtils.convertToDocAgnosticFormat(docObj as DoxityDocObj); - } else { - return typeDocUtils.convertToDocAgnosticFormat(docObj as TypeDocNode, this); - } - } -} diff --git a/packages/react-docs/src/ts/components/documentation.tsx b/packages/react-docs/src/ts/components/documentation.tsx index f5a117797..5c261a39b 100644 --- a/packages/react-docs/src/ts/components/documentation.tsx +++ b/packages/react-docs/src/ts/components/documentation.tsx @@ -15,6 +15,7 @@ import CircularProgress from 'material-ui/CircularProgress'; import * as React from 'react'; import { scroller } from 'react-scroll'; +import { DocsInfo } from '../docs_info'; import { AddressByContractName, DocAgnosticFormat, @@ -30,7 +31,6 @@ import { utils } from '../utils/utils'; import { Badge } from './badge'; import { Comment } from './comment'; -import { DocsInfo } from './docs_info'; import { EventDefinition } from './event_definition'; import { MethodBlock } from './method_block'; import { SourceLink } from './source_link'; diff --git a/packages/react-docs/src/ts/components/event_definition.tsx b/packages/react-docs/src/ts/components/event_definition.tsx index 8289650f5..68f60ddf9 100644 --- a/packages/react-docs/src/ts/components/event_definition.tsx +++ b/packages/react-docs/src/ts/components/event_definition.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; import { Event, EventArg } from '../types'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { Type } from './type'; export interface EventDefinitionProps { diff --git a/packages/react-docs/src/ts/components/interface.tsx b/packages/react-docs/src/ts/components/interface.tsx index 1c99495d7..92883089a 100644 --- a/packages/react-docs/src/ts/components/interface.tsx +++ b/packages/react-docs/src/ts/components/interface.tsx @@ -3,7 +3,7 @@ import * as React from 'react'; import { CustomType, TypeDocTypes } from '../types'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { MethodSignature } from './method_signature'; import { Type } from './type'; diff --git a/packages/react-docs/src/ts/components/method_block.tsx b/packages/react-docs/src/ts/components/method_block.tsx index 5ed7f42a1..529b9f9c7 100644 --- a/packages/react-docs/src/ts/components/method_block.tsx +++ b/packages/react-docs/src/ts/components/method_block.tsx @@ -6,7 +6,7 @@ import { Parameter, SolidityMethod, TypeDefinitionByName, TypescriptMethod } fro import { typeDocUtils } from '../utils/typedoc_utils'; import { Comment } from './comment'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { MethodSignature } from './method_signature'; import { SourceLink } from './source_link'; diff --git a/packages/react-docs/src/ts/components/method_signature.tsx b/packages/react-docs/src/ts/components/method_signature.tsx index e21d82287..6a394dd6d 100644 --- a/packages/react-docs/src/ts/components/method_signature.tsx +++ b/packages/react-docs/src/ts/components/method_signature.tsx @@ -5,7 +5,7 @@ import * as ReactDOM from 'react-dom'; import { Parameter, SolidityMethod, TypeDefinitionByName, TypescriptMethod } from '../types'; import { constants } from '../utils/constants'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { Type } from './type'; export interface MethodSignatureProps { diff --git a/packages/react-docs/src/ts/components/type.tsx b/packages/react-docs/src/ts/components/type.tsx index 780d87eae..504570aad 100644 --- a/packages/react-docs/src/ts/components/type.tsx +++ b/packages/react-docs/src/ts/components/type.tsx @@ -8,7 +8,7 @@ import { Type as TypeDef, TypeDefinitionByName, TypeDocTypes } from '../types'; import { constants } from '../utils/constants'; import { utils } from '../utils/utils'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { TypeDefinition } from './type_definition'; // Some types reference other libraries. For these types, we want to link the user to the relevant documentation. diff --git a/packages/react-docs/src/ts/components/type_definition.tsx b/packages/react-docs/src/ts/components/type_definition.tsx index 944a31f95..20a24495b 100644 --- a/packages/react-docs/src/ts/components/type_definition.tsx +++ b/packages/react-docs/src/ts/components/type_definition.tsx @@ -7,7 +7,7 @@ import { utils } from '../utils/utils'; import { Comment } from './comment'; import { CustomEnum } from './custom_enum'; -import { DocsInfo } from './docs_info'; +import { DocsInfo } from '../docs_info'; import { Enum } from './enum'; import { Interface } from './interface'; import { MethodSignature } from './method_signature'; diff --git a/packages/react-docs/src/ts/docs_info.ts b/packages/react-docs/src/ts/docs_info.ts new file mode 100644 index 000000000..84d41f58d --- /dev/null +++ b/packages/react-docs/src/ts/docs_info.ts @@ -0,0 +1,120 @@ +import { MenuSubsectionsBySection } from '@0xproject/react-shared'; +import compareVersions = require('compare-versions'); +import * as _ from 'lodash'; + +import { + ContractsByVersionByNetworkId, + DocAgnosticFormat, + DocsInfoConfig, + DocsMenu, + DoxityDocObj, + SectionsMap, + SupportedDocJson, + TypeDocNode, +} from './types'; +import { doxityUtils } from './utils/doxity_utils'; +import { typeDocUtils } from './utils/typedoc_utils'; + +export class DocsInfo { + public id: string; + public type: SupportedDocJson; + public displayName: string; + public packageUrl: string; + public menu: DocsMenu; + public sections: SectionsMap; + public sectionNameToMarkdown: { [sectionName: string]: string }; + public contractsByVersionByNetworkId?: ContractsByVersionByNetworkId; + private _docsInfo: DocsInfoConfig; + constructor(config: DocsInfoConfig) { + this.id = config.id; + this.type = config.type; + this.displayName = config.displayName; + this.packageUrl = config.packageUrl; + this.sections = config.sections; + this.sectionNameToMarkdown = config.sectionNameToMarkdown; + this.contractsByVersionByNetworkId = config.contractsByVersionByNetworkId; + this._docsInfo = config; + } + public isPublicType(typeName: string): boolean { + if (_.isUndefined(this._docsInfo.publicTypes)) { + return false; + } + const isPublic = _.includes(this._docsInfo.publicTypes, typeName); + return isPublic; + } + public getModulePathsIfExists(sectionName: string): string[] { + const modulePathsIfExists = this._docsInfo.sectionNameToModulePath[sectionName]; + return modulePathsIfExists; + } + public getMenu(selectedVersion?: string): { [section: string]: string[] } { + if (_.isUndefined(selectedVersion) || _.isUndefined(this._docsInfo.menuSubsectionToVersionWhenIntroduced)) { + return this._docsInfo.menu; + } + + const finalMenu = _.cloneDeep(this._docsInfo.menu); + if (_.isUndefined(finalMenu.contracts)) { + return finalMenu; + } + + // TODO: refactor to include more sections then simply the `contracts` section + finalMenu.contracts = _.filter(finalMenu.contracts, (contractName: string) => { + const versionIntroducedIfExists = this._docsInfo.menuSubsectionToVersionWhenIntroduced[contractName]; + if (!_.isUndefined(versionIntroducedIfExists)) { + const existsInSelectedVersion = compareVersions(selectedVersion, versionIntroducedIfExists) >= 0; + return existsInSelectedVersion; + } else { + return true; + } + }); + return finalMenu; + } + public getMenuSubsectionsBySection(docAgnosticFormat?: DocAgnosticFormat): MenuSubsectionsBySection { + const menuSubsectionsBySection = {} as MenuSubsectionsBySection; + if (_.isUndefined(docAgnosticFormat)) { + return menuSubsectionsBySection; + } + + const docSections = _.keys(this.sections); + _.each(docSections, sectionName => { + const docSection = docAgnosticFormat[sectionName]; + if (_.isUndefined(docSection)) { + return; // no-op + } + + if (!_.isUndefined(this.sections.types) && sectionName === this.sections.types) { + const sortedTypesNames = _.sortBy(docSection.types, 'name'); + const typeNames = _.map(sortedTypesNames, t => t.name); + menuSubsectionsBySection[sectionName] = typeNames; + } else { + let eventNames: string[] = []; + if (!_.isUndefined(docSection.events)) { + const sortedEventNames = _.sortBy(docSection.events, 'name'); + eventNames = _.map(sortedEventNames, m => m.name); + } + const sortedMethodNames = _.sortBy(docSection.methods, 'name'); + const methodNames = _.map(sortedMethodNames, m => m.name); + menuSubsectionsBySection[sectionName] = [...methodNames, ...eventNames]; + } + }); + return menuSubsectionsBySection; + } + public getTypeDefinitionsByName(docAgnosticFormat: DocAgnosticFormat) { + if (_.isUndefined(this.sections.types)) { + return {}; + } + + const typeDocSection = docAgnosticFormat[this.sections.types]; + const typeDefinitionByName = _.keyBy(typeDocSection.types, 'name'); + return typeDefinitionByName; + } + public isVisibleConstructor(sectionName: string): boolean { + return _.includes(this._docsInfo.visibleConstructors, sectionName); + } + public convertToDocAgnosticFormat(docObj: DoxityDocObj | TypeDocNode): DocAgnosticFormat { + if (this.type === SupportedDocJson.Doxity) { + return doxityUtils.convertToDocAgnosticFormat(docObj as DoxityDocObj); + } else { + return typeDocUtils.convertToDocAgnosticFormat(docObj as TypeDocNode, this); + } + } +} diff --git a/packages/react-docs/src/ts/index.ts b/packages/react-docs/src/ts/index.ts index ee2950c0e..85b0cca27 100644 --- a/packages/react-docs/src/ts/index.ts +++ b/packages/react-docs/src/ts/index.ts @@ -1,5 +1,5 @@ export { Documentation } from './components/documentation'; -export { DocsInfo } from './components/docs_info'; +export { DocsInfo } from './docs_info'; // Exported to give users of this library added flexibility if they want to build // a docs page from scratch using the individual components. diff --git a/packages/react-docs/src/ts/utils/typedoc_utils.ts b/packages/react-docs/src/ts/utils/typedoc_utils.ts index 13798889a..e4cea1e40 100644 --- a/packages/react-docs/src/ts/utils/typedoc_utils.ts +++ b/packages/react-docs/src/ts/utils/typedoc_utils.ts @@ -1,6 +1,6 @@ import * as _ from 'lodash'; -import { DocsInfo } from '../components/docs_info'; +import { DocsInfo } from '../docs_info'; import { CustomType, CustomTypeChild, -- cgit