Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Rich Text Editor #292

Merged
merged 19 commits into from
Jun 14, 2016
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
},
"dependencies": {
"classnames": "^2.1.2",
"draft-js": "^0.7.0",
"draft-js-export-html": "^0.2.2",
"draft-js-export-markdown": "^0.2.0",
"draft-js-import-markdown": "^0.1.6",
"favico.js": "^0.3.10",
"filesize": "^3.1.2",
"flux": "^2.0.3",
Expand Down
155 changes: 155 additions & 0 deletions src/RichText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {
Editor,
Modifier,
ContentState,
convertFromHTML,
DefaultDraftBlockRenderMap,
DefaultDraftInlineStyle,
CompositeDecorator
} from 'draft-js';
import * as sdk from './index';

const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {
element: 'p' // draft uses <div> by default which we don't really like, so we're using <p>
});

const STYLES = {
BOLD: 'strong',
CODE: 'code',
ITALIC: 'em',
STRIKETHROUGH: 's',
UNDERLINE: 'u'
};

const MARKDOWN_REGEX = {
LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we've done this to fit into the same model as richtext, but doing this manually with regexes rather than using the markd library feels like a major step back here. There must be lots of markdown syntax we don't support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this *only *handles the preview that's rendered in the composer (which
for obvious reasons can't be a proper rendering) -- the sent message still
uses the markdown library.

On Mon, Jun 13, 2016 at 11:29 PM David Baker notifications@github.com
wrote:

In src/RichText.js
#292 (comment)
:

+import * as sdk from './index';
+
+const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {

  • element: 'p' // draft uses
    by default which we don't really like, so we're using


    +});

+const STYLES = {

  • BOLD: 'strong',
  • CODE: 'code',
  • ITALIC: 'em',
  • STRIKETHROUGH: 's',
  • UNDERLINE: 'u'
    +};

+const MARKDOWN_REGEX = {

  • LINK: /(?:[([^]]+)](([^)]+)))|<(\w+://[^>]+)>/g,

I guess we've done this to fit into the same model as richtext, but doing
this manually with regexes rather than using the markd library feels like a
major step back here. There must be lots of markdown syntax we don't
support?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/matrix-org/matrix-react-sdk/pull/292/files/df69d1de44810d2c6d8a338be33b73ee8829d177#r66838004,
or mute the thread
https://github.com/notifications/unsubscribe/AAOlRL8py71C3WdN0zy-XEyk2avtK7rtks5qLZqZgaJpZM4IoK58
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I see. Still doesn't feel great to have two different types of parsing for the markdown, but if it's not on the critical message path then probably ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I was complaining about it on #vector-dev too, but there's very
little that can be done here.

On Tue, Jun 14, 2016 at 12:13 AM David Baker notifications@github.com
wrote:

In src/RichText.js
#292 (comment)
:

+import * as sdk from './index';
+
+const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {

  • element: 'p' // draft uses
    by default which we don't really like, so we're using


    +});

+const STYLES = {

  • BOLD: 'strong',
  • CODE: 'code',
  • ITALIC: 'em',
  • STRIKETHROUGH: 's',
  • UNDERLINE: 'u'
    +};

+const MARKDOWN_REGEX = {

  • LINK: /(?:[([^]]+)](([^)]+)))|<(\w+://[^>]+)>/g,

Ah right, I see. Still doesn't feel great to have two different types of
parsing for the markdown, but if it's not on the critical message path then
probably ok.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/matrix-org/matrix-react-sdk/pull/292/files/df69d1de44810d2c6d8a338be33b73ee8829d177#r66845074,
or mute the thread
https://github.com/notifications/unsubscribe/AAOlRHcbqZZTJzWOzcFdS4A5Xabm-zmlks5qLaTZgaJpZM4IoK58
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you parse it to markdown then render that markdown as the preview?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because at the end of the day, it still needs to be Markdown: using rendered Markdown will strip formatting characters and mangle tables, code blocks, etc. such that it becomes more like a rich text editor than a markdown one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mangle tables, code blocks, etc

I don't see how you can sensibly display these things "as-you-type" Markdown. If we can't do all of markdown, we'll end up with a mix of some stuff that is easy to render as markdown and some stuff which is too difficult to render sensibly so we don't bother. I'm not sure I see the value in this.

If we do have a way of representing all of Markdown as you type which doesn't make me want to stab the input box, then surely we should be using the actual hooks given to us in the marked library to implement custom Renderers for things like strong, em, codespan, etc. This would make things a lot cleaner and doesn't introduce subtle formatting differences between our hand-crafted regex and the marked parser. See https://github.com/chjj/marked/blob/88ce4df47c4d994dc1b1df1477a21fb893e11ddc/lib/marked.js#L857

ITALIC: /([\*_])([\w\s]+?)\1/g,
BOLD: /([\*_])\1([\w\s]+?)\1\1/g
};

const USERNAME_REGEX = /@\S+:\S+/g;
const ROOM_REGEX = /#\S+:\S+/g;

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, // always return true => don't filter any ranges out
(start, end) => {
// map style names to elements
let tags = block.getInlineStyleAt(start).map(style => STYLES[style]).filter(style => !!style);
// combine them to get well-nested HTML
let open = tags.map(tag => `<${tag}>`).join('');
let close = tags.map(tag => `</${tag}>`).reverse().join('');
// and get the HTML representation of this styled range (this .substring() should never fail)
content.push(`${open}${block.getText().substring(start, end)}${close}`);
}
);

return (`<${elem}>${content.join('')}</${elem}>`);
}).join('');
}

export function HTMLtoContentState(html: string): ContentState {
return ContentState.createFromBlockArray(convertFromHTML(html));
}

/**
* Returns a composite decorator which has access to provided scope.
*/
export function getScopedRTDecorators(scope: any): CompositeDecorator {
let MemberAvatar = sdk.getComponent('avatars.MemberAvatar');

let usernameDecorator = {
strategy: (contentBlock, callback) => {
findWithRegex(USERNAME_REGEX, contentBlock, callback);
},
component: (props) => {
let member = scope.room.getMember(props.children[0].props.text);
// unused until we make these decorators immutable (autocomplete needed)
let name = member ? member.name : null;
let avatar = member ? <MemberAvatar member={member} width={16} height={16}/> : null;
return <span className="mx_UserPill">{avatar} {props.children}</span>;
}
};
let roomDecorator = {
strategy: (contentBlock, callback) => {
findWithRegex(ROOM_REGEX, contentBlock, callback);
},
component: (props) => {
return <span className="mx_RoomPill">{props.children}</span>;
}
};

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;
}

/**
* 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
*/
function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: number, end: number) => any) {
const text = contentBlock.getText();
let matchArr, start;
while ((matchArr = regex.exec(text)) !== null) {
start = matchArr.index;
callback(start, start + matchArr[0].length);
}
}

/**
* Passes rangeToReplace to modifyFn and replaces it in contentState with the result.
*/
export function modifyText(contentState: ContentState, rangeToReplace: SelectionState,
modifyFn: (text: string) => string, inlineStyle, entityKey): ContentState {
let getText = (key) => contentState.getBlockForKey(key).getText(),
startKey = rangeToReplace.getStartKey(),
startOffset = rangeToReplace.getStartOffset(),
endKey = rangeToReplace.getEndKey(),
endOffset = rangeToReplace.getEndOffset(),
text = "";


for(let currentKey = startKey;
currentKey && currentKey !== endKey;
currentKey = contentState.getKeyAfter(currentKey)) {
let blockText = getText(currentKey);
text += blockText.substring(startOffset, blockText.length);

// from now on, we'll take whole blocks
startOffset = 0;
}

// add remaining part of last block
text += getText(endKey).substring(startOffset, endOffset);

return Modifier.replaceText(contentState, rangeToReplace, modifyFn(text), inlineStyle, entityKey);
}
1 change: 1 addition & 0 deletions src/component-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports.components['views.rooms.MemberList'] = require('./components/view
module.exports.components['views.rooms.MemberTile'] = require('./components/views/rooms/MemberTile');
module.exports.components['views.rooms.MessageComposer'] = require('./components/views/rooms/MessageComposer');
module.exports.components['views.rooms.MessageComposerInput'] = require('./components/views/rooms/MessageComposerInput');
module.exports.components['views.rooms.MessageComposerInputOld'] = require('./components/views/rooms/MessageComposerInputOld');
module.exports.components['views.rooms.PresenceLabel'] = require('./components/views/rooms/PresenceLabel');
module.exports.components['views.rooms.ReadReceiptMarker'] = require('./components/views/rooms/ReadReceiptMarker');
module.exports.components['views.rooms.RoomHeader'] = require('./components/views/rooms/RoomHeader');
Expand Down
5 changes: 4 additions & 1 deletion src/components/views/rooms/MessageComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var Modal = require('../../../Modal');
var sdk = require('../../../index');
var dis = require('../../../dispatcher');

import UserSettingsStore from '../../../UserSettingsStore';


module.exports = React.createClass({
displayName: 'MessageComposer',
Expand Down Expand Up @@ -131,7 +133,8 @@ module.exports = React.createClass({
var uploadInputStyle = {display: 'none'};
var MemberAvatar = sdk.getComponent('avatars.MemberAvatar');
var TintableSvg = sdk.getComponent("elements.TintableSvg");
var MessageComposerInput = sdk.getComponent("rooms.MessageComposerInput");
var MessageComposerInput = sdk.getComponent("rooms.MessageComposerInput" +
(UserSettingsStore.isFeatureEnabled('rich_text_editor') ? "" : "Old"));

var controls = [];

Expand Down
Loading