You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-11-19 05:22:13 +03:00
store composer state when typing in new composer
this doesn't use the MessageComposerStore on purpose so that both the new and old composer don't overwrite each others state, as the format is different.
This commit is contained in:
@@ -52,6 +52,7 @@ function selectionEquals(a: Selection, b: Selection): boolean {
|
|||||||
|
|
||||||
export default class BasicMessageEditor extends React.Component {
|
export default class BasicMessageEditor extends React.Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
|
onChange: PropTypes.func,
|
||||||
model: PropTypes.instanceOf(EditorModel).isRequired,
|
model: PropTypes.instanceOf(EditorModel).isRequired,
|
||||||
room: PropTypes.instanceOf(Room).isRequired,
|
room: PropTypes.instanceOf(Room).isRequired,
|
||||||
placeholder: PropTypes.string,
|
placeholder: PropTypes.string,
|
||||||
@@ -91,6 +92,10 @@ export default class BasicMessageEditor extends React.Component {
|
|||||||
this.setState({autoComplete: this.props.model.autoComplete});
|
this.setState({autoComplete: this.props.model.autoComplete});
|
||||||
this.historyManager.tryPush(this.props.model, caret, inputType, diff);
|
this.historyManager.tryPush(this.props.model, caret, inputType, diff);
|
||||||
TypingStore.sharedInstance().setSelfTyping(this.props.room.roomId, !this.props.model.isEmpty);
|
TypingStore.sharedInstance().setSelfTyping(this.props.room.roomId, !this.props.model.isEmpty);
|
||||||
|
|
||||||
|
if (this.props.onChange) {
|
||||||
|
this.props.onChange();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onInput = (event) => {
|
_onInput = (event) => {
|
||||||
|
|||||||
@@ -223,6 +223,7 @@ export default class SendMessageComposer extends React.Component {
|
|||||||
this.model.reset([]);
|
this.model.reset([]);
|
||||||
this._editorRef.clearUndoHistory();
|
this._editorRef.clearUndoHistory();
|
||||||
this._editorRef.focus();
|
this._editorRef.focus();
|
||||||
|
this._clearStoredEditorState();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@@ -231,11 +232,37 @@ export default class SendMessageComposer extends React.Component {
|
|||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
const partCreator = new CommandPartCreator(this.props.room, this.context.matrixClient);
|
const partCreator = new CommandPartCreator(this.props.room, this.context.matrixClient);
|
||||||
this.model = new EditorModel([], partCreator);
|
const parts = this._restoreStoredEditorState(partCreator) || [];
|
||||||
|
this.model = new EditorModel(parts, partCreator);
|
||||||
this.dispatcherRef = dis.register(this.onAction);
|
this.dispatcherRef = dis.register(this.onAction);
|
||||||
this.sendHistoryManager = new SendHistoryManager(this.props.room.roomId, 'mx_slate_composer_history_');
|
this.sendHistoryManager = new SendHistoryManager(this.props.room.roomId, 'mx_slate_composer_history_');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get _editorStateKey() {
|
||||||
|
return `cider_editor_state_${this.props.room.roomId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
_clearStoredEditorState() {
|
||||||
|
localStorage.removeItem(this._editorStateKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
_restoreStoredEditorState(partCreator) {
|
||||||
|
const json = localStorage.getItem(this._editorStateKey);
|
||||||
|
if (json) {
|
||||||
|
const serializedParts = JSON.parse(json);
|
||||||
|
const parts = serializedParts.map(p => partCreator.deserializePart(p));
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_saveStoredEditorState = () => {
|
||||||
|
if (this.model.isEmpty) {
|
||||||
|
this._clearStoredEditorState();
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(this._editorStateKey, JSON.stringify(this.model.serializeParts()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onAction = (payload) => {
|
onAction = (payload) => {
|
||||||
switch (payload.action) {
|
switch (payload.action) {
|
||||||
case 'reply_to_event':
|
case 'reply_to_event':
|
||||||
@@ -284,6 +311,7 @@ export default class SendMessageComposer extends React.Component {
|
|||||||
room={this.props.room}
|
room={this.props.room}
|
||||||
label={this.props.placeholder}
|
label={this.props.placeholder}
|
||||||
placeholder={this.props.placeholder}
|
placeholder={this.props.placeholder}
|
||||||
|
onChange={this._saveStoredEditorState}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user