From 28605f26874d6b7daec3099662aedf480039ec2e Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 27 Aug 2025 23:20:19 +0300 Subject: [PATCH] feat(react/floating_buttons): fancy title + keyboard shortcut --- .../widgets/floating_buttons/code_buttons.ts | 49 ------------------- .../client/src/widgets/react/ActionButton.tsx | 24 +++++++-- 2 files changed, 21 insertions(+), 52 deletions(-) delete mode 100644 apps/client/src/widgets/floating_buttons/code_buttons.ts diff --git a/apps/client/src/widgets/floating_buttons/code_buttons.ts b/apps/client/src/widgets/floating_buttons/code_buttons.ts deleted file mode 100644 index aab467c9d..000000000 --- a/apps/client/src/widgets/floating_buttons/code_buttons.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { t } from "../../services/i18n.js"; -import server from "../../services/server.js"; -import ws from "../../services/ws.js"; -import appContext, { type EventData } from "../../components/app_context.js"; -import toastService from "../../services/toast.js"; -import treeService from "../../services/tree.js"; -import NoteContextAwareWidget from "../note_context_aware_widget.js"; -import keyboardActionService from "../../services/keyboard_actions.js"; -import type FNote from "../../entities/fnote.js"; - -const TPL = /*html*/` -
- - - -
`; - -export default class CodeButtonsWidget extends NoteContextAwareWidget { - - private $openTriliumApiDocsButton!: JQuery; - private $saveToNoteButton!: JQuery; - - isEnabled() { - return super.isEnabled() && this.note && (this.note.mime.startsWith("application/javascript") || this.note.mime === "text/x-sqlite;schema=trilium"); - } - - doRender() { - this.$widget = $(TPL); - - this.$executeButton = this.$widget.find(".execute-button"); - this.$saveToNoteButton = this.$widget.find(".save-to-note-button"); - this.$saveToNoteButton.on("click", async () => { - - }); - - keyboardActionService.updateDisplayedShortcuts(this.$widget); - - this.contentSized(); - - super.doRender(); - } - -} diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsx index 9fa0e69b4..5636372d8 100644 --- a/apps/client/src/widgets/react/ActionButton.tsx +++ b/apps/client/src/widgets/react/ActionButton.tsx @@ -1,18 +1,36 @@ +import { useEffect, useRef, useState } from "preact/hooks"; import { CommandNames } from "../../components/app_context"; +import { useStaticTooltip } from "./hooks"; +import keyboard_actions from "../../services/keyboard_actions"; interface ActionButtonProps { text: string; - titlePosition?: "bottom"; // TODO: Use it + titlePosition?: "bottom" | "left"; // TODO: Use it icon: string; className?: string; onClick?: (e: MouseEvent) => void; triggerCommand?: CommandNames; } -export default function ActionButton({ text, icon, className, onClick, triggerCommand }: ActionButtonProps) { +export default function ActionButton({ text, icon, className, onClick, triggerCommand, titlePosition }: ActionButtonProps) { + const buttonRef = useRef(null); + const [ keyboardShortcut, setKeyboardShortcut ] = useState(); + + useStaticTooltip(buttonRef, { + title: keyboardShortcut?.length ? `${text} (${keyboardShortcut?.join(",")})` : text, + placement: titlePosition ?? "bottom", + fallbackPlacements: [ titlePosition ?? "bottom" ] + }); + + useEffect(() => { + if (triggerCommand) { + keyboard_actions.getAction(triggerCommand).then(action => setKeyboardShortcut(action?.effectiveShortcuts)); + } + }, [triggerCommand]); + return