import { t } from "../../services/i18n.js"; import utils from "../../services/utils.js"; import server from "../../services/server.js"; import toastService from "../../services/toast.js"; import appContext from "../../components/app_context.js"; import libraryLoader from "../../services/library_loader.js"; import openService from "../../services/open.js"; import protectedSessionHolder from "../../services/protected_session_holder.js"; import BasicWidget from "../basic_widget.js"; import dialogService from "../../services/dialog.js"; import options from "../../services/options.js"; import type FNote from "../../entities/fnote.js"; import type { NoteType } from "../../entities/fnote.js"; import { Dropdown, Modal } from "bootstrap"; const TPL = ` `; interface RevisionItem { noteId: string; revisionId: string; dateLastEdited: string; contentLength: number; type: NoteType; title: string; isProtected: boolean; mime: string; } interface FullRevision { content: string; mime: string; } export default class RevisionsDialog extends BasicWidget { private revisionItems: RevisionItem[]; private note: FNote | null; private revisionId: string | null; //@ts-ignore private modal: Modal; //@ts-ignore private listDropdown: Dropdown; private $list!: JQuery; private $listDropdown!: JQuery; private $content!: JQuery; private $title!: JQuery; private $titleButtons!: JQuery; private $eraseAllRevisionsButton!: JQuery; private $maximumRevisions!: JQuery; private $snapshotInterval!: JQuery; private $revisionSettingsButton!: JQuery; constructor() { super(); this.revisionItems = []; this.note = null; this.revisionId = null; } doRender() { this.$widget = $(TPL); //@ts-ignore this.modal = Modal.getOrCreateInstance(this.$widget); this.$list = this.$widget.find(".revision-list"); this.$listDropdown = this.$widget.find(".revision-list-dropdown"); //@ts-ignore this.listDropdown = Dropdown.getOrCreateInstance(this.$listDropdown, { autoClose: false }); this.$content = this.$widget.find(".revision-content"); this.$title = this.$widget.find(".revision-title"); this.$titleButtons = this.$widget.find(".revision-title-buttons"); this.$eraseAllRevisionsButton = this.$widget.find(".revisions-erase-all-revisions-button"); this.$snapshotInterval = this.$widget.find(".revisions-snapshot-interval"); this.$maximumRevisions = this.$widget.find(".maximum-revisions-for-current-note"); this.$revisionSettingsButton = this.$widget.find(".revision-settings-button"); this.listDropdown.show(); this.$listDropdown.parent().on("hide.bs.dropdown", (e) => { this.modal.hide(); }); this.$widget.on("shown.bs.modal", () => { this.$list.find(`[data-revision-id="${this.revisionId}"]`).trigger("focus"); }); this.$eraseAllRevisionsButton.on("click", async () => { if (!this.note) { return; } const text = t("revisions.confirm_delete_all"); if (await dialogService.confirm(text)) { await server.remove(`notes/${this.note.noteId}/revisions`); this.modal.hide(); toastService.showMessage(t("revisions.revisions_deleted")); } }); this.$list.on("focus", ".dropdown-item", (e) => { this.$list.find(".dropdown-item").each((i, el) => { $(el).toggleClass("active", el === e.target); }); this.setContentPane(); }); this.$revisionSettingsButton.on("click", async () => { appContext.tabManager.openContextWithNote("_optionsOther", { activate: true }); }); } async showRevisionsEvent({ noteId = appContext.tabManager.getActiveContextNoteId() }) { if (!noteId) { return; } utils.openDialog(this.$widget); await this.loadRevisions(noteId); } async loadRevisions(noteId: string) { this.$list.empty(); this.$content.empty(); this.$titleButtons.empty(); this.note = appContext.tabManager.getActiveContextNote(); this.revisionItems = await server.get(`notes/${noteId}/revisions`); for (const item of this.revisionItems) { this.$list.append( $('') .text(`${item.dateLastEdited.substr(0, 16)} (${utils.formatSize(item.contentLength)})`) .attr("data-revision-id", item.revisionId) .attr("title", t("revisions.revision_last_edited", { date: item.dateLastEdited })) ); } this.listDropdown.show(); if (this.revisionItems.length > 0) { if (!this.revisionId) { this.revisionId = this.revisionItems[0].revisionId; } } else { this.$title.text(t("revisions.no_revisions")); this.revisionId = null; } this.$eraseAllRevisionsButton.toggle(this.revisionItems.length > 0); // Show the footer of the revisions dialog this.$snapshotInterval.text(t("revisions.snapshot_interval", { seconds: options.getInt("revisionSnapshotTimeInterval") })); let revisionsNumberLimit: number | string = parseInt(this.note?.getLabelValue("versioningLimit") ?? ""); if (!Number.isInteger(revisionsNumberLimit)) { revisionsNumberLimit = options.getInt("revisionSnapshotNumberLimit") ?? 0; } if (revisionsNumberLimit === -1) { revisionsNumberLimit = "∞"; } this.$maximumRevisions.text(t("revisions.maximum_revisions", { number: revisionsNumberLimit })); } async setContentPane() { const revisionId = this.$list.find(".active").attr("data-revision-id"); const revisionItem = this.revisionItems.find((r) => r.revisionId === revisionId); if (!revisionItem) { return; } this.$title.html(revisionItem.title); this.renderContentButtons(revisionItem); await this.renderContent(revisionItem); } renderContentButtons(revisionItem: RevisionItem) { this.$titleButtons.empty(); const $restoreRevisionButton = $(``); $restoreRevisionButton.on("click", async () => { const text = t("revisions.confirm_restore"); if (await dialogService.confirm(text)) { await server.post(`revisions/${revisionItem.revisionId}/restore`); this.modal.hide(); toastService.showMessage(t("revisions.revision_restored")); } }); const $eraseRevisionButton = $(``); $eraseRevisionButton.on("click", async () => { const text = t("revisions.confirm_delete"); if (await dialogService.confirm(text)) { await server.remove(`revisions/${revisionItem.revisionId}`); this.loadRevisions(revisionItem.noteId); toastService.showMessage(t("revisions.revision_deleted")); } }); if (!revisionItem.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) { this.$titleButtons.append($restoreRevisionButton).append("   "); } this.$titleButtons.append($eraseRevisionButton).append("   "); const $downloadButton = $(``); $downloadButton.on("click", () => openService.downloadRevision(revisionItem.noteId, revisionItem.revisionId)); if (!revisionItem.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) { this.$titleButtons.append($downloadButton); } } async renderContent(revisionItem: RevisionItem) { this.$content.empty(); const fullRevision = await server.get(`revisions/${revisionItem.revisionId}`); if (revisionItem.type === "text") { this.$content.html(`
${fullRevision.content}
`); if (this.$content.find("span.math-tex").length > 0) { await libraryLoader.requireLibrary(libraryLoader.KATEX); renderMathInElement(this.$content[0], { trust: true }); } } else if (revisionItem.type === "code") { this.$content.html($("
")
                .text(fullRevision.content).html());
        } else if (revisionItem.type === "image") {
            if (fullRevision.mime === "image/svg+xml") {
                let encodedSVG = encodeURIComponent(fullRevision.content); //Base64 of other format images may be embedded in svg
                this.$content.html($("")
                    .attr("src", `data:${fullRevision.mime};utf8,${encodedSVG}`)
                    .css("max-width", "100%")
                    .css("max-height", "100%").html());
            } else {
                this.$content.html(
                    $("")
                        // the reason why we put this inline as base64 is that we do not want to let user copy this
                        // as a URL to be used in a note. Instead, if they copy and paste it into a note, it will be uploaded as a new note
                        .attr("src", `data:${fullRevision.mime};base64,${fullRevision.content}`)
                        .css("max-width", "100%")
                        .css("max-height", "100%").html()
                );
            }
        } else if (revisionItem.type === "file") {
            const $table = $("")
                .append($("")
                    .append(
                        $("").append($("").append(
                        $('
").text(t("revisions.mime")), $("").text(revisionItem.mime))) .append($("
").text(t("revisions.file_size")), $("").text(utils.formatSize(revisionItem.contentLength)))); if (fullRevision.content) { $table.append( $("
').append($('
').text(t("revisions.preview")), $('
').text(fullRevision.content))
                    )
                );
            }

            this.$content.html($table.html());
        } else if (["canvas", "mindMap"].includes(revisionItem.type)) {
            const encodedTitle = encodeURIComponent(revisionItem.title);

            this.$content.html(
                $("")
                    .attr("src", `api/revisions/${revisionItem.revisionId}/image/${encodedTitle}?${Math.random()}`)
                    .css("max-width", "100%")
                .html());
        } else if (revisionItem.type === "mermaid") {
            const encodedTitle = encodeURIComponent(revisionItem.title);

            this.$content.html(
                $("")
                    .attr("src", `api/revisions/${revisionItem.revisionId}/image/${encodedTitle}?${Math.random()}`)
                    .css("max-width", "100%")
                .html());

            this.$content.append($("
").text(fullRevision.content));
        } else {
            this.$content.text(t("revisions.preview_not_available"));
        }
    }
}