2017-07-05 12:24:55 +03:00
|
|
|
/*
|
2018-07-15 22:28:41 +03:00
|
|
|
Copyright 2017, 2018 Vector Creations Ltd
|
2017-07-05 12:24:55 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-07-15 22:28:41 +03:00
|
|
|
import { Value } from 'slate';
|
2017-07-05 12:24:55 +03:00
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
const localStoragePrefix = 'editor_state_';
|
2017-07-05 12:24:55 +03:00
|
|
|
|
|
|
|
/**
|
2018-07-16 18:56:08 +03:00
|
|
|
* A class for storing application state to do with the message composer (specifically in-progress message drafts).
|
|
|
|
* It does not worry about cleaning up on log out as this is handled in Lifecycle.js by localStorage.clear()
|
2017-07-05 12:24:55 +03:00
|
|
|
*/
|
2018-07-16 18:56:08 +03:00
|
|
|
class MessageComposerStore {
|
2017-07-05 12:24:55 +03:00
|
|
|
constructor() {
|
2018-07-16 18:56:08 +03:00
|
|
|
this.prefix = localStoragePrefix;
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
_getKey(roomId: string): string {
|
|
|
|
return this.prefix + roomId;
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
setEditorState(roomId: string, editorState: Value, richText: boolean) {
|
|
|
|
localStorage.setItem(this._getKey(roomId), JSON.stringify({
|
2018-07-17 00:04:42 +03:00
|
|
|
editor_state: editorState.toJSON({
|
|
|
|
preserveSelection: true,
|
2018-07-17 00:25:41 +03:00
|
|
|
// XXX: re-hydrating history is not currently supported by fromJSON
|
|
|
|
// preserveHistory: true,
|
2018-07-17 00:04:42 +03:00
|
|
|
// XXX: this seems like a workaround for selection.isSet being based on anchorKey instead of anchorPath
|
|
|
|
preserveKeys: true,
|
|
|
|
}),
|
2018-07-16 18:56:08 +03:00
|
|
|
rich_text: richText,
|
|
|
|
}));
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
getEditorState(roomId): {editor_state: Value, rich_text: boolean} {
|
|
|
|
const stateStr = localStorage.getItem(this._getKey(roomId));
|
2017-07-05 12:24:55 +03:00
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
let state;
|
|
|
|
if (stateStr) {
|
|
|
|
state = JSON.parse(stateStr);
|
|
|
|
|
|
|
|
// if it does not have the fields we expect then bail
|
|
|
|
if (!state || state.rich_text === undefined || state.editor_state === undefined) return;
|
|
|
|
state.editor_state = Value.fromJSON(state.editor_state);
|
2018-07-15 22:28:41 +03:00
|
|
|
}
|
2017-07-05 12:24:55 +03:00
|
|
|
|
2018-07-16 18:56:08 +03:00
|
|
|
return state;
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let singletonMessageComposerStore = null;
|
|
|
|
if (!singletonMessageComposerStore) {
|
|
|
|
singletonMessageComposerStore = new MessageComposerStore();
|
|
|
|
}
|
|
|
|
module.exports = singletonMessageComposerStore;
|