Skip to content

Commit

Permalink
fix #179; fix #180; better zod errors and api validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mProjectsCode committed Jan 9, 2024
1 parent 116917e commit c9d9555
Show file tree
Hide file tree
Showing 31 changed files with 371 additions and 167 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Obsidian Meta Bind Changelog

# Unreleased

- Improved validation errors for buttons and the API
- Removed deprecated input fields that had names in snake_case. Use the camelCase variants instead. The snake_case variants were deprecated since version `0.6.0`.

# 0.11.0

New Features
Expand Down
Binary file modified bun.lockb
Binary file not shown.
22 changes: 18 additions & 4 deletions exampleVault/Advanced Examples/Activity Tracker.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
---
activities:
- activity: sudying
status: 0
from: 02:02
to: 03:02
- {}
- {}
---

```js-engine
Expand All @@ -19,5 +17,21 @@ const columns = [
];
mb.createTable(container, context.file.path, component, bindTarget, tableHead, columns);
```

```js-engine
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
const bindTarget = mb.createBindTarget('activities');
const tableHead = ['From', 'To', 'Activity', 'Status'];
const columns = [
mb.inputField.createInputFieldDeclarationFromString('INPUT[time:scope^from]'),
mb.inputField.createInputFieldDeclarationFromString('INPUT[time:scope^to]'),
mb.inputField.createInputFieldDeclarationFromString('INPUT[inlineSelect(option(youtube), option(sudying), option(linch)):scope^activity]'),
mb.inputField.createInputFieldDeclarationFromString('INPUT[inlineSelect(option(-, unproductive), option(0, normal), option(+, productive)):scope^status]')
];
mb.createTable(container, context.file.path, component, bindTarget, tableHead, columns);
```
15 changes: 13 additions & 2 deletions exampleVault/Button Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,20 @@ hidden: false
id: ""
style: default
actions:
- type: sl
ms: 1000
- type: sleep
- type: command
command: obsidian-meta-bind-plugin:open-help
```


```meta-bind-button
label: Test
hidden: asdasd
id: ""
style: default
actions:
- type: command
command: obsidian-meta-bind-plugin:open-help
```
6 changes: 5 additions & 1 deletion exampleVault/Input Fields/Progress Bar.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
progress1: -6
progress2: 0.2
progress3: 4
progress3: 2
progress4: 3.6
---

```meta-bind
Expand All @@ -16,4 +17,7 @@ INPUT[progressBar(showcase, minValue(0), maxValue(1), stepSize(0.1)):progress2]
INPUT[progressBar(showcase, minValue(0), maxValue(10), stepSize(-1)):progress3]
```

```meta-bind
INPUT[progressBar(showcase, minValue(0), maxValue(10), stepSize(0.1)):progress4]
```

10 changes: 9 additions & 1 deletion exampleVault/Input Fields/Slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
slider1: 51
slider2: 2
slider3: 233
slider4: 0.1
---

### Simple Slider
Expand All @@ -16,7 +17,7 @@ INPUT[slider(showcase):slider1]
INPUT[slider(addLabels, showcase):slider1]
```

### Slider with custom min max values
### Slider with Custom Min Max Values

```meta-bind
INPUT[slider(addLabels, minValue(-20), maxValue(20), showcase):slider2]
Expand All @@ -25,3 +26,10 @@ INPUT[slider(addLabels, minValue(-20), maxValue(20), showcase):slider2]
```meta-bind
INPUT[slider(addLabels, minValue(0), maxValue(1000), showcase):slider3]
```

### Slider with Custom Step Size

```meta-bind
INPUT[slider(addLabels, minValue(0), maxValue(10), stepSize(0.1), showcase):slider4]
```

3 changes: 2 additions & 1 deletion exampleVault/Input Fields/Toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ INPUT[toggle(showcase):toggle1]

```meta-bind
INPUT[toggle(showcase, onValue(1), offValue(0), defaultValue(1)):toggle2]
```
```

13 changes: 13 additions & 0 deletions extraTypes/obsidian-ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ declare module 'obsidian' {
removeCommand: (commandId: string) => void;
};
}

interface Menu {
dom: HTMLElement;
items: MenuItem[];
onMouseOver: (evt: MouseEvent) => void;
}

interface MenuItem {
callback: () => void;
dom: HTMLElement;
setSubmenu: () => Menu;
disabled: boolean;
}
}

declare global {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@lemons_dev/parsinom": "^0.0.12",
"itertools-ts": "^1.27.0",
"mathjs": "^12.0.0",
"zod": "^3.22.4"
"zod": "^3.22.4",
"zod-validation-error": "^2.1.0"
}
}
66 changes: 66 additions & 0 deletions src/EditorMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { type Editor, type Menu, stringifyYaml } from 'obsidian';
import type MetaBindPlugin from './main';
import { createInputFieldInsertExamples, createViewFieldInsertExamples } from './faq/InputFieldExamples';
import { ButtonBuilderModal } from './fields/button/ButtonBuilderModal';

export function createEditorMenu(menu: Menu, editor: Editor, plugin: MetaBindPlugin): void {
const inputFieldExamples = createInputFieldInsertExamples(plugin);
const viewFieldExamples = createViewFieldInsertExamples(plugin);

menu.addItem(mbItem => {
mbItem.setTitle('Meta Bind');
mbItem.setIcon('blocks');

const mbSubmenu = mbItem.setSubmenu();

mbSubmenu.addItem(ipfItem => {
ipfItem.setTitle('Input Field');

const ipfSubmenu = ipfItem.setSubmenu();

for (const [type, declaration] of inputFieldExamples) {
ipfSubmenu.addItem(item => {
item.setTitle(type);
item.onClick(() => insetAtCursor(editor, declaration));
});
}
});

mbSubmenu.addItem(vfItem => {
vfItem.setTitle('View Field');

const vfSubmenu = vfItem.setSubmenu();

for (const [type, declaration] of viewFieldExamples) {
vfSubmenu.addItem(item => {
item.setTitle(type);
item.onClick(() => insetAtCursor(editor, declaration));
});
}
});

mbSubmenu.addItem(inlineButtonItem => {
inlineButtonItem.setTitle('Inline Button');
inlineButtonItem.onClick(() => {
insetAtCursor(editor, '`BUTTON[example-id]`');
});
});

mbSubmenu.addItem(buttonItem => {
buttonItem.setTitle('Button');
buttonItem.onClick(() => {
new ButtonBuilderModal(
plugin,
config => {
insetAtCursor(editor, `\`\`\`meta-bind-button\n${stringifyYaml(config)}\n\`\`\``);
},
'Insert',
).open();
});
});
});
}

function insetAtCursor(editor: Editor, text: string): void {
editor.replaceSelection(text);
}
Loading

0 comments on commit c9d9555

Please sign in to comment.