Basic Markdown highlighting
This commit is contained in:
parent
c0d7629980
commit
a5a3e4e915
2 changed files with 43 additions and 10 deletions
|
@ -52,7 +52,7 @@ const ROOM_REGEX = /#\S+:\S+/g;
|
||||||
/**
|
/**
|
||||||
* Returns a composite decorator which has access to provided scope.
|
* Returns a composite decorator which has access to provided scope.
|
||||||
*/
|
*/
|
||||||
export function getScopedDecorator(scope: any): CompositeDecorator {
|
export function getScopedRTDecorators(scope: any): CompositeDecorator {
|
||||||
let MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
|
let MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
|
||||||
|
|
||||||
let usernameDecorator = {
|
let usernameDecorator = {
|
||||||
|
@ -78,9 +78,42 @@ export function getScopedDecorator(scope: any): CompositeDecorator {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return new CompositeDecorator([usernameDecorator, roomDecorator]);
|
return [usernameDecorator, roomDecorator];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getScopedMDDecorators(scope: any): CompositeDecorator {
|
||||||
|
let markdownDecorators = ['BOLD', 'ITALIC'].map(
|
||||||
|
(style) => ({
|
||||||
|
strategy: (contentBlock, callback) => {
|
||||||
|
return findWithRegex(MARKDOWN_REGEX[style], contentBlock, callback);
|
||||||
|
},
|
||||||
|
component: (props) => (
|
||||||
|
<span className={"mx_MarkdownElement mx_Markdown_" + style}>
|
||||||
|
{props.children}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}));
|
||||||
|
|
||||||
|
markdownDecorators.push({
|
||||||
|
strategy: (contentBlock, callback) => {
|
||||||
|
return findWithRegex(MARKDOWN_REGEX.LINK, contentBlock, callback);
|
||||||
|
},
|
||||||
|
component: (props) => (
|
||||||
|
<a href="#" className="mx_MarkdownElement mx_Markdown_LINK">
|
||||||
|
{props.children}
|
||||||
|
</a>
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
return markdownDecorators;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MARKDOWN_REGEX = {
|
||||||
|
LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g,
|
||||||
|
ITALIC: /([\*_])([\w\s]+?)\1/g,
|
||||||
|
BOLD: /([\*_])\1([\w\s]+?)\1\1/g
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function that looks for regex matches within a ContentBlock and invokes {callback} with (start, end)
|
* Utility function that looks for regex matches within a ContentBlock and invokes {callback} with (start, end)
|
||||||
* From https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html
|
* From https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html
|
||||||
|
|
|
@ -99,10 +99,10 @@ export default class MessageComposerInput extends React.Component {
|
||||||
*/
|
*/
|
||||||
createEditorState(contentState: ?ContentState): EditorState {
|
createEditorState(contentState: ?ContentState): EditorState {
|
||||||
let func = contentState ? EditorState.createWithContent : EditorState.createEmpty;
|
let func = contentState ? EditorState.createWithContent : EditorState.createEmpty;
|
||||||
|
const decoratorFunc = this.state.isRichtextEnabled ? RichText.getScopedRTDecorators :
|
||||||
|
RichText.getScopedMDDecorators;
|
||||||
let args = contentState ? [contentState] : [];
|
let args = contentState ? [contentState] : [];
|
||||||
if(this.state.isRichtextEnabled) {
|
args.push(new CompositeDecorator(decoratorFunc(this.props)));
|
||||||
args.push(RichText.getScopedDecorator(this.props));
|
|
||||||
}
|
|
||||||
return func(...args);
|
return func(...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,11 +341,7 @@ export default class MessageComposerInput extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
enableRichtext(enabled: boolean) {
|
enableRichtext(enabled: boolean) {
|
||||||
this.setState({
|
if(enabled) {
|
||||||
isRichtextEnabled: enabled
|
|
||||||
});
|
|
||||||
|
|
||||||
if(!this.state.isRichtextEnabled) {
|
|
||||||
let html = mdownToHtml(this.state.editorState.getCurrentContent().getPlainText());
|
let html = mdownToHtml(this.state.editorState.getCurrentContent().getPlainText());
|
||||||
this.setState({
|
this.setState({
|
||||||
editorState: this.createEditorState(RichText.HTMLtoContentState(html))
|
editorState: this.createEditorState(RichText.HTMLtoContentState(html))
|
||||||
|
@ -357,6 +353,10 @@ export default class MessageComposerInput extends React.Component {
|
||||||
editorState: this.createEditorState(contentState)
|
editorState: this.createEditorState(contentState)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
isRichtextEnabled: enabled
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyCommand(command: string): boolean {
|
handleKeyCommand(command: string): boolean {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue