Skip to content

Commit

Permalink
feat(Threading): ✨ Commands to create a new <field> from the current …
Browse files Browse the repository at this point in the history
…note (fix #221)
  • Loading branch information
SkepticMystic committed Dec 29, 2021
1 parent d9525c0 commit ee76b56
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"LinkNote",
"TraverseNote",
"DownView",
"DendronNote"
"DendronNote",
"Threading"
]
}
47 changes: 46 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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));
}
Expand Down
66 changes: 65 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ee76b56

Please sign in to comment.