Notes/src/public/app/widgets/note_list.ts

131 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import NoteListRenderer from "../services/note_list_renderer.js";
2025-01-28 14:13:21 +02:00
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.js";
import type ViewMode from "./view_widgets/view_mode.js";
const TPL = `
<div class="note-list-widget">
2020-11-26 23:00:27 +01:00
<style>
.note-list-widget {
min-height: 0;
2020-12-13 20:13:57 +01:00
overflow: auto;
2020-11-26 23:00:27 +01:00
}
2025-01-28 14:13:21 +02:00
2020-12-13 23:27:42 +01:00
.note-list-widget .note-list {
padding: 10px;
}
.note-list-widget.full-height,
.note-list-widget.full-height .note-list-widget-content {
height: 100%;
}
2025-02-26 19:36:30 +02:00
.note-list-widget video {
height: 100%;
}
2020-11-26 23:00:27 +01:00
</style>
2025-01-28 14:13:21 +02:00
2020-11-26 23:00:27 +01:00
<div class="note-list-widget-content">
</div>
</div>`;
2021-05-22 12:35:41 +02:00
export default class NoteListWidget extends NoteContextAwareWidget {
2025-01-28 14:13:21 +02:00
private $content!: JQuery<HTMLElement>;
private isIntersecting?: boolean;
private noteIdRefreshed?: string;
private shownNoteId?: string | null;
private viewMode?: ViewMode | null;
2025-01-28 14:13:21 +02:00
isEnabled() {
2025-01-28 14:13:21 +02:00
return super.isEnabled() && this.noteContext?.hasNoteList();
}
doRender() {
this.$widget = $(TPL);
2021-06-15 21:53:22 +02:00
this.contentSized();
2025-01-09 18:07:02 +02:00
this.$content = this.$widget.find(".note-list-widget-content");
2020-12-27 22:19:27 +01:00
2025-01-09 18:07:02 +02:00
const observer = new IntersectionObserver(
(entries) => {
this.isIntersecting = entries[0].isIntersecting;
2020-12-27 22:19:27 +01:00
2025-01-09 18:07:02 +02:00
this.checkRenderStatus();
},
{
rootMargin: "50px",
threshold: 0.1
}
);
2020-12-27 22:19:27 +01:00
// there seems to be a race condition on Firefox which triggers the observer only before the widget is visible
// (intersection is false). https://github.com/zadam/trilium/issues/4165
setTimeout(() => observer.observe(this.$widget[0]), 10);
2020-12-27 22:19:27 +01:00
}
checkRenderStatus() {
// console.log("this.isIntersecting", this.isIntersecting);
// console.log(`${this.noteIdRefreshed} === ${this.noteId}`, this.noteIdRefreshed === this.noteId);
// console.log("this.shownNoteId !== this.noteId", this.shownNoteId !== this.noteId);
2025-01-28 14:13:21 +02:00
if (this.note && this.isIntersecting && this.noteIdRefreshed === this.noteId && this.shownNoteId !== this.noteId) {
2020-12-27 22:19:27 +01:00
this.shownNoteId = this.noteId;
this.renderNoteList(this.note);
}
}
2025-01-28 14:13:21 +02:00
async renderNoteList(note: FNote) {
2020-12-27 22:19:27 +01:00
const noteListRenderer = new NoteListRenderer(this.$content, note, note.getChildNoteIds());
this.$widget.toggleClass("full-height", noteListRenderer.isFullHeight);
2020-12-27 22:19:27 +01:00
await noteListRenderer.renderList();
this.viewMode = noteListRenderer.viewMode;
}
2021-02-05 23:19:43 +01:00
async refresh() {
this.shownNoteId = null;
await super.refresh();
}
2025-01-28 14:13:21 +02:00
async refreshNoteListEvent({ noteId }: EventData<"refreshNoteList">) {
if (this.isNote(noteId) && this.note) {
2021-09-29 13:19:21 +02:00
await this.renderNoteList(this.note);
}
}
/**
* We have this event so that we evaluate intersection only after note detail is loaded.
2023-06-30 11:18:34 +02:00
* If it's evaluated before note detail, then it's clearly intersected (visible) although after note detail load
* it is not intersected (visible) anymore.
*/
2025-01-28 14:13:21 +02:00
noteDetailRefreshedEvent({ ntxId }: EventData<"noteDetailRefreshed">) {
2021-05-22 12:42:34 +02:00
if (!this.isNoteContext(ntxId)) {
2020-12-27 22:19:27 +01:00
return;
}
this.noteIdRefreshed = this.noteId;
2020-12-27 22:19:27 +01:00
setTimeout(() => this.checkRenderStatus(), 100);
}
2025-01-28 14:13:21 +02:00
notesReloadedEvent({ noteIds }: EventData<"notesReloaded">) {
if (this.noteId && noteIds.includes(this.noteId)) {
2020-11-26 23:00:27 +01:00
this.refresh();
}
}
2021-05-27 23:17:13 +02:00
entitiesReloadedEvent(e: EventData<"entitiesReloaded">) {
if (e.loadResults.getAttributeRows().find((attr) => attr.noteId === this.noteId && attr.name && ["viewType", "expanded", "pageSize"].includes(attr.name))) {
this.refresh();
2021-05-27 23:17:13 +02:00
this.checkRenderStatus();
}
// Inform the view mode of changes and refresh if needed.
if (this.viewMode && this.viewMode.onEntitiesReloaded(e)) {
this.refresh();
this.checkRenderStatus();
}
2021-05-27 23:17:13 +02:00
}
}