2017-03-10 15:04:31 +00:00
|
|
|
//@flow
|
2017-06-23 17:30:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Aviral Dasgupta
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-03-10 15:04:31 +00:00
|
|
|
|
2017-07-20 17:01:39 +00:00
|
|
|
import {ContentState, convertToRaw, convertFromRaw} from 'draft-js';
|
2017-03-10 15:04:31 +00:00
|
|
|
import * as RichText from './RichText';
|
|
|
|
import Markdown from './Markdown';
|
|
|
|
import _clamp from 'lodash/clamp';
|
|
|
|
|
|
|
|
type MessageFormat = 'html' | 'markdown';
|
|
|
|
|
|
|
|
class HistoryItem {
|
2017-07-20 17:01:39 +00:00
|
|
|
|
|
|
|
// Keeping message for backwards-compatibility
|
|
|
|
message: string;
|
|
|
|
rawContentState: RawDraftContentState;
|
2017-03-10 15:04:31 +00:00
|
|
|
format: MessageFormat = 'html';
|
|
|
|
|
2017-07-20 17:01:39 +00:00
|
|
|
constructor(contentState: ?ContentState, format: ?MessageFormat) {
|
|
|
|
this.rawContentState = contentState ? convertToRaw(contentState) : null;
|
2017-03-10 15:04:31 +00:00
|
|
|
this.format = format;
|
|
|
|
}
|
|
|
|
|
2017-07-20 17:01:39 +00:00
|
|
|
toContentState(outputFormat: MessageFormat): ContentState {
|
|
|
|
const contentState = convertFromRaw(this.rawContentState);
|
|
|
|
if (outputFormat === 'markdown') {
|
2017-03-10 15:04:31 +00:00
|
|
|
if (this.format === 'html') {
|
2017-07-20 17:01:39 +00:00
|
|
|
console.info(outputFormat, 'to other format');
|
|
|
|
return ContentState.createFromText(RichText.stateToMarkdown(contentState));
|
2017-03-10 15:04:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.format === 'markdown') {
|
2017-07-20 17:01:39 +00:00
|
|
|
console.info(outputFormat, 'to other format');
|
|
|
|
return RichText.htmlToContentState(new Markdown(contentState).toHTML());
|
2017-03-10 15:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-20 17:01:39 +00:00
|
|
|
// history item has format === outputFormat
|
|
|
|
return contentState;
|
2017-03-10 15:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ComposerHistoryManager {
|
|
|
|
history: Array<HistoryItem> = [];
|
|
|
|
prefix: string;
|
|
|
|
lastIndex: number = 0;
|
2017-06-29 16:02:19 +00:00
|
|
|
currentIndex: number = 0;
|
2017-03-10 15:04:31 +00:00
|
|
|
|
|
|
|
constructor(roomId: string, prefix: string = 'mx_composer_history_') {
|
|
|
|
this.prefix = prefix + roomId;
|
|
|
|
|
|
|
|
// TODO: Performance issues?
|
2017-06-29 16:02:19 +00:00
|
|
|
let item;
|
|
|
|
for(; item = sessionStorage.getItem(`${this.prefix}[${this.currentIndex}]`); this.currentIndex++) {
|
2017-03-10 15:40:27 +00:00
|
|
|
this.history.push(
|
2017-06-29 16:02:19 +00:00
|
|
|
Object.assign(new HistoryItem(), JSON.parse(item)),
|
2017-03-10 15:40:27 +00:00
|
|
|
);
|
2017-03-10 15:04:31 +00:00
|
|
|
}
|
2017-06-29 16:02:19 +00:00
|
|
|
this.lastIndex = this.currentIndex;
|
2017-03-10 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:01:39 +00:00
|
|
|
save(contentState: ContentState, format: MessageFormat) {
|
|
|
|
const item = new HistoryItem(contentState, format);
|
2017-03-10 15:04:31 +00:00
|
|
|
this.history.push(item);
|
2017-03-10 15:40:27 +00:00
|
|
|
this.currentIndex = this.lastIndex + 1;
|
2017-03-10 15:04:31 +00:00
|
|
|
sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
getItem(offset: number, format: MessageFormat): ?ContentState {
|
|
|
|
this.currentIndex = _clamp(this.currentIndex + offset, 0, this.lastIndex - 1);
|
|
|
|
const item = this.history[this.currentIndex];
|
|
|
|
return item ? item.toContentState(format) : null;
|
|
|
|
}
|
|
|
|
}
|