make it work for MD mode (modulo history)

This commit is contained in:
Matthew Hodgson 2018-05-06 22:08:36 +01:00
parent 190f6d9483
commit ff42ef4a58
3 changed files with 52 additions and 37 deletions

View file

@ -34,17 +34,15 @@ class HistoryItem {
format: MessageFormat = 'rich'; format: MessageFormat = 'rich';
constructor(value: ?Value, format: ?MessageFormat) { constructor(value: ?Value, format: ?MessageFormat) {
this.rawContentState = contentState ? convertToRaw(contentState) : null; this.value = value;
this.format = format; this.format = format;
} }
toValue(outputFormat: MessageFormat): Value { toValue(outputFormat: MessageFormat): Value {
if (outputFormat === 'markdown') { if (outputFormat === 'markdown') {
if (this.format === 'rich') { if (this.format === 'rich') {
// convert a rich formatted history entry to its MD equivalent // convert a rich formatted history entry to its MD equivalent
const markdown = new Markdown({}); return Plain.deserialize(Md.serialize(value));
return new Value({ data: markdown.serialize(value) });
// return ContentState.createFromText(RichText.stateToMarkdown(contentState)); // return ContentState.createFromText(RichText.stateToMarkdown(contentState));
} }
else if (this.format === 'markdown') { else if (this.format === 'markdown') {
@ -53,9 +51,7 @@ class HistoryItem {
} else if (outputFormat === 'rich') { } else if (outputFormat === 'rich') {
if (this.format === 'markdown') { if (this.format === 'markdown') {
// convert MD formatted string to its rich equivalent. // convert MD formatted string to its rich equivalent.
const plain = new Plain({}); return Md.deserialize(Plain.serialize(value));
const md = new Md({});
return md.deserialize(plain.serialize(value));
// return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML()); // return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML());
} }
else if (this.format === 'rich') { else if (this.format === 'rich') {

View file

@ -1,4 +1,24 @@
/*
Copyright 2015 - 2017 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react'; import React from 'react';
/*
import { import {
Editor, Editor,
EditorState, EditorState,
@ -12,11 +32,15 @@ import {
SelectionState, SelectionState,
Entity, Entity,
} from 'draft-js'; } from 'draft-js';
import { stateToMarkdown as __stateToMarkdown } from 'draft-js-export-markdown';
*/
import Html from 'slate-html-serializer';
import * as sdk from './index'; import * as sdk from './index';
import * as emojione from 'emojione'; import * as emojione from 'emojione';
import {stateToHTML} from 'draft-js-export-html';
import {SelectionRange} from "./autocomplete/Autocompleter"; import { SelectionRange } from "./autocomplete/Autocompleter";
import {stateToMarkdown as __stateToMarkdown} from 'draft-js-export-markdown';
const MARKDOWN_REGEX = { const MARKDOWN_REGEX = {
LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g, LINK: /(?:\[([^\]]+)\]\(([^\)]+)\))|\<(\w+:\/\/[^\>]+)\>/g,
@ -33,6 +57,7 @@ const EMOJI_REGEX = new RegExp(emojione.unicodeRegexp, 'g');
const ZWS_CODE = 8203; const ZWS_CODE = 8203;
const ZWS = String.fromCharCode(ZWS_CODE); // zero width space const ZWS = String.fromCharCode(ZWS_CODE); // zero width space
export function stateToMarkdown(state) { export function stateToMarkdown(state) {
return __stateToMarkdown(state) return __stateToMarkdown(state)
.replace( .replace(
@ -40,19 +65,12 @@ export function stateToMarkdown(state) {
''); // this is *not* a zero width space, trust me :) ''); // this is *not* a zero width space, trust me :)
} }
export const contentStateToHTML = (contentState: ContentState) => { export const editorStateToHTML = (editorState: Value) => {
return stateToHTML(contentState, { return Html.deserialize(editorState);
inlineStyles: { }
UNDERLINE: {
element: 'u',
},
},
});
};
export function htmlToContentState(html: string): ContentState { export function htmlToEditorState(html: string): Value {
const blockArray = convertFromHTML(html).contentBlocks; return Html.serialize(html);
return ContentState.createFromBlockArray(blockArray);
} }
function unicodeToEmojiUri(str) { function unicodeToEmojiUri(str) {

View file

@ -19,7 +19,7 @@ import PropTypes from 'prop-types';
import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent'; import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent';
import { Editor } from 'slate-react'; import { Editor } from 'slate-react';
import { Value, Document } from 'slate'; import { Value, Document, Event } from 'slate';
import Html from 'slate-html-serializer'; import Html from 'slate-html-serializer';
import { Markdown as Md } from 'slate-md-serializer'; import { Markdown as Md } from 'slate-md-serializer';
@ -539,15 +539,12 @@ export default class MessageComposerInput extends React.Component {
// const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText()); // const md = new Markdown(this.state.editorState.getCurrentContent().getPlainText());
// contentState = RichText.htmlToContentState(md.toHTML()); // contentState = RichText.htmlToContentState(md.toHTML());
const plain = new Plain({}); value = Md.deserialize(Plain.serialize(this.state.editorState));
const md = new Md({});
value = md.deserialize(plain.serialize(this.state.editorState));
} else { } else {
// let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent());
// value = ContentState.createFromText(markdown); // value = ContentState.createFromText(markdown);
const markdown = new Markdown({}); value = Plain.deserialize(Md.serialize(this.state.editorState));
value = Value({ data: markdown.serialize(value) });
} }
Analytics.setRichtextMode(enabled); Analytics.setRichtextMode(enabled);
@ -559,6 +556,12 @@ export default class MessageComposerInput extends React.Component {
SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled); SettingsStore.setValue("MessageComposerInput.isRichTextEnabled", null, SettingLevel.ACCOUNT, enabled);
} }
onKeyDown = (ev: Event, change: Change, editor: Editor) => {
if (ev.keyCode === KeyCode.ENTER) {
return this.handleReturn(ev);
}
}
handleKeyCommand = (command: string): boolean => { handleKeyCommand = (command: string): boolean => {
/* /*
if (command === 'toggle-mode') { if (command === 'toggle-mode') {
@ -721,10 +724,7 @@ export default class MessageComposerInput extends React.Component {
} }
*/ */
const plain = new Plain({}); let contentText = Plain.serialize(contentState);
value = md.deserialize();
let contentText = plain.serialize(contentState);
let contentHTML; let contentHTML;
/* /*
@ -808,10 +808,10 @@ export default class MessageComposerInput extends React.Component {
shouldSendHTML = hasLink; shouldSendHTML = hasLink;
} }
*/ */
let shouldSendHTML = true; let shouldSendHTML = true;
if (shouldSendHTML) { if (shouldSendHTML) {
contentHTML = HtmlUtils.processHtmlForSending( contentHTML = HtmlUtils.processHtmlForSending(
RichText.contentStateToHTML(contentState), RichText.editorStateToHTML(contentState),
); );
} }
} else { } else {
@ -840,11 +840,12 @@ export default class MessageComposerInput extends React.Component {
return blockText; return blockText;
}).join('\n'); }).join('\n');
*/ */
const md = new Markdown(pt); const md = new Markdown(contentText);
// if contains no HTML and we're not quoting (needing HTML) // if contains no HTML and we're not quoting (needing HTML)
if (md.isPlainText() && !mustSendHTML) { if (md.isPlainText() && !mustSendHTML) {
contentText = md.toPlaintext(); contentText = md.toPlaintext();
} else { } else {
contentText = md.toPlaintext();
contentHTML = md.toHTML(); contentHTML = md.toHTML();
} }
} }
@ -898,7 +899,6 @@ export default class MessageComposerInput extends React.Component {
}); });
} }
this.client.sendMessage(this.props.room.roomId, content).then((res) => { this.client.sendMessage(this.props.room.roomId, content).then((res) => {
dis.dispatch({ dis.dispatch({
action: 'message_sent', action: 'message_sent',
@ -909,7 +909,7 @@ export default class MessageComposerInput extends React.Component {
this.setState({ this.setState({
editorState: this.createEditorState(), editorState: this.createEditorState(),
}); }, ()=>{ this.refs.editor.focus() });
return true; return true;
}; };
@ -1237,6 +1237,7 @@ export default class MessageComposerInput extends React.Component {
placeholder={this.props.placeholder} placeholder={this.props.placeholder}
value={this.state.editorState} value={this.state.editorState}
onChange={this.onEditorContentChanged} onChange={this.onEditorContentChanged}
onKeyDown={this.onKeyDown}
/* /*
blockStyleFn={MessageComposerInput.getBlockStyle} blockStyleFn={MessageComposerInput.getBlockStyle}
keyBindingFn={MessageComposerInput.getKeyBinding} keyBindingFn={MessageComposerInput.getKeyBinding}