Skip to content

Commit

Permalink
Store autoRestored property on renderer
Browse files Browse the repository at this point in the history
Leaves can switch between Markdown and Graph views, so
storing the leaf ID wasn't a reliable way to detect a new graph view
  • Loading branch information
Sanqui committed Jan 27, 2022
1 parent 1474e37 commit 1fbf35d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
21 changes: 4 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const DEFAULT_SETTINGS: PersistentGraphSettings = {
export default class PersistentGraphPlugin extends Plugin {
settings: PersistentGraphSettings;

// Since there is no event for a leaf being opened, only
// active leaf change, we have to keep a runtime list
// of graph leaf ids that have already been recovered
recoveredLeafIds: string[] = [];

findGraphLeaf() {
let activeLeaf = this.app.workspace.activeLeaf;
if (activeLeaf.view.getViewType() === "graph") {
Expand Down Expand Up @@ -101,17 +96,13 @@ export default class PersistentGraphPlugin extends Plugin {
}

onLayoutChange() {
let activeLeaf = this.app.workspace.activeLeaf;

if (activeLeaf.view.getViewType() != "graph") {
return;
}
const activeLeaf = this.app.workspace.activeLeaf;

if (this.recoveredLeafIds.contains(activeLeaf.id)) {
if (activeLeaf.view.getViewType() != "graph" || activeLeaf.view.renderer.autoRestored) {
return;
}

this.recoveredLeafIds.push(activeLeaf.id);
activeLeaf.view.renderer.autoRestored = true;

// We can't restore node positions right away
// because not all nodes have been created yet.
Expand All @@ -133,12 +124,8 @@ export default class PersistentGraphPlugin extends Plugin {
return;
}

if (leaf.view.renderer.autoRestored) {
return;
}

if (this.settings.automaticallyRestoreNodePositions) {
let currentNodeCount = leaf.view.renderer.nodes.length;
const currentNodeCount = leaf.view.renderer.nodes.length;

if (currentNodeCount === nodeCount) {
if (iterations >= 3) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "persistent-graph",
"name": "Persistent Graph",
"version": "0.1.3",
"version": "0.1.4",
"minAppVersion": "0.12.0",
"description": "Adds commands to save and restore the positions of nodes on the global graph view",
"author": "Sanqui",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "persistent-graph",
"version": "0.1.3",
"version": "0.1.4",
"description": "An Obsidian plugin for persisting graph state",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production"
"check": "tsc -noEmit -skipLibCheck",
"build": "node esbuild.config.mjs production"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit 1fbf35d

Please sign in to comment.