2016-06-09 18:23:09 +00:00
|
|
|
import {Editor, ContentState, convertFromHTML, DefaultDraftBlockRenderMap, DefaultDraftInlineStyle, CompositeDecorator} from 'draft-js';
|
2016-05-27 04:45:55 +00:00
|
|
|
const ReactDOM = require('react-dom');
|
2016-06-09 18:23:09 +00:00
|
|
|
var sdk = require('./index');
|
2016-05-27 04:45:55 +00:00
|
|
|
|
2016-05-28 06:28:22 +00:00
|
|
|
const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {
|
|
|
|
element: 'p' // draft uses <div> by default which we don't really like
|
|
|
|
});
|
|
|
|
|
2016-05-27 04:45:55 +00:00
|
|
|
const styles = {
|
|
|
|
BOLD: 'strong',
|
|
|
|
CODE: 'code',
|
|
|
|
ITALIC: 'em',
|
|
|
|
STRIKETHROUGH: 's',
|
|
|
|
UNDERLINE: 'u'
|
|
|
|
};
|
|
|
|
|
|
|
|
export function contentStateToHTML(contentState:ContentState): String {
|
|
|
|
const elem = contentState.getBlockMap().map((block) => {
|
2016-05-28 06:28:22 +00:00
|
|
|
const elem = BLOCK_RENDER_MAP.get(block.getType()).element;
|
2016-05-27 04:45:55 +00:00
|
|
|
const content = [];
|
|
|
|
block.findStyleRanges(() => true, (s, e) => {
|
|
|
|
const tags = block.getInlineStyleAt(s).map(style => styles[style]);
|
|
|
|
const open = tags.map(tag => `<${tag}>`).join('');
|
|
|
|
const close = tags.map(tag => `</${tag}>`).reverse().join('');
|
|
|
|
content.push(`${open}${block.getText().substring(s, e)}${close}`);
|
|
|
|
});
|
|
|
|
|
2016-05-28 06:28:22 +00:00
|
|
|
return (`<${elem}>${content.join('')}</${elem}>`);
|
2016-05-27 04:45:55 +00:00
|
|
|
}).join('');
|
|
|
|
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function HTMLtoContentState(html:String): ContentState {
|
|
|
|
return ContentState.createFromBlockArray(convertFromHTML(html));
|
|
|
|
}
|
2016-06-09 18:23:09 +00:00
|
|
|
|
|
|
|
const USERNAME_REGEX = /@\S+:\S+/g;
|
|
|
|
const ROOM_REGEX = /#\S+:\S+/g;
|
|
|
|
|
|
|
|
export function getScopedDecorator(scope) {
|
|
|
|
const MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
|
|
|
|
|
|
|
|
const usernameDecorator = {
|
|
|
|
strategy: (contentBlock, callback) => {
|
|
|
|
findWithRegex(USERNAME_REGEX, contentBlock, callback);
|
|
|
|
},
|
|
|
|
component: (props) => {
|
|
|
|
console.log(props.children);
|
|
|
|
console.log(props.children[0].props.text);
|
|
|
|
const member = scope.room.getMember(props.children[0].props.text);
|
|
|
|
console.log(scope);
|
|
|
|
window.scope = scope;
|
|
|
|
let name = null;
|
|
|
|
if(!!member) {
|
|
|
|
name = member.name;
|
|
|
|
}
|
|
|
|
console.log(member);
|
|
|
|
const avatar = member ? <MemberAvatar member={member} width={16} height={16} /> : null;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|