Merge pull request #330 from iclanton/clean-up-react-components

Clean up some functional components.
This commit is contained in:
Pete Gonzalez 2023-05-03 11:05:42 -07:00 коммит произвёл GitHub
Родитель f4b9d8659a 74577c78b9
Коммит a4114a0538
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 124 добавлений и 146 удалений

Просмотреть файл

@ -1,14 +1,10 @@
import * as React from 'react'; import * as React from 'react';
import { PlaygroundView } from './PlaygroundView'; import { PlaygroundView } from './PlaygroundView';
class App extends React.Component { export default function App(): JSX.Element {
public render(): React.ReactNode { return (
return ( <>
<> <PlaygroundView />
<PlaygroundView /> </>
</> );
);
}
} }
export default App;

Просмотреть файл

@ -8,38 +8,36 @@ export interface IDocAstViewProps {
theme?: string; theme?: string;
} }
export class DocAstView extends React.Component<IDocAstViewProps> { export function DocAstView(props: IDocAstViewProps): JSX.Element {
public render(): React.ReactNode { const parserContext: tsdoc.ParserContext | undefined = props.parserContext;
const parserContext: tsdoc.ParserContext | undefined = this.props.parserContext; const outputLines: string[] = [];
const outputLines: string[] = [];
if (parserContext && parserContext.docComment) { if (parserContext && parserContext.docComment) {
this._dumpTSDocTree(outputLines, parserContext.docComment); _dumpTSDocTree(outputLines, parserContext.docComment);
}
return (
<CodeEditor
className="playground-ast-text-editor"
readOnly={true}
value={outputLines.join('\n')}
disableLineNumbers={true}
theme={this.props.theme}
/>
);
} }
private _dumpTSDocTree(outputLines: string[], docNode: tsdoc.DocNode, indent: string = ''): void { return (
let dumpText: string = ''; <CodeEditor
if (docNode instanceof tsdoc.DocExcerpt) { className="playground-ast-text-editor"
const content: string = docNode.content.toString(); readOnly={true}
dumpText += `${indent}* ${docNode.excerptKind}=` + JSON.stringify(content); value={outputLines.join('\n')}
} else { disableLineNumbers={true}
dumpText += `${indent}- ${docNode.kind}`; theme={props.theme}
} />
outputLines.push(dumpText); );
}
for (const child of docNode.getChildNodes()) { function _dumpTSDocTree(outputLines: string[], docNode: tsdoc.DocNode, indent: string = ''): void {
this._dumpTSDocTree(outputLines, child, indent + ' '); let dumpText: string = '';
} if (docNode instanceof tsdoc.DocExcerpt) {
const content: string = docNode.content.toString();
dumpText += `${indent}* ${docNode.excerptKind}=` + JSON.stringify(content);
} else {
dumpText += `${indent}- ${docNode.kind}`;
}
outputLines.push(dumpText);
for (const child of docNode.getChildNodes()) {
_dumpTSDocTree(outputLines, child, indent + ' ');
} }
} }

Просмотреть файл

@ -16,94 +16,92 @@ export interface IDocDomViewProps {
theme?: string; theme?: string;
} }
export class DocDomView extends React.Component<IDocDomViewProps> { export function DocDomView(props: IDocDomViewProps): JSX.Element {
public render(): React.ReactNode { const parserContext: tsdoc.ParserContext | undefined = props.parserContext;
const parserContext: tsdoc.ParserContext | undefined = this.props.parserContext; let code: string = '';
let code: string = '';
if (parserContext && parserContext.docComment) { if (parserContext && parserContext.docComment) {
const unindentedCode: string = ReactDomServer.renderToStaticMarkup( const unindentedCode: string = ReactDomServer.renderToStaticMarkup(
<DocHtmlView docComment={parserContext.docComment} /> <DocHtmlView docComment={parserContext.docComment} />
);
code = this._indentHtml(unindentedCode);
}
return (
<CodeEditor
className="playground-dom-text-editor"
readOnly={true}
value={code}
language="html"
disableLineNumbers={true}
theme={this.props.theme}
/>
); );
code = _indentHtml(unindentedCode);
} }
// Given a string containing perfectly systematic HTML (like what React generates), return (
// this adds appropriate indentation <CodeEditor
private _indentHtml(html: string): string { className="playground-dom-text-editor"
const tagRegExp: RegExp = /\<\/|\<|\>/g; readOnly={true}
const output: string[] = []; value={code}
language="html"
disableLineNumbers={true}
theme={props.theme}
/>
);
}
const indentCharacters: string = ' '; // Given a string containing perfectly systematic HTML (like what React generates),
// this adds appropriate indentation
function _indentHtml(html: string): string {
const tagRegExp: RegExp = /\<\/|\<|\>/g;
const output: string[] = [];
let match: RegExpExecArray | null; const indentCharacters: string = ' ';
let lastIndex: number = 0;
let indentLevel: number = 0;
let lastTag: string = '';
for (;;) { let match: RegExpExecArray | null;
match = tagRegExp.exec(html); let lastIndex: number = 0;
let indentLevel: number = 0;
let lastTag: string = '';
if (!match) { for (;;) {
match = tagRegExp.exec(html);
if (!match) {
break;
}
const matchIndex: number = match.index;
const textBeforeMatch: string = html.substring(lastIndex, matchIndex);
if (textBeforeMatch.length > 0) {
output.push(textBeforeMatch);
}
switch (match[0]) {
case '<':
// Matched opening tag
if (output.length > 0) {
output.push('\n');
}
for (let i: number = 0; i < indentLevel; ++i) {
output.push(indentCharacters);
}
++indentLevel;
lastTag = '<';
break; break;
} case '</':
// Matched closing tag
--indentLevel;
const matchIndex: number = match.index; if (lastTag !== '<') {
output.push('\n');
const textBeforeMatch: string = html.substring(lastIndex, matchIndex);
if (textBeforeMatch.length > 0) {
output.push(textBeforeMatch);
}
switch (match[0]) {
case '<':
// Matched opening tag
if (output.length > 0) {
output.push('\n');
}
for (let i: number = 0; i < indentLevel; ++i) { for (let i: number = 0; i < indentLevel; ++i) {
output.push(indentCharacters); output.push(indentCharacters);
} }
}
++indentLevel; lastTag = '</';
break;
lastTag = '<'; case '>':
break; break;
case '</':
// Matched closing tag
--indentLevel;
if (lastTag !== '<') {
output.push('\n');
for (let i: number = 0; i < indentLevel; ++i) {
output.push(indentCharacters);
}
}
lastTag = '</';
break;
case '>':
break;
}
lastIndex = matchIndex;
} }
const endingText: string = html.substring(lastIndex); lastIndex = matchIndex;
output.push(endingText + '\n');
return output.join('');
} }
const endingText: string = html.substring(lastIndex);
output.push(endingText + '\n');
return output.join('');
} }

Просмотреть файл

@ -3,42 +3,28 @@ import * as React from 'react';
export interface IFlexDivProps export interface IFlexDivProps
extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {} extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {}
export class FlexRowDiv extends React.Component<IFlexDivProps> { export function FlexRowDiv(props: IFlexDivProps): JSX.Element {
public render(): React.ReactNode { const mergedProps: IFlexDivProps = {
const mergedProps: IFlexDivProps = { ...props,
...this.props style: {
}; display: 'flex',
flexDirection: 'row',
...props.style
}
};
if (mergedProps.style === undefined) { return React.createElement('div', mergedProps);
mergedProps.style = {};
}
if (mergedProps.style.display === undefined) {
mergedProps.style.display = 'flex';
}
if (mergedProps.style.flexDirection === undefined) {
mergedProps.style.flexDirection = 'row';
}
return React.createElement('div', mergedProps);
}
} }
export class FlexColDiv extends React.Component<IFlexDivProps> { export function FlexColDiv(props: IFlexDivProps): JSX.Element {
public render(): React.ReactNode { const mergedProps: IFlexDivProps = {
const mergedProps: IFlexDivProps = { ...props,
...this.props style: {
}; display: 'flex',
flexDirection: 'column',
...props.style
}
};
if (mergedProps.style === undefined) { return React.createElement('div', mergedProps);
mergedProps.style = {};
}
if (mergedProps.style.display === undefined) {
mergedProps.style.display = 'flex';
}
if (mergedProps.style.flexDirection === undefined) {
mergedProps.style.flexDirection = 'column';
}
return React.createElement('div', mergedProps);
}
} }