Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(text): classic toolbar sometimes not showing
Elian Doranc76f368fa0ea
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex c73fe5a425..bda3536da8 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -445,6 +445,7 @@ type EventMappings = {error: string;};searchRefreshed: { ntxId?: string | null };+ textEditorRefreshed: { ntxId?: string | null, editor: CKTextEditor };hoistedNoteChanged: {noteId: string;ntxId: string | null;diff --git a/apps/client/src/widgets/ribbon/FormattingToolbar.tsx b/apps/client/src/widgets/ribbon/FormattingToolbar.tsxindex 8828b15eea..f25234ead8 100644--- a/apps/client/src/widgets/ribbon/FormattingToolbar.tsx+++ b/apps/client/src/widgets/ribbon/FormattingToolbar.tsx@@ -1,4 +1,5 @@-import { useTriliumOption } from "../react/hooks";+import { useRef } from "preact/hooks";+import { useTriliumEvent, useTriliumOption } from "../react/hooks";import { TabContext } from "./ribbon-interface";/**@@ -10,10 +11,22 @@ import { TabContext } from "./ribbon-interface";** ! The toolbar is not only used in the ribbon, but also in the quick edit feature.*/-export default function FormattingToolbar({ hidden }: TabContext) {+export default function FormattingToolbar({ hidden, ntxId }: TabContext) {+ const containerRef = useRef<HTMLDivElement>(null);const [ textNoteEditorType ] = useTriliumOption("textNoteEditorType");+ useTriliumEvent("textEditorRefreshed", ({ ntxId: eventNtxId, editor }) => {+ if (eventNtxId !== ntxId) return;+ const toolbar = editor.ui.view.toolbar?.element;+ if (toolbar && containerRef.current) {+ containerRef.current.replaceChildren(toolbar);+ }+ });+return (textNoteEditorType === "ckeditor-classic" &&- <div className={`classic-toolbar-widget ${hidden ? "hidden-ext" : ""}`} />+ <div+ ref={containerRef}+ className={`classic-toolbar-widget ${hidden ? "hidden-ext" : ""}`}+ />)};diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 15c235f3f9..490eba95d6 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -251,6 +251,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext}initialized.current.resolve();+ parentComponent?.triggerEvent("textEditorRefreshed", { ntxId, editor });}}/>}@@ -304,19 +305,8 @@ function onNotificationWarning(data, evt) {function setupClassicEditor(editor: CKTextEditor, parentComponent: Component | undefined) {if (!parentComponent) return;- const $classicToolbarWidget = findClassicToolbar(parentComponent);-- $classicToolbarWidget.empty();- if ($classicToolbarWidget.length) {- const toolbarView = (editor as ClassicEditor).ui.view.toolbar;- if (toolbarView.element) {- $classicToolbarWidget[0].appendChild(toolbarView.element);- }- }if (utils.isMobile()) {- $classicToolbarWidget.addClass("visible");-// Reposition all dropdowns to point upwards instead of downwards.// See https://ckeditor.com/docs/ckeditor5/latest/examples/framework/bottom-toolbar-editor.html for more info.const toolbarView = (editor as ClassicEditor).ui.view.toolbar;@@ -333,24 +323,6 @@ function setupClassicEditor(editor: CKTextEditor, parentComponent: Component | u}}-function findClassicToolbar(parentComponent: Component): JQuery<HTMLElement> {- const $widget = $(parentComponent.$widget);-- if (!utils.isMobile()) {- const $parentSplit = $widget.parents(".note-split.type-text");-- if ($parentSplit.length) {- // The editor is in a normal tab.- return $parentSplit.find("> .ribbon-container .classic-toolbar-widget");- } else {- // The editor is in a popup.- return $widget.closest(".modal-body").find(".classic-toolbar-widget");- }- } else {- return $("body").find(".classic-toolbar-widget");- }-}-function EditableTextTouchBar({ watchdogRef, refreshTouchBarRef }: { watchdogRef: RefObject<EditorWatchdog | null>, refreshTouchBarRef: RefObject<() => void> }) {const [ headingSelectedIndex, setHeadingSelectedIndex ] = useState<number>();