mirror of
https://github.com/zadam/trilium.git
synced 2024-12-26 09:12:08 +08:00
link module
This commit is contained in:
parent
819ae7c4c0
commit
adc7d15819
7 changed files with 75 additions and 68 deletions
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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(')'));
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
68
public/javascripts/link.js
Normal file
68
public/javascripts/link.js
Normal file
|
@ -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
|
||||
};
|
||||
})();
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue