convert md<->rt if the stored editorState was in a different state

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2018-07-11 09:39:14 +01:00
parent abbb69dc36
commit fd4f9679df
No known key found for this signature in database
GPG key ID: 3F879DA5AD802A5E
2 changed files with 37 additions and 26 deletions

View file

@ -143,27 +143,6 @@ export default class MessageComposerInput extends React.Component {
Analytics.setRichtextMode(isRichTextEnabled); Analytics.setRichtextMode(isRichTextEnabled);
this.state = {
// whether we're in rich text or markdown mode
isRichTextEnabled,
// the currently displayed editor state (note: this is always what is modified on input)
editorState: this.createEditorState(
isRichTextEnabled,
MessageComposerStore.getEditorState(this.props.room.roomId),
),
// the original editor state, before we started tabbing through completions
originalEditorState: null,
// the virtual state "above" the history stack, the message currently being composed that
// we want to persist whilst browsing history
currentlyComposedEditorState: null,
// whether there were any completions
someCompletions: null,
};
this.client = MatrixClientPeg.get(); this.client = MatrixClientPeg.get();
// track whether we should be trying to show autocomplete suggestions on the current editor // track whether we should be trying to show autocomplete suggestions on the current editor
@ -296,19 +275,47 @@ export default class MessageComposerInput extends React.Component {
} }
] ]
}); });
const savedState = MessageComposerStore.getEditorState(this.props.room.roomId);
this.state = {
// whether we're in rich text or markdown mode
isRichTextEnabled,
// the currently displayed editor state (note: this is always what is modified on input)
editorState: this.createEditorState(
isRichTextEnabled,
savedState.editor_state,
savedState.rich_text,
),
// the original editor state, before we started tabbing through completions
originalEditorState: null,
// the virtual state "above" the history stack, the message currently being composed that
// we want to persist whilst browsing history
currentlyComposedEditorState: null,
// whether there were any completions
someCompletions: null,
};
} }
/* /*
* "Does the right thing" to create an Editor value, based on: * "Does the right thing" to create an Editor value, based on:
* - whether we've got rich text mode enabled * - whether we've got rich text mode enabled
* - contentState was passed in * - contentState was passed in
* - whether the contentState that was passed in was rich text
*/ */
createEditorState(richText: boolean, // eslint-disable-line no-unused-vars createEditorState(wantRichText: boolean, editorState: ?Value, wasRichText: ?boolean): Value {
editorState: ?Value): Value {
if (editorState instanceof Value) { if (editorState instanceof Value) {
if (wantRichText && !wasRichText) {
return this.mdToRichEditorState(editorState);
}
if (wasRichText && !wantRichText) {
return this.richToMdEditorState(editorState);
}
return editorState; return editorState;
} } else {
else {
// ...or create a new one. // ...or create a new one.
return Plain.deserialize('', { defaultBlock: DEFAULT_NODE }); return Plain.deserialize('', { defaultBlock: DEFAULT_NODE });
} }
@ -569,6 +576,7 @@ export default class MessageComposerInput extends React.Component {
dis.dispatch({ dis.dispatch({
action: 'editor_state', action: 'editor_state',
room_id: this.props.room.roomId, room_id: this.props.room.roomId,
rich_text: this.state.isRichTextEnabled,
editor_state: editorState, editor_state: editorState,
}); });

View file

@ -54,7 +54,10 @@ class MessageComposerStore extends Store {
_editorState(payload) { _editorState(payload) {
const editorStateMap = this._state.editorStateMap; const editorStateMap = this._state.editorStateMap;
editorStateMap[payload.room_id] = payload.editor_state; editorStateMap[payload.room_id] = {
editor_state: payload.editor_state,
rich_text: payload.rich_text,
};
localStorage.setItem('editor_state', JSON.stringify(editorStateMap)); localStorage.setItem('editor_state', JSON.stringify(editorStateMap));
this._setState({ this._setState({
editorStateMap: editorStateMap, editorStateMap: editorStateMap,