From 289b701158a5f8e832d98672be39d8cf8e2ffd9b Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Wed, 8 Sep 2021 21:39:00 +0200 Subject: [PATCH] feat(WriteBCToFile): :sparkles: Write Breadcrumbs to All files Three warning messages to ensure people are aware --- main.js | 27 +++++++++++++++++++++++++-- src/main.ts | 28 ++++++++++++++++++++++++++-- src/sharedFunctions.ts | 4 ++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index 639bf324..7eeb1388 100644 --- a/main.js +++ b/main.js @@ -6674,13 +6674,13 @@ const writeBCToFile = (app, plugin, currGraphs, file) => { Object.keys(hier[dir]).forEach(field => { const fieldG = hier[dir][field]; const succs = fieldG.predecessors(file.basename); - succs.forEach(succ => { + succs.forEach(async (succ) => { const { fieldName } = fieldG.node(succ); const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0]; let oppField = currHier[oppDir][0]; if (!oppField) oppField = `${fieldName}`; - createOrUpdateYaml(oppField, succ, file, frontmatter, api); + await createOrUpdateYaml(oppField, succ, file, frontmatter, api); }); }); }); @@ -37389,6 +37389,29 @@ class BreadcrumbsPlugin extends obsidian.Plugin { writeBCToFile(this.app, this, this.currGraphs, currFile); }, }); + this.addCommand({ + id: "Write-Breadcrumbs-to-All-Files", + name: "Write Breadcrumbs to **ALL** Files", + callback: () => { + const first = window.confirm("This action will write the implied Breadcrumbs of each file to that file.\nIt uses the MetaEdit plugins API to update the YAML, so it should only affect that frontmatter of your note.\nI can't promise that nothing bad will happen. **This operation cannot be undone**."); + if (first) { + const second = window.confirm('Are you sure? You have been warned that this operation will attempt to update all files with implied breadcrumbs.'); + if (second) { + const third = window.confirm('For real, please make a back up before'); + if (third) { + try { + this.app.vault.getMarkdownFiles().forEach(file => writeBCToFile(this.app, this, this.currGraphs, file)); + new obsidian.Notice('Operation Complete'); + } + catch (error) { + new obsidian.Notice(error); + console.log(error); + } + } + } + } + }, + }); this.addRibbonIcon("dice", "Breadcrumbs Visualisation", () => new VisModal(this.app, this).open()); this.addSettingTab(new BreadcrumbsSettingTab(this.app, this)); } diff --git a/src/main.ts b/src/main.ts index cf75a05f..334dd78b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,8 +3,7 @@ import { addIcon, EventRef, MarkdownView, - Notice, - Plugin, + Notice, Plugin, TFile, WorkspaceLeaf } from "obsidian"; @@ -324,6 +323,31 @@ export default class BreadcrumbsPlugin extends Plugin { }, }); + this.addCommand({ + id: "Write-Breadcrumbs-to-All-Files", + name: "Write Breadcrumbs to **ALL** Files", + callback: () => { + const first = window.confirm("This action will write the implied Breadcrumbs of each file to that file.\nIt uses the MetaEdit plugins API to update the YAML, so it should only affect that frontmatter of your note.\nI can't promise that nothing bad will happen. **This operation cannot be undone**."); + + if (first) { + const second = window.confirm('Are you sure? You have been warned that this operation will attempt to update all files with implied breadcrumbs.'); + if (second) { + const third = window.confirm('For real, please make a back up before'); + if (third) { + try { + this.app.vault.getMarkdownFiles().forEach(file => writeBCToFile(this.app, this, this.currGraphs, file)) + new Notice('Operation Complete') + } + catch (error) { + new Notice(error) + console.log(error) + } + } + } + } + + }, + }); this.addRibbonIcon("dice", "Breadcrumbs Visualisation", () => new VisModal(this.app, this).open() diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index f8198258..55555c50 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -670,13 +670,13 @@ export const writeBCToFile = (app: App, plugin: BreadcrumbsPlugin, currGraphs: B const fieldG = hier[dir][field]; const succs = fieldG.predecessors(file.basename) as string[]; - succs.forEach(succ => { + succs.forEach(async succ => { const { fieldName } = fieldG.node(succ); const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0] let oppField: string = currHier[oppDir][0]; if (!oppField) oppField = `${fieldName}` - createOrUpdateYaml(oppField, succ, file, frontmatter, api) + await createOrUpdateYaml(oppField, succ, file, frontmatter, api) }) }) })