mirror of https://github.com/vector-im/riot-web
modify ComposerHistoryManager
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> (cherry picked from commit d139dd6) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>pull/21833/head
parent
5b74c615ae
commit
5bd4104c96
|
@ -51,8 +51,8 @@ class HistoryItem {
|
||||||
export default class ComposerHistoryManager {
|
export default class ComposerHistoryManager {
|
||||||
history: Array<HistoryItem> = [];
|
history: Array<HistoryItem> = [];
|
||||||
prefix: string;
|
prefix: string;
|
||||||
lastIndex: number = 0;
|
lastIndex: number = 0; // used for indexing the storage
|
||||||
currentIndex: number = 0;
|
currentIndex: number = 0; // used for indexing the loaded validated history Array
|
||||||
|
|
||||||
constructor(roomId: string, prefix: string = 'mx_composer_history_') {
|
constructor(roomId: string, prefix: string = 'mx_composer_history_') {
|
||||||
this.prefix = prefix + roomId;
|
this.prefix = prefix + roomId;
|
||||||
|
@ -69,18 +69,19 @@ export default class ComposerHistoryManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.lastIndex = this.currentIndex;
|
this.lastIndex = this.currentIndex;
|
||||||
|
// reset currentIndex to account for any unserialisable history
|
||||||
|
this.currentIndex = this.history.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
save(value: Value, format: MessageFormat) {
|
save(value: Value, format: MessageFormat) {
|
||||||
const item = new HistoryItem(value, format);
|
const item = new HistoryItem(value, format);
|
||||||
this.history.push(item);
|
this.history.push(item);
|
||||||
this.currentIndex = this.lastIndex + 1;
|
this.currentIndex = this.history.length;
|
||||||
sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item.toJSON()));
|
sessionStorage.setItem(`${this.prefix}[${this.lastIndex++}]`, JSON.stringify(item.toJSON()));
|
||||||
}
|
}
|
||||||
|
|
||||||
getItem(offset: number): ?HistoryItem {
|
getItem(offset: number): ?HistoryItem {
|
||||||
this.currentIndex = _clamp(this.currentIndex + offset, 0, this.lastIndex - 1);
|
this.currentIndex = _clamp(this.currentIndex + offset, 0, this.history.length - 1);
|
||||||
const item = this.history[this.currentIndex];
|
return this.history[this.currentIndex];
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue