add image through "include note" will just insert image instead of standard include note element, #922

This commit is contained in:
zadam 2020-03-21 10:38:27 +01:00
parent 84d7097b1a
commit 6de0b19569
2 changed files with 28 additions and 3 deletions

View file

@ -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.");

View file

@ -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);
} );
}
}