diff --git a/apps/client/src/widgets/FloatingButtons.tsx b/apps/client/src/widgets/FloatingButtons.tsx index a2f3769fd..0f36c8a49 100644 --- a/apps/client/src/widgets/FloatingButtons.tsx +++ b/apps/client/src/widgets/FloatingButtons.tsx @@ -13,8 +13,11 @@ import attributes from "../services/attributes"; import appContext from "../components/app_context"; import protected_session_holder from "../services/protected_session_holder"; import options from "../services/options"; -import { AttributeRow } from "../services/load_results"; import { openInAppHelpFromUrl } from "../services/utils"; +import toast from "../services/toast"; +import server from "../services/server"; +import { SaveSqlConsoleResponse } from "@triliumnext/commons"; +import tree from "../services/tree"; interface FloatingButtonContext { parentComponent: Component; @@ -70,6 +73,10 @@ const FLOATING_BUTTON_DEFINITIONS: FloatingButtonDefinition[] = [ { component: OpenTriliumApiDocsButton, isEnabled: ({ note }) => note.mime.startsWith("application/javascript;env=") + }, + { + component: SaveToNoteButton, + isEnabled: ({ note }) => note.mime === "text/x-sqlite;schema=trilium" && note.isHiddenCompletely() } ]; @@ -238,6 +245,23 @@ function OpenTriliumApiDocsButton({ note }: FloatingButtonContext) { /> } +function SaveToNoteButton({ note }: FloatingButtonContext) { + return { + e.preventDefault(); + const { notePath } = await server.post("special-notes/save-sql-console", { sqlConsoleNoteId: note.noteId }); + if (notePath) { + toast.showMessage(t("code_buttons.sql_console_saved_message", { "note_path": await tree.getNotePathTitle(notePath) })); + // TODO: This hangs the navigation, for some reason. + //await ws.waitForMaxKnownEntityChangeId(); + await appContext.tabManager.getActiveContext()?.setNote(notePath); + } + }} + /> +} + /** * Show button that displays floating button after click on close button */ diff --git a/apps/client/src/widgets/floating_buttons/code_buttons.ts b/apps/client/src/widgets/floating_buttons/code_buttons.ts index c64fe7183..aab467c9d 100644 --- a/apps/client/src/widgets/floating_buttons/code_buttons.ts +++ b/apps/client/src/widgets/floating_buttons/code_buttons.ts @@ -17,16 +17,10 @@ const TPL = /*html*/` } - `; -// TODO: Deduplicate with server. -interface SaveSqlConsoleResponse { - notePath: string; -} - export default class CodeButtonsWidget extends NoteContextAwareWidget { private $openTriliumApiDocsButton!: JQuery; @@ -42,13 +36,7 @@ export default class CodeButtonsWidget extends NoteContextAwareWidget { this.$executeButton = this.$widget.find(".execute-button"); this.$saveToNoteButton = this.$widget.find(".save-to-note-button"); this.$saveToNoteButton.on("click", async () => { - const { notePath } = await server.post("special-notes/save-sql-console", { sqlConsoleNoteId: this.noteId }); - await ws.waitForMaxKnownEntityChangeId(); - - await appContext.tabManager.getActiveContext()?.setNote(notePath); - - toastService.showMessage(t("code_buttons.sql_console_saved_message", { notePath: await treeService.getNotePathTitle(notePath) })); }); keyboardActionService.updateDisplayedShortcuts(this.$widget); @@ -58,13 +46,4 @@ export default class CodeButtonsWidget extends NoteContextAwareWidget { super.doRender(); } - async refreshWithNote(note: FNote) { - this.$saveToNoteButton.toggle(note.mime === "text/x-sqlite;schema=trilium" && note.isHiddenCompletely()); - } - - async noteTypeMimeChangedEvent({ noteId }: EventData<"noteTypeMimeChanged">) { - if (this.isNote(noteId)) { - await this.refresh(); - } - } } diff --git a/apps/server/src/services/special_notes.ts b/apps/server/src/services/special_notes.ts index d4c37d764..4f6d76781 100644 --- a/apps/server/src/services/special_notes.ts +++ b/apps/server/src/services/special_notes.ts @@ -10,7 +10,7 @@ import SearchContext from "./search/search_context.js"; import { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } from "./hidden_subtree.js"; import { t } from "i18next"; import { BNote } from "./backend_script_entrypoint.js"; -import { SaveSearchNoteResponse } from "@triliumnext/commons"; +import { SaveSearchNoteResponse, SaveSqlConsoleResponse } from "@triliumnext/commons"; function getInboxNote(date: string) { const workspaceNote = hoistedNoteService.getWorkspaceNote(); @@ -67,7 +67,7 @@ async function saveSqlConsole(sqlConsoleNoteId: string) { } } - return result; + return result satisfies SaveSqlConsoleResponse; } function createSearchNote(searchString: string, ancestorNoteId: string) { diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts index 966e34418..0a9d36227 100644 --- a/packages/commons/src/lib/server_api.ts +++ b/packages/commons/src/lib/server_api.ts @@ -206,3 +206,5 @@ export interface CloneResponse { export interface ConvertToAttachmentResponse { attachment: AttachmentRow; } + +export type SaveSqlConsoleResponse = CloneResponse;