Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some functional components. #330

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions playground/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import * as React from 'react';
import { PlaygroundView } from './PlaygroundView';

class App extends React.Component {
public render(): React.ReactNode {
return (
<>
<PlaygroundView />
</>
);
}
export default function App(): JSX.Element {
return (
<>
<PlaygroundView />
</>
);
}

export default App;
56 changes: 27 additions & 29 deletions playground/src/DocAstView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,36 @@ export interface IDocAstViewProps {
theme?: string;
}

export class DocAstView extends React.Component<IDocAstViewProps> {
public render(): React.ReactNode {
const parserContext: tsdoc.ParserContext | undefined = this.props.parserContext;
const outputLines: string[] = [];
export function DocAstView(props: IDocAstViewProps): JSX.Element {
const parserContext: tsdoc.ParserContext | undefined = props.parserContext;
const outputLines: string[] = [];

if (parserContext && parserContext.docComment) {
this._dumpTSDocTree(outputLines, parserContext.docComment);
}

return (
<CodeEditor
className="playground-ast-text-editor"
readOnly={true}
value={outputLines.join('\n')}
disableLineNumbers={true}
theme={this.props.theme}
/>
);
if (parserContext && parserContext.docComment) {
_dumpTSDocTree(outputLines, parserContext.docComment);
}

private _dumpTSDocTree(outputLines: string[], docNode: tsdoc.DocNode, indent: string = ''): void {
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);
return (
<CodeEditor
className="playground-ast-text-editor"
readOnly={true}
value={outputLines.join('\n')}
disableLineNumbers={true}
theme={props.theme}
/>
);
}

function _dumpTSDocTree(outputLines: string[], docNode: tsdoc.DocNode, indent: string = ''): void {
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()) {
this._dumpTSDocTree(outputLines, child, indent + ' ');
}
for (const child of docNode.getChildNodes()) {
_dumpTSDocTree(outputLines, child, indent + ' ');
}
}
140 changes: 69 additions & 71 deletions playground/src/DocDomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,94 +16,92 @@ export interface IDocDomViewProps {
theme?: string;
}

export class DocDomView extends React.Component<IDocDomViewProps> {
public render(): React.ReactNode {
const parserContext: tsdoc.ParserContext | undefined = this.props.parserContext;
let code: string = '';

if (parserContext && parserContext.docComment) {
const unindentedCode: string = ReactDomServer.renderToStaticMarkup(
<DocHtmlView docComment={parserContext.docComment} />
);
code = this._indentHtml(unindentedCode);
}
export function DocDomView(props: IDocDomViewProps): JSX.Element {
const parserContext: tsdoc.ParserContext | undefined = props.parserContext;
let code: string = '';

return (
<CodeEditor
className="playground-dom-text-editor"
readOnly={true}
value={code}
language="html"
disableLineNumbers={true}
theme={this.props.theme}
/>
if (parserContext && parserContext.docComment) {
const unindentedCode: string = ReactDomServer.renderToStaticMarkup(
<DocHtmlView docComment={parserContext.docComment} />
);
code = _indentHtml(unindentedCode);
}

// Given a string containing perfectly systematic HTML (like what React generates),
// this adds appropriate indentation
private _indentHtml(html: string): string {
const tagRegExp: RegExp = /\<\/|\<|\>/g;
const output: string[] = [];
return (
<CodeEditor
className="playground-dom-text-editor"
readOnly={true}
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;
let lastIndex: number = 0;
let indentLevel: number = 0;
let lastTag: string = '';
const indentCharacters: string = ' ';

for (;;) {
match = tagRegExp.exec(html);
let match: RegExpExecArray | null;
let lastIndex: number = 0;
let indentLevel: number = 0;
let lastTag: string = '';

if (!match) {
break;
}
for (;;) {
match = tagRegExp.exec(html);

const matchIndex: number = match.index;
if (!match) {
break;
}

const textBeforeMatch: string = html.substring(lastIndex, matchIndex);
if (textBeforeMatch.length > 0) {
output.push(textBeforeMatch);
}
const matchIndex: number = match.index;

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);
}
const textBeforeMatch: string = html.substring(lastIndex, matchIndex);
if (textBeforeMatch.length > 0) {
output.push(textBeforeMatch);
}

++indentLevel;
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);
}

lastTag = '<';
break;
case '</':
// Matched closing tag
--indentLevel;
++indentLevel;

if (lastTag !== '<') {
output.push('\n');
for (let i: number = 0; i < indentLevel; ++i) {
output.push(indentCharacters);
}
}
lastTag = '<';
break;
case '</':
// Matched closing tag
--indentLevel;

lastTag = '</';
break;
case '>':
break;
}
if (lastTag !== '<') {
output.push('\n');
for (let i: number = 0; i < indentLevel; ++i) {
output.push(indentCharacters);
}
}

lastIndex = matchIndex;
lastTag = '</';
break;
case '>':
break;
}

const endingText: string = html.substring(lastIndex);
output.push(endingText + '\n');

return output.join('');
lastIndex = matchIndex;
}

const endingText: string = html.substring(lastIndex);
output.push(endingText + '\n');

return output.join('');
}
50 changes: 18 additions & 32 deletions playground/src/FlexDivs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,28 @@ import * as React from 'react';
export interface IFlexDivProps
extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {}

export class FlexRowDiv extends React.Component<IFlexDivProps> {
public render(): React.ReactNode {
const mergedProps: IFlexDivProps = {
...this.props
};

if (mergedProps.style === undefined) {
mergedProps.style = {};
}
if (mergedProps.style.display === undefined) {
mergedProps.style.display = 'flex';
}
if (mergedProps.style.flexDirection === undefined) {
mergedProps.style.flexDirection = 'row';
export function FlexRowDiv(props: IFlexDivProps): JSX.Element {
const mergedProps: IFlexDivProps = {
...props,
style: {
display: 'flex',
flexDirection: 'row',
...props.style
}
};

return React.createElement('div', mergedProps);
}
return React.createElement('div', mergedProps);
}

export class FlexColDiv extends React.Component<IFlexDivProps> {
public render(): React.ReactNode {
const mergedProps: IFlexDivProps = {
...this.props
};

if (mergedProps.style === undefined) {
mergedProps.style = {};
}
if (mergedProps.style.display === undefined) {
mergedProps.style.display = 'flex';
}
if (mergedProps.style.flexDirection === undefined) {
mergedProps.style.flexDirection = 'column';
export function FlexColDiv(props: IFlexDivProps): JSX.Element {
const mergedProps: IFlexDivProps = {
...props,
style: {
display: 'flex',
flexDirection: 'column',
...props.style
}
};

return React.createElement('div', mergedProps);
}
return React.createElement('div', mergedProps);
}