diff --git a/main.ts b/main.ts index 3b6a1a1..f040ced 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { App, debounce, normalizePath, Plugin, PluginSettingTab, Setting, TAbstractFile, TFile, TFolder, TextComponent, ToggleComponent } from "obsidian"; +import { App, debounce, normalizePath, Notice, Plugin, PluginSettingTab, Setting, TAbstractFile, TFile, TFolder, TextComponent, ToggleComponent } from "obsidian"; enum FolderNoteType { InsideFolder = "INSIDE_FOLDER", @@ -83,6 +83,58 @@ export default class Waypoint extends Plugin { this.app.workspace.activeLeaf.openFile(parentPoint); }, }); + this.addCommand({ + id: "insert_waypoint_flag", + name: "Insert Waypoint flag", + editorCallback: (editor) => { + const waypointStr = this.settings.waypointFlag; + const cursor = editor.getCursor(); + editor.replaceRange(waypointStr + "\n", cursor); + }, + }); + this.addCommand({ + id: "insert_landmark_flag", + name: "Insert Landmark flag", + editorCallback: (editor) => { + const landmarkStr = this.settings.landmarkFlag; + const cursor = editor.getCursor(); + editor.replaceRange(landmarkStr + "\n", cursor); + }, + }); + this.addCommand({ + id: "create_waypoint_file_in_active_folder", + name: "Create Waypoint file in active folder", + callback: async () => { + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + new Notice("No active file. Cannot determine the current folder."); + return; + } + + const currentFolder = activeFile.parent; + if (!currentFolder) return; + + if (currentFolder.isRoot()) { + new Notice("Cannot create a Waypoint file in the root folder of a vault."); + return; + } + + const folderName = currentFolder.name; + const notePath = normalizePath(`${currentFolder.path}/${folderName}.md`); + + const existingFile = this.app.vault.getAbstractFileByPath(notePath); + if (existingFile instanceof TFile) { + new Notice(`Waypoint file "${folderName}.md" already exists!`); + await this.app.workspace.getLeaf(false).openFile(existingFile); + return; + } + + const newContent = this.settings.waypointFlag + "\n"; + const newFile = await this.app.vault.create(notePath, newContent); + + await this.app.workspace.getLeaf(false).openFile(newFile); + }, + }); this.app.workspace.onLayoutReady(async () => { // Register events after layout is built to avoid initial wave of 'create' events this.registerEvent(