Skip to content

Build your personal knowledge base with Trilium Notes

client: Reduce code duplication

Elian Doranace237989cb7
diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js
index 7cbdfb351f..6422fa2368 100644
--- a/src/public/app/services/utils.js
+++ b/src/public/app/services/utils.js
@@ -508,6 +508,26 @@ function createImageSrcUrl(note) {
return `api/images/${note.noteId}/${encodeURIComponent(note.title)}?timestamp=${Date.now()}`;
}
+/**
+ * Given a string representation of an SVG, triggers a download of the file on the client device.
+ *
+ * @param {string} nameWithoutExtension the name of the file. The .svg suffix is automatically added to it.
+ * @param {string} svgContent the content of the SVG file download.
+ */
+function downloadSvg(nameWithoutExtension, svgContent) {
+ const filename = `${nameWithoutExtension}.svg`;
+ const element = document.createElement('a');
+ element.setAttribute('href', `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgContent)}`);
+ element.setAttribute('download', filename);
+
+ element.style.display = 'none';
+ document.body.appendChild(element);
+
+ element.click();
+
+ document.body.removeChild(element);
+}
+
export default {
reloadFrontendApp,
parseDate,
@@ -547,5 +567,6 @@ export default {
escapeRegExp,
areObjectsEqual,
copyHtmlToClipboard,
- createImageSrcUrl
+ createImageSrcUrl,
+ downloadSvg
};
diff --git a/src/public/app/widgets/mermaid.js b/src/public/app/widgets/mermaid.js
index cab9718600..204133d548 100644
--- a/src/public/app/widgets/mermaid.js
+++ b/src/public/app/widgets/mermaid.js
@@ -1,6 +1,7 @@
import libraryLoader from "../services/library_loader.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import server from "../services/server.js";
+import utils from "../services/utils.js";
const TPL = `<div class="mermaid-widget">
<style>
@@ -138,19 +139,6 @@ export default class MermaidWidget extends NoteContextAwareWidget {
}
const svg = await this.renderSvg();
- this.download(`${this.note.title}.svg`, svg);
- }
-
- download(filename, text) {
- const element = document.createElement('a');
- element.setAttribute('href', `data:image/svg+xml;charset=utf-8,${encodeURIComponent(text)}`);
- element.setAttribute('download', filename);
-
- element.style.display = 'none';
- document.body.appendChild(element);
-
- element.click();
-
- document.body.removeChild(element);
+ utils.downloadSvg(this.note.title, svg);
}
}
diff --git a/src/public/app/widgets/type_widgets/mind_map.js b/src/public/app/widgets/type_widgets/mind_map.js
index 28a899f66a..abaad5f6bc 100644
--- a/src/public/app/widgets/type_widgets/mind_map.js
+++ b/src/public/app/widgets/type_widgets/mind_map.js
@@ -1,5 +1,6 @@
import libraryLoader from "../../services/library_loader.js";
import TypeWidget from "./type_widget.js";
+import utils from "../../services/utils.js";
const TPL = `
<div class="note-detail-mind-map note-detail-printable">
@@ -114,20 +115,7 @@ export default class MindMapWidget extends TypeWidget {
}
const svg = await this.renderSvg();
- this.download(`${this.note.title}.svg`, svg);
- }
-
- download(filename, text) {
- const element = document.createElement('a');
- element.setAttribute('href', `data:image/svg+xml;charset=utf-8,${encodeURIComponent(text)}`);
- element.setAttribute('download', filename);
-
- element.style.display = 'none';
- document.body.appendChild(element);
-
- element.click();
-
- document.body.removeChild(element);
+ utils.downloadSvg(this.note.title, svg);
}
}
\ No newline at end of file