Skip to content

Build your personal knowledge base with Trilium Notes

link module

azivneradc7d158192f
diff --git a/public/javascripts/dialogs/add_link.js b/public/javascripts/dialogs/add_link.js
index 559c9cffaf..848d0369e4 100644
--- a/public/javascripts/dialogs/add_link.js
+++ b/public/javascripts/dialogs/add_link.js
@@ -29,7 +29,7 @@ const addLink = (function() {
minLength: 0,
change: () => {
const val = autoCompleteEl.val();
- const noteId = getNodeIdFromLabel(val);
+ const noteId = link.getNodeIdFromLabel(val);
if (noteId) {
setDefaultLinkTitle(noteId);
@@ -38,7 +38,7 @@ const addLink = (function() {
// this is called when user goes through autocomplete list with keyboard
// at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is
focus: (event, ui) => {
- const noteId = getNodeIdFromLabel(ui.item.value);
+ const noteId = link.getNodeIdFromLabel(ui.item.value);
setDefaultLinkTitle(noteId);
}
@@ -48,7 +48,7 @@ const addLink = (function() {
formEl.submit(() => {
let val = autoCompleteEl.val();
- const noteId = getNodeIdFromLabel(val);
+ const noteId = link.getNodeIdFromLabel(val);
if (noteId) {
const linkTitle = linkTitleEl.val();
diff --git a/public/javascripts/dialogs/event_log.js b/public/javascripts/dialogs/event_log.js
index 3cee7c4fd6..e3b0630ba6 100644
--- a/public/javascripts/dialogs/event_log.js
+++ b/public/javascripts/dialogs/event_log.js
@@ -23,7 +23,7 @@ const eventLog = (function() {
const dateTime = formatDateTime(getDateFromTS(event.date_added));
if (event.note_id) {
- const noteLink = createNoteLink(event.note_id).prop('outerHTML');
+ const noteLink = link.createNoteLink(event.note_id).prop('outerHTML');
event.comment = event.comment.replace('<note>', noteLink);
}
diff --git a/public/javascripts/dialogs/jump_to_note.js b/public/javascripts/dialogs/jump_to_note.js
index bbc45698d9..f324684d50 100644
--- a/public/javascripts/dialogs/jump_to_note.js
+++ b/public/javascripts/dialogs/jump_to_note.js
@@ -23,7 +23,7 @@ const jumpToNote = (function() {
formEl.submit(() => {
const val = autoCompleteEl.val();
- const noteId = getNodeIdFromLabel(val);
+ const noteId = link.getNodeIdFromLabel(val);
if (noteId) {
getNodeByKey(noteId).setActive();
diff --git a/public/javascripts/dialogs/recent_changes.js b/public/javascripts/dialogs/recent_changes.js
index 51dd6658f8..2d6c5d4759 100644
--- a/public/javascripts/dialogs/recent_changes.js
+++ b/public/javascripts/dialogs/recent_changes.js
@@ -37,7 +37,7 @@ const recentChanges = (function() {
changesListEl.append($('<li>')
.append(formattedTime + ' - ')
- .append(createNoteLink(change.note_id))
+ .append(link.createNoteLink(change.note_id))
.append(' (').append(revLink).append(')'));
}
diff --git a/public/javascripts/init.js b/public/javascripts/init.js
index 506552d250..61f96e8dd7 100644
--- a/public/javascripts/init.js
+++ b/public/javascripts/init.js
@@ -86,66 +86,4 @@ $(document).tooltip({
function isElectron() {
return window && window.process && window.process.type;
-}
-
-// when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
-// of opening the link in new window/tab
-$(document).on('click', "a[action='note']", goToInternalNote);
-$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
-$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
-
-function goToInternalNote(e) {
- const linkEl = $(e.target);
- let noteId = linkEl.attr("note-id");
-
- if (!noteId) {
- noteId = getNoteIdFromLink(linkEl.attr('href'));
- }
-
- if (noteId) {
- getNodeByKey(noteId).setActive();
-
- // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
- $("[role='tooltip']").remove();
-
- if (glob.activeDialog) {
- try {
- glob.activeDialog.dialog('close');
- }
- catch (e) {}
- }
-
- e.preventDefault();
- }
-}
-
-function getNoteIdFromLink(url) {
- const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
-
- if (noteIdMatch === null) {
- return null;
- }
- else {
- return noteIdMatch[1];
- }
-}
-
-function getNodeIdFromLabel(label) {
- const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
-
- if (noteIdMatch !== null) {
- return noteIdMatch[1];
- }
-
- return null;
-}
-
-function createNoteLink(noteId) {
- const noteLink = $("<a>", {
- href: 'javascript:',
- text: getFullName(noteId)
- }).attr('action', 'note')
- .attr('note-id', noteId);
-
- return noteLink;
}
\ No newline at end of file
diff --git a/public/javascripts/link.js b/public/javascripts/link.js
new file mode 100644
index 0000000000..c8836bfef1
--- /dev/null
+++ b/public/javascripts/link.js
@@ -0,0 +1,68 @@
+const link = (function() {
+ function getNoteIdFromLink(url) {
+ const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
+
+ if (noteIdMatch === null) {
+ return null;
+ }
+ else {
+ return noteIdMatch[1];
+ }
+ }
+
+ function getNodeIdFromLabel(label) {
+ const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
+
+ if (noteIdMatch !== null) {
+ return noteIdMatch[1];
+ }
+
+ return null;
+ }
+
+ function createNoteLink(noteId) {
+ const noteLink = $("<a>", {
+ href: 'javascript:',
+ text: getFullName(noteId)
+ }).attr('action', 'note')
+ .attr('note-id', noteId);
+
+ return noteLink;
+ }
+
+ function goToInternalNote(e) {
+ const linkEl = $(e.target);
+ let noteId = linkEl.attr("note-id");
+
+ if (!noteId) {
+ noteId = getNoteIdFromLink(linkEl.attr('href'));
+ }
+
+ if (noteId) {
+ getNodeByKey(noteId).setActive();
+
+ // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
+ $("[role='tooltip']").remove();
+
+ if (glob.activeDialog) {
+ try {
+ glob.activeDialog.dialog('close');
+ }
+ catch (e) {}
+ }
+
+ e.preventDefault();
+ }
+ }
+
+ // when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
+ // of opening the link in new window/tab
+ $(document).on('click', "a[action='note']", goToInternalNote);
+ $(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
+ $(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
+
+ return {
+ getNodeIdFromLabel,
+ createNoteLink
+ };
+})();
\ No newline at end of file
diff --git a/views/index.ejs b/views/index.ejs
index d4e44c4034..b91ad89d81 100644
--- a/views/index.ejs
+++ b/views/index.ejs
@@ -282,6 +282,7 @@
<script src="javascripts/dialogs/recent_changes.js"></script>
<script src="javascripts/dialogs/event_log.js"></script>
+ <script src="javascripts/link.js"></script>
<script src="javascripts/status.js"></script>
<script src="javascripts/sync.js"></script>
<script src="javascripts/utils.js"></script>