2019-06-12 21:32:47 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-06-12 21:32:47 +01:00
|
|
|
Copyright 2017 Aviral Dasgupta
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-06-12 21:32:47 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { clamp } from "lodash";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2020-10-06 14:47:53 +01:00
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type SerializedPart } from "./editor/parts";
|
|
|
|
|
import type EditorModel from "./editor/model";
|
2019-06-12 21:32:47 +01:00
|
|
|
|
2020-10-06 14:47:53 +01:00
|
|
|
interface IHistoryItem {
|
|
|
|
|
parts: SerializedPart[];
|
|
|
|
|
replyEventId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 15:34:49 +02:00
|
|
|
export default class SendHistoryManager {
|
2022-12-16 12:29:59 +00:00
|
|
|
public history: Array<IHistoryItem> = [];
|
|
|
|
|
public prefix: string;
|
|
|
|
|
public lastIndex = 0; // used for indexing the storage
|
|
|
|
|
public currentIndex = 0; // used for indexing the loaded validated history Array
|
2019-06-12 21:32:47 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(roomId: string, prefix: string) {
|
2019-06-12 21:32:47 +01:00
|
|
|
this.prefix = prefix + roomId;
|
|
|
|
|
|
|
|
|
|
// TODO: Performance issues?
|
2019-08-20 17:18:46 +02:00
|
|
|
let index = 0;
|
|
|
|
|
let itemJSON;
|
|
|
|
|
|
|
|
|
|
while ((itemJSON = sessionStorage.getItem(`${this.prefix}[${index}]`))) {
|
2019-06-12 21:32:47 +01:00
|
|
|
try {
|
2020-10-08 09:51:31 +01:00
|
|
|
this.history.push(JSON.parse(itemJSON));
|
2019-06-12 21:32:47 +01:00
|
|
|
} catch (e) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("Throwing away unserialisable history", e);
|
2019-08-20 17:18:46 +02:00
|
|
|
break;
|
2019-06-12 21:32:47 +01:00
|
|
|
}
|
2019-08-20 17:18:46 +02:00
|
|
|
++index;
|
2019-06-12 21:32:47 +01:00
|
|
|
}
|
2019-08-20 17:18:46 +02:00
|
|
|
this.lastIndex = this.history.length - 1;
|
2019-06-12 21:32:47 +01:00
|
|
|
// reset currentIndex to account for any unserialisable history
|
2019-08-20 17:18:46 +02:00
|
|
|
this.currentIndex = this.lastIndex + 1;
|
2019-06-12 21:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static createItem(model: EditorModel, replyEvent?: MatrixEvent): IHistoryItem {
|
2020-10-06 14:47:53 +01:00
|
|
|
return {
|
|
|
|
|
parts: model.serializeParts(),
|
|
|
|
|
replyEventId: replyEvent ? replyEvent.getId() : undefined,
|
|
|
|
|
};
|
2019-06-12 21:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public save(editorModel: EditorModel, replyEvent?: MatrixEvent): void {
|
2020-10-06 14:47:53 +01:00
|
|
|
const item = SendHistoryManager.createItem(editorModel, replyEvent);
|
|
|
|
|
this.history.push(item);
|
|
|
|
|
this.currentIndex = this.history.length;
|
|
|
|
|
this.lastIndex += 1;
|
|
|
|
|
sessionStorage.setItem(`${this.prefix}[${this.lastIndex}]`, JSON.stringify(item));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public getItem(offset: number): IHistoryItem {
|
2020-08-28 18:53:43 +01:00
|
|
|
this.currentIndex = clamp(this.currentIndex + offset, 0, this.history.length - 1);
|
2019-06-12 21:32:47 +01:00
|
|
|
return this.history[this.currentIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|