mirror of
https://github.com/zadam/trilium.git
synced 2025-12-04 22:44:25 +01:00
refactor(react/launch_bar): extract note launcher from bookmark button
This commit is contained in:
parent
1e05dc937c
commit
1963b5732a
@ -13,7 +13,7 @@ import HistoryNavigationButton from "../launch_bar/HistoryNavigation.jsx";
|
||||
import AiChatButton from "../launch_bar/AiChatButton.jsx";
|
||||
import ProtectedSessionStatusWidget from "../launch_bar/ProtectedSessionStatusWidget.jsx";
|
||||
import { VNode } from "preact";
|
||||
import CommandButton from "../launch_bar/CommandButton.jsx";
|
||||
import { CommandButton } from "../launch_bar/GenericButtons.jsx";
|
||||
|
||||
interface InnerWidget extends BasicWidget {
|
||||
settings?: {
|
||||
|
||||
@ -1,13 +1,12 @@
|
||||
import { useMemo } from "preact/hooks";
|
||||
import { LaunchBarActionButton, LaunchBarDropdownButton, type LaunchBarWidgetProps } from "./launch_bar_widgets";
|
||||
import { LaunchBarDropdownButton, type LaunchBarWidgetProps } from "./launch_bar_widgets";
|
||||
import { CSSProperties } from "preact";
|
||||
import type FNote from "../../entities/fnote";
|
||||
import { useChildNotes, useNoteLabel, useNoteLabelBoolean, useNoteProperty } from "../react/hooks";
|
||||
import appContext from "../../components/app_context";
|
||||
import { escapeHtml, isCtrlKey } from "../../services/utils";
|
||||
import link_context_menu from "../../menus/link_context_menu";
|
||||
import { escapeHtml } from "../../services/utils";
|
||||
import "./BookmarkButtons.css";
|
||||
import NoteLink from "../react/NoteLink";
|
||||
import { NoteLauncher } from "./GenericButtons";
|
||||
|
||||
const PARENT_NOTE_ID = "_lbBookmarks";
|
||||
|
||||
@ -30,40 +29,7 @@ function SingleBookmark({ note }: { note: FNote }) {
|
||||
const [ bookmarkFolder ] = useNoteLabelBoolean(note, "bookmarkFolder");
|
||||
return bookmarkFolder
|
||||
? <BookmarkFolder note={note} />
|
||||
: <OpenNoteButtonWidget note={note} />
|
||||
}
|
||||
|
||||
function OpenNoteButtonWidget({ note }: { note: FNote }) {
|
||||
const [ iconClass ] = useNoteLabel(note, "iconClass");
|
||||
const title = useNoteProperty(note, "title");
|
||||
|
||||
async function launch(evt: MouseEvent) {
|
||||
if (evt.which === 3) {
|
||||
return;
|
||||
}
|
||||
const hoistedNoteId = getHoistedNoteId(note);
|
||||
const ctrlKey = isCtrlKey(evt);
|
||||
|
||||
if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
|
||||
const activate = evt.shiftKey ? true : false;
|
||||
await appContext.tabManager.openInNewTab(note.noteId, hoistedNoteId, activate);
|
||||
} else {
|
||||
await appContext.tabManager.openInSameTab(note.noteId);
|
||||
}
|
||||
}
|
||||
|
||||
return title && iconClass && (
|
||||
<LaunchBarActionButton
|
||||
icon={iconClass}
|
||||
text={escapeHtml(title)}
|
||||
onClick={launch}
|
||||
onAuxClick={launch}
|
||||
onContextMenu={evt => {
|
||||
evt.preventDefault();
|
||||
link_context_menu.openContextMenu(note.noteId, evt);
|
||||
}}
|
||||
/>
|
||||
)
|
||||
: <NoteLauncher launcherNote={note} targetNoteId={note.noteId} />
|
||||
}
|
||||
|
||||
function BookmarkFolder({ note }: { note: FNote }) {
|
||||
@ -92,7 +58,3 @@ function BookmarkFolder({ note }: { note: FNote }) {
|
||||
</LaunchBarDropdownButton>
|
||||
)
|
||||
}
|
||||
|
||||
function getHoistedNoteId(noteToOpen: FNote) {
|
||||
return noteToOpen.getRelationValue("hoistedNote") || appContext.tabManager.getActiveContext()?.hoistedNoteId;
|
||||
}
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
import { CommandNames } from "../../components/app_context";
|
||||
import FNote from "../../entities/fnote";
|
||||
import { escapeHtml } from "../../services/utils";
|
||||
import { useNoteLabel, useNoteProperty } from "../react/hooks";
|
||||
import { LaunchBarActionButton } from "./launch_bar_widgets";
|
||||
|
||||
export default function CommandButton({ launcherNote }: { launcherNote: FNote }) {
|
||||
const [ iconClass ] = useNoteLabel(launcherNote, "iconClass");
|
||||
const [ command ] = useNoteLabel(launcherNote, "command");
|
||||
const title = useNoteProperty(launcherNote, "title");
|
||||
|
||||
return iconClass && title && command && (
|
||||
<LaunchBarActionButton
|
||||
icon={iconClass}
|
||||
text={escapeHtml(title)}
|
||||
triggerCommand={command as CommandNames}
|
||||
/>
|
||||
)
|
||||
}
|
||||
53
apps/client/src/widgets/launch_bar/GenericButtons.tsx
Normal file
53
apps/client/src/widgets/launch_bar/GenericButtons.tsx
Normal file
@ -0,0 +1,53 @@
|
||||
import appContext, { CommandNames } from "../../components/app_context";
|
||||
import FNote from "../../entities/fnote";
|
||||
import link_context_menu from "../../menus/link_context_menu";
|
||||
import { escapeHtml, isCtrlKey } from "../../services/utils";
|
||||
import { useNoteLabel, useNoteProperty } from "../react/hooks";
|
||||
import { LaunchBarActionButton } from "./launch_bar_widgets";
|
||||
|
||||
export function CommandButton({ launcherNote }: { launcherNote: FNote }) {
|
||||
const [ iconClass ] = useNoteLabel(launcherNote, "iconClass");
|
||||
const [ command ] = useNoteLabel(launcherNote, "command");
|
||||
const title = useNoteProperty(launcherNote, "title");
|
||||
|
||||
return iconClass && title && command && (
|
||||
<LaunchBarActionButton
|
||||
icon={iconClass}
|
||||
text={escapeHtml(title)}
|
||||
triggerCommand={command as CommandNames}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export function NoteLauncher({ launcherNote, targetNoteId, hoistedNoteId }: { launcherNote: FNote, targetNoteId: string, hoistedNoteId?: string }) {
|
||||
const [ iconClass ] = useNoteLabel(launcherNote, "iconClass");
|
||||
const title = useNoteProperty(launcherNote, "title");
|
||||
|
||||
async function launch(evt: MouseEvent) {
|
||||
if (evt.which === 3) {
|
||||
return;
|
||||
}
|
||||
const hoistedNoteIdWithDefault = hoistedNoteId || launcherNote.getRelationValue("hoistedNote") || appContext.tabManager.getActiveContext()?.hoistedNoteId;
|
||||
const ctrlKey = isCtrlKey(evt);
|
||||
|
||||
if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
|
||||
const activate = evt.shiftKey ? true : false;
|
||||
await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteIdWithDefault, activate);
|
||||
} else {
|
||||
await appContext.tabManager.openInSameTab(targetNoteId);
|
||||
}
|
||||
}
|
||||
|
||||
return title && iconClass && (
|
||||
<LaunchBarActionButton
|
||||
icon={iconClass}
|
||||
text={escapeHtml(title)}
|
||||
onClick={launch}
|
||||
onAuxClick={launch}
|
||||
onContextMenu={evt => {
|
||||
evt.preventDefault();
|
||||
link_context_menu.openContextMenu(targetNoteId, evt);
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user