mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 03:29:02 +01:00 
			
		
		
		
	chore(react/ribbon): support reference links in attributes
This commit is contained in:
		
							parent
							
								
									168d25c020
								
							
						
					
					
						commit
						73ca285b7a
					
				| @ -162,14 +162,6 @@ export default class AttributeEditorWidget extends NoteContextAwareWidget implem | |||||||
|         this.lastUpdatedNoteId = this.noteId; |         this.lastUpdatedNoteId = this.noteId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string) { |  | ||||||
|         const { noteId } = linkService.parseNavigationStateFromUrl(href); |  | ||||||
|         const note = noteId ? await froca.getNote(noteId, true) : null; |  | ||||||
|         const title = note ? note.title : "[missing]"; |  | ||||||
| 
 |  | ||||||
|         $el.text(title); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     async createNoteForReferenceLink(title: string) { |     async createNoteForReferenceLink(title: string) { | ||||||
|         let result; |         let result; | ||||||
|         if (this.notePath) { |         if (this.notePath) { | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| import { useEffect, useRef, useState } from "preact/hooks" | import { useContext, useEffect, useRef, useState } from "preact/hooks" | ||||||
| import { AttributeEditor as CKEditorAttributeEditor, MentionFeed, ModelElement, ModelNode, ModelPosition } from "@triliumnext/ckeditor5"; | import { AttributeEditor as CKEditorAttributeEditor, MentionFeed, ModelElement, ModelNode, ModelPosition } from "@triliumnext/ckeditor5"; | ||||||
| import { t } from "../../../services/i18n"; | import { t } from "../../../services/i18n"; | ||||||
| import server from "../../../services/server"; | import server from "../../../services/server"; | ||||||
| @ -12,6 +12,10 @@ import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; | |||||||
| import attribute_parser, { Attribute } from "../../../services/attribute_parser"; | import attribute_parser, { Attribute } from "../../../services/attribute_parser"; | ||||||
| import ActionButton from "../../react/ActionButton"; | import ActionButton from "../../react/ActionButton"; | ||||||
| import { escapeQuotes } from "../../../services/utils"; | import { escapeQuotes } from "../../../services/utils"; | ||||||
|  | import { ParentComponent } from "../../react/react_utils"; | ||||||
|  | import Component from "../../../components/component"; | ||||||
|  | import link from "../../../services/link"; | ||||||
|  | import froca from "../../../services/froca"; | ||||||
| 
 | 
 | ||||||
| const HELP_TEXT = ` | const HELP_TEXT = ` | ||||||
| <p>${t("attribute_editor.help_text_body1")}</p> | <p>${t("attribute_editor.help_text_body1")}</p> | ||||||
| @ -66,14 +70,18 @@ const mentionSetup: MentionFeed[] = [ | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| export default function AttributeEditor({ note, componentId }: { note: FNote, componentId: string }) { | export default function AttributeEditor({ note, componentId }: { note: FNote, componentId: string }) { | ||||||
|  |     const parentComponent = useContext(ParentComponent); | ||||||
|  |     injectLoadReferenceLinkTitle(parentComponent); | ||||||
| 
 | 
 | ||||||
|     const [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">(); |     const [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">(); | ||||||
|     const [ error, setError ] = useState<unknown>(); |     const [ error, setError ] = useState<unknown>(); | ||||||
|     const [ needsSaving, setNeedsSaving ] = useState(false); |     const [ needsSaving, setNeedsSaving ] = useState(false); | ||||||
|     const [ initialValue, setInitialValue ] = useState<string>(""); |     const [ initialValue, setInitialValue ] = useState<string>(""); | ||||||
|  | 
 | ||||||
|     const lastSavedContent = useRef<string>(); |     const lastSavedContent = useRef<string>(); | ||||||
|     const currentValueRef = useRef(initialValue); |     const currentValueRef = useRef(initialValue); | ||||||
|     const wrapperRef = useRef<HTMLDivElement>(null); |     const wrapperRef = useRef<HTMLDivElement>(null); | ||||||
|  | 
 | ||||||
|     const { showTooltip, hideTooltip } = useTooltip(wrapperRef, { |     const { showTooltip, hideTooltip } = useTooltip(wrapperRef, { | ||||||
|         trigger: "focus", |         trigger: "focus", | ||||||
|         html: true, |         html: true, | ||||||
| @ -96,7 +104,7 @@ export default function AttributeEditor({ note, componentId }: { note: FNote, co | |||||||
|         // attrs are not resorted if position changes after the initial load
 |         // attrs are not resorted if position changes after the initial load
 | ||||||
|         ownedAttributes.sort((a, b) => a.position - b.position); |         ownedAttributes.sort((a, b) => a.position - b.position); | ||||||
| 
 | 
 | ||||||
|         let htmlAttrs = getPreprocessedData("<p>" + (await attribute_renderer.renderAttributes(ownedAttributes, true)).html() + "</p>"); |         let htmlAttrs = ("<p>" + (await attribute_renderer.renderAttributes(ownedAttributes, true)).html() + "</p>"); | ||||||
| 
 | 
 | ||||||
|         if (saved) { |         if (saved) { | ||||||
|             lastSavedContent.current = htmlAttrs; |             lastSavedContent.current = htmlAttrs; | ||||||
| @ -164,7 +172,10 @@ export default function AttributeEditor({ note, componentId }: { note: FNote, co | |||||||
|                     }} |                     }} | ||||||
|                     onChange={(currentValue) => { |                     onChange={(currentValue) => { | ||||||
|                         currentValueRef.current = currentValue ?? ""; |                         currentValueRef.current = currentValue ?? ""; | ||||||
|                         setNeedsSaving((lastSavedContent.current ?? "").trimEnd() !== getPreprocessedData(currentValue ?? "").trimEnd()); |                          | ||||||
|  |                         const oldValue = getPreprocessedData(lastSavedContent.current ?? "").trimEnd(); | ||||||
|  |                         const newValue = getPreprocessedData(currentValue ?? "").trimEnd();                 | ||||||
|  |                         setNeedsSaving(oldValue !== newValue); | ||||||
|                         setError(undefined); |                         setError(undefined); | ||||||
|                     }} |                     }} | ||||||
|                     onClick={(e, pos) => { |                     onClick={(e, pos) => { | ||||||
| @ -254,3 +265,14 @@ function getClickIndex(pos: ModelPosition) { | |||||||
| 
 | 
 | ||||||
|     return clickIndex; |     return clickIndex; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | function injectLoadReferenceLinkTitle(component: Component | 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); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran