From 7eb23ab5e15a87791e84cbffef07d125c7eb902c Mon Sep 17 00:00:00 2001 From: zsviczian Date: Wed, 15 Jan 2025 08:22:38 +0000 Subject: [PATCH] Set local graph when embeddable is activated/deactivated #2200 --- package-lock.json | 8 ++++---- src/utils/customEmbeddableUtils.ts | 13 ++++++++++++- src/view/components/CustomEmbeddable.tsx | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 127283b..34fc513 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@popperjs/core": "^2.11.8", "@zsviczian/colormaster": "^1.2.2", - "@zsviczian/excalidraw": "0.17.6-25", + "@zsviczian/excalidraw": "0.17.6-26", "chroma-js": "^2.4.2", "clsx": "^2.0.0", "es6-promise-pool": "2.5.0", @@ -3315,9 +3315,9 @@ "license": "MIT" }, "node_modules/@zsviczian/excalidraw": { - "version": "0.17.6-25", - "resolved": "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.17.6-25.tgz", - "integrity": "sha512-Hq/LVJoQHTc8ECKrqLf1hZRh7xEDI5W7SoVjBhLl5vwbhUxrYPu7j83g01kDiqKxybq8YK0kQULhEck64dyh0A==", + "version": "0.17.6-26", + "resolved": "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.17.6-26.tgz", + "integrity": "sha512-UAqr7b7cxIbOvK1u0NKqgAs0wB9KYUsVc6Q2J+yviM4ae+wkTXp8qW/V4mdWADJ3lTG5RgXCb9ausIKfSkPNRg==", "dependencies": { "@braintree/sanitize-url": "6.0.2", "@excalidraw/random-username": "1.1.0", diff --git a/src/utils/customEmbeddableUtils.ts b/src/utils/customEmbeddableUtils.ts index a4d5661..633783d 100644 --- a/src/utils/customEmbeddableUtils.ts +++ b/src/utils/customEmbeddableUtils.ts @@ -1,7 +1,7 @@ import { NonDeletedExcalidrawElement } from "@zsviczian/excalidraw/types/excalidraw/element/types"; import { DEVICE, REG_LINKINDEX_INVALIDCHARS } from "src/constants/constants"; import { getParentOfClass } from "./obsidianUtils"; -import { TFile, WorkspaceLeaf } from "obsidian"; +import { App, TFile, WorkspaceLeaf } from "obsidian"; import { getLinkParts } from "./utils"; import ExcalidrawView from "src/view/ExcalidrawView"; @@ -55,4 +55,15 @@ export const generateEmbeddableLink = (src: string, theme: "light" | "dark"):str } }*/ return src; +} + +export function setFileToLocalGraph(app: App, file: TFile) { + let lgv; + app.workspace.iterateAllLeaves((l) => { + if (l.view?.getViewType() === "localgraph") lgv = l.view; + }); + if (lgv) { + //@ts-ignore + lgv.loadFile(file); + } } \ No newline at end of file diff --git a/src/view/components/CustomEmbeddable.tsx b/src/view/components/CustomEmbeddable.tsx index e276555..eebea30 100644 --- a/src/view/components/CustomEmbeddable.tsx +++ b/src/view/components/CustomEmbeddable.tsx @@ -6,7 +6,7 @@ import { ConstructableWorkspaceSplit, getContainerForDocument, isObsidianThemeDa import { DEVICE, EXTENDED_EVENT_TYPES, KEYBOARD_EVENT_TYPES } from "src/constants/constants"; import { ExcalidrawImperativeAPI, UIAppState } from "@zsviczian/excalidraw/types/excalidraw/types"; import { ObsidianCanvasNode } from "src/view/managers/CanvasNodeFactory"; -import { processLinkText, patchMobileView } from "src/utils/customEmbeddableUtils"; +import { processLinkText, patchMobileView, setFileToLocalGraph } from "src/utils/customEmbeddableUtils"; import { EmbeddableMDCustomProps } from "src/shared/Dialogs/EmbeddableSettings"; declare module "obsidian" { @@ -154,6 +154,15 @@ function RenderObsidianView( }; //cleanup on unmount }, [isActiveRef.current, containerRef.current]); + //set local graph to view when deactivating embeddables + React.useEffect(() => { + if(file === view.file) { + return; + } + if(!isActiveRef.current) { + setFileToLocalGraph(view.app, view.file); + } + }, [isActiveRef.current]); //-------------------------------------------------------------------------------- //Mount the workspace leaf or the canvas node depending on subpath @@ -408,6 +417,10 @@ function RenderObsidianView( return; } + if(file !== view.file) { + setFileToLocalGraph(view.app, file); + } + if(leafRef.current.leaf?.view?.getViewType() === "markdown") { //Handle markdown leaf //@ts-ignore