1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-17 23:27:48 +02:00
Files
joplin/packages/app-desktop/gui/NoteList/utils/useRenderedNotes.ts

102 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-08-11 18:18:32 +01:00
import { useState } from 'react';
import { ListRenderer, ListRendererDepependency } from './types';
import { NoteEntity } from '@joplin/lib/services/database/types';
import { Size } from '@joplin/utils/types';
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
import * as Mustache from 'mustache';
2023-08-12 15:45:19 +01:00
import { createHash } from 'crypto';
2023-08-11 18:18:32 +01:00
interface RenderedNote {
id: string;
2023-08-12 15:45:19 +01:00
hash: string;
2023-08-11 18:18:32 +01:00
html: string;
}
2023-08-12 15:45:19 +01:00
const hashContent = (content: any) => {
return createHash('sha1').update(JSON.stringify(content)).digest('hex');
};
2023-08-13 12:21:12 +01:00
const prepareViewProps = async (dependencies: ListRendererDepependency[], note: NoteEntity, itemSize: Size, selected: boolean, itemIndex: number) => {
2023-08-12 15:45:19 +01:00
const output: any = {};
for (const dep of dependencies) {
if (dep.startsWith('note.')) {
const splitted = dep.split('.');
if (splitted.length !== 2) throw new Error(`Invalid dependency name: ${dep}`);
const propName = splitted.pop();
if (!output.note) output.note = {};
if (!(propName in note)) throw new Error(`Invalid dependency name: ${dep}`);
output.note[propName] = (note as any)[propName];
2023-08-11 18:18:32 +01:00
}
2023-08-12 15:45:19 +01:00
if (dep.startsWith('item.size.')) {
const splitted = dep.split('.');
if (splitted.length !== 3) throw new Error(`Invalid dependency name: ${dep}`);
const propName = splitted.pop();
if (!output.item) output.item = {};
if (!output.item.size) output.item.size = {};
if (!(propName in itemSize)) throw new Error(`Invalid dependency name: ${dep}`);
output.item.size[propName] = (itemSize as any)[propName];
}
2023-08-11 18:18:32 +01:00
2023-08-12 15:45:19 +01:00
if (dep === 'item.selected') {
if (!output.item) output.item = {};
output.item.selected = selected;
}
2023-08-13 12:21:12 +01:00
if (dep === 'item.index') {
if (!output.item) output.item = {};
output.item.index = itemIndex;
}
2023-08-12 15:45:19 +01:00
}
return output;
};
2023-08-11 18:18:32 +01:00
2023-08-12 15:45:19 +01:00
const useRenderedNotes = (startNoteIndex: number, endNoteIndex: number, notes: NoteEntity[], selectedNoteIds: string[], itemSize: Size, listRenderer: ListRenderer) => {
const [renderedNotes, setRenderedNotes] = useState<Record<string, RenderedNote>>({});
useAsyncEffect(async (event) => {
2023-08-13 12:21:12 +01:00
const renderNote = async (note: NoteEntity, noteIndex: number): Promise<void> => {
2023-08-11 18:18:32 +01:00
const view = await listRenderer.onRenderNote(await prepareViewProps(
listRenderer.dependencies,
note,
itemSize,
2023-08-13 12:21:12 +01:00
selectedNoteIds.includes(note.id),
noteIndex
2023-08-11 18:18:32 +01:00
));
2023-08-12 15:45:19 +01:00
if (event.cancelled) return null;
const viewHash = hashContent(view);
setRenderedNotes(prev => {
if (prev[note.id] && prev[note.id].hash === viewHash) return prev;
return {
...prev,
[note.id]: {
id: note.id,
hash: viewHash,
html: Mustache.render(listRenderer.itemTemplate, view),
},
};
2023-08-11 18:18:32 +01:00
});
2023-08-12 15:45:19 +01:00
};
2023-08-11 18:18:32 +01:00
2023-08-12 15:45:19 +01:00
const promises: Promise<void>[] = [];
for (let i = startNoteIndex; i <= endNoteIndex; i++) {
const note = notes[i];
2023-08-13 12:21:12 +01:00
promises.push(renderNote(note, i));
2023-08-12 15:45:19 +01:00
}
2023-08-11 18:18:32 +01:00
2023-08-12 15:45:19 +01:00
await Promise.all(promises);
}, [startNoteIndex, endNoteIndex, notes, selectedNoteIds, itemSize, listRenderer]);
2023-08-11 18:18:32 +01:00
return renderedNotes;
};
export default useRenderedNotes;