2025-10-04 11:07:16 +03:00
|
|
|
import { useEffect, useRef, useState } from "preact/hooks";
|
|
|
|
|
import "./NoteMap.css";
|
2025-10-04 13:04:40 +03:00
|
|
|
import { getMapRootNoteId, getThemeStyle, MapType, NoteMapWidgetMode, rgb2hex } from "./utils";
|
2025-10-04 11:40:31 +03:00
|
|
|
import { RefObject } from "preact";
|
|
|
|
|
import FNote from "../../entities/fnote";
|
2025-10-04 12:29:37 +03:00
|
|
|
import { useElementSize, useNoteContext, useNoteLabel } from "../react/hooks";
|
2025-10-04 11:40:31 +03:00
|
|
|
import ForceGraph, { LinkObject, NodeObject } from "force-graph";
|
|
|
|
|
import { loadNotesAndRelations, NotesAndRelationsData } from "./data";
|
2025-10-04 12:29:37 +03:00
|
|
|
import { CssData, setupRendering } from "./rendering";
|
2025-10-04 12:55:08 +03:00
|
|
|
import ActionButton from "../react/ActionButton";
|
|
|
|
|
import { t } from "../../services/i18n";
|
2025-10-04 11:07:16 +03:00
|
|
|
|
2025-10-04 11:40:31 +03:00
|
|
|
interface NoteMapProps {
|
|
|
|
|
note: FNote;
|
|
|
|
|
widgetMode: NoteMapWidgetMode;
|
2025-10-04 12:29:37 +03:00
|
|
|
parentRef: RefObject<HTMLElement>;
|
2025-10-04 11:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-04 12:29:37 +03:00
|
|
|
export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {
|
2025-10-04 11:07:16 +03:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const styleResolverRef = useRef<HTMLDivElement>(null);
|
2025-10-04 12:55:08 +03:00
|
|
|
const [ mapTypeRaw, setMapType ] = useNoteLabel(note, "mapType");
|
2025-10-04 11:40:31 +03:00
|
|
|
const mapType: MapType = mapTypeRaw === "tree" ? "tree" : "link";
|
2025-10-04 11:07:16 +03:00
|
|
|
|
2025-10-04 11:40:31 +03:00
|
|
|
const graphRef = useRef<ForceGraph<NodeObject, LinkObject<NodeObject>>>();
|
2025-10-04 12:29:37 +03:00
|
|
|
const containerSize = useElementSize(parentRef);
|
2025-10-04 11:40:31 +03:00
|
|
|
|
|
|
|
|
// Build the note graph instance.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
if (!container) return;
|
2025-10-04 12:29:37 +03:00
|
|
|
const graph = new ForceGraph(container);
|
|
|
|
|
|
2025-10-04 11:40:31 +03:00
|
|
|
graphRef.current = graph;
|
|
|
|
|
|
|
|
|
|
const mapRootId = getMapRootNoteId(note.noteId, note, widgetMode);
|
|
|
|
|
if (!mapRootId) return;
|
|
|
|
|
|
|
|
|
|
const labelValues = (name: string) => note.getLabels(name).map(l => l.value) ?? [];
|
|
|
|
|
const excludeRelations = labelValues("mapExcludeRelation");
|
|
|
|
|
const includeRelations = labelValues("mapIncludeRelation");
|
2025-10-04 12:29:37 +03:00
|
|
|
loadNotesAndRelations(mapRootId, excludeRelations, includeRelations, mapType).then((notesAndRelations) => {
|
2025-10-04 12:31:35 +03:00
|
|
|
if (!containerRef.current || !styleResolverRef.current) return;
|
|
|
|
|
const cssData = getCssData(containerRef.current, styleResolverRef.current);
|
2025-10-04 12:29:37 +03:00
|
|
|
setupRendering(graph, {
|
|
|
|
|
cssData,
|
|
|
|
|
noteId: note.noteId,
|
|
|
|
|
noteIdToSizeMap: notesAndRelations.noteIdToSizeMap,
|
|
|
|
|
notesAndRelations,
|
|
|
|
|
themeStyle: getThemeStyle(),
|
2025-10-04 13:04:40 +03:00
|
|
|
widgetMode,
|
|
|
|
|
mapType
|
2025-10-04 12:29:37 +03:00
|
|
|
});
|
|
|
|
|
graph.graphData(notesAndRelations);
|
2025-10-04 11:40:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => container.replaceChildren();
|
2025-10-04 12:55:08 +03:00
|
|
|
}, [ note, mapType ]);
|
2025-10-04 11:40:31 +03:00
|
|
|
|
2025-10-04 12:29:37 +03:00
|
|
|
// React to container size
|
2025-10-04 11:40:31 +03:00
|
|
|
useEffect(() => {
|
2025-10-04 12:29:37 +03:00
|
|
|
if (!containerSize || !graphRef.current) return;
|
|
|
|
|
graphRef.current.width(containerSize.width).height(containerSize.height);
|
|
|
|
|
}, [ containerSize?.width, containerSize?.height ]);
|
2025-10-04 11:40:31 +03:00
|
|
|
|
2025-10-04 12:31:35 +03:00
|
|
|
return (
|
|
|
|
|
<div className="note-map-widget">
|
2025-10-04 12:55:08 +03:00
|
|
|
<div className="btn-group btn-group-sm map-type-switcher content-floating-buttons top-left" role="group">
|
2025-10-04 12:58:24 +03:00
|
|
|
<MapTypeSwitcher type="link" icon="bx bx-network-chart" text={t("note-map.button-link-map")} currentMapType={mapType} setMapType={setMapType} />
|
|
|
|
|
<MapTypeSwitcher type="tree" icon="bx bx-sitemap" text={t("note-map.button-tree-map")} currentMapType={mapType} setMapType={setMapType} />
|
2025-10-04 12:55:08 +03:00
|
|
|
</div>
|
|
|
|
|
|
2025-10-04 12:31:35 +03:00
|
|
|
<div ref={styleResolverRef} class="style-resolver" />
|
|
|
|
|
<div ref={containerRef} className="note-map-container" />
|
|
|
|
|
</div>
|
|
|
|
|
)
|
2025-10-04 11:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-04 12:58:24 +03:00
|
|
|
function MapTypeSwitcher({ icon, text, type, currentMapType, setMapType }: {
|
|
|
|
|
icon: string;
|
|
|
|
|
text: string;
|
|
|
|
|
type: MapType;
|
|
|
|
|
currentMapType: MapType;
|
|
|
|
|
setMapType: (type: MapType) => void;
|
|
|
|
|
}) {
|
|
|
|
|
return (
|
|
|
|
|
<ActionButton
|
|
|
|
|
icon={icon} text={text}
|
|
|
|
|
disabled={currentMapType === type}
|
|
|
|
|
onClick={() => setMapType(type)}
|
|
|
|
|
frame
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 11:07:16 +03:00
|
|
|
function getCssData(container: HTMLElement, styleResolver: HTMLElement): CssData {
|
|
|
|
|
const containerStyle = window.getComputedStyle(container);
|
|
|
|
|
const styleResolverStyle = window.getComputedStyle(styleResolver);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fontFamily: containerStyle.fontFamily,
|
|
|
|
|
textColor: rgb2hex(containerStyle.color),
|
|
|
|
|
mutedTextColor: rgb2hex(styleResolverStyle.color)
|
|
|
|
|
}
|
|
|
|
|
}
|