mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	chore(react/note_icon): react to icon changes
This commit is contained in:
		| @@ -20,20 +20,6 @@ export default class NoteIconWidget extends NoteContextAwareWidget { | |||||||
|         this.$iconList = this.$widget.find(".icon-list"); |         this.$iconList = this.$widget.find(".icon-list"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { |  | ||||||
|         if (this.noteId && loadResults.isNoteReloaded(this.noteId)) { |  | ||||||
|             this.refresh(); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         for (const attr of loadResults.getAttributeRows()) { |  | ||||||
|             if (attr.type === "label" && ["iconClass", "workspaceIconClass"].includes(attr.name ?? "") && attributeService.isAffecting(attr, this.note)) { |  | ||||||
|                 this.refresh(); |  | ||||||
|                 break; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     async renderDropdown() { |     async renderDropdown() { | ||||||
|         this.$iconList.empty(); |         this.$iconList.empty(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| import Dropdown from "./react/Dropdown"; | import Dropdown from "./react/Dropdown"; | ||||||
| import "./note_icon.css"; | import "./note_icon.css"; | ||||||
| import { t } from "i18next"; | import { t } from "i18next"; | ||||||
| import { useNoteContext } from "./react/hooks"; | import { useNoteContext, useNoteLabel } from "./react/hooks"; | ||||||
| import { useCallback, useEffect, useRef, useState } from "preact/hooks"; | import { useCallback, useEffect, useRef, useState } from "preact/hooks"; | ||||||
| import server from "../services/server"; | import server from "../services/server"; | ||||||
| import type { Category, Icon } from "./icon_list"; | import type { Category, Icon } from "./icon_list"; | ||||||
| @@ -28,26 +28,24 @@ let iconToCountCache!: Promise<IconToCountCache> | null; | |||||||
|  |  | ||||||
| export default function NoteIcon() { | export default function NoteIcon() { | ||||||
|     const { note, viewScope } = useNoteContext(); |     const { note, viewScope } = useNoteContext(); | ||||||
|     const [ icon, setIcon ] = useState("bx bx-empty"); |     const [ icon, setIcon ] = useState<string | null | undefined>(); | ||||||
|  |     const [ iconClass ] = useNoteLabel(note, "iconClass"); | ||||||
|  |     const [ workspaceIconClass ] = useNoteLabel(note, "workspaceIconClass"); | ||||||
|  |  | ||||||
|     const refreshIcon = useCallback(() => { |     useEffect(() => { | ||||||
|         if (note) { |         setIcon(note?.getIcon()); | ||||||
|             setIcon(note.getIcon()); |     }, [ note, iconClass, workspaceIconClass ]); | ||||||
|         } |  | ||||||
|     }, [ note ]); |  | ||||||
|  |  | ||||||
|     useEffect(refreshIcon, [ note ]); |  | ||||||
|  |  | ||||||
|     return ( |     return ( | ||||||
|         <Dropdown |         <Dropdown | ||||||
|             className="note-icon-widget" |             className="note-icon-widget" | ||||||
|             title={t("note_icon.change_note_icon")} |             title={t("note_icon.change_note_icon")} | ||||||
|             dropdownContainerStyle={{ width: "610px" }} |             dropdownContainerStyle={{ width: "610px" }} | ||||||
|             buttonClassName={`note-icon ${icon}`} |             buttonClassName={`note-icon ${icon ?? "bx bx-empty"}`} | ||||||
|             hideToggleArrow |             hideToggleArrow | ||||||
|             disabled={viewScope?.viewMode !== "default"} |             disabled={viewScope?.viewMode !== "default"} | ||||||
|         > |         > | ||||||
|             <NoteIconList note={note} /> |             { note && <NoteIconList note={note} /> } | ||||||
|         </Dropdown> |         </Dropdown> | ||||||
|     ) |     ) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks"; | import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks"; | ||||||
| import appContext, { BeforeUploadListener, EventData, EventNames } from "../../components/app_context"; | import { EventData, EventNames } from "../../components/app_context"; | ||||||
| import { ParentComponent } from "./react_utils"; | import { ParentComponent } from "./react_utils"; | ||||||
| import SpacedUpdate from "../../services/spaced_update"; | import SpacedUpdate from "../../services/spaced_update"; | ||||||
| import { OptionNames } from "@triliumnext/commons"; | import { OptionNames } from "@triliumnext/commons"; | ||||||
| @@ -9,8 +9,7 @@ import Component from "../../components/component"; | |||||||
| import NoteContext from "../../components/note_context"; | import NoteContext from "../../components/note_context"; | ||||||
| import { ReactWrappedWidget } from "../basic_widget"; | import { ReactWrappedWidget } from "../basic_widget"; | ||||||
| import FNote from "../../entities/fnote"; | import FNote from "../../entities/fnote"; | ||||||
| import froca from "../../services/froca"; | import attributes from "../../services/attributes"; | ||||||
| import toast from "../../services/toast"; |  | ||||||
|  |  | ||||||
| type TriliumEventHandler<T extends EventNames> = (data: EventData<T>) => void; | type TriliumEventHandler<T extends EventNames> = (data: EventData<T>) => void; | ||||||
| const registeredHandlers: Map<Component, Map<EventNames, TriliumEventHandler<any>[]>> = new Map(); | const registeredHandlers: Map<Component, Map<EventNames, TriliumEventHandler<any>[]>> = new Map(); | ||||||
| @@ -316,3 +315,26 @@ export function useNoteProperty<T extends keyof FNote>(note: FNote | null | unde | |||||||
|  |  | ||||||
|     return note[property]; |     return note[property]; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function useNoteLabel(note: FNote | undefined | null, labelName: string): [string | undefined, (newValue: string) => void] { | ||||||
|  |     const [ labelValue, setNoteValue ] = useState<string | null | undefined>(note?.getLabelValue(labelName)); | ||||||
|  |  | ||||||
|  |     useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => { | ||||||
|  |         for (const attr of loadResults.getAttributeRows()) { | ||||||
|  |             if (attr.type === "label" && attr.name === labelName && attributes.isAffecting(attr, note)) { | ||||||
|  |                 setNoteValue(attr.value ?? null); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     const setter = useCallback((value: string | undefined) => { | ||||||
|  |         if (note) { | ||||||
|  |             attributes.setLabel(note.noteId, labelName, value) | ||||||
|  |         } | ||||||
|  |     }, [note]); | ||||||
|  |  | ||||||
|  |     return [ | ||||||
|  |         labelValue, | ||||||
|  |         setter | ||||||
|  |     ] as const; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user