From 34f5bfbe2f94cb6935731009a9ad96d056ddbbe0 Mon Sep 17 00:00:00 2001 From: Unarray <48868686+Unarray@users.noreply.github.com> Date: Wed, 23 Aug 2023 15:44:12 +0200 Subject: [PATCH] Create `GenerateTree` command --- src/commands/GenerateTree.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/commands/GenerateTree.ts diff --git a/src/commands/GenerateTree.ts b/src/commands/GenerateTree.ts new file mode 100644 index 0000000..7703d2d --- /dev/null +++ b/src/commands/GenerateTree.ts @@ -0,0 +1,35 @@ +import type { Command, Editor } from "obsidian"; +import { dialog } from "@electron/remote"; +import { getFiles } from "#/utils/path"; +import { explorerEntityToCallout, filesToExplorerEntity } from "#/utils/parser"; +import { beginningString } from "#/utils/regex"; +import { sep as separator } from "path"; + +export class GenerateTree implements Command { + + public readonly id = "generate-tree"; + + public readonly name = "Generate a File Tree"; + + public editorCallback = async(editor: Editor): Promise => { + const dialogResponse = await dialog.showOpenDialog({ + properties: ["openDirectory"] + }); + + if (dialogResponse.canceled) return; + + const selectedPath = dialogResponse.filePaths[0]; + const removePath = selectedPath.substring(0, selectedPath.lastIndexOf(separator) + separator.length); + const regex = beginningString(removePath); + const files = (await getFiles(selectedPath)).map(file => file.replace(regex, "")); + const structure = filesToExplorerEntity(files); + const callouts = explorerEntityToCallout(structure); + const cursorLine = editor.getCursor("to").line; + + editor.setLine( + cursorLine, + `${editor.getLine(cursorLine)}\n${callouts}` + ); + }; + +} \ No newline at end of file