mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	refactor(react/ribbon): use custom method for injecting handlers
This commit is contained in:
		
							parent
							
								
									82914fc2aa
								
							
						
					
					
						commit
						a934760960
					
				@ -539,3 +539,10 @@ export function useTooltip(elRef: RefObject<HTMLElement>, config: Partial<Toolti
 | 
			
		||||
 | 
			
		||||
    return { showTooltip, hideTooltip };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function useLegacyImperativeHandlers(handlers: Record<string, Function>) {
 | 
			
		||||
    const parentComponent = useContext(ParentComponent);
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        Object.assign(parentComponent as any, handlers);
 | 
			
		||||
    }, [ handlers ])
 | 
			
		||||
}
 | 
			
		||||
@ -1,10 +1,10 @@
 | 
			
		||||
import { useContext, useEffect, useRef, useState } from "preact/hooks"
 | 
			
		||||
import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks"
 | 
			
		||||
import { AttributeEditor as CKEditorAttributeEditor, MentionFeed, ModelElement, ModelNode, ModelPosition } from "@triliumnext/ckeditor5";
 | 
			
		||||
import { t } from "../../../services/i18n";
 | 
			
		||||
import server from "../../../services/server";
 | 
			
		||||
import note_autocomplete, { Suggestion } from "../../../services/note_autocomplete";
 | 
			
		||||
import CKEditor, { CKEditorApi } from "../../react/CKEditor";
 | 
			
		||||
import { useLegacyWidget, useTooltip, useTriliumEventBeta } from "../../react/hooks";
 | 
			
		||||
import { useLegacyImperativeHandlers, useLegacyWidget, useTooltip, useTriliumEventBeta } from "../../react/hooks";
 | 
			
		||||
import FAttribute from "../../../entities/fattribute";
 | 
			
		||||
import attribute_renderer from "../../../services/attribute_renderer";
 | 
			
		||||
import FNote from "../../../entities/fnote";
 | 
			
		||||
@ -77,9 +77,6 @@ const mentionSetup: MentionFeed[] = [
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export default function AttributeEditor({ note, componentId, notePath }: { note: FNote, componentId: string, notePath?: string | null }) {
 | 
			
		||||
    const parentComponent = useContext(ParentComponent);
 | 
			
		||||
    injectLoadReferenceLinkTitle(parentComponent, notePath);
 | 
			
		||||
 | 
			
		||||
    const [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">();
 | 
			
		||||
    const [ error, setError ] = useState<unknown>();
 | 
			
		||||
    const [ needsSaving, setNeedsSaving ] = useState(false);
 | 
			
		||||
@ -228,6 +225,28 @@ export default function AttributeEditor({ note, componentId, notePath }: { note:
 | 
			
		||||
        setTimeout(() => editorRef.current?.focus(), 0);
 | 
			
		||||
    }, []);
 | 
			
		||||
 | 
			
		||||
    // Interaction with CKEditor.
 | 
			
		||||
    useLegacyImperativeHandlers(useMemo(() => ({
 | 
			
		||||
        loadReferenceLinkTitle: async ($el: JQuery<HTMLElement>, href: string) => {
 | 
			
		||||
            const { noteId } = link.parseNavigationStateFromUrl(href);
 | 
			
		||||
            const note = noteId ? await froca.getNote(noteId, true) : null;
 | 
			
		||||
            const title = note ? note.title : "[missing]";
 | 
			
		||||
 | 
			
		||||
            $el.text(title);
 | 
			
		||||
        },
 | 
			
		||||
        createNoteForReferenceLink: async (title: string) => {
 | 
			
		||||
            let result;
 | 
			
		||||
            if (notePath) {
 | 
			
		||||
                result = await note_create.createNoteWithTypePrompt(notePath, {
 | 
			
		||||
                    activate: false,
 | 
			
		||||
                    title: title
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return result?.note?.getBestNotePathString();
 | 
			
		||||
        }   
 | 
			
		||||
    }), [ notePath ]))
 | 
			
		||||
    
 | 
			
		||||
    return (
 | 
			
		||||
        <>
 | 
			
		||||
            <div
 | 
			
		||||
@ -373,25 +392,3 @@ function getClickIndex(pos: ModelPosition) {
 | 
			
		||||
 | 
			
		||||
    return clickIndex;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function injectLoadReferenceLinkTitle(component: Component | null, notePath?: string | null) {
 | 
			
		||||
    if (!component) return;
 | 
			
		||||
    (component as any).loadReferenceLinkTitle = async ($el: JQuery<HTMLElement>, href: string) => {
 | 
			
		||||
        const { noteId } = link.parseNavigationStateFromUrl(href);
 | 
			
		||||
        const note = noteId ? await froca.getNote(noteId, true) : null;
 | 
			
		||||
        const title = note ? note.title : "[missing]";
 | 
			
		||||
 | 
			
		||||
        $el.text(title);
 | 
			
		||||
    }
 | 
			
		||||
    (component as any).createNoteForReferenceLink = async (title: string) => {
 | 
			
		||||
        let result;
 | 
			
		||||
        if (notePath) {
 | 
			
		||||
            result = await note_create.createNoteWithTypePrompt(notePath, {
 | 
			
		||||
                activate: false,
 | 
			
		||||
                title: title
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return result?.note?.getBestNotePathString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user