linking attachments finished

This commit is contained in:
zadam 2023-05-29 10:21:34 +02:00
parent 0aa119af2c
commit 9d52f80c2f
5 changed files with 78 additions and 65 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,21 @@ function getNotePathFromUrl(url) {
return notePathMatch === null ? null : notePathMatch[1]; return notePathMatch === null ? null : notePathMatch[1];
} }
async function getLinkIcon(noteId, viewMode) {
let icon;
if (viewMode === 'default') {
const note = await froca.getNote(noteId);
icon = note.getIcon();
} else if (viewMode === 'source') {
icon = 'bx bx-code-curly';
} else if (viewMode === 'attachments') {
icon = 'bx bx-file';
}
return icon;
}
async function createLink(notePath, options = {}) { async function createLink(notePath, options = {}) {
if (!notePath || !notePath.trim()) { if (!notePath || !notePath.trim()) {
logError("Missing note path"); logError("Missing note path");
@ -46,17 +61,7 @@ async function createLink(notePath, options = {}) {
const $container = $("<span>"); const $container = $("<span>");
if (showNoteIcon) { if (showNoteIcon) {
let icon; let icon = await getLinkIcon(noteId, viewMode);
if (viewMode === 'default') {
const note = await froca.getNote(noteId);
icon = note.getIcon();
} else if (viewMode === 'source') {
icon = 'bx-code-curly';
} else if (viewMode === 'attachments') {
icon = 'bx-file';
}
if (icon) { if (icon) {
$container $container
@ -252,33 +257,71 @@ function linkContextMenu(e) {
linkContextMenuService.openContextMenu(notePath, e, viewScope, null); linkContextMenuService.openContextMenu(notePath, e, viewScope, null);
} }
async function loadReferenceLinkTitle($el) { async function loadReferenceLinkTitle($el, href = null) {
const url = $el.attr("href"); href = href || $el.find("a").attr("href");
if (!url) { if (!href) {
console.warn("Empty URL for parsing"); console.warn("Empty URL for parsing: " + $el[0].outerHTML);
return; return;
} }
const {noteId} = parseNavigationStateFromUrl(url); const {noteId, viewScope} = parseNavigationStateFromUrl(href);
const note = await froca.getNote(noteId, true); const note = await froca.getNote(noteId, true);
let title;
if (!note) {
title = '[missing]';
}
else {
title = note.isDeleted ? `${note.title} (deleted)` : note.title;
}
if (note) { if (note) {
$el.addClass(note.getColorClass()); $el.addClass(note.getColorClass());
} }
const title = await getReferenceLinkTitle(href);
$el.text(title); $el.text(title);
if (note) { if (note) {
$el.prepend($("<span>").addClass(note.getIcon())); const icon = await getLinkIcon(noteId, viewScope.viewMode);
$el.prepend($("<span>").addClass(icon));
}
}
async function getReferenceLinkTitle(href) {
const {noteId, viewScope} = parseNavigationStateFromUrl(href);
if (!noteId) {
return "[missing note]";
}
const note = await froca.getNote(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
const attachment = await note.getAttachmentById(viewScope.attachmentId);
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
}
}
function getReferenceLinkTitleSync(href) {
const {noteId, viewScope} = parseNavigationStateFromUrl(href);
if (!noteId) {
return "[missing note]";
}
const note = froca.getNoteFromCache(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
if (!note.attachments) {
return "[loading title...]";
}
const attachment = note.attachments.find(att => att.attachmentId === viewScope.attachmentId);
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
} }
} }
@ -313,6 +356,8 @@ export default {
createLink, createLink,
goToLink, goToLink,
loadReferenceLinkTitle, loadReferenceLinkTitle,
getReferenceLinkTitle,
getReferenceLinkTitleSync,
calculateHash, calculateHash,
parseNavigationStateFromUrl parseNavigationStateFromUrl
}; };

View File

@ -457,7 +457,7 @@ export default class AttributeEditorWidget extends NoteContextAwareWidget {
} }
async loadReferenceLinkTitle($el) { async loadReferenceLinkTitle($el) {
const {noteId} = linkService.parseNavigationStateFromUrl($el.attr("href")); const {noteId} = linkService.parseNavigationStateFromUrl($el.find("a").attr("href"));
const note = await froca.getNote(noteId, true); const note = await froca.getNote(noteId, true);
let title; let title;

View File

@ -97,48 +97,16 @@ export default class AbstractTextTypeWidget extends TypeWidget {
} }
} }
async loadReferenceLinkTitle($el) { async loadReferenceLinkTitle($el, href = null) {
await linkService.loadReferenceLinkTitle($el); await linkService.loadReferenceLinkTitle($el, href);
} }
async getReferenceLinkTitle(href) { async getReferenceLinkTitle(href) {
const {noteId, viewScope} = linkService.parseNavigationStateFromUrl(href); return await linkService.getReferenceLinkTitle(href);
if (!noteId) {
return "[missing note]";
}
const note = await froca.getNote(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
const attachment = await note.getAttachmentById(viewScope.attachmentId);
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
}
} }
getReferenceLinkTitleSync(href) { getReferenceLinkTitleSync(href) {
const {noteId, viewScope} = linkService.parseNavigationStateFromUrl(href); return linkService.getReferenceLinkTitleSync(href);
if (!noteId) {
return "[missing note]";
}
const note = froca.getNoteFromCache(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
const attachment = note.attachments[viewScope.attachmentId];
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
}
} }
refreshIncludedNote($container, noteId) { refreshIncludedNote($container, noteId) {