From 52de4c6482d46b845f7ca4ec2a28a645113ae7df Mon Sep 17 00:00:00 2001 From: celeste Date: Tue, 7 Mar 2023 17:14:42 -0800 Subject: [PATCH] add root node upon switching files --- main.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main.ts b/main.ts index abfef9c..92d1b34 100644 --- a/main.ts +++ b/main.ts @@ -7,6 +7,7 @@ import { Plugin, PluginSettingTab, Setting, + TFile, WorkspaceLeaf, setIcon, } from "obsidian"; @@ -507,6 +508,10 @@ export default class LoomPlugin extends Plugin { ) this.editor = leaf.view.editor; }); + + if (!this.state[file.path]) { + this.initializeFile(file); + } }) ); @@ -546,6 +551,28 @@ export default class LoomPlugin extends Plugin { }); } + initializeFile(file: TFile) { + // coerce to NoteState because `current` will be defined + this.state[file.path] = { + hoisted: [] as string[], + nodes: {}, + } as NoteState; + + const text = this.editor.getValue(); + + const id = uuidv4(); + this.state[file.path].nodes[id] = { + text, + parentId: null, + unread: false, + collapsed: false, + }; + this.state[file.path].current = id; + + this.save(); + this.view.render(); + } + async complete(model: string, maxTokens: number, n: number) { const file = this.app.workspace.getActiveFile(); if (!file) return;