/*
ELYSIA MARKDOWN STUDIO v1.0 - Export Module
Multi-format document export
*/
import Utils from "./utils.js";
import DB from "./db.js";
const Export = {
init() {
document.getElementById("btn-export").addEventListener("click", () => {
Utils.modal.open("modal-export");
});
document.querySelectorAll(".export-btn").forEach(btn => {
btn.addEventListener("click", () => {
const format = btn.getAttribute("data-format");
this.exportDocument(format);
});
});
},
exportDocument(format) {
const title = window.app?.currentDoc?.title || "document";
const content = window.app?.editor.getContent();
if (!content && format !== "all" && format !== "import") {
Utils.toast.warning("No content to export");
return;
}
Utils.modal.close("modal-export");
switch (format) {
case "md":
this.exportMarkdown(content, title);
break;
case "html":
this.exportHTML(content, title);
break;
case "artifact":
this.exportArtifact(content, title);
break;
case "txt":
this.exportPlainText(content, title);
break;
case "json":
this.exportJSON(content, title);
break;
case "clipboard":
this.copyToClipboard(content);
break;
case "all":
this.exportAllDocuments();
break;
case "import":
this.importDocuments();
break;
}
},
exportMarkdown(content, title) {
const filename = Utils.sanitizeFilename(title) + ".md";
Utils.downloadFile(content, filename, "text/markdown");
Utils.toast.success("Markdown exported!");
},
exportHTML(content, title) {
const html = window.app?.preview.getHTML();
const fullHTML = `
${title}
${html}
`;
const filename = Utils.sanitizeFilename(title) + ".html";
Utils.downloadFile(fullHTML, filename, "text/html");
Utils.toast.success("HTML exported!");
},
exportArtifact(content, title) {
const html = window.app?.preview.getHTML();
const artifact = `
${title}
${html}
`;
const filename = Utils.sanitizeFilename(title) + "_artifact.html";
Utils.downloadFile(artifact, filename, "text/html");
Utils.toast.success("Artifact exported!");
},
exportPlainText(content, title) {
// Remove markdown formatting
let plainText = content;
plainText = plainText.replace(/^#+\s+/gm, ""); // Headers
plainText = plainText.replace(/\*\*(.*?)\*\*/g, "$1"); // Bold
plainText = plainText.replace(/\*(.*?)\*/g, "$1"); // Italic
plainText = plainText.replace(/~~(.*?)~~/g, "$1"); // Strikethrough
plainText = plainText.replace(/`(.*?)`/g, "$1"); // Inline code
plainText = plainText.replace(/\[(.*?)\]\(.*?\)/g, "$1"); // Links
const filename = Utils.sanitizeFilename(title) + ".txt";
Utils.downloadFile(plainText, filename, "text/plain");
Utils.toast.success("Plain text exported!");
},
exportJSON(content, title) {
const doc = window.app?.currentDoc || {};
const data = {
title,
content,
wordCount: Utils.countWords(content),
charCount: Utils.countChars(content),
tags: doc.tags || [],
exportDate: new Date().toISOString(),
metadata: {
createdAt: doc.createdAt,
updatedAt: doc.updatedAt,
favorite: doc.favorite
}
};
const json = JSON.stringify(data, null, 2);
const filename = Utils.sanitizeFilename(title) + ".json";
Utils.downloadFile(json, filename, "application/json");
Utils.toast.success("JSON exported!");
},
async exportAllDocuments() {
try {
const allData = await DB.exportAll();
if (!allData) {
Utils.toast.error("Failed to export documents");
return;
}
const json = JSON.stringify(allData, null, 2);
const filename = `elysia-studio-backup-${Date.now()}.json`;
Utils.downloadFile(json, filename, "application/json");
Utils.toast.success(`Exported ${allData.documents.length} documents!`);
} catch (err) {
console.error("Export all failed:", err);
Utils.toast.error("Failed to export documents");
}
},
importDocuments() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = async e => {
const file = e.target.files[0];
if (!file) return;
try {
const text = await file.text();
const data = JSON.parse(text);
const success = await DB.importAll(data);
if (success) {
Utils.toast.success("Documents imported successfully!");
window.app?.documents.loadDocuments();
}
} catch (err) {
console.error("Import failed:", err);
Utils.toast.error("Failed to import documents. Invalid file format.");
}
};
input.click();
},
// Copy markdown to clipboard
async copyToClipboard(content) {
try {
await navigator.clipboard.writeText(content);
Utils.toast.success("✅ Markdown copied to clipboard!");
} catch (err) {
console.error("Clipboard copy failed:", err);
Utils.toast.error("Failed to copy to clipboard");
}
}
};
export default Export;