mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
refactor(server/utils): use a "real" Map for toMap
This commit is contained in:
parent
f0ba056bb7
commit
ab0c84a57e
@ -731,13 +731,13 @@ function updateNoteData(noteId: string, content: string, attachments: Attachment
|
|||||||
note.setContent(newContent, { forceFrontendReload });
|
note.setContent(newContent, { forceFrontendReload });
|
||||||
|
|
||||||
if (attachments?.length > 0) {
|
if (attachments?.length > 0) {
|
||||||
const existingAttachmentsByTitle = toMap(note.getAttachments({ includeContentLength: false }), "title");
|
const existingAttachmentsByTitle = toMap(note.getAttachments({ includeContentLength: false }), "title");
|
||||||
|
|
||||||
for (const { attachmentId, role, mime, title, position, content } of attachments) {
|
for (const { attachmentId, role, mime, title, position, content } of attachments) {
|
||||||
if (attachmentId || !(title in existingAttachmentsByTitle)) {
|
const existingAttachment = existingAttachmentsByTitle.get(title);
|
||||||
|
if (attachmentId || !existingAttachment) {
|
||||||
note.saveAttachment({ attachmentId, role, mime, title, content, position });
|
note.saveAttachment({ attachmentId, role, mime, title, content, position });
|
||||||
} else {
|
} else {
|
||||||
const existingAttachment = existingAttachmentsByTitle[title];
|
|
||||||
existingAttachment.role = role;
|
existingAttachment.role = role;
|
||||||
existingAttachment.mime = mime;
|
existingAttachment.mime = mime;
|
||||||
existingAttachment.position = position;
|
existingAttachment.position = position;
|
||||||
|
@ -247,13 +247,15 @@ export function normalize(str: string) {
|
|||||||
return removeDiacritic(str).toLowerCase();
|
return removeDiacritic(str).toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toMap<T extends Record<string, any>>(list: T[], key: keyof T): Record<string, T> {
|
export function toMap<T extends Record<string, any>>(list: T[], key: keyof T) {
|
||||||
const map: Record<string, T> = {};
|
const map = new Map<string, T>();
|
||||||
|
|
||||||
for (const el of list) {
|
for (const el of list) {
|
||||||
map[el[key]] = el;
|
const keyForMap = el[key];
|
||||||
|
if (!keyForMap) continue;
|
||||||
|
// TriliumNextTODO: do we need to handle the case when the same key is used?
|
||||||
|
// currently this will overwrite the existing entry in the map
|
||||||
|
map.set(keyForMap, el);
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user