From 4182f6043af37d0871bdb0cbb22c1ba228687206 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 13 Dec 2025 16:26:01 +0200 Subject: [PATCH] feat(layout/formatting_toolbar): render cached components --- .../src/widgets/ribbon/FormattingToolbar.tsx | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/apps/client/src/widgets/ribbon/FormattingToolbar.tsx b/apps/client/src/widgets/ribbon/FormattingToolbar.tsx index 0e52ca8db..c1e7d103f 100644 --- a/apps/client/src/widgets/ribbon/FormattingToolbar.tsx +++ b/apps/client/src/widgets/ribbon/FormattingToolbar.tsx @@ -1,4 +1,4 @@ -import { useRef } from "preact/hooks"; +import { useEffect, useRef, useState } from "preact/hooks"; import { useActiveNoteContext, useIsNoteReadOnly, useNoteProperty, useTriliumEvent, useTriliumOption } from "../react/hooks"; import { TabContext } from "./ribbon-interface"; @@ -37,22 +37,52 @@ export default function FormattingToolbar({ hidden, ntxId }: TabContext) { ); }; +const toolbarCache = new Map(); + export function FixedFormattingToolbar() { const containerRef = useRef(null); const [ textNoteEditorType ] = useTriliumOption("textNoteEditorType"); - const { note, noteContext } = useActiveNoteContext(); + const { note, noteContext, ntxId, viewScope } = useActiveNoteContext(); const noteType = useNoteProperty(note, "type"); const { isReadOnly } = useIsNoteReadOnly(note, noteContext); const shown = ( + viewScope?.viewMode === "default" && textNoteEditorType === "ckeditor-classic" && noteType === "text" && !isReadOnly ); + const [ toolbarToRender, setToolbarToRender ] = useState(); + + // Populate the cache with the toolbar of every note context. + useTriliumEvent("textEditorRefreshed", ({ ntxId: eventNtxId, editor }) => { + if (!eventNtxId) return; + const toolbar = editor.ui.view.toolbar?.element; + toolbarCache.set(eventNtxId, toolbar); + // Replace on the spot if the editor crashed. + if (eventNtxId === ntxId) { + setToolbarToRender(toolbar); + } + }); + + // Switch between the cached toolbar when user navigates to a different note context. + useEffect(() => { + if (!ntxId) return; + setToolbarToRender(toolbarCache.get(ntxId)); + }, [ ntxId, noteContext ]); + + // Render the toolbar. + useEffect(() => { + if (toolbarToRender) { + containerRef.current?.replaceChildren(toolbarToRender); + } else { + containerRef.current?.replaceChildren(); + } + }, [ toolbarToRender ]); return (
Hi
+ /> ); }