From ee76b5691477c52ebb4a7672a6fd3631ce9f97e3 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Wed, 29 Dec 2021 11:31:34 +0200 Subject: [PATCH] feat(Threading): :sparkles: Commands to create a new from the current note (fix #221) --- .vscode/settings.json | 3 +- main.js | 47 +++++++++++++++++++++++++++++- src/main.ts | 66 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e9423bc7..6295553f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,7 @@ "LinkNote", "TraverseNote", "DownView", - "DendronNote" + "DendronNote", + "Threading" ] } \ No newline at end of file diff --git a/main.js b/main.js index 7bf56dae..567ba5b6 100644 --- a/main.js +++ b/main.js @@ -51595,7 +51595,12 @@ class BCPlugin extends require$$0.Plugin { // TODO Check if this note already has this field let content = await app.vault.read(file); const splits = splitAtYaml(content); - content = splits[0] + `\n${field}:: [[${succ}]]` + splits[1]; + content = + splits[0] + + (splits[0].length ? "\n" : "") + + `${field}:: [[${succ}]]` + + (splits[1].length ? "\n" : "") + + splits[1]; await app.vault.modify(file, content); } } @@ -51885,6 +51890,46 @@ class BCPlugin extends require$$0.Plugin { }, }); }); + getFields(settings.userHiers).forEach((field) => { + this.addCommand({ + id: `new-file-with-curr-as-${field}`, + name: `Create a new '${field}' from the current note`, + callback: async () => { + var _a, _b; + const { app } = this; + const { userHiers, writeBCsInline } = settings; + const currFile = app.workspace.getActiveFile(); + if (!currFile) + return; + const newFilePath = app.fileManager.getNewFileParent(currFile.path); + const oppField = (_a = getOppFields(userHiers, field)[0]) !== null && _a !== void 0 ? _a : fallbackOppField(field, getFieldInfo(userHiers, field).fieldDir); + const newFile = await app.vault.create(require$$0.normalizePath(`${newFilePath.path}/${field} of ${currFile.basename}.md`), writeBCsInline + ? `${oppField}:: [[${currFile.basename}]]` + : `---\n${oppField}: ['${currFile.basename}']\n---`); + if (!writeBCsInline) { + const { api } = (_b = app.plugins.plugins.metaedit) !== null && _b !== void 0 ? _b : {}; + if (!api) { + new require$$0.Notice("Metaedit must be enabled to write to yaml. Alternatively, toggle the setting `Write Breadcrumbs Inline` to use Dataview inline fields instead."); + return; + } + await createOrUpdateYaml(field, newFile.basename, currFile, app.metadataCache.getFileCache(currFile).frontmatter, api); + } + else { + // TODO Check if this note already has this field + let content = await app.vault.read(currFile); + const splits = splitAtYaml(content); + content = + splits[0] + + (splits[0].length ? "\n" : "") + + `${field}:: [[${newFile.basename}]]` + + (splits[1].length ? "\n" : "") + + splits[1]; + await app.vault.modify(currFile, content); + } + app.workspace.activeLeaf.openFile(newFile); + }, + }); + }); this.addRibbonIcon(addFeatherIcon("tv"), "Breadcrumbs Visualisation", () => new VisModal(this.app, this).open()); this.addSettingTab(new BCSettingTab(this.app, this)); } diff --git a/src/main.ts b/src/main.ts index 708d42b8..2335ac3b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -421,6 +421,65 @@ export default class BCPlugin extends Plugin { }); }); + getFields(settings.userHiers).forEach((field: string) => { + this.addCommand({ + id: `new-file-with-curr-as-${field}`, + name: `Create a new '${field}' from the current note`, + callback: async () => { + const { app } = this; + const { userHiers, writeBCsInline } = settings; + + const currFile = app.workspace.getActiveFile(); + if (!currFile) return; + + const newFilePath = app.fileManager.getNewFileParent(currFile.path); + + const oppField = + getOppFields(userHiers, field)[0] ?? + fallbackOppField(field, getFieldInfo(userHiers, field).fieldDir); + + const newFile = await app.vault.create( + normalizePath( + `${newFilePath.path}/${field} of ${currFile.basename}.md` + ), + writeBCsInline + ? `${oppField}:: [[${currFile.basename}]]` + : `---\n${oppField}: ['${currFile.basename}']\n---` + ); + + if (!writeBCsInline) { + const { api } = app.plugins.plugins.metaedit ?? {}; + if (!api) { + new Notice( + "Metaedit must be enabled to write to yaml. Alternatively, toggle the setting `Write Breadcrumbs Inline` to use Dataview inline fields instead." + ); + return; + } + await createOrUpdateYaml( + field, + newFile.basename, + currFile, + app.metadataCache.getFileCache(currFile).frontmatter, + api + ); + } else { + // TODO Check if this note already has this field + let content = await app.vault.read(currFile); + const splits = splitAtYaml(content); + content = + splits[0] + + (splits[0].length ? "\n" : "") + + `${field}:: [[${newFile.basename}]]` + + (splits[1].length ? "\n" : "") + + splits[1]; + + await app.vault.modify(currFile, content); + } + app.workspace.activeLeaf.openFile(newFile); + }, + }); + }); + this.addRibbonIcon( addFeatherIcon("tv") as string, "Breadcrumbs Visualisation", @@ -457,7 +516,12 @@ export default class BCPlugin extends Plugin { // TODO Check if this note already has this field let content = await app.vault.read(file); const splits = splitAtYaml(content); - content = splits[0] + `\n${field}:: [[${succ}]]` + splits[1]; + content = + splits[0] + + (splits[0].length ? "\n" : "") + + `${field}:: [[${succ}]]` + + (splits[1].length ? "\n" : "") + + splits[1]; await app.vault.modify(file, content); }