mirror of
https://github.com/zadam/trilium.git
synced 2025-12-06 07:24:25 +01:00
chore(launch_bar): reintroduce keyboard shortcuts
This commit is contained in:
parent
17241be4bc
commit
fdb6677153
@ -1,7 +1,7 @@
|
|||||||
import utils from "./utils.js";
|
import utils from "./utils.js";
|
||||||
|
|
||||||
type ElementType = HTMLElement | Document;
|
type ElementType = HTMLElement | Document;
|
||||||
type Handler = (e: KeyboardEvent) => void;
|
export type Handler = (e: KeyboardEvent) => void;
|
||||||
|
|
||||||
export interface ShortcutBinding {
|
export interface ShortcutBinding {
|
||||||
element: HTMLElement | Document;
|
element: HTMLElement | Document;
|
||||||
|
|||||||
@ -2,16 +2,22 @@ import appContext from "../../components/app_context";
|
|||||||
import FNote from "../../entities/fnote";
|
import FNote from "../../entities/fnote";
|
||||||
import link_context_menu from "../../menus/link_context_menu";
|
import link_context_menu from "../../menus/link_context_menu";
|
||||||
import { escapeHtml, isCtrlKey } from "../../services/utils";
|
import { escapeHtml, isCtrlKey } from "../../services/utils";
|
||||||
|
import { useGlobalShortcut, useNoteLabel } from "../react/hooks";
|
||||||
import { LaunchBarActionButton, useLauncherIconAndTitle } from "./launch_bar_widgets";
|
import { LaunchBarActionButton, useLauncherIconAndTitle } from "./launch_bar_widgets";
|
||||||
|
|
||||||
export function CustomNoteLauncher({ launcherNote, getTargetNoteId, getHoistedNoteId }: {
|
export function CustomNoteLauncher({ launcherNote, getTargetNoteId, getHoistedNoteId }: {
|
||||||
launcherNote: FNote,
|
launcherNote: FNote;
|
||||||
getTargetNoteId: (launcherNote: FNote) => string | null | Promise<string | null>,
|
getTargetNoteId: (launcherNote: FNote) => string | null | Promise<string | null>;
|
||||||
getHoistedNoteId?: (launcherNote: FNote) => string | null
|
getHoistedNoteId?: (launcherNote: FNote) => string | null;
|
||||||
|
keyboardShortcut?: string;
|
||||||
}) {
|
}) {
|
||||||
const { icon, title } = useLauncherIconAndTitle(launcherNote);
|
const { icon, title } = useLauncherIconAndTitle(launcherNote);
|
||||||
|
|
||||||
async function launch(evt: MouseEvent) {
|
// Keyboard shortcut.
|
||||||
|
const [ shortcut ] = useNoteLabel(launcherNote, "keyboardShortcut");
|
||||||
|
useGlobalShortcut(shortcut, launch);
|
||||||
|
|
||||||
|
async function launch(evt: MouseEvent | KeyboardEvent) {
|
||||||
if (evt.which === 3) {
|
if (evt.which === 3) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
|
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
|
||||||
import { useLegacyWidget, useNoteContext, useNoteLabel, useNoteRelationTarget, useTriliumOptionBool } from "../react/hooks";
|
import { useGlobalShortcut, useLegacyWidget, useNoteContext, useNoteLabel, useNoteRelationTarget, useTriliumOptionBool } from "../react/hooks";
|
||||||
import { ParentComponent } from "../react/react_utils";
|
import { ParentComponent } from "../react/react_utils";
|
||||||
import BasicWidget from "../basic_widget";
|
import BasicWidget from "../basic_widget";
|
||||||
import FNote from "../../entities/fnote";
|
import FNote from "../../entities/fnote";
|
||||||
@ -52,20 +52,27 @@ export function NoteLauncher({ launcherNote, ...restProps }: { launcherNote: FNo
|
|||||||
|
|
||||||
export function ScriptLauncher({ launcherNote }: LauncherNoteProps) {
|
export function ScriptLauncher({ launcherNote }: LauncherNoteProps) {
|
||||||
const { icon, title } = useLauncherIconAndTitle(launcherNote);
|
const { icon, title } = useLauncherIconAndTitle(launcherNote);
|
||||||
|
|
||||||
|
async function launch() {
|
||||||
|
if (launcherNote.isLabelTruthy("scriptInLauncherContent")) {
|
||||||
|
await launcherNote.executeScript();
|
||||||
|
} else {
|
||||||
|
const script = await launcherNote.getRelationTarget("script");
|
||||||
|
if (script) {
|
||||||
|
await script.executeScript();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keyboard shortcut.
|
||||||
|
const [ shortcut ] = useNoteLabel(launcherNote, "keyboardShortcut");
|
||||||
|
useGlobalShortcut(shortcut, launch);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LaunchBarActionButton
|
<LaunchBarActionButton
|
||||||
icon={icon}
|
icon={icon}
|
||||||
text={title}
|
text={title}
|
||||||
onClick={async () => {
|
onClick={launch}
|
||||||
if (launcherNote.isLabelTruthy("scriptInLauncherContent")) {
|
|
||||||
await launcherNote.executeScript();
|
|
||||||
} else {
|
|
||||||
const script = await launcherNote.getRelationTarget("script");
|
|
||||||
if (script) {
|
|
||||||
await script.executeScript();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,9 +20,9 @@ import options, { type OptionValue } from "../../services/options";
|
|||||||
import protected_session_holder from "../../services/protected_session_holder";
|
import protected_session_holder from "../../services/protected_session_holder";
|
||||||
import SpacedUpdate from "../../services/spaced_update";
|
import SpacedUpdate from "../../services/spaced_update";
|
||||||
import toast, { ToastOptions } from "../../services/toast";
|
import toast, { ToastOptions } from "../../services/toast";
|
||||||
import utils, { escapeRegExp, reloadFrontendApp } from "../../services/utils";
|
import utils, { escapeRegExp, randomString, reloadFrontendApp } from "../../services/utils";
|
||||||
import server from "../../services/server";
|
import server from "../../services/server";
|
||||||
import { removeIndividualBinding } from "../../services/shortcuts";
|
import shortcuts, { Handler, removeIndividualBinding } from "../../services/shortcuts";
|
||||||
import froca from "../../services/froca";
|
import froca from "../../services/froca";
|
||||||
|
|
||||||
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
|
export function useTriliumEvent<T extends EventNames>(eventName: T, handler: (data: EventData<T>) => void) {
|
||||||
@ -812,6 +812,21 @@ export function useKeyboardShortcuts(scope: "code-detail" | "text-detail", conta
|
|||||||
}, [ scope, containerRef, parentComponent, ntxId ]);
|
}, [ scope, containerRef, parentComponent, ntxId ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a global shortcut. Internally it uses the shortcut service and assignes a random namespace to make it unique.
|
||||||
|
*
|
||||||
|
* @param keyboardShortcut the keyboard shortcut combination to register.
|
||||||
|
* @param handler the corresponding handler to be called when the keyboard shortcut is invoked by the user.
|
||||||
|
*/
|
||||||
|
export function useGlobalShortcut(keyboardShortcut: string | null | undefined, handler: Handler) {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!keyboardShortcut) return;
|
||||||
|
const namespace = randomString(10);
|
||||||
|
shortcuts.bindGlobalShortcut(keyboardShortcut, handler, namespace);
|
||||||
|
return () => shortcuts.removeGlobalShortcut(namespace);
|
||||||
|
}, [ keyboardShortcut, handler ]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates that the current note is in read-only mode, while an editing mode is available,
|
* Indicates that the current note is in read-only mode, while an editing mode is available,
|
||||||
* and provides a way to switch to editing mode.
|
* and provides a way to switch to editing mode.
|
||||||
|
|||||||
@ -27,6 +27,7 @@ type Labels = {
|
|||||||
// Launch bar
|
// Launch bar
|
||||||
bookmarkFolder: boolean;
|
bookmarkFolder: boolean;
|
||||||
command: string;
|
command: string;
|
||||||
|
keyboardShortcut: string;
|
||||||
|
|
||||||
// Collection-specific
|
// Collection-specific
|
||||||
viewType: string;
|
viewType: string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user