From d467db222784182384e5f72a70922c37abaa603d Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 28 Dec 2019 21:10:02 +0100 Subject: [PATCH] unify API for creating note links --- .../javascripts/dialogs/recent_changes.js | 5 +++- src/public/javascripts/services/link.js | 23 +++++++++---------- src/public/javascripts/services/link_map.js | 2 +- .../javascripts/services/note_detail_book.js | 2 +- .../services/note_detail_relation_map.js | 2 +- .../javascripts/services/tab_context.js | 2 +- .../javascripts/widgets/edited_notes.js | 2 +- .../javascripts/widgets/similar_notes.js | 2 +- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/public/javascripts/dialogs/recent_changes.js b/src/public/javascripts/dialogs/recent_changes.js index af2a94bd4..703cb145f 100644 --- a/src/public/javascripts/dialogs/recent_changes.js +++ b/src/public/javascripts/dialogs/recent_changes.js @@ -44,7 +44,10 @@ export async function showDialog() { const note = await treeCache.getNote(change.noteId); const notePath = await treeService.getSomeNotePath(note); - noteLink = await linkService.createNoteLinkWithPath(notePath, change.title); + noteLink = await linkService.createNoteLink(notePath, { + title: change.title, + showNotePath: true + }); } changesListEl.append($('
  • ') diff --git a/src/public/javascripts/services/link.js b/src/public/javascripts/services/link.js index dcf210d5d..c1f74bf9a 100644 --- a/src/public/javascripts/services/link.js +++ b/src/public/javascripts/services/link.js @@ -9,7 +9,11 @@ function getNotePathFromUrl(url) { return notePathMatch === null ? null : notePathMatch[1]; } -async function createNoteLink(notePath, noteTitle = null, tooltip = true) { +async function createNoteLink(notePath, options = {}) { + let noteTitle = options.title; + const showTooltip = options.showTooltip === undefined ? true : options.showTooltip; + const showNotePath = options.showNotePath === undefined ? false : options.showNotePath; + if (!noteTitle) { const {noteId, parentNoteId} = treeUtils.getNoteIdAndParentIdFromNotePath(notePath); @@ -22,30 +26,26 @@ async function createNoteLink(notePath, noteTitle = null, tooltip = true) { }).attr('data-action', 'note') .attr('data-note-path', notePath); - if (!tooltip) { + if (!showTooltip) { $noteLink.addClass("no-tooltip-preview"); } - return $noteLink; -} + const $container = $("").append($noteLink); -async function createNoteLinkWithPath(notePath, noteTitle = null) { - const $link = await createNoteLink(notePath, noteTitle); + if (showNotePath) { + notePath = await treeService.resolveNotePath(notePath); - const $res = $("").append($link); - - if (notePath.includes("/")) { const noteIds = notePath.split("/"); noteIds.pop(); // remove last element const parentNotePath = noteIds.join("/").trim(); if (parentNotePath) { - $res.append($("").text(" (" + await treeUtils.getNotePathTitle(parentNotePath) + ")")); + $container.append($("").text(" (" + await treeUtils.getNotePathTitle(parentNotePath) + ")")); } } - return $res; + return $container; } function getNotePathFromLink($link) { @@ -180,7 +180,6 @@ $(document).on('contextmenu', ".note-detail-render a", newTabContextMenu); export default { getNotePathFromUrl, createNoteLink, - createNoteLinkWithPath, addLinkToEditor, addTextToEditor, goToLink diff --git a/src/public/javascripts/services/link_map.js b/src/public/javascripts/services/link_map.js index 967415a97..37bc0c77f 100644 --- a/src/public/javascripts/services/link_map.js +++ b/src/public/javascripts/services/link_map.js @@ -88,7 +88,7 @@ export default class LinkMap { .addClass("note-box") .prop("id", noteBoxId); - linkService.createNoteLink(noteId, note.title).then($link => { + linkService.createNoteLink(noteId, {title: note.title}).then($link => { $link.on('click', e => { try { $link.tooltip('dispose'); diff --git a/src/public/javascripts/services/note_detail_book.js b/src/public/javascripts/services/note_detail_book.js index 09ed86bf5..f2bbd8e5e 100644 --- a/src/public/javascripts/services/note_detail_book.js +++ b/src/public/javascripts/services/note_detail_book.js @@ -137,7 +137,7 @@ class NoteDetailBook { .attr('data-note-id', childNote.noteId) .css("flex-basis", ZOOMS[this.zoomLevel].width) .addClass("type-" + type) - .append($('
    ').append(await linkService.createNoteLink(childNotePath, null, false))) + .append($('
    ').append(await linkService.createNoteLink(childNotePath, {showTooltip: false}))) .append($('
    ') .css("max-height", ZOOMS[this.zoomLevel].height) .append(await this.getNoteContent(type, childNote))); diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index 45feb0d92..c46ffde32 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -494,7 +494,7 @@ class NoteDetailRelationMap { } async createNoteBox(noteId, title, x, y) { - const $link = await linkService.createNoteLink(noteId, title); + const $link = await linkService.createNoteLink(noteId, {title}); $link.mousedown(e => { console.log(e); diff --git a/src/public/javascripts/services/tab_context.js b/src/public/javascripts/services/tab_context.js index f5f5125db..86aa65fa9 100644 --- a/src/public/javascripts/services/tab_context.js +++ b/src/public/javascripts/services/tab_context.js @@ -379,7 +379,7 @@ class TabContext { async addPath(notePath, isCurrent) { const title = await treeUtils.getNotePathTitle(notePath); - const noteLink = await linkService.createNoteLink(notePath, title); + const noteLink = await linkService.createNoteLink(notePath, {title}); noteLink .addClass("no-tooltip-preview") diff --git a/src/public/javascripts/widgets/edited_notes.js b/src/public/javascripts/widgets/edited_notes.js index ee7dbe793..01ebf07b1 100644 --- a/src/public/javascripts/widgets/edited_notes.js +++ b/src/public/javascripts/widgets/edited_notes.js @@ -45,7 +45,7 @@ class EditedNotesWidget extends StandardWidget { $item.append($("").text(editedNote.title + " (deleted)")); } else { - $item.append(editedNote.notePath ? await linkService.createNoteLinkWithPath(editedNote.notePath.join("/")) : editedNote.title); + $item.append(editedNote.notePath ? await linkService.createNoteLink(editedNote.notePath.join("/"), {showNotePath: true}) : editedNote.title); } $list.append($item); diff --git a/src/public/javascripts/widgets/similar_notes.js b/src/public/javascripts/widgets/similar_notes.js index e4e338032..21e6d2b41 100644 --- a/src/public/javascripts/widgets/similar_notes.js +++ b/src/public/javascripts/widgets/similar_notes.js @@ -39,7 +39,7 @@ class SimilarNotesWidget extends StandardWidget { } const $item = $("
  • ") - .append(await linkService.createNoteLinkWithPath(similarNote.notePath.join("/"))); + .append(await linkService.createNoteLink(similarNote.notePath.join("/"), {showNotePath: true})); $list.append($item); }