WIP store history as raw content state

Not sure this solves any problems because we still have to convert from md and back
pull/21833/head
Luke Barnard 2017-07-20 18:01:39 +01:00
parent 26b16b076c
commit 0cc890c020
2 changed files with 22 additions and 23 deletions

View File

@ -15,36 +15,40 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {ContentState} from 'draft-js'; import {ContentState, convertToRaw, convertFromRaw} from 'draft-js';
import * as RichText from './RichText'; import * as RichText from './RichText';
import Markdown from './Markdown'; import Markdown from './Markdown';
import _flow from 'lodash/flow';
import _clamp from 'lodash/clamp'; import _clamp from 'lodash/clamp';
type MessageFormat = 'html' | 'markdown'; type MessageFormat = 'html' | 'markdown';
class HistoryItem { class HistoryItem {
message: string = '';
// Keeping message for backwards-compatibility
message: string;
rawContentState: RawDraftContentState;
format: MessageFormat = 'html'; format: MessageFormat = 'html';
constructor(message: string, format: MessageFormat) { constructor(contentState: ?ContentState, format: ?MessageFormat) {
this.message = message; this.rawContentState = contentState ? convertToRaw(contentState) : null;
this.format = format; this.format = format;
} }
toContentState(format: MessageFormat): ContentState { toContentState(outputFormat: MessageFormat): ContentState {
let {message} = this; const contentState = convertFromRaw(this.rawContentState);
if (format === 'markdown') { if (outputFormat === 'markdown') {
if (this.format === 'html') { if (this.format === 'html') {
message = _flow([RichText.htmlToContentState, RichText.stateToMarkdown])(message); console.info(outputFormat, 'to other format');
return ContentState.createFromText(RichText.stateToMarkdown(contentState));
} }
return ContentState.createFromText(message);
} else { } else {
if (this.format === 'markdown') { if (this.format === 'markdown') {
message = new Markdown(message).toHTML(); console.info(outputFormat, 'to other format');
return RichText.htmlToContentState(new Markdown(contentState).toHTML());
} }
return RichText.htmlToContentState(message);
} }
// history item has format === outputFormat
return contentState;
} }
} }
@ -67,8 +71,8 @@ export default class ComposerHistoryManager {
this.lastIndex = this.currentIndex; this.lastIndex = this.currentIndex;
} }
addItem(message: string, format: MessageFormat) { save(contentState: ContentState, format: MessageFormat) {
const item = new HistoryItem(message, format); const item = new HistoryItem(contentState, format);
this.history.push(item); this.history.push(item);
this.currentIndex = this.lastIndex + 1; this.currentIndex = this.lastIndex + 1;
sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item)); sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item));

View File

@ -762,15 +762,10 @@ export default class MessageComposerInput extends React.Component {
let sendHtmlFn = this.client.sendHtmlMessage; let sendHtmlFn = this.client.sendHtmlMessage;
let sendTextFn = this.client.sendTextMessage; let sendTextFn = this.client.sendTextMessage;
if (this.state.isRichtextEnabled) { this.historyManager.save(
this.historyManager.addItem( contentState,
contentHTML ? contentHTML : contentText, this.state.isRichtextEnabled ? 'html' : 'markdown',
contentHTML ? 'html' : 'markdown', );
);
} else {
// Always store MD input as input history
this.historyManager.addItem(contentText, 'markdown');
}
if (contentText.startsWith('/me')) { if (contentText.startsWith('/me')) {
contentText = contentText.substring(4); contentText = contentText.substring(4);