Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
reduced dependencies of utils
azivnerd3316cd09cfb
diff --git a/src/public/javascripts/dialogs/labels.js b/src/public/javascripts/dialogs/labels.jsindex 87c1482fa8..1786395192 100644--- a/src/public/javascripts/dialogs/labels.js+++ b/src/public/javascripts/dialogs/labels.js@@ -1,6 +1,7 @@import noteDetailService from '../services/note_detail.js';import utils from '../services/utils.js';import server from '../services/server.js';+import infoService from "../services/info.js";const $dialog = $("#labels-dialog");const $saveLabelsButton = $("#save-labels-button");@@ -91,7 +92,7 @@ function LabelsModel() {addLastEmptyRow();- utils.showMessage("Labels have been saved.");+ infoService.showMessage("Labels have been saved.");noteDetailService.loadLabelList();};diff --git a/src/public/javascripts/dialogs/recent_notes.js b/src/public/javascripts/dialogs/recent_notes.jsindex 23464c1e0d..d51df4db83 100644--- a/src/public/javascripts/dialogs/recent_notes.js+++ b/src/public/javascripts/dialogs/recent_notes.js@@ -1,7 +1,7 @@import treeService from '../services/tree.js';import messagingService from '../services/messaging.js';import server from '../services/server.js';-import utils from "../services/utils";+import utils from "../services/utils.js";const $dialog = $("#recent-notes-dialog");const $searchInput = $('#recent-notes-search-input');diff --git a/src/public/javascripts/dialogs/settings.js b/src/public/javascripts/dialogs/settings.jsindex d7a24b7664..283067f1ec 100644--- a/src/public/javascripts/dialogs/settings.js+++ b/src/public/javascripts/dialogs/settings.js@@ -3,6 +3,7 @@import protectedSessionHolder from '../services/protected_session_holder.js';import utils from '../services/utils.js';import server from '../services/server.js';+import infoService from "../services/info.js";const $dialog = $("#settings-dialog");const $tabs = $("#settings-tabs");@@ -38,7 +39,7 @@ async function saveSettings(settingName, settingValue) {value: settingValue});- utils.showMessage("Settings change have been saved.");+ infoService.showMessage("Settings change have been saved.");}export default {@@ -80,7 +81,7 @@ addModule((function() {protectedSessionHolder.resetProtectedSession();}else {- utils.showError(result.message);+ infoService.showError(result.message);}});@@ -164,27 +165,27 @@ addModule((async function () {$forceFullSyncButton.click(async () => {await server.post('sync/force-full-sync');- utils.showMessage("Full sync triggered");+ infoService.showMessage("Full sync triggered");});$fillSyncRowsButton.click(async () => {await server.post('sync/fill-sync-rows');- utils.showMessage("Sync rows filled successfully");+ infoService.showMessage("Sync rows filled successfully");});$anonymizeButton.click(async () => {await server.post('anonymization/anonymize');- utils.showMessage("Created anonymized database");+ infoService.showMessage("Created anonymized database");});$cleanupSoftDeletedButton.click(async () => {if (confirm("Do you really want to clean up soft-deleted items?")) {await server.post('cleanup/cleanup-soft-deleted-items');- utils.showMessage("Soft deleted items have been cleaned up");+ infoService.showMessage("Soft deleted items have been cleaned up");}});@@ -192,14 +193,14 @@ addModule((async function () {if (confirm("Do you really want to clean up unused images?")) {await server.post('cleanup/cleanup-unused-images');- utils.showMessage("Unused images have been cleaned up");+ infoService.showMessage("Unused images have been cleaned up");}});$vacuumDatabaseButton.click(async () => {await server.post('cleanup/vacuum-database');- utils.showMessage("Database has been vacuumed");+ infoService.showMessage("Database has been vacuumed");});return {};diff --git a/src/public/javascripts/dialogs/sql_console.js b/src/public/javascripts/dialogs/sql_console.jsindex 69bb0a2f15..bd69bc091b 100644--- a/src/public/javascripts/dialogs/sql_console.js+++ b/src/public/javascripts/dialogs/sql_console.js@@ -1,5 +1,6 @@import utils from '../services/utils.js';import server from '../services/server.js';+import infoService from "../services/info.js";const $dialog = $("#sql-console-dialog");const $query = $('#sql-console-query');@@ -60,11 +61,11 @@ async function execute(e) {});if (!result.success) {- utils.showError(result.error);+ infoService.showError(result.error);return;}else {- utils.showMessage("Query was executed successfully.");+ infoService.showMessage("Query was executed successfully.");}const rows = result.rows;diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.jsindex cdd655269c..578a1ba9a3 100644--- a/src/public/javascripts/services/bundle.js+++ b/src/public/javascripts/services/bundle.js@@ -1,4 +1,4 @@-import ScriptContext from "./script_context";+import ScriptContext from "./script_context.js";async function executeBundle(bundle) {const apiContext = ScriptContext(bundle.note, bundle.allNotes);diff --git a/src/public/javascripts/services/context_menu.js b/src/public/javascripts/services/context_menu.jsindex ac8e87ed93..a5d96b70d3 100644--- a/src/public/javascripts/services/context_menu.js+++ b/src/public/javascripts/services/context_menu.js@@ -7,6 +7,7 @@ import treeChangesService from './tree_changes.js';import treeUtils from './tree_utils.js';import utils from './utils.js';import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';+import infoService from "./info.js";const $tree = $("#tree");@@ -33,7 +34,7 @@ async function pasteAfter(node) {// just do nothing}else {- utils.throwError("Unrecognized clipboard mode=" + clipboardMode);+ infoService.throwError("Unrecognized clipboard mode=" + clipboardMode);}}@@ -56,7 +57,7 @@ async function pasteInto(node) {// just do nothing}else {- utils.throwError("Unrecognized clipboard mode=" + mode);+ infoService.throwError("Unrecognized clipboard mode=" + mode);}}@@ -64,14 +65,14 @@ function copy(nodes) {clipboardIds = nodes.map(node => node.data.noteId);clipboardMode = 'copy';- utils.showMessage("Note(s) have been copied into clipboard.");+ infoService.showMessage("Note(s) have been copied into clipboard.");}function cut(nodes) {clipboardIds = nodes.map(node => node.key);clipboardMode = 'cut';- utils.showMessage("Note(s) have been cut into clipboard.");+ infoService.showMessage("Note(s) have been cut into clipboard.");}const contextMenuSettings = {diff --git a/src/public/javascripts/services/info.js b/src/public/javascripts/services/info.jsnew file mode 100644index 0000000000..b20f6262dc--- /dev/null+++ b/src/public/javascripts/services/info.js@@ -0,0 +1,39 @@+import messagingService from "./messaging.js";++function showMessage(message) {+ console.log(now(), "message: ", message);++ $.notify({+ // options+ message: message+ }, {+ // settings+ type: 'success',+ delay: 3000+ });+}++function showError(message, delay = 10000) {+ console.log(now(), "error: ", message);++ $.notify({+ // options+ message: message+ }, {+ // settings+ type: 'danger',+ delay: delay+ });+}++function throwError(message) {+ messagingService.logError(message);++ throw new Error(message);+}++export default {+ showMessage,+ showError,+ throwError+}\ No newline at end of filediff --git a/src/public/javascripts/services/messaging.js b/src/public/javascripts/services/messaging.jsindex dc57ed818a..b40f7a3762 100644--- a/src/public/javascripts/services/messaging.js+++ b/src/public/javascripts/services/messaging.js@@ -1,4 +1,5 @@import utils from './utils.js';+import infoService from "./info.js";const $changesToPushCount = $("#changes-to-push-count");@@ -45,10 +46,10 @@ function handleMessage(event) {$changesToPushCount.html(message.changesToPushCount);}else if (message.type === 'sync-hash-check-failed') {- utils.utils.showError("Sync check failed!", 60000);+ infoService.showError("Sync check failed!", 60000);}else if (message.type === 'consistency-checks-failed') {- utils.showError("Consistency checks failed! See logs for details.", 50 * 60000);+ infoService.showError("Consistency checks failed! See logs for details.", 50 * 60000);}}@@ -91,7 +92,7 @@ setTimeout(() => {await connectionBrokenNotification.close();connectionBrokenNotification = null;- utils.showMessage("Re-connected to server");+ infoService.showMessage("Re-connected to server");}ws.send(JSON.stringify({diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.jsindex e3a895c6da..51d3fbeda5 100644--- a/src/public/javascripts/services/note_detail.js+++ b/src/public/javascripts/services/note_detail.js@@ -6,6 +6,7 @@ import utils from './utils.js';import server from './server.js';import messagingService from "./messaging.js";import bundleService from "./bundle.js";+import infoService from "./info.js";const $noteTitle = $("#note-title");@@ -109,7 +110,7 @@ function updateNoteFromInputs(note) {// nothing}else {- utils.throwError("Unrecognized type: " + note.detail.type);+ infoService.throwError("Unrecognized type: " + note.detail.type);}const title = $noteTitle.val();@@ -124,7 +125,7 @@ async function saveNoteToServer(note) {isNoteChanged = false;- utils.showMessage("Saved!");+ infoService.showMessage("Saved!");}function setNoteBackgroundIfProtected(note) {@@ -318,7 +319,7 @@ function focus() {// do nothing}else {- utils.throwError('Unrecognized type: ' + note.detail.type);+ infoService.throwError('Unrecognized type: ' + note.detail.type);}}@@ -343,7 +344,7 @@ async function executeCurrentNote() {await server.post('script/run/' + getCurrentNoteId());}- utils.showMessage("Note executed");+ infoService.showMessage("Note executed");}}@@ -368,7 +369,7 @@ function getAttachmentUrl() {messagingService.subscribeToMessages(syncData => {if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) {- utils.showMessage('Reloading note because of background changes');+ infoService.showMessage('Reloading note because of background changes');reload();}diff --git a/src/public/javascripts/services/note_type.js b/src/public/javascripts/services/note_type.jsindex 0c23ff3645..127de49c3a 100644--- a/src/public/javascripts/services/note_type.js+++ b/src/public/javascripts/services/note_type.js@@ -1,6 +1,7 @@import treeService from './tree.js';import noteDetail from './note_detail.js';import utils from './utils.js';+import infoService from "./info.js";const $executeScriptButton = $("#execute-script-button");const noteTypeModel = new NoteTypeModel();@@ -74,7 +75,7 @@ function NoteTypeModel() {// ignore and do nothing, "type" will be hidden since it's not possible to switch to and from search}else {- utils.throwError('Unrecognized type: ' + type);+ infoService.throwError('Unrecognized type: ' + type);}};diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.jsindex a9afdfb5c5..cddbe619d8 100644--- a/src/public/javascripts/services/protected_session.js+++ b/src/public/javascripts/services/protected_session.js@@ -3,6 +3,7 @@ import noteDetail from './note_detail.js';import utils from './utils.js';import server from './server.js';import protectedSessionHolder from './protected_session_holder.js';+import infoService from "./info.js";const $dialog = $("#protected-session-password-dialog");const $passwordForm = $("#protected-session-password-form");@@ -48,7 +49,7 @@ async function setupProtectedSession() {const response = await enterProtectedSession(password);if (!response.success) {- utils.showError("Wrong password.");+ infoService.showError("Wrong password.");return;}@@ -123,7 +124,7 @@ async function protectSubTree(noteId, protect) {await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0));- utils.showMessage("Request to un/protect sub tree has finished successfully");+ infoService.showMessage("Request to un/protect sub tree has finished successfully");treeService.reload();noteDetail.reload();diff --git a/src/public/javascripts/services/server.js b/src/public/javascripts/services/server.jsindex c777a7fe0c..947ab865bc 100644--- a/src/public/javascripts/services/server.js+++ b/src/public/javascripts/services/server.js@@ -1,5 +1,6 @@import protectedSessionHolder from './protected_session_holder.js';import utils from './utils.js';+import infoService from "./info.js";function getHeaders() {let protectedSessionId = null;@@ -74,8 +75,8 @@ async function ajax(url, method, data) {return await $.ajax(options).catch(e => {const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText;- utils.showError(message);- utils.throwError(message);+ infoService.showError(message);+ infoService.throwError(message);});}diff --git a/src/public/javascripts/services/sync.js b/src/public/javascripts/services/sync.jsindex 7dd6cbde05..85e9a58f03 100644--- a/src/public/javascripts/services/sync.js+++ b/src/public/javascripts/services/sync.js@@ -1,17 +1,18 @@import utils from './utils.js';+import infoService from "./info.js";async function syncNow() {const result = await server.post('sync/now');if (result.success) {- utils.showMessage("Sync finished successfully.");+ infoService.showMessage("Sync finished successfully.");}else {if (result.message.length > 50) {result.message = result.message.substr(0, 50);}- utils.showError("Sync failed: " + result.message);+ infoService.showError("Sync failed: " + result.message);}}@@ -20,7 +21,7 @@ $("#sync-now-button").click(syncNow);async function forceNoteSync(noteId) {const result = await server.post('sync/force-note-sync/' + noteId);- utils.showMessage("Note added to sync queue.");+ infoService.showMessage("Note added to sync queue.");}export default {diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.jsindex e98e6255bc..c6dfaef2a5 100644--- a/src/public/javascripts/services/tree.js+++ b/src/public/javascripts/services/tree.js@@ -11,6 +11,7 @@ import server from './server.js';import recentNotesDialog from '../dialogs/recent_notes.js';import editTreePrefixDialog from '../dialogs/edit_tree_prefix.js';import treeCache from './tree_cache.js';+import infoService from "./info.js";const $tree = $("#tree");const $parentList = $("#parent-list");@@ -27,7 +28,7 @@ async function getNote(noteId) {const note = await treeCache.getNote(noteId);if (!note) {- utils.throwError(`Can't find title for noteId='${noteId}'`);+ infoService.throwError(`Can't find title for noteId='${noteId}'`);}return note;@@ -296,7 +297,7 @@ async function showParentList(noteId, node) {const parents = await note.getParentNotes();if (!parents.length) {- utils.throwError("Can't find parents for noteId=" + noteId);+ infoService.throwError("Can't find parents for noteId=" + noteId);}if (parents.length <= 1) {@@ -355,7 +356,7 @@ async function getSomeNotePath(note) {const parents = await cur.getParentNotes();if (!parents.length) {- utils.throwError("Can't find parents for " + cur);+ infoService.throwError("Can't find parents for " + cur);}cur = parents[0];@@ -831,12 +832,12 @@ async function createNote(node, parentNoteId, target, isProtected) {node.renderTitle();}else {- utils.throwError("Unrecognized target: " + target);+ infoService.throwError("Unrecognized target: " + target);}clearSelectedNodes(); // to unmark previously active node- utils.showMessage("Created!");+ infoService.showMessage("Created!");}async function sortAlphabetically(noteId) {diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.jsindex 2ce6371f0b..ce22d9d405 100644--- a/src/public/javascripts/services/tree_cache.js+++ b/src/public/javascripts/services/tree_cache.js@@ -1,6 +1,7 @@import utils from "./utils.js";import Branch from "../entities/branch.js";import NoteShort from "../entities/note_short.js";+import infoService from "./info.js";class TreeCache {load(noteRows, branchRows) {@@ -59,7 +60,7 @@ class TreeCache {const branch = this.childParentToBranch[key];if (!branch) {- utils.throwError("Cannot find branch for child-parent=" + key);+ infoService.throwError("Cannot find branch for child-parent=" + key);}return branch;diff --git a/src/public/javascripts/services/tree_changes.js b/src/public/javascripts/services/tree_changes.jsindex 74183e5854..56ceb7d05e 100644--- a/src/public/javascripts/services/tree_changes.js+++ b/src/public/javascripts/services/tree_changes.js@@ -1,6 +1,7 @@import treeService from './tree.js';import utils from './utils.js';import server from './server.js';+import infoService from "./info.js";async function moveBeforeNode(nodesToMove, beforeNode) {for (const nodeToMove of nodesToMove) {@@ -86,7 +87,7 @@ async function deleteNodes(nodes) {treeService.reload();- utils.showMessage("Note(s) has been deleted.");+ infoService.showMessage("Note(s) has been deleted.");}async function moveNodeUpInHierarchy(node) {diff --git a/src/public/javascripts/services/utils.js b/src/public/javascripts/services/utils.jsindex 1094461265..32e6a5a1d5 100644--- a/src/public/javascripts/services/utils.js+++ b/src/public/javascripts/services/utils.js@@ -1,41 +1,7 @@-import messagingService from './messaging.js';-function reloadApp() {window.location.reload(true);}-function showMessage(message) {- console.log(now(), "message: ", message);-- $.notify({- // options- message: message- }, {- // settings- type: 'success',- delay: 3000- });-}--function showError(message, delay = 10000) {- console.log(now(), "error: ", message);-- $.notify({- // options- message: message- }, {- // settings- type: 'danger',- delay: delay- });-}--function throwError(message) {- messagingService.logError(message);-- throw new Error(message);-}-function parseDate(str) {try {return new Date(Date.parse(str));@@ -80,17 +46,11 @@ function isElectron() {function assertArguments() {for (const i in arguments) {if (!arguments[i]) {- throwError(`Argument idx#${i} should not be falsy: ${arguments[i]}`);+ throw new Error(`Argument idx#${i} should not be falsy: ${arguments[i]}`);}}}-function assert(expr, message) {- if (!expr) {- throwError(message);- }-}-function isTopLevelNode(node) {return isRootNode(node.getParent());}@@ -236,9 +196,6 @@ function bindShortcut(keyboardShortcut, handler) {export default {reloadApp,- showMessage,- showError,- throwError,parseDate,padNum,formatTime,@@ -249,7 +206,6 @@ export default {now,isElectron,assertArguments,- assert,isTopLevelNode,isRootNode,escapeHtml,