mirror of
https://github.com/zadam/trilium.git
synced 2025-11-26 02:24:23 +01:00
chore(print): basic infrastructure to support reporting progress
This commit is contained in:
parent
63d430c3d9
commit
6ca941e8e9
@ -7,6 +7,7 @@ import content_renderer, { applyInlineMermaid } from "./services/content_rendere
|
||||
interface RendererProps {
|
||||
note: FNote;
|
||||
onReady: () => void;
|
||||
onProgressChanged?: (progress: number) => void;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@ -23,13 +24,16 @@ async function main() {
|
||||
|
||||
function App({ note, noteId }: { note: FNote | null | undefined, noteId: string }) {
|
||||
const sentReadyEvent = useRef(false);
|
||||
const onProgressChanged = useCallback((progress: number) => {
|
||||
window.dispatchEvent(new CustomEvent("note-load-progress", { detail: { progress } }));
|
||||
}, []);
|
||||
const onReady = useCallback(() => {
|
||||
if (sentReadyEvent.current) return;
|
||||
window.dispatchEvent(new Event("note-ready"));
|
||||
window._noteReady = true;
|
||||
sentReadyEvent.current = true;
|
||||
}, []);
|
||||
const props: RendererProps | undefined | null = note && { note, onReady };
|
||||
const props: RendererProps | undefined | null = note && { note, onReady, onProgressChanged };
|
||||
|
||||
if (!note || !props) return <Error404 noteId={noteId} />
|
||||
|
||||
@ -89,7 +93,7 @@ function SingleNoteRenderer({ note, onReady }: RendererProps) {
|
||||
</>;
|
||||
}
|
||||
|
||||
function CollectionRenderer({ note, onReady }: RendererProps) {
|
||||
function CollectionRenderer({ note, onReady, onProgressChanged }: RendererProps) {
|
||||
const viewType = useNoteViewType(note);
|
||||
return <CustomNoteList
|
||||
viewType={viewType}
|
||||
@ -103,6 +107,7 @@ function CollectionRenderer({ note, onReady }: RendererProps) {
|
||||
await loadCustomCss(note);
|
||||
onReady();
|
||||
}}
|
||||
onProgressChanged={onProgressChanged}
|
||||
/>;
|
||||
}
|
||||
|
||||
|
||||
@ -162,6 +162,10 @@ export default function NoteDetail() {
|
||||
return;
|
||||
}
|
||||
|
||||
iframe.contentWindow.addEventListener("note-load-progress", (e) => {
|
||||
console.log("Got ", e);
|
||||
});
|
||||
|
||||
iframe.contentWindow.addEventListener("note-ready", () => {
|
||||
toast.closePersistent("printing");
|
||||
iframe.contentWindow?.print();
|
||||
|
||||
@ -26,9 +26,10 @@ interface NoteListProps {
|
||||
media: ViewModeMedia;
|
||||
viewType: ViewTypeOptions | undefined;
|
||||
onReady?: () => void;
|
||||
onProgressChanged?(progress: number): void;
|
||||
}
|
||||
|
||||
export default function NoteList(props: Pick<NoteListProps, "displayOnlyCollections" | "media" | "onReady">) {
|
||||
export default function NoteList(props: Pick<NoteListProps, "displayOnlyCollections" | "media" | "onReady" | "onProgressChanged">) {
|
||||
const { note, noteContext, notePath, ntxId } = useNoteContext();
|
||||
const viewType = useNoteViewType(note);
|
||||
const [ enabled, setEnabled ] = useState(noteContext?.hasNoteList());
|
||||
@ -43,7 +44,7 @@ export function SearchNoteList(props: Omit<NoteListProps, "isEnabled" | "viewTyp
|
||||
return <CustomNoteList {...props} isEnabled={true} viewType={viewType} />
|
||||
}
|
||||
|
||||
export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, ...restProps }: NoteListProps) {
|
||||
export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, onProgressChanged, ...restProps }: NoteListProps) {
|
||||
const widgetRef = useRef<HTMLDivElement>(null);
|
||||
const noteIds = useNoteIds(shouldEnable ? note : null, viewType, ntxId);
|
||||
const isFullHeight = (viewType && viewType !== "list" && viewType !== "grid");
|
||||
@ -86,6 +87,8 @@ export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePa
|
||||
viewConfig: viewModeConfig.config,
|
||||
saveConfig: viewModeConfig.storeFn,
|
||||
onReady: onReady ?? (() => {}),
|
||||
onProgressChanged: onProgressChanged ?? (() => {}),
|
||||
|
||||
...restProps
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,4 +17,5 @@ export interface ViewModeProps<T extends object> {
|
||||
saveConfig(newConfig: T): void;
|
||||
media: ViewModeMedia;
|
||||
onReady(): void;
|
||||
onProgressChanged?(progress: number): void;
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ interface NotesWithContent {
|
||||
contentEl: HTMLElement;
|
||||
}
|
||||
|
||||
export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: ViewModeProps<{}>) {
|
||||
export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady, onProgressChanged }: ViewModeProps<{}>) {
|
||||
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
|
||||
const [ notesWithContent, setNotesWithContent ] = useState<NotesWithContent[]>();
|
||||
|
||||
@ -33,6 +33,10 @@ export function ListPrintView({ note, noteIds: unfilteredNoteIds, onReady }: Vie
|
||||
noteIdsSet.add(note.noteId);
|
||||
notesWithContent.push({ note, contentEl });
|
||||
|
||||
if (onProgressChanged) {
|
||||
onProgressChanged((notesWithContent.length / noteIds.length) * 100);
|
||||
}
|
||||
|
||||
if (note.hasChildren()) {
|
||||
const filteredChildNotes = await filterChildNotes(note);
|
||||
for (const childNote of filteredChildNotes) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user