From 57b6271c0bbb60c5997cb6ef7bf4f2345cdb4837 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 29 May 2021 13:24:14 +0200 Subject: [PATCH] similar notes section widget --- src/public/app/dialogs/clone_to.js | 2 +- src/public/app/layouts/desktop_layout.js | 2 + src/public/app/widgets/similar_notes.js | 151 ------------------ .../type_property_widgets/note_paths.js | 2 +- .../type_property_widgets/similar_notes.js | 98 ++++++++++++ 5 files changed, 102 insertions(+), 153 deletions(-) delete mode 100644 src/public/app/widgets/similar_notes.js create mode 100644 src/public/app/widgets/type_property_widgets/similar_notes.js diff --git a/src/public/app/dialogs/clone_to.js b/src/public/app/dialogs/clone_to.js index 34910aa1e..e1ec951fa 100644 --- a/src/public/app/dialogs/clone_to.js +++ b/src/public/app/dialogs/clone_to.js @@ -16,7 +16,7 @@ let clonedNoteIds; export async function showDialog(noteIds) { if (!noteIds || noteIds.length === 0) { - noteIds = [ appContext.tabManager.getActiveContextNoteId() ] + noteIds = [ appContext.tabManager.getActiveContextNoteId() ]; } clonedNoteIds = []; diff --git a/src/public/app/layouts/desktop_layout.js b/src/public/app/layouts/desktop_layout.js index 7293d83c8..a0e6fee27 100644 --- a/src/public/app/layouts/desktop_layout.js +++ b/src/public/app/layouts/desktop_layout.js @@ -38,6 +38,7 @@ import BookPropertiesWidget from "../widgets/type_property_widgets/book_properti import ShowNoteSourceButton from "../widgets/buttons/show_note_source.js"; import LinkMapWidget from "../widgets/type_property_widgets/link_map.js"; import NotePathsWidget from "../widgets/type_property_widgets/note_paths.js"; +import SimilarNotesWidget from "../widgets/type_property_widgets/similar_notes.js"; export default class DesktopLayout { constructor(customWidgets) { @@ -115,6 +116,7 @@ export default class DesktopLayout { .section(new InheritedAttributesWidget()) .section(new NotePathsWidget()) .section(new LinkMapWidget()) + .section(new SimilarNotesWidget()) .section(new NoteInfoWidget()) .button(new ButtonWidget() .icon('bx bx-history') diff --git a/src/public/app/widgets/similar_notes.js b/src/public/app/widgets/similar_notes.js deleted file mode 100644 index a23c8bb2e..000000000 --- a/src/public/app/widgets/similar_notes.js +++ /dev/null @@ -1,151 +0,0 @@ -import linkService from "../services/link.js"; -import server from "../services/server.js"; -import froca from "../services/froca.js"; -import NoteContextAwareWidget from "./note_context_aware_widget.js"; -import options from "../services/options.js"; - -const TPL = ` -
- - -
-
- -
- -
-
- -
-
-`; - -export default class SimilarNotesWidget extends NoteContextAwareWidget { - isEnabled() { - return super.isEnabled() - && this.note.type !== 'search' - && !this.note.hasLabel('similarNotesWidgetDisabled'); - } - - doRender() { - this.$widget = $(TPL); - this.overflowing(); - - this.$similarNotesWrapper = this.$widget.find(".similar-notes-wrapper"); - this.$expanderText = this.$widget.find(".area-expander-text"); - - this.$expander = this.$widget.find('.area-expander'); - this.$expander.on('click', async () => { - const collapse = this.$similarNotesWrapper.is(":visible"); - - await options.save('similarNotesExpanded', !collapse); - - this.triggerEvent(`similarNotesCollapsedStateChanged`, {collapse}); - }); - - return this.$widget; - } - - noteSwitched() { - const noteId = this.noteId; - - this.toggleInt(false); - this.$similarNotesWrapper.hide(); // we'll open it in refresh() if needed - - // avoid executing this expensive operation multiple times when just going through notes (with keyboard especially) - // until the users settles on a note - setTimeout(() => { - if (this.noteId === noteId) { - this.refresh(); - } - }, 1000); - } - - async refresh() { - if (!this.isEnabled()) { - return; - } - - // remember which title was when we found the similar notes - this.title = this.note.title; - - const similarNotes = await server.get('similar-notes/' + this.noteId); - - if (!similarNotes) { - this.toggleInt(false); - return; - } - - this.toggleInt(similarNotes.length > 0); - - if (similarNotes.length === 0) { - return; - } - - if (options.is('similarNotesExpanded')) { - this.$similarNotesWrapper.show(); - } - - this.$expanderText.text(`${similarNotes.length} similar note${similarNotes.length === 1 ? '': "s"}`); - - const noteIds = similarNotes.flatMap(note => note.notePath); - - await froca.getNotes(noteIds, true); // preload all at once - - const $list = $('
'); - - for (const similarNote of similarNotes) { - const note = await froca.getNote(similarNote.noteId, true); - - if (!note) { - continue; - } - - const $item = (await linkService.createNoteLink(similarNote.notePath.join("/"))) - .css("font-size", 24 * (1 - 1 / (1 + similarNote.score))); - - $list.append($item); - } - - this.$similarNotesWrapper.empty().append($list); - } - - entitiesReloadedEvent({loadResults}) { - if (this.note && this.title !== this.note.title) { - this.rendered = false; - - this.refresh(); - } - } - - /** - * This event is used to synchronize collapsed state of all the tab-cached widgets since they are all rendered - * separately but should behave uniformly for the user. - */ - similarNotesCollapsedStateChangedEvent({collapse}) { - if (collapse) { - this.$similarNotesWrapper.slideUp(200); - } else { - this.$similarNotesWrapper.slideDown(200); - } - } -} diff --git a/src/public/app/widgets/type_property_widgets/note_paths.js b/src/public/app/widgets/type_property_widgets/note_paths.js index d3f42c4ec..0e3c3b1b5 100644 --- a/src/public/app/widgets/type_property_widgets/note_paths.js +++ b/src/public/app/widgets/type_property_widgets/note_paths.js @@ -44,7 +44,7 @@ export default class NotePathsWidget extends NoteContextAwareWidget { getTitle() { return { - show: this.isEnabled(), + show: true, title: 'Note Paths', icon: 'bx bx-collection' }; diff --git a/src/public/app/widgets/type_property_widgets/similar_notes.js b/src/public/app/widgets/type_property_widgets/similar_notes.js new file mode 100644 index 000000000..1f63de56c --- /dev/null +++ b/src/public/app/widgets/type_property_widgets/similar_notes.js @@ -0,0 +1,98 @@ +import linkService from "../../services/link.js"; +import server from "../../services/server.js"; +import froca from "../../services/froca.js"; +import NoteContextAwareWidget from "../note_context_aware_widget.js"; + +const TPL = ` +
+ + +
+
+`; + +export default class SimilarNotesWidget extends NoteContextAwareWidget { + static getType() { return "similar-notes"; } + + isEnabled() { + return super.isEnabled() + && this.note.type !== 'search' + && !this.note.hasLabel('similarNotesWidgetDisabled'); + } + + getTitle() { + return { + show: this.isEnabled(), + title: 'Similar Notes', + icon: 'bx bx-bar-chart' + }; + } + + doRender() { + this.$widget = $(TPL); + this.overflowing(); + + this.$similarNotesWrapper = this.$widget.find(".similar-notes-wrapper"); + } + + async refreshWithNote() { + // remember which title was when we found the similar notes + this.title = this.note.title; + + const similarNotes = await server.get('similar-notes/' + this.noteId); + + if (similarNotes.length === 0) { + this.$similarNotesWrapper.empty().append("No similar notes found."); + + return; + } + + const noteIds = similarNotes.flatMap(note => note.notePath); + + await froca.getNotes(noteIds, true); // preload all at once + + const $list = $('
'); + + for (const similarNote of similarNotes) { + const note = await froca.getNote(similarNote.noteId, true); + + if (!note) { + continue; + } + + const $item = (await linkService.createNoteLink(similarNote.notePath.join("/"))) + .css("font-size", 24 * (1 - 1 / (1 + similarNote.score))); + + $list.append($item); + } + + this.$similarNotesWrapper.empty().append($list); + } + + entitiesReloadedEvent({loadResults}) { + if (this.note && this.title !== this.note.title) { + this.rendered = false; + + this.refresh(); + } + } +}