2016-06-09 20:23:09 +02:00
|
|
|
import {Editor, ContentState, convertFromHTML, DefaultDraftBlockRenderMap, DefaultDraftInlineStyle, CompositeDecorator} from 'draft-js';
|
2016-06-11 12:22:08 +02:00
|
|
|
import * as sdk from './index';
|
2016-05-27 06:45:55 +02:00
|
|
|
|
2016-05-28 08:28:22 +02:00
|
|
|
const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {
|
2016-06-11 12:22:08 +02:00
|
|
|
element: 'p' // draft uses <div> by default which we don't really like, so we're using <p>
|
2016-05-28 08:28:22 +02:00
|
|
|
});
|
|
|
|
|
2016-05-27 06:45:55 +02:00
|
|
|
const styles = {
|
|
|
|
BOLD: 'strong',
|
|
|
|
CODE: 'code',
|
|
|
|
ITALIC: 'em',
|
|
|
|
STRIKETHROUGH: 's',
|
|
|
|
UNDERLINE: 'u'
|
|
|
|
};
|
|
|
|
|
2016-06-11 12:22:08 +02:00
|
|
|
export function contentStateToHTML(contentState: ContentState): string {
|
|
|
|
return contentState.getBlockMap().map((block) => {
|
|
|
|
let elem = BLOCK_RENDER_MAP.get(block.getType()).element;
|
|
|
|
let content = [];
|
|
|
|
block.findStyleRanges(() => true, (start, end) => {
|
|
|
|
const tags = block.getInlineStyleAt(start).map(style => styles[style]);
|
2016-05-27 06:45:55 +02:00
|
|
|
const open = tags.map(tag => `<${tag}>`).join('');
|
|
|
|
const close = tags.map(tag => `</${tag}>`).reverse().join('');
|
2016-06-11 12:22:08 +02:00
|
|
|
content.push(`${open}${block.getText().substring(start, end)}${close}`);
|
2016-05-27 06:45:55 +02:00
|
|
|
});
|
|
|
|
|
2016-05-28 08:28:22 +02:00
|
|
|
return (`<${elem}>${content.join('')}</${elem}>`);
|
2016-05-27 06:45:55 +02:00
|
|
|
}).join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function HTMLtoContentState(html:String): ContentState {
|
|
|
|
return ContentState.createFromBlockArray(convertFromHTML(html));
|
|
|
|
}
|
2016-06-09 20:23:09 +02:00
|
|
|
|
|
|
|
const USERNAME_REGEX = /@\S+:\S+/g;
|
|
|
|
const ROOM_REGEX = /#\S+:\S+/g;
|
|
|
|
|
2016-06-11 12:22:08 +02:00
|
|
|
/**
|
|
|
|
* Returns a composite decorator which has access to provided scope.
|
|
|
|
*
|
|
|
|
* @param scope
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2016-06-09 20:23:09 +02:00
|
|
|
export function getScopedDecorator(scope) {
|
|
|
|
const MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
|
|
|
|
|
|
|
|
const usernameDecorator = {
|
|
|
|
strategy: (contentBlock, callback) => {
|
|
|
|
findWithRegex(USERNAME_REGEX, contentBlock, callback);
|
|
|
|
},
|
|
|
|
component: (props) => {
|
2016-06-11 12:22:08 +02:00
|
|
|
let member = scope.room.getMember(props.children[0].props.text);
|
2016-06-09 20:23:09 +02:00
|
|
|
let name = null;
|
|
|
|
if(!!member) {
|
|
|
|
name = member.name;
|
|
|
|
}
|
|
|
|
console.log(member);
|
2016-06-11 12:22:08 +02:00
|
|
|
let avatar = member ? <MemberAvatar member={member} width={16} height={16} /> : null;
|
2016-06-09 20:23:09 +02:00
|
|
|
return <span className="mx_UserPill">{avatar} {props.children}</span>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const roomDecorator = {
|
|
|
|
strategy: (contentBlock, callback) => {
|
|
|
|
findWithRegex(ROOM_REGEX, contentBlock, callback);
|
|
|
|
},
|
|
|
|
component: (props) => {
|
|
|
|
return <span className="mx_RoomPill">{props.children}</span>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return new CompositeDecorator([usernameDecorator, roomDecorator]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findWithRegex(regex, contentBlock, callback) {
|
|
|
|
const text = contentBlock.getText();
|
|
|
|
let matchArr, start;
|
|
|
|
while ((matchArr = regex.exec(text)) !== null) {
|
|
|
|
start = matchArr.index;
|
|
|
|
callback(start, start + matchArr[0].length);
|
|
|
|
}
|
|
|
|
}
|