mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	port history navigation, remove redundance
This commit is contained in:
		| @@ -2,17 +2,36 @@ import utils from "../../services/utils.js"; | |||||||
| import contextMenu from "../../menus/context_menu.js"; | import contextMenu from "../../menus/context_menu.js"; | ||||||
| import treeService from "../../services/tree.js"; | import treeService from "../../services/tree.js"; | ||||||
| import ButtonFromNoteWidget from "./button_from_note.js"; | import ButtonFromNoteWidget from "./button_from_note.js"; | ||||||
|  | import type FNote from "../../entities/fnote.js"; | ||||||
|  | import type { CommandNames } from "../../components/app_context.js"; | ||||||
|  | 
 | ||||||
|  | interface WebContents { | ||||||
|  |     history: string[]; | ||||||
|  |     getActiveIndex(): number; | ||||||
|  |     clearHistory(): void; | ||||||
|  |     canGoBack(): boolean; | ||||||
|  |     canGoForward(): boolean; | ||||||
|  |     goToIndex(index: string): void; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | interface ContextMenuItem { | ||||||
|  |     title: string; | ||||||
|  |     idx: string; | ||||||
|  |     uiIcon: string; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| export default class HistoryNavigationButton extends ButtonFromNoteWidget { | export default class HistoryNavigationButton extends ButtonFromNoteWidget { | ||||||
|     constructor(launcherNote, command) { |     private webContents?: WebContents; | ||||||
|  | 
 | ||||||
|  |     constructor(launcherNote: FNote, command: string) { | ||||||
|         super(); |         super(); | ||||||
| 
 | 
 | ||||||
|         this.title(() => launcherNote.title) |         this.title(() => launcherNote.title) | ||||||
|             .icon(() => launcherNote.getIcon()) |             .icon(() => launcherNote.getIcon()) | ||||||
|             .command(() => command) |             .command(() => command as CommandNames) | ||||||
|             .titlePlacement("right") |             .titlePlacement("right") | ||||||
|             .buttonNoteIdProvider(() => launcherNote.noteId) |             .buttonNoteIdProvider(() => launcherNote.noteId) | ||||||
|             .onContextMenu((e) => this.showContextMenu(e)) |             .onContextMenu((e) => { if (e) this.showContextMenu(e); }) | ||||||
|             .class("launcher-button"); |             .class("launcher-button"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @@ -23,35 +42,31 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { | |||||||
|             this.webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); |             this.webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); | ||||||
| 
 | 
 | ||||||
|             // without this, the history is preserved across frontend reloads
 |             // without this, the history is preserved across frontend reloads
 | ||||||
|             this.webContents.clearHistory(); |             this.webContents?.clearHistory(); | ||||||
| 
 | 
 | ||||||
|             this.refresh(); |             this.refresh(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async showContextMenu(e) { |     async showContextMenu(e: JQuery.ContextMenuEvent) { | ||||||
|         e.preventDefault(); |         e.preventDefault(); | ||||||
| 
 | 
 | ||||||
|         // API is broken and will be replaced: https://github.com/electron/electron/issues/33899
 |         if (!this.webContents || this.webContents.history.length < 2) { | ||||||
|         // until then no context menu
 |  | ||||||
|         if (true) { |  | ||||||
|             // avoid warning in dev console
 |  | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (this.webContents.history.length < 2) { |         let items: ContextMenuItem[] = []; | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         let items = []; |  | ||||||
| 
 | 
 | ||||||
|         const activeIndex = this.webContents.getActiveIndex(); |         const activeIndex = this.webContents.getActiveIndex(); | ||||||
|  |         const history = this.webContents.history; | ||||||
| 
 | 
 | ||||||
|         for (const idx in this.webContents.history) { |         for (const idx in history) { | ||||||
|             const url = this.webContents.history[idx]; |             const url = history[idx]; | ||||||
|             const [_, notePathWithTab] = url.split("#"); |             const parts = url.split("#"); | ||||||
|             // broken: use linkService.parseNavigationStateFromUrl();
 |             if (parts.length < 2) continue; | ||||||
|             const [notePath, ntxId] = notePathWithTab.split("-"); | 
 | ||||||
|  |             const notePathWithTab = parts[1]; | ||||||
|  |             const notePath = notePathWithTab.split("-")[0]; | ||||||
| 
 | 
 | ||||||
|             const title = await treeService.getNotePathTitle(notePath); |             const title = await treeService.getNotePathTitle(notePath); | ||||||
| 
 | 
 | ||||||
| @@ -59,9 +74,9 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { | |||||||
|                 title, |                 title, | ||||||
|                 idx, |                 idx, | ||||||
|                 uiIcon: |                 uiIcon: | ||||||
|                     idx == activeIndex |                     parseInt(idx) === activeIndex | ||||||
|                         ? "bx bx-radio-circle-marked" // compare with type coercion!
 |                         ? "bx bx-radio-circle-marked" // compare with type coercion!
 | ||||||
|                         : idx < activeIndex |                         : parseInt(idx) < activeIndex | ||||||
|                           ? "bx bx-left-arrow-alt" |                           ? "bx bx-left-arrow-alt" | ||||||
|                           : "bx bx-right-arrow-alt" |                           : "bx bx-right-arrow-alt" | ||||||
|             }); |             }); | ||||||
| @@ -77,22 +92,14 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { | |||||||
|             x: e.pageX, |             x: e.pageX, | ||||||
|             y: e.pageY, |             y: e.pageY, | ||||||
|             items, |             items, | ||||||
|             selectMenuItemHandler: ({ idx }) => this.webContents.goToIndex(idx) |             selectMenuItemHandler: (item: any) => { | ||||||
|  |                 if (item && item.idx && this.webContents) { | ||||||
|  |                     this.webContents.goToIndex(item.idx); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     refresh() { |  | ||||||
|         if (!utils.isElectron()) { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         // disabling this because in electron 9 there's a weird performance problem which makes these webContents calls
 |  | ||||||
|         // block UI thread for > 1 second on specific notes (book notes displaying underlying render notes with scripts)
 |  | ||||||
| 
 |  | ||||||
|         // this.$backInHistory.toggleClass('disabled', !this.webContents.canGoBack());
 |  | ||||||
|         // this.$forwardInHistory.toggleClass('disabled', !this.webContents.canGoForward());
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     activeNoteChangedEvent() { |     activeNoteChangedEvent() { | ||||||
|         this.refresh(); |         this.refresh(); | ||||||
|     } |     } | ||||||
		Reference in New Issue
	
	Block a user