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

Commit 004cc42

Browse files
authored
Merge pull request #1298 from matrix-org/luke/store-history-as-raw-content
Store history as raw content
2 parents 6baca05 + ecef9cf commit 004cc42

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/ComposerHistoryManager.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,38 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18-
import {ContentState} from 'draft-js';
18+
import {ContentState, convertToRaw, convertFromRaw} from 'draft-js';
1919
import * as RichText from './RichText';
2020
import Markdown from './Markdown';
21-
import _flow from 'lodash/flow';
2221
import _clamp from 'lodash/clamp';
2322

2423
type MessageFormat = 'html' | 'markdown';
2524

2625
class HistoryItem {
27-
message: string = '';
26+
27+
// Keeping message for backwards-compatibility
28+
message: string;
29+
rawContentState: RawDraftContentState;
2830
format: MessageFormat = 'html';
2931

30-
constructor(message: string, format: MessageFormat) {
31-
this.message = message;
32+
constructor(contentState: ?ContentState, format: ?MessageFormat) {
33+
this.rawContentState = contentState ? convertToRaw(contentState) : null;
3234
this.format = format;
3335
}
3436

35-
toContentState(format: MessageFormat): ContentState {
36-
let {message} = this;
37-
if (format === 'markdown') {
37+
toContentState(outputFormat: MessageFormat): ContentState {
38+
const contentState = convertFromRaw(this.rawContentState);
39+
if (outputFormat === 'markdown') {
3840
if (this.format === 'html') {
39-
message = _flow([RichText.htmlToContentState, RichText.stateToMarkdown])(message);
41+
return ContentState.createFromText(RichText.stateToMarkdown(contentState));
4042
}
41-
return ContentState.createFromText(message);
4243
} else {
4344
if (this.format === 'markdown') {
44-
message = new Markdown(message).toHTML();
45+
return RichText.htmlToContentState(new Markdown(contentState.getPlainText()).toHTML());
4546
}
46-
return RichText.htmlToContentState(message);
4747
}
48+
// history item has format === outputFormat
49+
return contentState;
4850
}
4951
}
5052

@@ -67,8 +69,8 @@ export default class ComposerHistoryManager {
6769
this.lastIndex = this.currentIndex;
6870
}
6971

70-
addItem(message: string, format: MessageFormat) {
71-
const item = new HistoryItem(message, format);
72+
save(contentState: ContentState, format: MessageFormat) {
73+
const item = new HistoryItem(contentState, format);
7274
this.history.push(item);
7375
this.currentIndex = this.lastIndex + 1;
7476
sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item));

src/components/views/rooms/MessageComposerInput.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -808,15 +808,10 @@ export default class MessageComposerInput extends React.Component {
808808
let sendHtmlFn = this.client.sendHtmlMessage;
809809
let sendTextFn = this.client.sendTextMessage;
810810

811-
if (this.state.isRichtextEnabled) {
812-
this.historyManager.addItem(
813-
contentHTML ? contentHTML : contentText,
814-
contentHTML ? 'html' : 'markdown',
815-
);
816-
} else {
817-
// Always store MD input as input history
818-
this.historyManager.addItem(contentText, 'markdown');
819-
}
811+
this.historyManager.save(
812+
contentState,
813+
this.state.isRichtextEnabled ? 'html' : 'markdown',
814+
);
820815

821816
if (contentText.startsWith('/me')) {
822817
contentText = contentText.substring(4);

0 commit comments

Comments
 (0)