2025-06-19 22:44:02 +03:00
|
|
|
import { escapeQuotes } from "../../services/utils.js";
|
2022-06-16 14:21:24 +02:00
|
|
|
import treeService from "../../services/tree.js";
|
2025-03-20 10:38:45 +02:00
|
|
|
import importService, { type UploadFilesOptions } from "../../services/import.js";
|
2022-06-16 14:21:24 +02:00
|
|
|
import options from "../../services/options.js";
|
|
|
|
import BasicWidget from "../basic_widget.js";
|
2024-07-23 17:38:22 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-02-21 20:41:00 +01:00
|
|
|
import { Modal, Tooltip } from "bootstrap";
|
2025-03-20 10:38:45 +02:00
|
|
|
import type { EventData } from "../../components/app_context.js";
|
2025-06-19 22:44:02 +03:00
|
|
|
import { openDialog } from "../../services/dialog.js";
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-04-01 23:24:21 +03:00
|
|
|
const TPL = /*html*/`
|
2022-06-16 14:21:24 +02:00
|
|
|
<div class="import-dialog modal fade mx-auto" tabindex="-1" role="dialog">
|
|
|
|
<div class="modal-dialog modal-lg" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5 class="modal-title">${t("import.importIntoNote")}</h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("import.close")}"></button>
|
2022-06-16 14:21:24 +02:00
|
|
|
</div>
|
|
|
|
<form class="import-form">
|
|
|
|
<div class="modal-body">
|
|
|
|
<div class="form-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<label for="import-file-upload-input"><strong>${t("import.chooseImportFile")}</strong></label>
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-02-06 07:17:58 +02:00
|
|
|
<label class="tn-file-input tn-input-field">
|
|
|
|
<input type="file" class="import-file-upload-input form-control-file" multiple />
|
|
|
|
</label>
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
<p>${t("import.importDescription")} <strong class="import-note-title"></strong>.
|
2022-06-16 14:21:24 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<strong>${t("import.options")}:</strong>
|
2022-06-16 14:21:24 +02:00
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-29 17:26:24 +02:00
|
|
|
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.safeImportTooltip"))}">
|
2022-06-16 14:21:24 +02:00
|
|
|
<input class="safe-import-checkbox" value="1" type="checkbox" checked>
|
2025-01-09 18:07:02 +02:00
|
|
|
<span>${t("import.safeImport")}</span>
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-29 17:26:24 +02:00
|
|
|
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.explodeArchivesTooltip"))}">
|
2022-06-16 14:21:24 +02:00
|
|
|
<input class="explode-archives-checkbox" value="1" type="checkbox" checked>
|
2025-01-09 18:07:02 +02:00
|
|
|
<span>${t("import.explodeArchives")}</span>
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-29 17:26:24 +02:00
|
|
|
<label class="tn-checkbox" data-bs-toggle="tooltip" title="${escapeQuotes(t("import.shrinkImagesTooltip"))}">
|
2025-01-09 18:07:02 +02:00
|
|
|
<input class="shrink-images-checkbox" value="1" type="checkbox" checked> <span>${t("import.shrinkImages")}</span>
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-24 22:35:45 +02:00
|
|
|
<label class="tn-checkbox">
|
2022-06-16 14:21:24 +02:00
|
|
|
<input class="text-imported-as-text-checkbox" value="1" type="checkbox" checked>
|
2025-01-09 18:07:02 +02:00
|
|
|
${t("import.textImportedAsText")}
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-24 22:35:45 +02:00
|
|
|
<label class="tn-checkbox">
|
2025-01-09 18:07:02 +02:00
|
|
|
<input class="code-imported-as-code-checkbox" value="1" type="checkbox" checked> ${t("import.codeImportedAsCode")}
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="checkbox">
|
2025-01-24 22:35:45 +02:00
|
|
|
<label class="tn-checkbox">
|
2022-06-16 14:21:24 +02:00
|
|
|
<input class="replace-underscores-with-spaces-checkbox" value="1" type="checkbox" checked>
|
2025-01-09 18:07:02 +02:00
|
|
|
${t("import.replaceUnderscoresWithSpaces")}
|
2022-06-16 14:21:24 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2025-01-09 18:07:02 +02:00
|
|
|
<button class="import-button btn btn-primary">${t("import.import")}</button>
|
2022-06-16 14:21:24 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class ImportDialog extends BasicWidget {
|
2025-03-20 10:38:45 +02:00
|
|
|
|
|
|
|
private parentNoteId: string | null;
|
|
|
|
|
|
|
|
private $form!: JQuery<HTMLElement>;
|
|
|
|
private $noteTitle!: JQuery<HTMLElement>;
|
|
|
|
private $fileUploadInput!: JQuery<HTMLInputElement>;
|
|
|
|
private $importButton!: JQuery<HTMLElement>;
|
|
|
|
private $safeImportCheckbox!: JQuery<HTMLElement>;
|
|
|
|
private $shrinkImagesCheckbox!: JQuery<HTMLElement>;
|
|
|
|
private $textImportedAsTextCheckbox!: JQuery<HTMLElement>;
|
|
|
|
private $codeImportedAsCodeCheckbox!: JQuery<HTMLElement>;
|
|
|
|
private $explodeArchivesCheckbox!: JQuery<HTMLElement>;
|
|
|
|
private $replaceUnderscoresWithSpacesCheckbox!: JQuery<HTMLElement>;
|
|
|
|
|
2022-06-16 14:21:24 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.parentNoteId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2025-03-20 10:38:45 +02:00
|
|
|
Modal.getOrCreateInstance(this.$widget[0]);
|
2024-09-03 17:08:07 +02:00
|
|
|
|
2022-06-16 14:21:24 +02:00
|
|
|
this.$form = this.$widget.find(".import-form");
|
|
|
|
this.$noteTitle = this.$widget.find(".import-note-title");
|
|
|
|
this.$fileUploadInput = this.$widget.find(".import-file-upload-input");
|
|
|
|
this.$importButton = this.$widget.find(".import-button");
|
|
|
|
this.$safeImportCheckbox = this.$widget.find(".safe-import-checkbox");
|
|
|
|
this.$shrinkImagesCheckbox = this.$widget.find(".shrink-images-checkbox");
|
|
|
|
this.$textImportedAsTextCheckbox = this.$widget.find(".text-imported-as-text-checkbox");
|
|
|
|
this.$codeImportedAsCodeCheckbox = this.$widget.find(".code-imported-as-code-checkbox");
|
|
|
|
this.$explodeArchivesCheckbox = this.$widget.find(".explode-archives-checkbox");
|
|
|
|
this.$replaceUnderscoresWithSpacesCheckbox = this.$widget.find(".replace-underscores-with-spaces-checkbox");
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$form.on("submit", () => {
|
2022-06-16 14:21:24 +02:00
|
|
|
// disabling so that import is not triggered again.
|
|
|
|
this.$importButton.attr("disabled", "disabled");
|
|
|
|
|
2025-03-20 10:38:45 +02:00
|
|
|
if (this.parentNoteId) {
|
|
|
|
this.importIntoNote(this.parentNoteId);
|
|
|
|
}
|
2022-06-16 14:21:24 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$fileUploadInput.on("change", () => {
|
2022-06-16 14:21:24 +02:00
|
|
|
if (this.$fileUploadInput.val()) {
|
|
|
|
this.$importButton.removeAttr("disabled");
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2022-06-16 14:21:24 +02:00
|
|
|
this.$importButton.attr("disabled", "disabled");
|
|
|
|
}
|
|
|
|
});
|
2022-11-22 23:53:08 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
let _ = [...this.$widget.find('[data-bs-toggle="tooltip"]')].forEach((element) => {
|
2025-02-21 20:41:00 +01:00
|
|
|
Tooltip.getOrCreateInstance(element, {
|
2024-09-03 17:08:07 +02:00
|
|
|
html: true
|
|
|
|
});
|
2022-11-22 23:53:08 +01:00
|
|
|
});
|
2022-06-16 14:21:24 +02:00
|
|
|
}
|
|
|
|
|
2025-03-20 10:38:45 +02:00
|
|
|
async showImportDialogEvent({ noteId }: EventData<"showImportDialog">) {
|
2022-06-16 14:21:24 +02:00
|
|
|
this.parentNoteId = noteId;
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$fileUploadInput.val("").trigger("change"); // to trigger Import button disabling listener below
|
2022-06-16 14:21:24 +02:00
|
|
|
|
|
|
|
this.$safeImportCheckbox.prop("checked", true);
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$shrinkImagesCheckbox.prop("checked", options.is("compressImages"));
|
2022-06-16 14:21:24 +02:00
|
|
|
this.$textImportedAsTextCheckbox.prop("checked", true);
|
|
|
|
this.$codeImportedAsCodeCheckbox.prop("checked", true);
|
|
|
|
this.$explodeArchivesCheckbox.prop("checked", true);
|
|
|
|
this.$replaceUnderscoresWithSpacesCheckbox.prop("checked", true);
|
|
|
|
|
|
|
|
this.$noteTitle.text(await treeService.getNoteTitle(this.parentNoteId));
|
|
|
|
|
2025-06-19 22:44:02 +03:00
|
|
|
openDialog(this.$widget);
|
2022-06-16 14:21:24 +02:00
|
|
|
}
|
|
|
|
|
2025-03-20 10:38:45 +02:00
|
|
|
async importIntoNote(parentNoteId: string) {
|
|
|
|
const files = Array.from(this.$fileUploadInput[0].files ?? []); // shallow copy since we're resetting the upload button below
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-03-20 10:38:45 +02:00
|
|
|
const boolToString = ($el: JQuery<HTMLElement>) => ($el.is(":checked") ? "true" : "false");
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-03-20 10:38:45 +02:00
|
|
|
const options: UploadFilesOptions = {
|
2022-06-16 14:21:24 +02:00
|
|
|
safeImport: boolToString(this.$safeImportCheckbox),
|
|
|
|
shrinkImages: boolToString(this.$shrinkImagesCheckbox),
|
|
|
|
textImportedAsText: boolToString(this.$textImportedAsTextCheckbox),
|
|
|
|
codeImportedAsCode: boolToString(this.$codeImportedAsCodeCheckbox),
|
|
|
|
explodeArchives: boolToString(this.$explodeArchivesCheckbox),
|
|
|
|
replaceUnderscoresWithSpaces: boolToString(this.$replaceUnderscoresWithSpacesCheckbox)
|
|
|
|
};
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.modal("hide");
|
2022-06-16 14:21:24 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
await importService.uploadFiles("notes", parentNoteId, files, options);
|
2022-06-16 14:21:24 +02:00
|
|
|
}
|
|
|
|
}
|