From fa406d3ded897e7b9cbf223d5ce2b80406742a12 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 30 Mar 2023 23:48:26 +0200 Subject: [PATCH] wip attachment widget --- .../0218__migrate_images_to_attachments.js | 6 +- src/becca/entities/bnote.js | 14 ++- src/public/app/widgets/attachment_detail.js | 101 ++++++++++++++++++ .../widgets/buttons/attachments_actions.js | 54 +--------- .../app/widgets/type_widgets/attachments.js | 4 +- src/routes/api/notes.js | 19 +++- src/routes/routes.js | 1 + 7 files changed, 138 insertions(+), 61 deletions(-) create mode 100644 src/public/app/widgets/attachment_detail.js diff --git a/db/migrations/0218__migrate_images_to_attachments.js b/db/migrations/0218__migrate_images_to_attachments.js index 5100abe06..76ba971da 100644 --- a/db/migrations/0218__migrate_images_to_attachments.js +++ b/db/migrations/0218__migrate_images_to_attachments.js @@ -11,11 +11,13 @@ module.exports = () => { try { const attachment = note.convertToParentAttachment({force: false}); - log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`) + if (attachment) { + log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`); + } } catch (e) { log.error(`Cannot convert note '${note.noteId}' to attachment: ${e.message} ${e.stack}`); } } }); -}; \ No newline at end of file +}; diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 2c459663c..69ecffa0b 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -1086,11 +1086,15 @@ class BNote extends AbstractBeccaEntity { .map(row => new BAttachment(row)); } - /** @returns {BAttachment|undefined} */ - getAttachmentByName(name) { - return sql.getRows("SELECT * FROM attachments WHERE parentId = ? AND name = ? AND isDeleted = 0", [this.noteId, name]) - .map(row => new BAttachment(row)) - [0]; + /** @returns {BAttachment|null} */ + getAttachmentById(attachmentId) { + return sql.getRows(` + SELECT attachments.* + FROM attachments + WHERE parentId = ? + AND attachmentId = ? + AND isDeleted = 0`, [this.noteId, attachmentId]) + .map(row => new BAttachment(row))[0]; } /** diff --git a/src/public/app/widgets/attachment_detail.js b/src/public/app/widgets/attachment_detail.js new file mode 100644 index 000000000..bf8a1fb5d --- /dev/null +++ b/src/public/app/widgets/attachment_detail.js @@ -0,0 +1,101 @@ +import TypeWidget from "./type_widget.js"; +import server from "../../services/server.js"; +import utils from "../../services/utils.js"; +import AttachmentActionsWidget from "../buttons/attachments_actions.js"; +import BasicWidget from "./basic_widget.js"; + +const TPL = ` +
+ + +
+
`; + +export default class AttachmentDetailWidget extends BasicWidget { + constructor(attachment) { + super(); + + this.attachment = attachment; + } + + doRender() { + this.$widget = $(TPL); + this.$wrapper = this.$widget.find('.attachment-detail-wrapper'); + + super.doRender(); + } + + async doRefresh(note) { + this.$list.empty(); + this.children = []; + + const attachments = await server.get(`notes/${this.noteId}/attachments?includeContent=true`); + + if (attachments.length === 0) { + this.$list.html("This note has no attachments."); + + return; + } + + for (const attachment of attachments) { + const attachmentActionsWidget = new AttachmentActionsWidget(attachment); + this.child(attachmentActionsWidget); + + this.$list.append( + $('
') + .append( + $('
') + .append($('

').append($('').text(attachment.title))) + .append( + $('
') + .text(`Role: ${attachment.role}, Size: ${utils.formatSize(attachment.contentLength)}`) + ) + .append($('
')) // spacer + .append(attachmentActionsWidget.render()) + ) + .append( + $('
') + .append(this.renderContent(attachment)) + ) + ); + } + } + + renderContent(attachment) { + if (attachment.content) { + return $("
").text(attachment.content);
+        } else if (attachment.role === 'image') {
+            return ``;
+        } else {
+            return '';
+        }
+    }
+}
diff --git a/src/public/app/widgets/buttons/attachments_actions.js b/src/public/app/widgets/buttons/attachments_actions.js
index 1fc1983b5..6d6121862 100644
--- a/src/public/app/widgets/buttons/attachments_actions.js
+++ b/src/public/app/widgets/buttons/attachments_actions.js
@@ -1,4 +1,5 @@
 import BasicWidget from "../basic_widget.js";
+import server from "../../services/server.js";
 
 const TPL = `