From 6de0b1956994e58817e8db49688fce00fc640d39 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 21 Mar 2020 10:38:27 +0100 Subject: [PATCH] add image through "include note" will just insert image instead of standard include note element, #922 --- .../javascripts/dialogs/include_note.js | 19 ++++++++++++++++--- .../javascripts/widgets/type_widgets/text.js | 12 ++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/public/javascripts/dialogs/include_note.js b/src/public/javascripts/dialogs/include_note.js index 790cf1c70..ccda2f235 100644 --- a/src/public/javascripts/dialogs/include_note.js +++ b/src/public/javascripts/dialogs/include_note.js @@ -1,6 +1,7 @@ import treeService from '../services/tree.js'; import noteAutocompleteService from '../services/note_autocomplete.js'; import utils from "../services/utils.js"; +import treeCache from "../services/tree_cache.js"; const $dialog = $("#include-note-dialog"); const $form = $("#include-note-form"); @@ -20,15 +21,27 @@ export async function showDialog(widget) { noteAutocompleteService.showRecentNotes($autoComplete); } +async function includeNote(notePath) { + const noteId = treeService.getNoteIdFromNotePath(notePath); + const note = await treeCache.getNote(noteId); + + if (note.type === 'image') { + // there's no benefit to use insert note functionlity for images + // so we'll just add an IMG tag + textTypeWidget.addImage(noteId); + } + else { + textTypeWidget.addIncludeNote(noteId); + } +} + $form.on('submit', () => { const notePath = $autoComplete.getSelectedPath(); if (notePath) { $dialog.modal('hide'); - const includedNoteId = treeService.getNoteIdFromNotePath(notePath); - - textTypeWidget.addIncludeNote(includedNoteId); + includeNote(notePath); } else { console.error("No noteId to include."); diff --git a/src/public/javascripts/widgets/type_widgets/text.js b/src/public/javascripts/widgets/type_widgets/text.js index 2362761e1..6d40cdd90 100644 --- a/src/public/javascripts/widgets/type_widgets/text.js +++ b/src/public/javascripts/widgets/type_widgets/text.js @@ -279,4 +279,16 @@ export default class TextTypeWidget extends TypeWidget { })); } ); } + + async addImage(noteId) { + const note = await treeCache.getNote(noteId); + + this.textEditor.model.change( writer => { + const src = `api/images/${note.noteId}/${note.title}`; + + const imageElement = writer.createElement( 'image', { 'src': src } ); + + this.textEditor.model.insertContent(imageElement, this.textEditor.model.document.selection); + } ); + } } \ No newline at end of file