Skip to content

Build your personal knowledge base with Trilium Notes

7.8 KB
Raw Blame
import "./FormattingToolbar.css";

import clsx from "clsx";
import { useEffect, useRef, useState } from "preact/hooks";

import NoteContext from "../../components/note_context";
import FNote from "../../entities/fnote";
import options from "../../services/options";
import {
    useActiveNoteContext, useDocumentFocus, useNoteContext, useNoteProperty, useTriliumEvent,
    useTriliumEvents, useTriliumOption
} from "../react/hooks";
import { useNestedEditor } from "../type_widgets/text/editable_embed";

/**
 * Handles the editing toolbar of a single note context when the CKEditor is in decoupled mode, as
 * used by the quick edit popup. The main window uses `FixedFormattingToolbar` instead.
 *
 * This toolbar is only enabled if the user has selected the classic CKEditor.
 *
 * The mobile toolbar is handled separately (see `MobileEditorToolbar`).
 */
export default function FormattingToolbar() {
    const containerRef = useRef<HTMLDivElement>(null);
    const { note, noteContext, ntxId } = useNoteContext();
    const [ textNoteEditorType ] = useTriliumOption("textNoteEditorType");
    const [ shown, setShown ] = useState(false);
    const [ noteToolbar, setNoteToolbar ] = useState<HTMLElement | null>();
    const nestedEditor = useNestedEditor(ntxId);
    const toolbar = nestedEditor?.ui.view.toolbar?.element ?? noteToolbar;

    useEffect(() => {
        let active = true;
        void showFormattingToolbar(note, noteContext).then((result) => {
            // Ignore a stale resolution if the note changed while the check was pending.
            if (active) {
                setShown(result);
            }
        });
        return () => {
            active = false;
        };
    }, [ note, noteContext ]);

    // Attach the toolbar from the CKEditor.
    useTriliumEvent("textEditorRefreshed", ({ ntxId: eventNtxId, editor }) => {
        if (eventNtxId !== ntxId) return;
        setNoteToolbar(editor.ui.view.toolbar?.element);
    });

    useEffect(() => {
        if (toolbar) {
            containerRef.current?.replaceChildren(toolbar);
        } else {
            containerRef.current?.replaceChildren();
        }
    }, [ toolbar ]);

    return (textNoteEditorType === "ckeditor-classic" &&
        <div
            ref={containerRef}
            className={`classic-toolbar-widget ${!shown ? "hidden-ext" : ""}`}
        />
    );
};

async function showFormattingToolbar(note: FNote | null | undefined, noteContext: NoteContext | undefined) {
    return note?.type === "text" && noteContext?.viewScope?.viewMode === "default"
        && options.get("textNoteEditorType") === "ckeditor-classic"
        && !(await noteContext?.isReadOnly());
}

const toolbarCache = new Map<string, HTMLElement | null | undefined>();

export function FixedFormattingToolbar() {
    const containerRef = useRef<HTMLDivElement>(null);
    const { note, noteContext, ntxId } = useActiveNoteContext();
    const noteType = useNoteProperty(note, "type");
    const renderState = useRenderState(noteContext, note);
    // In zen mode, the toolbar shows only while a text note's split has the focus.
    const isTextSplitFocused = useDocumentFocus(".note-split.type-text");
    const [ toolbarToRender, setToolbarToRender ] = useState<HTMLElement | null | undefined>();
    const nestedEditor = useNestedEditor(ntxId);
    const shownToolbar = nestedEditor?.ui.view.toolbar?.element ?? toolbarToRender;

    // Keyboard shortcut.
    const lastFocusedElement = useRef<Element>(null);
    useTriliumEvent("toggleRibbonTabClassicEditor", () => {
        if (!shownToolbar) return;
        if (!shownToolbar.contains(document.activeElement)) {
            // Focus to the fixed formatting toolbar.
            lastFocusedElement.current = document.activeElement;
            shownToolbar.querySelector<HTMLButtonElement>(".ck-toolbar__items button")?.focus();
        } else {
            // Focus back to the last selection.
            (lastFocusedElement.current as HTMLElement)?.focus();
            lastFocusedElement.current = null;
        }
    });

    // 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);
        }
    });

    // Clean the cache when tabs are closed.
    useTriliumEvent("noteContextRemoved", ({ ntxIds: eventNtxIds }) => {
        for (const eventNtxId of eventNtxIds) {
            toolbarCache.delete(eventNtxId);
        }
    });

    // Switch between the cached toolbar when user navigates to a different note context.
    useEffect(() => {
        if (!ntxId) return;
        const toolbar = toolbarCache.get(ntxId);
        if (toolbar) {
            setToolbarToRender(toolbar);
        }
    }, [ ntxId, noteType, noteContext ]);

    // Render the toolbar.
    useEffect(() => {
        if (shownToolbar) {
            containerRef.current?.replaceChildren(shownToolbar);
        } else {
            containerRef.current?.replaceChildren();
        }
    }, [ shownToolbar ]);

    return (
        <div
            ref={containerRef}
            className={clsx("classic-toolbar-widget", {
                "hidden-ext": renderState === "hidden",
                "disabled": renderState === "disabled",
                "split-focused": isTextSplitFocused
            })}
        />
    );
}

function useRenderState(activeNoteContext: NoteContext | undefined, activeNote: FNote | null | undefined) {
    const [ textNoteEditorType ] = useTriliumOption("textNoteEditorType");
    const [ state, setState ] = useState("hidden");

    useTriliumEvents([ "newNoteContextCreated", "noteContextRemoved", "readOnlyTemporarilyDisabled", "noteTypeMimeChanged" ], () => {
        getFormattingToolbarState(activeNoteContext, activeNote, textNoteEditorType).then(setState);
    });

    useEffect(() => {
        getFormattingToolbarState(activeNoteContext, activeNote, textNoteEditorType).then(setState);
    }, [ activeNoteContext, activeNote, textNoteEditorType ]);

    return state;
}

export async function getFormattingToolbarState(activeNoteContext: NoteContext | undefined, activeNote: FNote | null | undefined, textNoteEditorType: string) {
    if (!activeNoteContext || textNoteEditorType !== "ckeditor-classic") {
        return "hidden";
    }

    const subContexts = activeNoteContext?.getMainContext().getSubContexts() ?? [];
    if (subContexts.length === 1) {
        if (activeNote?.type !== "text" || activeNoteContext.viewScope?.viewMode !== "default") {
            return "hidden";
        }

        const isReadOnly = await activeNoteContext.isReadOnly();
        if (isReadOnly) {
            return "hidden";
        }

        return "visible";
    }

    // If there are multiple note contexts (e.g. splits), the logic is slightly different.
    const textNoteContexts = subContexts.filter(s => s.note?.type === "text" && s.viewScope?.viewMode === "default");
    const textNoteContextsReadOnly = await Promise.all(textNoteContexts.map(sc => sc.isReadOnly()));

    // If all text notes are hidden, no need to display the toolbar at all.
    if (textNoteContextsReadOnly.indexOf(false) === -1) {
        return "hidden";
    }

    // If the current subcontext is not a text note, but there is at least an editable text then it must be disabled.
    if (activeNote?.type !== "text") return "disabled";

    // If the current subcontext is a text note, it must not be read-only.
    const subContextIndex = textNoteContexts.indexOf(activeNoteContext);
    if (subContextIndex !== -1) {
        if (textNoteContextsReadOnly[subContextIndex]) return "disabled";
    }
    if (activeNoteContext.viewScope?.viewMode !== "default") return "disabled";
    return "visible";
}