import server from "../../services/server.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import toastService from "../../services/toast.js";
import openService from "../../services/open.js";
import utils from "../../services/utils.js";
import { t } from "../../services/i18n.js";
import type FNote from "../../entities/fnote.js";
const TPL = /*html*/`
`;
export default class ImagePropertiesWidget extends NoteContextAwareWidget {
private $copyReferenceToClipboardButton!: JQuery;
private $uploadNewRevisionButton!: JQuery;
private $uploadNewRevisionInput!: JQuery;
private $fileName!: JQuery;
private $fileType!: JQuery;
private $fileSize!: JQuery;
private $openButton!: JQuery;
private $imageDownloadButton!: JQuery;
get name() {
return "imageProperties";
}
get toggleCommand() {
return "toggleRibbonTabImageProperties";
}
isEnabled() {
return this.note && this.note.type === "image";
}
getTitle() {
return {
show: this.isEnabled(),
activate: true,
};
}
doRender() {
this.$widget = $(TPL);
this.contentSized();
this.$copyReferenceToClipboardButton = this.$widget.find(".image-copy-reference-to-clipboard");
this.$copyReferenceToClipboardButton.on("click", () => this.triggerEvent(`copyImageReferenceToClipboard`, { ntxId: this.noteContext?.ntxId }));
this.$uploadNewRevisionButton = this.$widget.find(".image-upload-new-revision");
this.$uploadNewRevisionInput = this.$widget.find(".image-upload-new-revision-input");
this.$fileName = this.$widget.find(".image-filename");
this.$fileType = this.$widget.find(".image-filetype");
this.$fileSize = this.$widget.find(".image-filesize");
this.$openButton = this.$widget.find(".image-open");
this.$openButton.on("click", () => this.noteId && this.note && openService.openNoteExternally(this.noteId, this.note.mime));
this.$imageDownloadButton = this.$widget.find(".image-download");
this.$imageDownloadButton.on("click", () => this.noteId && openService.downloadFileNote(this.noteId));
this.$uploadNewRevisionButton.on("click", () => {
this.$uploadNewRevisionInput.trigger("click");
});
this.$uploadNewRevisionInput.on("change", async () => {
const fileToUpload = this.$uploadNewRevisionInput[0].files[0]; // copy to allow reset below
this.$uploadNewRevisionInput.val("");
const result = await server.upload(`images/${this.noteId}`, fileToUpload);
if (result.uploaded) {
toastService.showMessage(t("image_properties.upload_success"));
await utils.clearBrowserCache();
this.refresh();
} else {
toastService.showError(t("image_properties.upload_failed", { message: result.message }));
}
});
}
async refreshWithNote(note: FNote) {
this.$widget.show();
const blob = await this.note?.getBlob();
this.$fileName.text(note.getLabelValue("originalFileName") || "?");
this.$fileSize.text(utils.formatSize(blob?.contentLength ?? 0));
this.$fileType.text(note.mime);
}
}