diff --git a/src/utils/exportUtils/HtmlExport.tsx b/src/utils/exportUtils/HtmlExport.tsx index 2896fdb1c2..3ec04a460b 100644 --- a/src/utils/exportUtils/HtmlExport.tsx +++ b/src/utils/exportUtils/HtmlExport.tsx @@ -89,7 +89,7 @@ export default class HTMLExporter extends Exporter { return renderToStaticMarkup(avatar); } - protected async wrapHTML(content: string): Promise { + protected async wrapHTML(content: string, currentPage: number, nbPages: number): Promise { const roomAvatar = await this.getRoomAvatar(); const exportDate = formatFullDateNoDayNoTime(new Date()); const creator = this.room.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender(); @@ -128,6 +128,29 @@ export default class HTMLExporter extends Exporter { ); const topicText = topic ? _t("Topic: %(topic)s", { topic }) : ""; + const previousMessagesLink = renderToStaticMarkup( + currentPage !== 0 ? ( +
+ + Previous group of messages + +
+ ) : ( + <> + ), + ); + + const nextMessagesLink = renderToStaticMarkup( + currentPage < nbPages - 1 ? ( +
+ + Next group of messages + +
+ ) : ( + <> + ), + ); return ` @@ -168,6 +191,7 @@ export default class HTMLExporter extends Exporter {
${topic}
+ ${previousMessagesLink}
-
- ${roomAvatar} -

${this.room.name}

-

${createdText}

${exportedText}

-
-

${topicText}

-
+ ${ + currentPage == 0 + ? `
+ ${roomAvatar} +

${this.room.name}

+

${createdText}

${exportedText}

+
+

${topicText}

+
` + : "" + } ${content}
@@ -205,6 +233,7 @@ export default class HTMLExporter extends Exporter {
+ ${nextMessagesLink} @@ -381,7 +410,12 @@ export default class HTMLExporter extends Exporter { return eventTile; } - protected async createHTML(events: MatrixEvent[], start: number): Promise { + protected async createHTML( + events: MatrixEvent[], + start: number, + currentPage: number, + nbPages: number, + ): Promise { let content = ""; let prevEvent: MatrixEvent | null = null; for (let i = start; i < Math.min(start + 1000, events.length); i++) { @@ -405,7 +439,7 @@ export default class HTMLExporter extends Exporter { content += body; prevEvent = event; } - return this.wrapHTML(content); + return this.wrapHTML(content, currentPage, nbPages); } public async export(): Promise { @@ -428,7 +462,7 @@ export default class HTMLExporter extends Exporter { const usedClasses = new Set(); for (let page = 0; page < res.length / 1000; page++) { - const html = await this.createHTML(res, page * 1000); + const html = await this.createHTML(res, page * 1000, page, res.length / 1000); const document = new DOMParser().parseFromString(html, "text/html"); document.querySelectorAll("*").forEach((element) => { element.classList.forEach((c) => usedClasses.add(c)); diff --git a/test/utils/exportUtils/HTMLExport-test.ts b/test/utils/exportUtils/HTMLExport-test.ts index 7e1f7a53b7..bbdfa9e647 100644 --- a/test/utils/exportUtils/HTMLExport-test.ts +++ b/test/utils/exportUtils/HTMLExport-test.ts @@ -315,4 +315,45 @@ describe("HTMLExport", () => { expect(fileName).not.toMatch(/^files\/hello/); } }); + + it("should add link to next and previous file", async () => { + const exporter = new HTMLExporter( + room, + ExportType.LastNMessages, + { + attachmentsIncluded: false, + maxSize: 1_024 * 1_024, + }, + () => {}, + ); + + // test link to the first page + //@ts-ignore private access + exporter.wrapHTML("", 0, 3).then((res) => { + expect(res).not.toContain("Previous group of messages"); + expect(res).toContain( + '', + ); + }); + + // test link for a middle page + //@ts-ignore private access + exporter.wrapHTML("", 1, 3).then((res) => { + expect(res).toContain( + '', + ); + expect(res).toContain( + '', + ); + }); + + // test link for last page + //@ts-ignore private access + exporter.wrapHTML("", 2, 3).then((res) => { + expect(res).toContain( + '', + ); + expect(res).not.toContain("Next group of messages"); + }); + }); }); diff --git a/test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap b/test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap index c47170d3ed..e4b6872ebf 100644 --- a/test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap +++ b/test/utils/exportUtils/__snapshots__/HTMLExport-test.ts.snap @@ -40,6 +40,7 @@ exports[`HTMLExport should export 1`] = `
+
- -

!myroom:example.org

-

created this room.

This is the start of export of !myroom:example.org. Exported by @userId:matrix.org at 2022/11/17.

-
-

-
+ +

!myroom:example.org

+

created this room.

This is the start of export of !myroom:example.org. Exported by @userId:matrix.org at 2022/11/17.

+
+

+
  • @user49:example.com
    Message #49
  • @user48:example.com
    Message #48
  • @user47:example.com
    Message #47
  • @user46:example.com
    Message #46
  • @user45:example.com
    Message #45
  • @user44:example.com
    Message #44
  • @user43:example.com
    Message #43
  • @user42:example.com
    Message #42
  • @user41:example.com
    Message #41
  • @user40:example.com
    Message #40
  • @user39:example.com
    Message #39
  • @user38:example.com
    Message #38
  • @user37:example.com
    Message #37
  • @user36:example.com
    Message #36
  • @user35:example.com
    Message #35
  • @user34:example.com
    Message #34
  • @user33:example.com
    Message #33
  • @user32:example.com
    Message #32
  • @user31:example.com
    Message #31
  • @user30:example.com
    Message #30
  • @user29:example.com
    Message #29
  • @user28:example.com
    Message #28
  • @user27:example.com
    Message #27
  • @user26:example.com
    Message #26
  • @user25:example.com
    Message #25
  • @user24:example.com
    Message #24
  • @user23:example.com
    Message #23
  • @user22:example.com
    Message #22
  • @user21:example.com
    Message #21
  • @user20:example.com
    Message #20
  • @user19:example.com
    Message #19
  • @user18:example.com
    Message #18
  • @user17:example.com
    Message #17
  • @user16:example.com
    Message #16
  • @user15:example.com
    Message #15
  • @user14:example.com
    Message #14
  • @user13:example.com
    Message #13
  • @user12:example.com
    Message #12
  • @user11:example.com
    Message #11
  • @user10:example.com
    Message #10
  • @user9:example.com
    Message #9
  • @user8:example.com
    Message #8
  • @user7:example.com
    Message #7
  • @user6:example.com
    Message #6
  • @user5:example.com
    Message #5
  • @user4:example.com
    Message #4
  • @user3:example.com
    Message #3
  • @user2:example.com
    Message #2
  • @user1:example.com
    Message #1
  • @user0:example.com
    Message #0
  • @@ -77,6 +78,7 @@ exports[`HTMLExport should export 1`] = `
    +