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.
|
|
|
|
*/
|
|
|
|
import dis from '../dispatcher';
|
2018-04-23 03:13:18 +03:00
|
|
|
import { Store } from 'flux/utils';
|
2018-07-15 22:28:41 +03:00
|
|
|
import { Value } from 'slate';
|
2017-07-05 12:24:55 +03:00
|
|
|
|
|
|
|
const INITIAL_STATE = {
|
2018-04-23 03:13:18 +03:00
|
|
|
// a map of room_id to rich text editor composer state
|
|
|
|
editorStateMap: localStorage.getItem('editor_state') ?
|
|
|
|
JSON.parse(localStorage.getItem('editor_state')) : {},
|
2017-07-05 12:24:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-04-23 03:13:18 +03:00
|
|
|
* A class for storing application state to do with the message composer (specifically
|
|
|
|
* in-progress message drafts). This is a simple
|
2017-07-05 12:24:55 +03:00
|
|
|
* flux store that listens for actions and updates its state accordingly, informing any
|
|
|
|
* listeners (views) of state changes.
|
|
|
|
*/
|
|
|
|
class MessageComposerStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
// Initialise state
|
2017-07-05 15:38:34 +03:00
|
|
|
this._state = Object.assign({}, INITIAL_STATE);
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_setState(newState) {
|
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
__onDispatch(payload) {
|
|
|
|
switch (payload.action) {
|
2018-04-23 03:13:18 +03:00
|
|
|
case 'editor_state':
|
2018-05-12 22:04:58 +03:00
|
|
|
this._editorState(payload);
|
2017-07-05 12:24:55 +03:00
|
|
|
break;
|
|
|
|
case 'on_logged_out':
|
|
|
|
this.reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 22:04:58 +03:00
|
|
|
_editorState(payload) {
|
2017-07-05 12:24:55 +03:00
|
|
|
const editorStateMap = this._state.editorStateMap;
|
2018-07-11 11:39:14 +03:00
|
|
|
editorStateMap[payload.room_id] = {
|
|
|
|
editor_state: payload.editor_state,
|
|
|
|
rich_text: payload.rich_text,
|
|
|
|
};
|
2018-04-23 03:13:18 +03:00
|
|
|
localStorage.setItem('editor_state', JSON.stringify(editorStateMap));
|
2017-07-05 12:24:55 +03:00
|
|
|
this._setState({
|
|
|
|
editorStateMap: editorStateMap,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-12 22:04:58 +03:00
|
|
|
getEditorState(roomId) {
|
2018-07-15 22:28:41 +03:00
|
|
|
const editorStateMap = this._state.editorStateMap;
|
|
|
|
// const entry = this._state.editorStateMap[roomId];
|
|
|
|
if (editorStateMap[roomId] && !Value.isValue(editorStateMap[roomId].editor_state)) {
|
|
|
|
// rehydrate lazily to prevent massive churn at launch and cache it
|
|
|
|
editorStateMap[roomId].editor_state = Value.fromJSON(editorStateMap[roomId].editor_state);
|
|
|
|
}
|
|
|
|
// explicitly don't setState here because the value didn't actually change, we just hydrated it,
|
|
|
|
// if a listener received an update they too would call this method and have a hydrated Value
|
|
|
|
return editorStateMap[roomId];
|
2017-07-05 12:24:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this._state = Object.assign({}, INITIAL_STATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let singletonMessageComposerStore = null;
|
|
|
|
if (!singletonMessageComposerStore) {
|
|
|
|
singletonMessageComposerStore = new MessageComposerStore();
|
|
|
|
}
|
|
|
|
module.exports = singletonMessageComposerStore;
|