mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	fix(views/table): unable to update state for newly created rows
This commit is contained in:
		| @@ -43,19 +43,9 @@ export default class NoteListRenderer { | |||||||
|  |  | ||||||
|     async renderList() { |     async renderList() { | ||||||
|         const args = this.args; |         const args = this.args; | ||||||
|  |         const viewMode = this.#buildViewMode(args); | ||||||
|         let noteIds: string[]; |  | ||||||
|         if (this.viewType === "list" || this.viewType === "grid") { |  | ||||||
|             noteIds = args.parentNote.getChildNoteIds(); |  | ||||||
|         } else { |  | ||||||
|             noteIds = await args.parentNote.getSubtreeNoteIds(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         const viewMode = this.#buildViewMode({ |  | ||||||
|             ...args, |  | ||||||
|             noteIds |  | ||||||
|         }); |  | ||||||
|         this.viewMode = viewMode; |         this.viewMode = viewMode; | ||||||
|  |         await viewMode.beforeRender(); | ||||||
|         return await viewMode.renderList(); |         return await viewMode.renderList(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -168,12 +168,6 @@ export default class NoteListWidget extends NoteContextAwareWidget { | |||||||
|             this.refresh(); |             this.refresh(); | ||||||
|             this.checkRenderStatus(); |             this.checkRenderStatus(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Inform the view mode of changes and refresh if needed. |  | ||||||
|         if (this.viewMode && this.viewMode.onEntitiesReloaded(e)) { |  | ||||||
|             this.refresh(); |  | ||||||
|             this.checkRenderStatus(); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     buildTouchBarCommand(data: CommandListenerData<"buildTouchBar">) { |     buildTouchBarCommand(data: CommandListenerData<"buildTouchBar">) { | ||||||
|   | |||||||
| @@ -113,7 +113,6 @@ export default class CalendarView extends ViewMode<{}> { | |||||||
|  |  | ||||||
|     private $root: JQuery<HTMLElement>; |     private $root: JQuery<HTMLElement>; | ||||||
|     private $calendarContainer: JQuery<HTMLElement>; |     private $calendarContainer: JQuery<HTMLElement>; | ||||||
|     private noteIds: string[]; |  | ||||||
|     private calendar?: Calendar; |     private calendar?: Calendar; | ||||||
|     private isCalendarRoot: boolean; |     private isCalendarRoot: boolean; | ||||||
|     private lastView?: string; |     private lastView?: string; | ||||||
| @@ -124,7 +123,6 @@ export default class CalendarView extends ViewMode<{}> { | |||||||
|  |  | ||||||
|         this.$root = $(TPL); |         this.$root = $(TPL); | ||||||
|         this.$calendarContainer = this.$root.find(".calendar-container"); |         this.$calendarContainer = this.$root.find(".calendar-container"); | ||||||
|         this.noteIds = args.noteIds; |  | ||||||
|         this.isCalendarRoot = false; |         this.isCalendarRoot = false; | ||||||
|         args.$parent.append(this.$root); |         args.$parent.append(this.$root); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -161,7 +161,7 @@ const TPL = /*html*/` | |||||||
| class ListOrGridView extends ViewMode<{}> { | class ListOrGridView extends ViewMode<{}> { | ||||||
|     private $noteList: JQuery<HTMLElement>; |     private $noteList: JQuery<HTMLElement>; | ||||||
|  |  | ||||||
|     private noteIds: string[]; |     private filteredNoteIds!: string[]; | ||||||
|     private page?: number; |     private page?: number; | ||||||
|     private pageSize?: number; |     private pageSize?: number; | ||||||
|     private showNotePath?: boolean; |     private showNotePath?: boolean; | ||||||
| @@ -174,13 +174,6 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|         super(args, viewType); |         super(args, viewType); | ||||||
|         this.$noteList = $(TPL); |         this.$noteList = $(TPL); | ||||||
|  |  | ||||||
|         const includedNoteIds = this.getIncludedNoteIds(); |  | ||||||
|  |  | ||||||
|         this.noteIds = args.noteIds.filter((noteId) => !includedNoteIds.has(noteId) && noteId !== "_hidden"); |  | ||||||
|  |  | ||||||
|         if (this.noteIds.length === 0) { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         args.$parent.append(this.$noteList); |         args.$parent.append(this.$noteList); | ||||||
|  |  | ||||||
| @@ -204,8 +197,14 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|         return new Set(includedLinks.map((rel) => rel.value)); |         return new Set(includedLinks.map((rel) => rel.value)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async beforeRender() { | ||||||
|  |         super.beforeRender(); | ||||||
|  |         const includedNoteIds = this.getIncludedNoteIds(); | ||||||
|  |         this.filteredNoteIds = this.noteIds.filter((noteId) => !includedNoteIds.has(noteId) && noteId !== "_hidden"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     async renderList() { |     async renderList() { | ||||||
|         if (this.noteIds.length === 0 || !this.page || !this.pageSize) { |         if (this.filteredNoteIds.length === 0 || !this.page || !this.pageSize) { | ||||||
|             this.$noteList.hide(); |             this.$noteList.hide(); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @@ -226,7 +225,7 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|         const startIdx = (this.page - 1) * this.pageSize; |         const startIdx = (this.page - 1) * this.pageSize; | ||||||
|         const endIdx = startIdx + this.pageSize; |         const endIdx = startIdx + this.pageSize; | ||||||
|  |  | ||||||
|         const pageNoteIds = this.noteIds.slice(startIdx, Math.min(endIdx, this.noteIds.length)); |         const pageNoteIds = this.filteredNoteIds.slice(startIdx, Math.min(endIdx, this.filteredNoteIds.length)); | ||||||
|         const pageNotes = await froca.getNotes(pageNoteIds); |         const pageNotes = await froca.getNotes(pageNoteIds); | ||||||
|  |  | ||||||
|         for (const note of pageNotes) { |         for (const note of pageNotes) { | ||||||
| @@ -246,7 +245,7 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         const pageCount = Math.ceil(this.noteIds.length / this.pageSize); |         const pageCount = Math.ceil(this.filteredNoteIds.length / this.pageSize); | ||||||
|  |  | ||||||
|         $pager.toggle(pageCount > 1); |         $pager.toggle(pageCount > 1); | ||||||
|  |  | ||||||
| @@ -257,7 +256,7 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|                 lastPrinted = true; |                 lastPrinted = true; | ||||||
|  |  | ||||||
|                 const startIndex = (i - 1) * this.pageSize + 1; |                 const startIndex = (i - 1) * this.pageSize + 1; | ||||||
|                 const endIndex = Math.min(this.noteIds.length, i * this.pageSize); |                 const endIndex = Math.min(this.filteredNoteIds.length, i * this.pageSize); | ||||||
|  |  | ||||||
|                 $pager.append( |                 $pager.append( | ||||||
|                     i === this.page |                     i === this.page | ||||||
| @@ -279,7 +278,7 @@ class ListOrGridView extends ViewMode<{}> { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // no need to distinguish "note" vs "notes" since in case of one result, there's no paging at all |         // no need to distinguish "note" vs "notes" since in case of one result, there's no paging at all | ||||||
|         $pager.append(`<span class="note-list-pager-total-count">(${this.noteIds.length} notes)</span>`); |         $pager.append(`<span class="note-list-pager-total-count">(${this.filteredNoteIds.length} notes)</span>`); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async renderNote(note: FNote, expand: boolean = false) { |     async renderNote(note: FNote, expand: boolean = false) { | ||||||
|   | |||||||
| @@ -101,7 +101,6 @@ export default class TableView extends ViewMode<StateInfo> { | |||||||
|  |  | ||||||
|     private $root: JQuery<HTMLElement>; |     private $root: JQuery<HTMLElement>; | ||||||
|     private $container: JQuery<HTMLElement>; |     private $container: JQuery<HTMLElement>; | ||||||
|     private args: ViewModeArgs; |  | ||||||
|     private spacedUpdate: SpacedUpdate; |     private spacedUpdate: SpacedUpdate; | ||||||
|     private api?: Tabulator; |     private api?: Tabulator; | ||||||
|     private newAttribute?: Attribute; |     private newAttribute?: Attribute; | ||||||
| @@ -112,7 +111,6 @@ export default class TableView extends ViewMode<StateInfo> { | |||||||
|  |  | ||||||
|         this.$root = $(TPL); |         this.$root = $(TPL); | ||||||
|         this.$container = this.$root.find(".table-view-container"); |         this.$container = this.$root.find(".table-view-container"); | ||||||
|         this.args = args; |  | ||||||
|         this.spacedUpdate = new SpacedUpdate(() => this.onSave(), 5_000); |         this.spacedUpdate = new SpacedUpdate(() => this.onSave(), 5_000); | ||||||
|         this.persistentData = {}; |         this.persistentData = {}; | ||||||
|         args.$parent.append(this.$root); |         args.$parent.append(this.$root); | ||||||
| @@ -257,9 +255,9 @@ export default class TableView extends ViewMode<StateInfo> { | |||||||
|             this.#manageColumnUpdate(); |             this.#manageColumnUpdate(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (loadResults.getBranchRows().some(branch => branch.parentNoteId === this.parentNote.noteId || this.args.noteIds.includes(branch.parentNoteId ?? "")) |         if (loadResults.getBranchRows().some(branch => branch.parentNoteId === this.parentNote.noteId || this.noteIds.includes(branch.parentNoteId ?? "")) | ||||||
|             || loadResults.getNoteIds().some(noteId => this.args.noteIds.includes(noteId) |             || loadResults.getNoteIds().some(noteId => this.noteIds.includes(noteId) | ||||||
|             || loadResults.getAttributeRows().some(attr => this.args.noteIds.includes(attr.noteId!)))) { |             || loadResults.getAttributeRows().some(attr => this.noteIds.includes(attr.noteId!)))) { | ||||||
|             this.#manageRowsUpdate(); |             this.#manageRowsUpdate(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| import type { EventData } from "../../components/app_context.js"; | import type { EventData } from "../../components/app_context.js"; | ||||||
|  | import appContext from "../../components/app_context.js"; | ||||||
| import Component from "../../components/component.js"; | import Component from "../../components/component.js"; | ||||||
| import type FNote from "../../entities/fnote.js"; | import type FNote from "../../entities/fnote.js"; | ||||||
| import type { ViewTypeOptions } from "../../services/note_list_renderer.js"; | import type { ViewTypeOptions } from "../../services/note_list_renderer.js"; | ||||||
| @@ -8,7 +9,6 @@ export interface ViewModeArgs { | |||||||
|     $parent: JQuery<HTMLElement>; |     $parent: JQuery<HTMLElement>; | ||||||
|     parentNote: FNote; |     parentNote: FNote; | ||||||
|     parentNotePath?: string | null; |     parentNotePath?: string | null; | ||||||
|     noteIds: string[]; |  | ||||||
|     showNotePath?: boolean; |     showNotePath?: boolean; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -17,6 +17,8 @@ export default abstract class ViewMode<T extends object> extends Component { | |||||||
|     private _viewStorage: ViewModeStorage<T> | null; |     private _viewStorage: ViewModeStorage<T> | null; | ||||||
|     protected parentNote: FNote; |     protected parentNote: FNote; | ||||||
|     protected viewType: ViewTypeOptions; |     protected viewType: ViewTypeOptions; | ||||||
|  |     protected noteIds: string[]; | ||||||
|  |     protected args: ViewModeArgs; | ||||||
|  |  | ||||||
|     constructor(args: ViewModeArgs, viewType: ViewTypeOptions) { |     constructor(args: ViewModeArgs, viewType: ViewTypeOptions) { | ||||||
|         super(); |         super(); | ||||||
| @@ -25,6 +27,12 @@ export default abstract class ViewMode<T extends object> extends Component { | |||||||
|         // note list must be added to the DOM immediately, otherwise some functionality scripting (canvas) won't work |         // note list must be added to the DOM immediately, otherwise some functionality scripting (canvas) won't work | ||||||
|         args.$parent.empty(); |         args.$parent.empty(); | ||||||
|         this.viewType = viewType; |         this.viewType = viewType; | ||||||
|  |         this.args = args; | ||||||
|  |         this.noteIds = []; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async beforeRender() { | ||||||
|  |         await this.#refreshNoteIds(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     abstract renderList(): Promise<JQuery<HTMLElement> | undefined>; |     abstract renderList(): Promise<JQuery<HTMLElement> | undefined>; | ||||||
| @@ -39,6 +47,16 @@ export default abstract class ViewMode<T extends object> extends Component { | |||||||
|         // Do nothing by default. |         // Do nothing by default. | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     entitiesReloadedEvent(e: EventData<"entitiesReloaded">) { | ||||||
|  |         if (e.loadResults.getBranchRows().some(branch => branch.parentNoteId === this.parentNote.noteId || this.noteIds.includes(branch.parentNoteId ?? ""))) { | ||||||
|  |             this.#refreshNoteIds(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (this.onEntitiesReloaded(e)) { | ||||||
|  |             appContext.triggerEvent("refreshNoteList", { noteId: this.parentNote.noteId }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     get isReadOnly() { |     get isReadOnly() { | ||||||
|         return this.parentNote.hasLabel("readOnly"); |         return this.parentNote.hasLabel("readOnly"); | ||||||
|     } |     } | ||||||
| @@ -52,4 +70,14 @@ export default abstract class ViewMode<T extends object> extends Component { | |||||||
|         return this._viewStorage; |         return this._viewStorage; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async #refreshNoteIds() { | ||||||
|  |         let noteIds: string[]; | ||||||
|  |         if (this.viewType === "list" || this.viewType === "grid") { | ||||||
|  |             noteIds = this.args.parentNote.getChildNoteIds(); | ||||||
|  |         } else { | ||||||
|  |             noteIds = await this.args.parentNote.getSubtreeNoteIds(); | ||||||
|  |         } | ||||||
|  |         this.noteIds = noteIds; | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user