From b12cf4bfd32e12552a03300e95fd42ad4cb21a7c Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 31 Jul 2021 09:58:10 +0200 Subject: [PATCH] feat(CreateIndex): :sparkles: add Global Index command This introduces a new command, "Copy a Global Index to the clipboard". --- src/MatrixView.ts | 61 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/MatrixView.ts b/src/MatrixView.ts index 8681b1f7..0ba5ce8d 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -36,15 +36,20 @@ export default class MatrixView extends ItemView { name: "Copy a Local Index to the clipboard", callback: async () => { const settings = this.plugin.settings; - const currFile = this.app.workspace.getActiveFile(); + const currFile = this.app.workspace.getActiveFile().basename; const allPaths = this.dfsAllPaths( closeImpliedLinks( this.plugin.currGraphs.gChildren, this.plugin.currGraphs.gParents ), - this.app.workspace.getActiveFile().basename + currFile + ); + const index = this.createIndex( + currFile + "\n", + allPaths, + currFile, + settings ); - const index = this.createIndex(allPaths, currFile.basename, settings); debug(settings, { index }); await navigator.clipboard.writeText(index).then( () => new Notice("Index copied to clipboard"), @@ -52,6 +57,41 @@ export default class MatrixView extends ItemView { ); }, }); + + this.plugin.addCommand({ + id: "global-index", + name: "Copy a Global Index to the clipboard", + callback: async () => { + const { gParents, gChildren } = this.plugin.currGraphs; + + const terminals = gParents.sinks(); + console.log({ terminals }); + + const settings = this.plugin.settings; + const currFile = this.app.workspace.getActiveFile().basename; + + let globalIndex = ""; + terminals.forEach((terminal) => { + globalIndex += terminal + "\n"; + const allPaths = this.dfsAllPaths( + closeImpliedLinks(gChildren, gParents), + terminal + ); + globalIndex = this.createIndex( + globalIndex, + allPaths, + terminal, + settings + ); + }); + + debug(settings, { globalIndex }); + await navigator.clipboard.writeText(globalIndex).then( + () => new Notice("Index copied to clipboard"), + () => new Notice("Could not copy index to clipboard") + ); + }, + }); } getViewType(): string { @@ -151,6 +191,8 @@ export default class MatrixView extends ItemView { } createIndex( + // Gotta give it a starting index. This allows it to work for the global index feat + index: string, allPaths: string[][], currFile: string, settings: BreadcrumbsSettings @@ -159,7 +201,6 @@ export default class MatrixView extends ItemView { const reversed = copy.map((path) => path.reverse()); reversed.forEach((path) => path.shift()); - let txt = currFile + "\n"; const indent = " "; const visited: { [node: string]: number[] } = {}; reversed.forEach((path) => { @@ -173,11 +214,11 @@ export default class MatrixView extends ItemView { ) { continue; } else { - txt += `${indent.repeat(depth)}- `; - txt += settings.wikilinkIndex ? "[[" : ""; - txt += currNode; - txt += settings.wikilinkIndex ? "]]" : ""; - txt += "\n"; + index += `${indent.repeat(depth)}- `; + index += settings.wikilinkIndex ? "[[" : ""; + index += currNode; + index += settings.wikilinkIndex ? "]]" : ""; + index += "\n"; if (!visited.hasOwnProperty(currNode)) { visited[currNode] = []; @@ -186,7 +227,7 @@ export default class MatrixView extends ItemView { } } }); - return txt; + return index; } async draw(): Promise {