Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 06c53bd

Browse files
committed
wip
1 parent 77c4ee0 commit 06c53bd

File tree

7 files changed

+111
-66
lines changed

7 files changed

+111
-66
lines changed

docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import Editor, { EDITOR_BACKGROUND_COLOR, EDITOR_GUTTER_COLOR } from 'docs/src/c
1313
import { babelConfig, importResolver } from 'docs/src/components/Playground/renderConfig'
1414
import ComponentControls from '../ComponentControls'
1515
import ComponentExampleTitle from './ComponentExampleTitle'
16-
import SourceManager, { SourceManagerRenderProps } from './SourceCodeManager'
16+
import SourceManager, {
17+
SourceManagerRenderProps,
18+
} from '../ComponentSourceManager/ComponentSourceManager'
1719
import { ThemeInput, ThemePrepared } from 'src/themes/types'
1820
import { mergeThemeVariables } from '../../../../../src/lib/mergeThemes'
1921
import { ThemeContext } from '../../../context/theme-context'
@@ -287,20 +289,29 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
287289
}
288290

289291
handleCodeLanguageChange = language => () => {
290-
this.props.handleCodeLanguageChange(language)
292+
const { handleCodeLanguageChange, wasCodeChanged } = this.props
293+
294+
if (wasCodeChanged) {
295+
if (confirm('Lose your changes?')) {
296+
handleCodeLanguageChange(language)
297+
}
298+
} else {
299+
handleCodeLanguageChange(language)
300+
}
291301
}
292302

293303
renderAPIsMenu = (): JSX.Element => {
294-
const menuItems = _.map(this.props.codeAPIs, ({ active, enabled, name }, type) => (
304+
const { componentAPIs, currentCodeAPI } = this.props
305+
const menuItems = _.map(componentAPIs, ({ name, supported }, type) => (
295306
<Menu.Item
296-
active={active}
307+
active={currentCodeAPI === type}
297308
content={
298309
<span>
299310
{name}
300-
{enabled && <em> (not supported)</em>}
311+
{!supported && <em> (not supported)</em>}
301312
</span>
302313
}
303-
disabled={!enabled}
314+
disabled={!supported}
304315
key={type}
305316
onClick={this.handleCodeApiChange(type)}
306317
/>
@@ -310,17 +321,17 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
310321
}
311322

312323
renderLanguagesMenu = (): JSX.Element => {
313-
const { currentLanguage } = this.props
324+
const { currentCodeLanguage } = this.props
314325

315326
return (
316327
<Menu.Menu position="right">
317328
<Menu.Item
318-
active={currentLanguage === 'js'}
329+
active={currentCodeLanguage === 'js'}
319330
content="JavaScript"
320331
onClick={this.handleCodeLanguageChange('js')}
321332
/>
322333
<Menu.Item
323-
active={currentLanguage === 'ts'}
334+
active={currentCodeLanguage === 'ts'}
324335
content="TypeScript"
325336
onClick={this.handleCodeLanguageChange('ts')}
326337
/>
@@ -329,7 +340,7 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
329340
}
330341

331342
renderCodeEditorMenu = (): JSX.Element => {
332-
const { canBeFormatted, handleCodeFormat, wasChanged } = this.props
343+
const { canCodeBeFormatted, handleCodeFormat, wasCodeChanged } = this.props
333344
const { copiedCode } = this.state
334345

335346
// TODO: !!!!
@@ -350,17 +361,17 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
350361
<SourceRender.Consumer>
351362
{({ error }) => (
352363
<Menu.Item
353-
icon={(error && 'bug') || (canBeFormatted ? 'magic' : 'check')}
364+
icon={(error && 'bug') || (canCodeBeFormatted ? 'magic' : 'check')}
354365
color={error ? 'red' : undefined}
355366
active={error}
356367
content="Prettier"
357368
onClick={handleCodeFormat}
358-
style={!canBeFormatted ? disabledStyle : undefined}
369+
style={!canCodeBeFormatted ? disabledStyle : undefined}
359370
/>
360371
)}
361372
</SourceRender.Consumer>
362373
<Menu.Item
363-
style={!wasChanged ? disabledStyle : undefined}
374+
style={!wasCodeChanged ? disabledStyle : undefined}
364375
icon="refresh"
365376
content="Reset"
366377
onClick={this.resetSourceCode}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { codeAPIs } from './SourceCodeManager'
21
export { default, ComponentExampleProps } from './ComponentExample'

docs/src/components/ComponentDoc/ComponentExamples.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react'
44

55
import { exampleGroupsContext, exampleSourcesContext } from 'docs/src/utils'
66
import { Grid, List } from 'semantic-ui-react'
7-
import { codeAPIs } from './ComponentExample'
7+
import { componentAPIs } from './ComponentSourceManager'
88
import ContributionPrompt from './ContributionPrompt'
99

1010
interface ComponentExamplesProps {
@@ -84,8 +84,8 @@ export default class ComponentExamples extends React.Component<ComponentExamples
8484
const examplesPattern = `\.\/${displayName}[\\w\/]*\/\\w+Example`
8585

8686
const [shorthandExtension, normalExtension] = [
87-
codeAPIs.shorthand.fileSuffix,
88-
codeAPIs.children.fileSuffix,
87+
componentAPIs.shorthand.fileSuffix,
88+
componentAPIs.children.fileSuffix,
8989
].map(pattern => `${pattern}.source.json`)
9090

9191
const [normalRegExp, shorthandRegExp] = [normalExtension, shorthandExtension].map(

docs/src/components/ComponentDoc/ComponentExample/SourceCodeManager.ts renamed to docs/src/components/ComponentDoc/ComponentSourceManager/ComponentSourceManager.ts

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1+
import * as _ from 'lodash'
12
import * as React from 'react'
2-
import { exampleSourcesContext } from '../../../utils'
3+
34
import { ExampleSource } from '../../../types'
45
import { safeFormatCode } from '../../../utils/formatCode'
5-
6-
type SourceManagerCodeAPI = {
7-
name: string
8-
fileSuffix: string
9-
10-
active?: boolean
11-
enabled?: boolean
12-
}
13-
14-
type SourceManagerCodeAPIs = {
15-
children: SourceManagerCodeAPI
16-
shorthand: SourceManagerCodeAPI
17-
}
6+
import { componentAPIs as APIdefinitions, ComponentAPIs } from './componentAPIs'
7+
import getExampleSource from './getExampeSource'
188

199
export type SourceManagerRenderProps = SourceManagerState & {
20-
handleCodeAPIChange: (newApi: keyof SourceManagerCodeAPIs) => void
10+
handleCodeAPIChange: (newApi: keyof ComponentAPIs) => void
2111
handleCodeChange: (newCode: string) => void
2212
handleCodeFormat: () => void
2313
handleCodeLanguageChange: (newLanguage: SourceManagerLanguage) => void
@@ -31,67 +21,76 @@ export type SourceManagerProps = {
3121
children: (renderProps: SourceManagerRenderProps) => React.ReactNode
3222
}
3323

24+
type StateManagerComponentAPIs = ComponentAPIs<{
25+
sourceCode: ExampleSource | undefined
26+
supported: boolean
27+
}>
28+
3429
export type SourceManagerState = {
35-
currentLanguage: SourceManagerLanguage
36-
currentAPI: keyof SourceManagerCodeAPIs
30+
currentCodeLanguage: SourceManagerLanguage
31+
currentCodeAPI: keyof ComponentAPIs
3732

38-
codeAPIs: SourceManagerCodeAPIs
33+
componentAPIs: StateManagerComponentAPIs
3934
currentCode?: string
4035
formattedCode?: string
4136
originalCode?: string
4237

43-
canBeFormatted: boolean
44-
wasChanged: boolean
45-
}
46-
47-
export const codeAPIs: SourceManagerCodeAPIs = {
48-
children: { name: 'Children API', fileSuffix: '' },
49-
shorthand: { name: 'Shorthand API', fileSuffix: '.shorthand' },
38+
canCodeBeFormatted: boolean
39+
wasCodeChanged: boolean
5040
}
5141

5242
export class SourceManager extends React.Component<SourceManagerProps, SourceManagerState> {
53-
state = {
54-
currentLanguage: 'js' as SourceManagerLanguage,
55-
currentAPI: 'children' as keyof SourceManagerCodeAPIs,
56-
57-
codeAPIs,
58-
59-
canBeFormatted: false,
60-
wasChanged: false,
43+
constructor(props: SourceManagerProps) {
44+
super(props)
45+
46+
const componentAPIs = _.mapValues(APIdefinitions, (definition, name: keyof ComponentAPIs) => {
47+
const sourceCode = getExampleSource(props.examplePath, name)
48+
49+
return {
50+
...definition,
51+
sourceCode,
52+
supported: !!sourceCode,
53+
}
54+
}) as StateManagerComponentAPIs
55+
56+
this.state = {
57+
currentCodeLanguage: 'js' as SourceManagerLanguage,
58+
currentCodeAPI: _.findKey(componentAPIs, { supported: true }) as keyof ComponentAPIs,
59+
60+
componentAPIs,
61+
canCodeBeFormatted: false,
62+
wasCodeChanged: false,
63+
}
6164
}
6265

6366
static getDerivedStateFromProps(
6467
props: SourceManagerProps,
6568
state: SourceManagerState,
6669
): Partial<SourceManagerState> {
67-
const { examplePath } = props
68-
const { currentAPI, currentLanguage, currentCode } = state
70+
const { componentAPIs, currentCodeAPI, currentCodeLanguage, currentCode: storedCode } = state
6971

70-
const sourcePath = `${examplePath.replace(/^components/, '.')}${
71-
codeAPIs[currentAPI].fileSuffix
72-
}.source.json`
73-
const sourceCode: ExampleSource = exampleSourcesContext(sourcePath)
72+
const sourceCodes = componentAPIs[currentCodeAPI].sourceCode
73+
const originalCode = sourceCodes[currentCodeLanguage]
7474

75-
const originalCode = sourceCode[currentLanguage]
76-
const currentCode1 = currentCode || originalCode
75+
const currentCode = storedCode || originalCode
7776
const formattedCode = safeFormatCode(
78-
currentCode1,
79-
currentLanguage === 'ts' ? 'typescript' : 'babylon',
77+
currentCode,
78+
currentCodeLanguage === 'ts' ? 'typescript' : 'babylon',
8079
)
8180

8281
return {
8382
originalCode,
84-
currentCode: currentCode1,
83+
currentCode,
8584
formattedCode,
8685

87-
canBeFormatted: !!formattedCode ? currentCode1 !== formattedCode : false,
88-
wasChanged: originalCode !== currentCode1,
86+
canCodeBeFormatted: !!formattedCode ? currentCode !== formattedCode : false,
87+
wasCodeChanged: originalCode !== currentCode,
8988
}
9089
}
9190

92-
handleCodeAPIChange = (newAPI: keyof SourceManagerCodeAPIs): void => {
91+
handleCodeAPIChange = (newAPI: keyof ComponentAPIs): void => {
9392
this.setState({
94-
currentAPI: newAPI,
93+
currentCodeAPI: newAPI,
9594
currentCode: undefined,
9695
})
9796
}
@@ -110,7 +109,7 @@ export class SourceManager extends React.Component<SourceManagerProps, SourceMan
110109

111110
handleLanguageChange = (newLanguage: SourceManagerLanguage): void => {
112111
this.setState({
113-
currentLanguage: newLanguage,
112+
currentCodeLanguage: newLanguage,
114113
currentCode: undefined,
115114
})
116115
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type ComponentAPI<T> = {
2+
name: string
3+
fileSuffix: string
4+
} & T
5+
6+
export type ComponentAPIs<T = {}> = {
7+
children: ComponentAPI<T>
8+
shorthand: ComponentAPI<T>
9+
}
10+
11+
export const componentAPIs: ComponentAPIs = {
12+
children: { name: 'Children API', fileSuffix: '' },
13+
shorthand: { name: 'Shorthand API', fileSuffix: '.shorthand' },
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ExampleSource } from 'docs/src/types'
2+
import { exampleSourcesContext } from 'docs/src/utils'
3+
import { componentAPIs, ComponentAPIs } from './componentAPIs'
4+
5+
const getExampleSource = (
6+
examplePath: string,
7+
componentAPI: keyof ComponentAPIs,
8+
): ExampleSource | undefined => {
9+
const sourcePath = `${examplePath.replace(/^components/, '.')}${
10+
componentAPIs[componentAPI].fileSuffix
11+
}.source.json`
12+
13+
try {
14+
return exampleSourcesContext(sourcePath)
15+
} catch (e) {
16+
return undefined
17+
}
18+
}
19+
20+
export default getExampleSource
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './componentAPIs'
2+
export * from './ComponentSourceManager'

0 commit comments

Comments
 (0)