Skip to content

Commit

Permalink
feat(Threading): ✨ Choose a template to run for each direction
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Jan 3, 2022
1 parent b0b56da commit f46dba1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 22 deletions.
53 changes: 42 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21120,6 +21120,7 @@ const DEFAULT_SETTINGS = {
noPathMessage: `This note has no real or implied parents`,
threadIntoNewPane: false,
threadingTemplate: "{{field}} of {{current}}",
threadingDirTemplates: { up: "", same: "", down: "", next: "", prev: "" },
trailSeperator: "→",
treatCurrNodeAsImpliedSibling: false,
trimDendronNotes: false,
Expand Down Expand Up @@ -25712,21 +25713,35 @@ class BCSettingTab extends require$$0.PluginSettingTab {
new require$$0.Setting(threadingDetails)
.setName("New Note Name Template")
.setDesc(fragWithHTML(`When threading into a new note, choose the template for the new note name.</br>
The default is <code>{{field}} of {{current}}</code>.</br>
Options include:</br>
<ul>
<li><code>{{field}}</code>: the field being thread into</li>
<li><code>{{dir}}</code>: the direction being thread into</li>
<li><code>{{current}}</code>: the current note name</li>
<li><code>{{date}}</code>: the current date (Set the format in the setting below)</li>
</ul>`))
The default is <code>{{field}} of {{current}}</code>.</br>
Options include:</br>
<ul>
<li><code>{{field}}</code>: the field being thread into</li>
<li><code>{{dir}}</code>: the direction being thread into</li>
<li><code>{{current}}</code>: the current note name</li>
<li><code>{{date}}</code>: the current date (Set the format in the setting below)</li>
</ul>`))
.addText((text) => {
text.setValue(settings.threadingTemplate);
text.inputEl.onblur = async () => {
settings.threadingTemplate = text.getValue();
await plugin.saveSettings();
};
});
const threadDirTemplatesSetting = new require$$0.Setting(threadingDetails)
.setClass("thread-dir-templates")
.setName("Templater Template per Direction")
.setDesc(fragWithHTML(`For each direction to be thread into, choose a Templater template to insert into the new note.</br>
Give the basename, or the full file path (e.g. <code>Templates/Parent Template</code>).`));
DIRECTIONS$1.forEach((dir) => threadDirTemplatesSetting.addText((text) => {
text
.setPlaceholder(ARROW_DIRECTIONS[dir])
.setValue(settings.threadingDirTemplates[dir]);
text.inputEl.onblur = async () => {
settings.threadingDirTemplates[dir] = text.getValue();
await plugin.saveSettings();
};
}));
new require$$0.Setting(threadingDetails)
.setName("Date Format")
.setDesc("The date format used in the Threading Template (setting above)")
Expand Down Expand Up @@ -51985,12 +52000,22 @@ class BCPlugin extends require$$0.Plugin {
if (i === 1)
newBasename += ` ${i}`;
else
newBasename = newBasename.slice(0, -1) + ` ${i}`;
newBasename = newBasename.slice(0, -2) + ` ${i}`;
i++;
}
const newFile = await app.vault.create(require$$0.normalizePath(`${newFileParent.path}/${newBasename}.md`), writeBCsInline
const crumb = writeBCsInline
? `${oppField}:: [[${currFile.basename}]]`
: `---\n${oppField}: ['${currFile.basename}']\n---`);
: `---\n${oppField}: ['${currFile.basename}']\n---`;
const templatePath = settings.threadingDirTemplates[dir];
let newContent = crumb;
if (templatePath) {
const templateFile = app.metadataCache.getFirstLinkpathDest(templatePath, "");
const template = await app.vault.cachedRead(templateFile);
newContent = template.replace(/\{\{BC-thread-crumb\}\}/i, writeBCsInline
? `${oppField}:: [[${currFile.basename}]]`
: `${oppField}: ['${currFile.basename}']`);
}
const newFile = await app.vault.create(require$$0.normalizePath(`${newFileParent.path}/${newBasename}.md`), newContent);
if (!writeBCsInline) {
const { api } = (_b = app.plugins.plugins.metaedit) !== null && _b !== void 0 ? _b : {};
if (!api) {
Expand All @@ -52015,6 +52040,12 @@ class BCPlugin extends require$$0.Plugin {
? app.workspace.splitActiveLeaf()
: app.workspace.activeLeaf;
await leaf.openFile(newFile, { active: true, mode: "source" });
if (app.plugins.plugins["templater-obsidian"]) {
app.commands.executeCommandById("templater-obsidian:replace-in-file-templater");
}
else {
new require$$0.Notice("The Templater plugin must be enabled to resolve the templates in the new note");
}
const editor = leaf.view.editor;
editor.setCursor(editor.getValue().length);
},
Expand Down
39 changes: 31 additions & 8 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import KoFi from "./Components/KoFi.svelte";
import UserHierarchies from "./Components/UserHierarchies.svelte";
import {
ALLUNLINKED,
ARROW_DIRECTIONS,
DEFAULT_SETTINGS,
DIRECTIONS,
MATRIX_VIEW,
REAlCLOSED,
RELATIONS,
Expand Down Expand Up @@ -951,14 +953,14 @@ export class BCSettingTab extends PluginSettingTab {
.setDesc(
fragWithHTML(
`When threading into a new note, choose the template for the new note name.</br>
The default is <code>{{field}} of {{current}}</code>.</br>
Options include:</br>
<ul>
<li><code>{{field}}</code>: the field being thread into</li>
<li><code>{{dir}}</code>: the direction being thread into</li>
<li><code>{{current}}</code>: the current note name</li>
<li><code>{{date}}</code>: the current date (Set the format in the setting below)</li>
</ul>`
The default is <code>{{field}} of {{current}}</code>.</br>
Options include:</br>
<ul>
<li><code>{{field}}</code>: the field being thread into</li>
<li><code>{{dir}}</code>: the direction being thread into</li>
<li><code>{{current}}</code>: the current note name</li>
<li><code>{{date}}</code>: the current date (Set the format in the setting below)</li>
</ul>`
)
)
.addText((text) => {
Expand All @@ -968,6 +970,27 @@ export class BCSettingTab extends PluginSettingTab {
await plugin.saveSettings();
};
});
const threadDirTemplatesSetting = new Setting(threadingDetails)
.setClass("thread-dir-templates")
.setName("Templater Template per Direction")
.setDesc(
fragWithHTML(
`For each direction to be thread into, choose a Templater template to insert into the new note.</br>
Give the basename, or the full file path (e.g. <code>Templates/Parent Template</code>).`
)
);

DIRECTIONS.forEach((dir) =>
threadDirTemplatesSetting.addText((text) => {
text
.setPlaceholder(ARROW_DIRECTIONS[dir])
.setValue(settings.threadingDirTemplates[dir]);
text.inputEl.onblur = async () => {
settings.threadingDirTemplates[dir] = text.getValue();
await plugin.saveSettings();
};
})
);

new Setting(threadingDetails)
.setName("Date Format")
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export const DEFAULT_SETTINGS: BCSettings = {
noPathMessage: `This note has no real or implied parents`,
threadIntoNewPane: false,
threadingTemplate: "{{field}} of {{current}}",
threadingDirTemplates: { up: "", same: "", down: "", next: "", prev: "" },
trailSeperator: "→",
treatCurrNodeAsImpliedSibling: false,
trimDendronNotes: false,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface BCSettings {
squareDirectionsOrder: (0 | 1 | 2 | 3 | 4)[];
threadIntoNewPane: boolean;
threadingTemplate: string;
threadingDirTemplates: { [dir in Directions]: string };
trailSeperator: string;
treatCurrNodeAsImpliedSibling: boolean;
trimDendronNotes: boolean;
Expand Down
35 changes: 32 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,30 @@ export default class BCPlugin extends Plugin {
i++;
}

const crumb = writeBCsInline
? `${oppField}:: [[${currFile.basename}]]`
: `---\n${oppField}: ['${currFile.basename}']\n---`;

const templatePath = settings.threadingDirTemplates[dir];
let newContent = crumb;
if (templatePath) {
const templateFile = app.metadataCache.getFirstLinkpathDest(
templatePath,
""
);

const template = await app.vault.cachedRead(templateFile);
newContent = template.replace(
/\{\{BC-thread-crumb\}\}/i,
writeBCsInline
? `${oppField}:: [[${currFile.basename}]]`
: `${oppField}: ['${currFile.basename}']`
);
}

const newFile = await app.vault.create(
normalizePath(`${newFileParent.path}/${newBasename}.md`),
writeBCsInline
? `${oppField}:: [[${currFile.basename}]]`
: `---\n${oppField}: ['${currFile.basename}']\n---`
newContent
);

if (!writeBCsInline) {
Expand Down Expand Up @@ -496,6 +515,16 @@ export default class BCPlugin extends Plugin {
: app.workspace.activeLeaf;

await leaf.openFile(newFile, { active: true, mode: "source" });
if (app.plugins.plugins["templater-obsidian"]) {
app.commands.executeCommandById(
"templater-obsidian:replace-in-file-templater"
);
} else {
new Notice(
"The Templater plugin must be enabled to resolve the templates in the new note"
);
}

const editor = leaf.view.editor as Editor;
editor.setCursor(editor.getValue().length);
},
Expand Down
5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@
visibility: hidden;
opacity: 0;
}

.thread-dir-templates .setting-item-control {
display: flex;
flex-direction: column;
}

0 comments on commit f46dba1

Please sign in to comment.