From a1fb84f14d9382d96ee74b4945372efeea5bbf22 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 3 Dec 2020 22:54:24 +0100 Subject: [PATCH] unification of tooltip and note list renderers --- src/public/app/services/attribute_renderer.js | 16 ++++- .../app/services/note_content_renderer.js | 58 +++++++++++-------- src/public/app/services/note_list_renderer.js | 17 +++++- src/public/app/services/note_tooltip.js | 45 ++++---------- .../type_widgets/abstract_text_type_widget.js | 4 +- src/public/stylesheets/style.css | 2 +- 6 files changed, 76 insertions(+), 66 deletions(-) diff --git a/src/public/app/services/attribute_renderer.js b/src/public/app/services/attribute_renderer.js index a5a1bd3e7..e8986b513 100644 --- a/src/public/app/services/attribute_renderer.js +++ b/src/public/app/services/attribute_renderer.js @@ -63,7 +63,7 @@ async function createNoteLink(noteId) { } async function renderAttributes(attributes, renderIsInheritable) { - const $container = $(""); + const $container = $(''); for (let i = 0; i < attributes.length; i++) { const attribute = attributes[i]; @@ -79,7 +79,19 @@ async function renderAttributes(attributes, renderIsInheritable) { return $container; } +async function renderNormalAttributes(note) { + const attrs = note.getAttributes().filter(attr => !attr.isDefinition() && !attr.isAutoLink); + + const $renderedAttributes = await renderAttributes(attrs, false); + + return { + count: attrs.length, + $renderedAttributes + } +} + export default { renderAttribute, - renderAttributes + renderAttributes, + renderNormalAttributes } diff --git a/src/public/app/services/note_content_renderer.js b/src/public/app/services/note_content_renderer.js index 998bce448..46bafd319 100644 --- a/src/public/app/services/note_content_renderer.js +++ b/src/public/app/services/note_content_renderer.js @@ -4,38 +4,42 @@ import protectedSessionService from "./protected_session.js"; import protectedSessionHolder from "./protected_session_holder.js"; import libraryLoader from "./library_loader.js"; import openService from "./open.js"; +import treeCache from "./tree_cache.js"; async function getRenderedContent(note, options = {}) { options = Object.assign({ - trim: false + trim: false, + tooltip: false }, options); const type = getRenderingType(note); - let $rendered; + const $renderedContent = $('
'); if (type === 'text') { - const fullNote = await server.get('notes/' + note.noteId); + const noteComplement = await treeCache.getNoteComplement(note.noteId); - $rendered = $('
').html(trim(fullNote.content, options.trim)); + $renderedContent.append($('
').html(trim(noteComplement.content, options.trim))); - if ($rendered.find('span.math-tex').length > 0) { + if ($renderedContent.find('span.math-tex').length > 0) { await libraryLoader.requireLibrary(libraryLoader.KATEX); - renderMathInElement($rendered[0], {}); + renderMathInElement($renderedContent[0], {}); } } else if (type === 'code') { const fullNote = await server.get('notes/' + note.noteId); - $rendered = $("
").text(trim(fullNote.content, options.trim));
+        $renderedContent.append($("
").text(trim(fullNote.content, options.trim)));
     }
     else if (type === 'image') {
-        $rendered = $("")
-            .attr("src", `api/images/${note.noteId}/${note.title}`)
-            .css("max-width", "100%");
+        $renderedContent.append(
+            $("")
+                .attr("src", `api/images/${note.noteId}/${note.title}`)
+                .css("max-width", "100%")
+        );
     }
-    else if (type === 'file' || type === 'pdf') {
+    else if (!options.tooltip && (type === 'file' || type === 'pdf')) {
         const $downloadButton = $('');
         const $openButton = $('');
 
@@ -45,44 +49,50 @@ async function getRenderedContent(note, options = {}) {
         // open doesn't work for protected notes since it works through browser which isn't in protected session
         $openButton.toggle(!note.isProtected);
 
-        $rendered = $('
'); + const $content = $('
'); if (type === 'pdf') { const $pdfPreview = $(''); $pdfPreview.attr("src", openService.getUrlForDownload("api/notes/" + note.noteId + "/open")); - $rendered.append($pdfPreview); + $content.append($pdfPreview); } - $rendered.append( + $content.append( $("
") .append($downloadButton) .append('   ') .append($openButton) ); + + $renderedContent.append($content); } else if (type === 'render') { - $rendered = $('
'); + const $content = $('
'); - await renderService.render(note, $rendered, this.ctx); + await renderService.render(note, $content, this.ctx); + + $renderedContent.append($content); } - else if (type === 'protected-session') { + else if (!options.tooltip && type === 'protected-session') { const $button = $(``) .on('click', protectedSessionService.enterProtectedSession); - $rendered = $("
") - .append("
This note is protected and to access it you need to enter password.
") - .append("
") - .append($button); + $renderedContent.append( + $("
") + .append("
This note is protected and to access it you need to enter password.
") + .append("
") + .append($button) + ); } else { - $rendered = $("Content of this note cannot be displayed in the book format"); + $renderedContent.append($("Content of this note cannot be displayed in the book format")); } - $rendered.addClass(note.getCssClass()); + $renderedContent.addClass(note.getCssClass()); return { - renderedContent: $rendered, + $renderedContent, type }; } diff --git a/src/public/app/services/note_list_renderer.js b/src/public/app/services/note_list_renderer.js index f2735642d..ce525288f 100644 --- a/src/public/app/services/note_list_renderer.js +++ b/src/public/app/services/note_list_renderer.js @@ -2,6 +2,7 @@ import linkService from "./link.js"; import noteContentRenderer from "./note_content_renderer.js"; import treeCache from "./tree_cache.js"; import attributeService from "./attributes.js"; +import attributeRenderer from "./attribute_renderer.js"; const TPL = `
@@ -56,6 +57,14 @@ const TPL = ` margin-bottom: 0; } + .note-book-title .rendered-note-attributes { + font-size: medium; + } + + .note-book-title .rendered-note-attributes:before { + content: "\\00a0\\00a0"; + } + .note-book-card .note-book-card { border: 1px solid var(--main-border-color); } @@ -244,16 +253,18 @@ class NoteListRenderer { } } - // TODO: we should also render (promoted) attributes async renderNote(note, expand = false) { const $expander = $(''); + const {$renderedAttributes} = await attributeRenderer.renderNormalAttributes(note); + const $card = $('
') .attr('data-note-id', note.noteId) .append( $('
') .append($expander) .append(await linkService.createNoteLink(note.noteId, {showTooltip: false})) + .append($renderedAttributes) ); $expander.on('click', () => this.toggleContent($card, note, !$card.hasClass("expanded"))); @@ -288,11 +299,11 @@ class NoteListRenderer { const $content = $('
'); try { - const {renderedContent, type} = await noteContentRenderer.getRenderedContent(note, { + const {$renderedContent, type} = await noteContentRenderer.getRenderedContent(note, { trim: this.viewType === 'grid' // for grid only short content is needed }); - $content.append(renderedContent); + $content.append($renderedContent); $content.addClass("type-" + type); } catch (e) { console.log(`Caught error while rendering note ${note.noteId} of type ${note.type}: ${e.message}, stack: ${e.stack}`); diff --git a/src/public/app/services/note_tooltip.js b/src/public/app/services/note_tooltip.js index 10a685efe..5c90419c2 100644 --- a/src/public/app/services/note_tooltip.js +++ b/src/public/app/services/note_tooltip.js @@ -3,7 +3,7 @@ import linkService from "./link.js"; import treeCache from "./tree_cache.js"; import utils from "./utils.js"; import attributeRenderer from "./attribute_renderer.js"; -import libraryLoader from "./library_loader.js"; +import noteContentRenderer from "./note_content_renderer.js"; function setupGlobalTooltip() { $(document).on("mouseenter", "a", mouseEnterHandler); @@ -45,9 +45,7 @@ async function mouseEnterHandler() { const noteId = treeService.getNoteIdFromNotePath(notePath); const note = await treeCache.getNote(noteId); - const noteComplement = await treeCache.getNoteComplement(noteId); - - const content = await renderTooltip(note, noteComplement); + const content = await renderTooltip(note); if (utils.isHtmlEmpty(content)) { return; @@ -79,7 +77,7 @@ function mouseLeaveHandler() { $(this).tooltip('dispose'); } -async function renderTooltip(note, noteComplement) { +async function renderTooltip(note) { if (note.isDeleted) { return '
Note has been deleted.
'; } @@ -92,37 +90,16 @@ async function renderTooltip(note, noteComplement) { let content = $("
").text(await treeService.getNotePathTitle(someNotePath)).prop('outerHTML'); - const attributes = note.getAttributes() - .filter(attr => !attr.isDefinition()); + const {$renderedAttributes} = await attributeRenderer.renderNormalAttributes(note); - if (attributes.length > 0) { - content += '
Attributes: ' - + (await attributeRenderer.renderAttributes(attributes)).html() - + '
' - } + const {$renderedContent} = await noteContentRenderer.getRenderedContent(note, { + tooltip: true, + trim: true + }); - if (note.type === 'text' && !utils.isHtmlEmpty(noteComplement.content)) { - const $content = $('
').append(noteComplement.content); - - if ($content.find('span.math-tex').length > 0) { - await libraryLoader.requireLibrary(libraryLoader.KATEX); - - renderMathInElement($content[0], {}); - } - - content += $content[0].outerHTML; - } - else if (note.type === 'code' && noteComplement.content && noteComplement.content.trim()) { - content += $("
")
-            .text(noteComplement.content)
-            .prop('outerHTML');
-    }
-    else if (note.type === 'image') {
-        content += $("")
-            .prop("src", `api/images/${note.noteId}/${note.title}`)
-            .prop('outerHTML');
-    }
-    // other types of notes don't have tooltip preview
+    content = content
+        + $renderedAttributes[0].outerHTML
+        + $renderedContent[0].outerHTML;
 
     return content;
 }
diff --git a/src/public/app/widgets/type_widgets/abstract_text_type_widget.js b/src/public/app/widgets/type_widgets/abstract_text_type_widget.js
index 2d4a6443d..dbb44fcf8 100644
--- a/src/public/app/widgets/type_widgets/abstract_text_type_widget.js
+++ b/src/public/app/widgets/type_widgets/abstract_text_type_widget.js
@@ -36,11 +36,11 @@ export default class AbstractTextTypeWidget extends TypeWidget {
                     .append($link)
             );
 
-            const {renderedContent, type} = await noteContentRenderer.getRenderedContent(note);
+            const {$renderedContent, type} = await noteContentRenderer.getRenderedContent(note);
 
             $el.append(
                 $(`
`) - .append(renderedContent) + .append($renderedContent) ); } } diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css index 53fd80990..8126befc0 100644 --- a/src/public/stylesheets/style.css +++ b/src/public/stylesheets/style.css @@ -411,7 +411,7 @@ table.promoted-attributes-in-tooltip td, table.promoted-attributes-in-tooltip th box-shadow: 10px 10px 93px -25px #aaaaaa; } -.tooltip-attributes { +.rendered-note-attributes { color: var(--muted-text-color); margin-bottom: 7px; }