WIP store history as raw content state

Not sure this solves any problems because we still have to convert from md and back
This commit is contained in:
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.
*/
import {ContentState} from 'draft-js';
import {ContentState, convertToRaw, convertFromRaw} from 'draft-js';
import * as RichText from './RichText';
import Markdown from './Markdown';
import _flow from 'lodash/flow';
import _clamp from 'lodash/clamp';
type MessageFormat = 'html' | 'markdown';
class HistoryItem {
message: string = '';
// Keeping message for backwards-compatibility
message: string;
rawContentState: RawDraftContentState;
format: MessageFormat = 'html';
constructor(message: string, format: MessageFormat) {
this.message = message;
constructor(contentState: ?ContentState, format: ?MessageFormat) {
this.rawContentState = contentState ? convertToRaw(contentState) : null;
this.format = format;
}
toContentState(format: MessageFormat): ContentState {
let {message} = this;
if (format === 'markdown') {
toContentState(outputFormat: MessageFormat): ContentState {
const contentState = convertFromRaw(this.rawContentState);
if (outputFormat === 'markdown') {
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 {
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;
}
addItem(message: string, format: MessageFormat) {
const item = new HistoryItem(message, format);
save(contentState: ContentState, format: MessageFormat) {
const item = new HistoryItem(contentState, format);
this.history.push(item);
this.currentIndex = this.lastIndex + 1;
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 sendTextFn = this.client.sendTextMessage;
if (this.state.isRichtextEnabled) {
this.historyManager.addItem(
contentHTML ? contentHTML : contentText,
contentHTML ? 'html' : 'markdown',
);
} else {
// Always store MD input as input history
this.historyManager.addItem(contentText, 'markdown');
}
this.historyManager.save(
contentState,
this.state.isRichtextEnabled ? 'html' : 'markdown',
);
if (contentText.startsWith('/me')) {
contentText = contentText.substring(4);