Use extra property to determine the content type
This commit is contained in:
parent
69f1d721f2
commit
2ad7409750
|
|
@ -8,6 +8,7 @@ import Container from '../common/Container';
|
||||||
import * as config from '../config';
|
import * as config from '../config';
|
||||||
import {StyleRulesCallback} from '@material-ui/core/styles/withStyles';
|
import {StyleRulesCallback} from '@material-ui/core/styles/withStyles';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
|
import {RenderMode, contentType} from './extras';
|
||||||
|
|
||||||
const styles: StyleRulesCallback = () => ({
|
const styles: StyleRulesCallback = () => ({
|
||||||
header: {
|
header: {
|
||||||
|
|
@ -53,6 +54,7 @@ interface IProps {
|
||||||
date: string;
|
date: string;
|
||||||
content: string;
|
content: string;
|
||||||
fDelete: VoidFunction;
|
fDelete: VoidFunction;
|
||||||
|
extras?: IMessageExtras;
|
||||||
height: (height: number) => void;
|
height: (height: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,8 +64,20 @@ class Message extends React.PureComponent<IProps & WithStyles<typeof styles>> {
|
||||||
public componentDidMount = () =>
|
public componentDidMount = () =>
|
||||||
this.props.height(this.node ? this.node.getBoundingClientRect().height : 0);
|
this.props.height(this.node ? this.node.getBoundingClientRect().height : 0);
|
||||||
|
|
||||||
|
private renderContent = () => {
|
||||||
|
const content = this.props.content;
|
||||||
|
switch (contentType(this.props.extras)) {
|
||||||
|
case RenderMode.Markdown:
|
||||||
|
return <ReactMarkdown source={content} escapeHtml={true} />;
|
||||||
|
case RenderMode.Plain:
|
||||||
|
default:
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public render(): React.ReactNode {
|
public render(): React.ReactNode {
|
||||||
const {fDelete, classes, title, date, content, image} = this.props;
|
const {fDelete, classes, title, date, image} = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`${classes.wrapperPadding} message`} ref={(ref) => (this.node = ref)}>
|
<div className={`${classes.wrapperPadding} message`} ref={(ref) => (this.node = ref)}>
|
||||||
<Container style={{display: 'flex'}}>
|
<Container style={{display: 'flex'}}>
|
||||||
|
|
@ -93,7 +107,7 @@ class Message extends React.PureComponent<IProps & WithStyles<typeof styles>> {
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</div>
|
</div>
|
||||||
<Typography component="div" className={`${classes.content} content`}>
|
<Typography component="div" className={`${classes.content} content`}>
|
||||||
<ReactMarkdown source={content} escapeHtml={true} />
|
{this.renderContent()}
|
||||||
</Typography>
|
</Typography>
|
||||||
</div>
|
</div>
|
||||||
</Container>
|
</Container>
|
||||||
|
|
|
||||||
|
|
@ -116,6 +116,7 @@ class Messages extends Component<IProps & Stores<'messagesStore' | 'appStore'>,
|
||||||
date={message.date}
|
date={message.date}
|
||||||
content={message.message}
|
content={message.message}
|
||||||
image={message.image}
|
image={message.image}
|
||||||
|
extras={message.extras}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
export enum RenderMode {
|
||||||
|
Markdown = 'text/markdown',
|
||||||
|
Plain = 'text/plain',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const contentType = (extras?: IMessageExtras): RenderMode => {
|
||||||
|
const type = extract(extras, 'client::display', 'contentType');
|
||||||
|
const valid = Object.keys(RenderMode)
|
||||||
|
.map((k) => RenderMode[k])
|
||||||
|
.some((mode) => mode === type);
|
||||||
|
return valid ? type : RenderMode.Plain;
|
||||||
|
};
|
||||||
|
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
const extract = (extras: IMessageExtras | undefined, key: string, path: string): any => {
|
||||||
|
if (!extras) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!extras[key]) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!extras[key][path]) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return extras[key][path];
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue