mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-15 02:31:29 +03:00
Start implementation of plain text export
This commit is contained in:
parent
cff4521106
commit
9e6b8ff9f5
5 changed files with 74 additions and 4 deletions
|
@ -238,12 +238,12 @@ function textForServerACLEvent(ev): () => string | null {
|
|||
function textForMessageEvent(ev): () => string | null {
|
||||
return () => {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
let message = senderDisplayName + ': ' + ev.getContent().body;
|
||||
let message = ev.getContent().body;
|
||||
if (ev.getContent().msgtype === "m.emote") {
|
||||
message = "* " + senderDisplayName + " " + message;
|
||||
} else if (ev.getContent().msgtype === "m.image") {
|
||||
message = _t('%(senderDisplayName)s sent an image.', {senderDisplayName});
|
||||
}
|
||||
} else message = senderDisplayName + ': ' + message;
|
||||
return message;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ export default class RoomHeader extends React.Component {
|
|||
_exportConversationalHistory = async () => {
|
||||
await exportConversationalHistory(
|
||||
this.props.room,
|
||||
exportFormats.HTML,
|
||||
exportFormats.LOGS,
|
||||
exportTypes.START_DATE,
|
||||
{
|
||||
startDate: parseInt(new Date("2021.05.20").getTime().toFixed(0)),
|
||||
|
|
|
@ -146,5 +146,5 @@ export default abstract class Exporter {
|
|||
return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype);
|
||||
}
|
||||
|
||||
abstract export(): Promise<Blob>;
|
||||
abstract export(): Promise<any>;
|
||||
}
|
||||
|
|
68
src/utils/exportUtils/PlainTextExport.ts
Normal file
68
src/utils/exportUtils/PlainTextExport.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import streamSaver from "streamsaver";
|
||||
import Exporter from "./Exporter";
|
||||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||
import { formatFullDateNoDay } from "../../DateUtils";
|
||||
import { _t } from "../../languageHandler";
|
||||
import * as ponyfill from "web-streams-polyfill/ponyfill"
|
||||
import { haveTileForEvent } from "../../components/views/rooms/EventTile";
|
||||
import { exportTypes } from "./exportUtils";
|
||||
import { exportOptions } from "./exportUtils";
|
||||
import { textForEvent } from "../../TextForEvent";
|
||||
|
||||
export default class PlainTextExporter extends Exporter {
|
||||
protected totalSize: number;
|
||||
protected mediaOmitText: string;
|
||||
|
||||
constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) {
|
||||
super(room, exportType, exportOptions);
|
||||
this.totalSize = 0;
|
||||
this.mediaOmitText = !this.exportOptions.attachmentsIncluded
|
||||
? _t("Media omitted")
|
||||
: _t("Media omitted - file size limit exceeded");
|
||||
window.addEventListener("beforeunload", this.onBeforeUnload)
|
||||
}
|
||||
|
||||
protected onBeforeUnload = (e: BeforeUnloadEvent) => {
|
||||
e.preventDefault();
|
||||
return e.returnValue = "Are you sure you want to exit during this export?";
|
||||
}
|
||||
|
||||
protected async createOutput(events: MatrixEvent[]) {
|
||||
let content = "";
|
||||
for (const event of events) {
|
||||
if (!haveTileForEvent(event)) continue;
|
||||
content += `${new Date(event.getTs()).toLocaleString()} - ${textForEvent(event)}\n`;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
public async export() {
|
||||
console.info("Starting export process...");
|
||||
console.info("Fetching events...");
|
||||
const fetchStart = performance.now();
|
||||
const res = await this.getRequiredEvents();
|
||||
const fetchEnd = performance.now();
|
||||
|
||||
console.log(`Fetched ${res.length} events in ${(fetchEnd - fetchStart)/1000} s`);
|
||||
console.info("Creating Output...");
|
||||
|
||||
const text = await this.createOutput(res);
|
||||
|
||||
const filename = `matrix-export-${formatFullDateNoDay(new Date())}.txt`;
|
||||
|
||||
console.info("Writing to a file...");
|
||||
//Support for firefox browser
|
||||
streamSaver.WritableStream = ponyfill.WritableStream
|
||||
//Create a writable stream to the directory
|
||||
const fileStream = streamSaver.createWriteStream(filename);
|
||||
const writer = fileStream.getWriter();
|
||||
const data = new TextEncoder().encode(text);
|
||||
await writer.write(data);
|
||||
await writer.close();
|
||||
const exportEnd = performance.now();
|
||||
console.info(`Export Successful! Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
|
||||
window.removeEventListener("beforeunload", this.onBeforeUnload);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
import HTMLExporter from "./HtmlExport";
|
||||
import PlainTextExporter from "./PlainTextExport";
|
||||
|
||||
export enum exportFormats {
|
||||
HTML = "HTML",
|
||||
|
@ -34,6 +35,7 @@ const exportConversationalHistory = async (
|
|||
case exportFormats.JSON:
|
||||
break;
|
||||
case exportFormats.LOGS:
|
||||
await new PlainTextExporter(room, exportType, exportOptions).export();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue