mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	port history navigation, remove redundance
This commit is contained in:
		
							parent
							
								
									c67445f511
								
							
						
					
					
						commit
						a3d9b04d96
					
				@ -2,17 +2,36 @@ import utils from "../../services/utils.js";
 | 
			
		||||
import contextMenu from "../../menus/context_menu.js";
 | 
			
		||||
import treeService from "../../services/tree.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 {
 | 
			
		||||
    constructor(launcherNote, command) {
 | 
			
		||||
    private webContents?: WebContents;
 | 
			
		||||
 | 
			
		||||
    constructor(launcherNote: FNote, command: string) {
 | 
			
		||||
        super();
 | 
			
		||||
 | 
			
		||||
        this.title(() => launcherNote.title)
 | 
			
		||||
            .icon(() => launcherNote.getIcon())
 | 
			
		||||
            .command(() => command)
 | 
			
		||||
            .command(() => command as CommandNames)
 | 
			
		||||
            .titlePlacement("right")
 | 
			
		||||
            .buttonNoteIdProvider(() => launcherNote.noteId)
 | 
			
		||||
            .onContextMenu((e) => this.showContextMenu(e))
 | 
			
		||||
            .onContextMenu((e) => { if (e) this.showContextMenu(e); })
 | 
			
		||||
            .class("launcher-button");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -23,35 +42,31 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
 | 
			
		||||
            this.webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
 | 
			
		||||
 | 
			
		||||
            // without this, the history is preserved across frontend reloads
 | 
			
		||||
            this.webContents.clearHistory();
 | 
			
		||||
            this.webContents?.clearHistory();
 | 
			
		||||
 | 
			
		||||
            this.refresh();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async showContextMenu(e) {
 | 
			
		||||
    async showContextMenu(e: JQuery.ContextMenuEvent) {
 | 
			
		||||
        e.preventDefault();
 | 
			
		||||
 | 
			
		||||
        // API is broken and will be replaced: https://github.com/electron/electron/issues/33899
 | 
			
		||||
        // until then no context menu
 | 
			
		||||
        if (true) {
 | 
			
		||||
            // avoid warning in dev console
 | 
			
		||||
        if (!this.webContents || this.webContents.history.length < 2) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (this.webContents.history.length < 2) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let items = [];
 | 
			
		||||
        let items: ContextMenuItem[] = [];
 | 
			
		||||
 | 
			
		||||
        const activeIndex = this.webContents.getActiveIndex();
 | 
			
		||||
        const history = this.webContents.history;
 | 
			
		||||
 | 
			
		||||
        for (const idx in this.webContents.history) {
 | 
			
		||||
            const url = this.webContents.history[idx];
 | 
			
		||||
            const [_, notePathWithTab] = url.split("#");
 | 
			
		||||
            // broken: use linkService.parseNavigationStateFromUrl();
 | 
			
		||||
            const [notePath, ntxId] = notePathWithTab.split("-");
 | 
			
		||||
        for (const idx in history) {
 | 
			
		||||
            const url = history[idx];
 | 
			
		||||
            const parts = url.split("#");
 | 
			
		||||
            if (parts.length < 2) continue;
 | 
			
		||||
 | 
			
		||||
            const notePathWithTab = parts[1];
 | 
			
		||||
            const notePath = notePathWithTab.split("-")[0];
 | 
			
		||||
 | 
			
		||||
            const title = await treeService.getNotePathTitle(notePath);
 | 
			
		||||
 | 
			
		||||
@ -59,9 +74,9 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
 | 
			
		||||
                title,
 | 
			
		||||
                idx,
 | 
			
		||||
                uiIcon:
 | 
			
		||||
                    idx == activeIndex
 | 
			
		||||
                    parseInt(idx) === activeIndex
 | 
			
		||||
                        ? "bx bx-radio-circle-marked" // compare with type coercion!
 | 
			
		||||
                        : idx < activeIndex
 | 
			
		||||
                        : parseInt(idx) < activeIndex
 | 
			
		||||
                          ? "bx bx-left-arrow-alt"
 | 
			
		||||
                          : "bx bx-right-arrow-alt"
 | 
			
		||||
            });
 | 
			
		||||
@ -77,22 +92,14 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget {
 | 
			
		||||
            x: e.pageX,
 | 
			
		||||
            y: e.pageY,
 | 
			
		||||
            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() {
 | 
			
		||||
        this.refresh();
 | 
			
		||||
    }
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user