Skip to content

Commit 045545f

Browse files
committed
fix #297; fix #292
1 parent 60be2c5 commit 045545f

File tree

9 files changed

+105
-57
lines changed

9 files changed

+105
-57
lines changed

bun.lockb

1.1 KB
Binary file not shown.

exampleVault/Buttons/Templater Buttons.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ action:
1010
type: "replaceSelf"
1111
replacement: "templates/templater/Templater Template.md"
1212
templater: true
13+
```
14+
15+
```meta-bind-button
16+
label: Insert Text
17+
hidden: false
18+
class: ""
19+
tooltip: ""
20+
id: ""
21+
style: default
22+
action:
23+
type: "replaceSelf"
24+
replacement: "[[Templater Template]]"
25+
templater: true
1326
```

packages/core/src/fields/button/ButtonActionRunner.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { expectType, openURL } from 'packages/core/src/utils/Utils';
2323
import { parseLiteral } from 'packages/core/src/utils/Literal';
2424

2525
import { type NotePosition } from 'packages/core/src/config/APIConfigs';
26+
import { ErrorLevel, MetaBindParsingError } from 'packages/core/src/utils/errors/MetaBindErrors';
2627

2728
export class ButtonActionRunner {
2829
plugin: IPlugin;
@@ -31,6 +32,20 @@ export class ButtonActionRunner {
3132
this.plugin = plugin;
3233
}
3334

35+
resolveFilePath(filePath: string, relativeFilePath?: string | undefined): string {
36+
const targetFilePath = MDLinkParser.isLink(filePath) ? MDLinkParser.parseLink(filePath).target : filePath;
37+
const resolvedFilePath = this.plugin.internal.getFilePathByName(targetFilePath, relativeFilePath);
38+
if (resolvedFilePath === undefined) {
39+
throw new MetaBindParsingError({
40+
errorLevel: ErrorLevel.ERROR,
41+
cause: 'file not found',
42+
effect: `could not resolve path or link "${filePath}" relative to "${relativeFilePath}"`,
43+
});
44+
}
45+
46+
return resolvedFilePath;
47+
}
48+
3449
createDefaultButtonConfig(): ButtonConfig {
3550
return {
3651
label: 'This is a button',
@@ -288,7 +303,7 @@ export class ButtonActionRunner {
288303
}
289304

290305
const replacement = action.templater
291-
? await this.plugin.internal.evaluateTemplaterTemplate(action.replacement, filePath)
306+
? await this.plugin.internal.evaluateTemplaterTemplate(this.resolveFilePath(action.replacement), filePath)
292307
: action.replacement;
293308

294309
splitContent = [
@@ -329,7 +344,7 @@ export class ButtonActionRunner {
329344
}
330345

331346
const replacement = action.templater
332-
? await this.plugin.internal.evaluateTemplaterTemplate(action.replacement, filePath)
347+
? await this.plugin.internal.evaluateTemplaterTemplate(this.resolveFilePath(action.replacement), filePath)
333348
: action.replacement;
334349

335350
splitContent = [
@@ -362,11 +377,15 @@ export class ButtonActionRunner {
362377
throw new Error('Line number out of bounds');
363378
}
364379

365-
const replacement = action.templater
366-
? await this.plugin.internal.evaluateTemplaterTemplate(action.value, filePath)
380+
const insertString = action.templater
381+
? await this.plugin.internal.evaluateTemplaterTemplate(this.resolveFilePath(action.value), filePath)
367382
: action.value;
368383

369-
splitContent = [...splitContent.slice(0, action.line - 1), replacement, ...splitContent.slice(action.line - 1)];
384+
splitContent = [
385+
...splitContent.slice(0, action.line - 1),
386+
insertString,
387+
...splitContent.slice(action.line - 1),
388+
];
370389

371390
await this.plugin.internal.writeFilePath(filePath, splitContent.join('\n'));
372391
}

packages/core/src/utils/faq/FaqComponent.svelte renamed to packages/core/src/utils/playground/PlaygroundComponent.svelte

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
createInputFieldFAQExamples,
66
VIEW_FIELD_EXAMPLE_DECLARATIONS,
77
} from 'packages/core/src/utils/InputFieldExamples';
8-
import InputFieldExampleComponent from 'packages/core/src/utils/faq/InputFieldExampleComponent.svelte';
9-
import ViewFieldExampleComponent from 'packages/core/src/utils/faq/ViewFieldExampleComponent.svelte';
8+
import InputFieldExampleComponent from 'packages/core/src/utils/playground/InputFieldExampleComponent.svelte';
9+
import ViewFieldExampleComponent from 'packages/core/src/utils/playground/ViewFieldExampleComponent.svelte';
1010
import Button from 'packages/core/src/utils/components/Button.svelte';
1111
import { ErrorCollection } from 'packages/core/src/utils/errors/ErrorCollection';
1212
import { ErrorLevel, MetaBindExampleError } from 'packages/core/src/utils/errors/MetaBindErrors';
1313
import { DocsUtils } from 'packages/core/src/utils/DocsUtils';
1414
import { ButtonStyleType } from 'packages/core/src/config/ButtonConfig';
1515
import { IPlugin } from 'packages/core/src/IPlugin';
16+
import FlexRow from '../components/FlexRow.svelte';
1617
1718
export let plugin: IPlugin;
1819
@@ -44,32 +45,37 @@
4445
</script>
4546

4647
<div class="mb-faq-view markdown-rendered">
47-
<h1>Meta Bind FAQ</h1>
48+
<FlexRow>
49+
<h1>Meta Bind Playground</h1>
50+
<span>
51+
<Button
52+
variant={ButtonStyleType.PRIMARY}
53+
on:click={() => {
54+
DocsUtils.open(DocsUtils.linkToHome());
55+
}}
56+
>
57+
Docs
58+
</Button>
59+
<Button
60+
on:click={() => {
61+
DocsUtils.open(DocsUtils.linkToGithub());
62+
}}
63+
>
64+
GitHub
65+
</Button>
66+
<Button
67+
on:click={() => {
68+
DocsUtils.open(DocsUtils.linkToIssues());
69+
}}
70+
>
71+
Report Issue
72+
</Button>
73+
</span>
74+
</FlexRow>
4875

49-
<h2>Quick Access</h2>
50-
<p>
51-
<Button
52-
variant={ButtonStyleType.PRIMARY}
53-
on:click={() => {
54-
DocsUtils.open(DocsUtils.linkToHome());
55-
}}
56-
>Docs
57-
</Button>
58-
<Button
59-
on:click={() => {
60-
DocsUtils.open(DocsUtils.linkToGithub());
61-
}}
62-
>GitHub
63-
</Button>
64-
<Button
65-
on:click={() => {
66-
DocsUtils.open(DocsUtils.linkToIssues());
67-
}}
68-
>Report Issue
69-
</Button>
70-
</p>
76+
<h2>Quick Reference</h2>
7177

72-
<h2>Error Messages</h2>
78+
<h3>Error Messages</h3>
7379
<p>
7480
When creating <a href={DocsUtils.linkToInputFields()}>Input Fields</a> or
7581
<a href={DocsUtils.linkToViewFields()}>View Fields</a>
@@ -93,7 +99,28 @@
9399
when clicked.
94100
</p>
95101

96-
<h2>Input Fields</h2>
102+
<h3>Unloaded Message</h3>
103+
<p>
104+
A message like this <span class="mb-warning">[MB_UNLOADED] ...</span> means that Obsidian told Meta Bind to unload
105+
the field that was once displayed there. This usually happens when Meta Bind was disabled, such as after a plugin
106+
update, or when another plugin is interfering with Meta Bind.
107+
</p>
108+
<p>Usually reopening the note or restarting Obsidian causes the field to display normally again.</p>
109+
110+
<h3>Bind Targets</h3>
111+
<p>
112+
<a href="https://mprojectscode.github.io/obsidian-meta-bind-plugin-docs/guides/inputfields/#binding-to-metadata"
113+
>Bind Targets</a
114+
>
115+
let the plugin know what frontmatter properties to bind
116+
<a href={DocsUtils.linkToInputFields()}>Input Fields</a>
117+
and
118+
<a href={DocsUtils.linkToViewFields()}>View Fields</a> to.
119+
</p>
120+
121+
<h2>Playground</h2>
122+
123+
<h3>Input Fields</h3>
97124
<p>
98125
<a href={DocsUtils.linkToInputFields()}>Input Fields</a> let you change the frontmatter of your notes from inside
99126
of notes.
@@ -103,7 +130,7 @@
103130
<InputFieldExampleComponent declaration={example[1]} plugin={plugin}></InputFieldExampleComponent>
104131
{/each}
105132

106-
<h2>View Fields</h2>
133+
<h3>View Fields</h3>
107134
<p>
108135
<a href={DocsUtils.linkToViewFields()}>View Fields</a> let you view and perform calculations using the frontmatter
109136
of your notes from inside of notes. They will update instantly to reflect changes to the frontmatter made by input
@@ -115,15 +142,4 @@
115142
<ViewFieldExampleComponent declaration={example} plugin={plugin}></ViewFieldExampleComponent>
116143
{/each}
117144
{/each}
118-
119-
<h2>Bind Targets</h2>
120-
<p>
121-
<a href="https://mprojectscode.github.io/obsidian-meta-bind-plugin-docs/guides/inputfields/#binding-to-metadata"
122-
>Bind Targets</a
123-
>
124-
let the plugin know what frontmatter properties to bind
125-
<a href={DocsUtils.linkToInputFields()}>Input Fields</a>
126-
and
127-
<a href={DocsUtils.linkToViewFields()}>View Fields</a> to.
128-
</p>
129145
</div>

packages/obsidian/src/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { registerCm5HLModes } from 'packages/obsidian/src/cm6/Cm5_Modes';
2020
import { createMarkdownRenderChildWidgetEditorPlugin } from 'packages/obsidian/src/cm6/Cm6_ViewPlugin';
2121
import { DependencyManager } from 'packages/obsidian/src/dependencies/DependencyManager';
2222
import { Version } from 'packages/obsidian/src/dependencies/Version';
23-
import { FaqView, MB_FAQ_VIEW_TYPE } from 'packages/obsidian/src/faq/FaqView';
23+
import { PlaygroundView, MB_PLAYGROUND_VIEW_TYPE } from 'packages/obsidian/src/playground/PlaygroundView';
2424
import { MetaBindSettingTab } from 'packages/obsidian/src/settings/SettingsTab';
2525
import { ObsidianNotePosition } from 'packages/obsidian/src/ObsidianNotePosition';
2626
import { RenderChildType } from 'packages/core/src/config/APIConfigs';
@@ -98,7 +98,7 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
9898
registerCm5HLModes(this);
9999

100100
// misc
101-
this.registerView(MB_FAQ_VIEW_TYPE, leaf => new FaqView(leaf, this));
101+
this.registerView(MB_PLAYGROUND_VIEW_TYPE, leaf => new PlaygroundView(leaf, this));
102102
this.addStatusBarBuildIndicator();
103103

104104
if (this.settings.enableEditorRightClickMenu) {
@@ -259,18 +259,18 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
259259
});
260260

261261
this.addCommand({
262-
id: 'open-faq',
263-
name: 'Open Meta Bind FAQ',
262+
id: 'open-playground',
263+
name: 'Open Meta Bind Playground',
264264
callback: () => {
265-
void this.activateView(MB_FAQ_VIEW_TYPE);
265+
void this.activateView(MB_PLAYGROUND_VIEW_TYPE);
266266
},
267267
});
268268

269269
this.addCommand({
270270
id: 'open-help',
271271
name: 'Open Meta Bind Help',
272272
callback: () => {
273-
void this.activateView(MB_FAQ_VIEW_TYPE);
273+
void this.activateView(MB_PLAYGROUND_VIEW_TYPE);
274274
},
275275
});
276276

packages/obsidian/src/faq/FaqView.ts renamed to packages/obsidian/src/playground/PlaygroundView.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ItemView, type WorkspaceLeaf } from 'obsidian';
2-
import FaqComponent from 'packages/core/src/utils/faq/FaqComponent.svelte';
2+
import FaqComponent from 'packages/core/src/utils/playground/PlaygroundComponent.svelte';
33
import type MetaBindPlugin from 'packages/obsidian/src/main';
44

5-
export const MB_FAQ_VIEW_TYPE = 'mb-faq-view-type';
5+
export const MB_PLAYGROUND_VIEW_TYPE = 'mb-playground-view-type';
66

7-
export class FaqView extends ItemView {
7+
export class PlaygroundView extends ItemView {
88
component: FaqComponent | undefined;
99
plugin: MetaBindPlugin;
1010

@@ -14,11 +14,11 @@ export class FaqView extends ItemView {
1414
}
1515

1616
getViewType(): string {
17-
return MB_FAQ_VIEW_TYPE;
17+
return MB_PLAYGROUND_VIEW_TYPE;
1818
}
1919

2020
getDisplayText(): string {
21-
return 'Meta Bind FAQ';
21+
return 'Meta Bind Playground';
2222
}
2323

2424
// eslint-disable-next-line @typescript-eslint/require-await

packages/obsidian/src/settings/SettingsTab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type App, ButtonComponent, PluginSettingTab, Setting } from 'obsidian';
22
import { DEFAULT_SETTINGS, weekdays } from 'packages/core/src/Settings';
33
import { DocsUtils } from 'packages/core/src/utils/DocsUtils';
4-
import { MB_FAQ_VIEW_TYPE } from 'packages/obsidian/src/faq/FaqView';
4+
import { MB_PLAYGROUND_VIEW_TYPE } from 'packages/obsidian/src/playground/PlaygroundView';
55
import type MetaBindPlugin from 'packages/obsidian/src/main';
66
import { MetaBindBuild } from 'packages/obsidian/src/main';
77
import { ButtonTemplatesSettingModal } from 'packages/obsidian/src/settings/buttonTemplateSetting/ButtonTemplatesSettingModal';
@@ -46,7 +46,7 @@ export class MetaBindSettingTab extends PluginSettingTab {
4646
.addButton(cb => {
4747
cb.setButtonText('Open FAQ');
4848
cb.onClick(() => {
49-
void this.plugin.activateView(MB_FAQ_VIEW_TYPE);
49+
void this.plugin.activateView(MB_PLAYGROUND_VIEW_TYPE);
5050
});
5151
})
5252
.addButton(cb => {

0 commit comments

Comments
 (0)