Skip to content

Commit e61a73e

Browse files
committed
readd list suggester ipf
1 parent 3ffcdaa commit e61a73e

File tree

10 files changed

+177
-45
lines changed

10 files changed

+177
-45
lines changed

CHANGELOG.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# Obsidian Meta Bind Changelog
22

3-
43
# Unreleased
54

65
Breaking Changes
76

8-
- Renamed all CSS variables to start with `mb-` for consistency
7+
- Renamed all CSS variables to start with `mb-` for consistency
98

109
New Features
1110

12-
- View Fields can now render as plain text or as markdown e.g.
13-
- `VIEW[{a} * {b}]` or `VIEW[{a} * {b}][math]` this does math as before
14-
- `VIEW[this is plain text][text]` this renders as plain text
15-
- `VIEW[this is **markdown**][text(renderMarkdown)]` this renders as markdown
16-
- `VIEW[this is hidden][text(hidden)]` this does not render
17-
- View Fields can now save their computed value to another frontmatter property
18-
- `VIEW[{a} * {b}][math:c]` will save `a * b` in `c`
19-
- Meta Bind Table, a way to build an expandable table from an array of objects where each cell is an input or view field
20-
- currently only accessible via the JavaScript API
21-
- New Input Field type `listSuggester` a list where new items are added with a suggester
22-
- New argument for `suggester`, `useLinks(true | false)` that can be used to turn off the usage of links
23-
- New `stepSize` argument for `slider` and `progressBar`, thanks @dbarenholz
24-
- New `limit` argument for `text` and `textArea`, thanks @dbarenholz
11+
- View Fields can now render as plain text or as markdown e.g.
12+
- `VIEW[{a} * {b}]` or `VIEW[{a} * {b}][math]` this does math as before
13+
- `VIEW[this is plain text][text]` this renders as plain text
14+
- `VIEW[this is **markdown**][text(renderMarkdown)]` this renders as markdown
15+
- `VIEW[this is hidden][text(hidden)]` this does not render
16+
- View Fields can now save their computed value to another frontmatter property
17+
- `VIEW[{a} * {b}][math:c]` will save `a * b` in `c`
18+
- Meta Bind Table, a way to build an expandable table from an array of objects where each cell is an input or view field
19+
- currently only accessible via the JavaScript API
20+
- New Input Field type `listSuggester` a list where new items are added with a suggester
21+
- New argument for `suggester`, `useLinks(true | false)` that can be used to turn off the usage of links
22+
- New `stepSize` argument for `slider` and `progressBar`, thanks @dbarenholz
23+
- New `limit` argument for `text` and `textArea`, thanks @dbarenholz
2524

2625
Changes
27-
- Rewrote all Input Fields to fix Input Fields sometimes overriding frontmatter when changing the frontmatter manually in edit mode
26+
27+
- Rewrote all Input Fields to fix Input Fields sometimes overriding frontmatter when changing the frontmatter manually in edit mode
2828

2929
# 0.6.3
3030

exampleVault/Input Fields/List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ list:
55
- berries
66
list2:
77
- "[[Other/Example Notes/Example Note with Image.md|Example Note with Image]]"
8-
- something
98
- "[[Other/Example Notes/Example Note with Callouts.md|Example Note with Callouts]]"
9+
- something
1010
list3:
1111
- Example Note with Image
1212
- Example Note with Callouts

src/inputFields/_new/NewInputFieldFactory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ProgressBarIPF } from './fields/ProgressBar/ProgressBarIPF';
1616
import { InlineSelectIPF } from './fields/InlineSelect/InlineSelectIPF';
1717
import { ImageSuggesterIPF } from './fields/ImageSuggester/ImageSuggesterIPF';
1818
import { ListIPF } from './fields/List/ListIPF';
19+
import { ListSuggesterIPF } from './fields/ListSuggester/ListSuggesterIPF';
1920

2021
export type NewInputField =
2122
| ToggleIPF
@@ -31,7 +32,8 @@ export type NewInputField =
3132
| ProgressBarIPF
3233
| InlineSelectIPF
3334
| ImageSuggesterIPF
34-
| ListIPF;
35+
| ListIPF
36+
| ListSuggesterIPF;
3537

3638
export class NewInputFieldFactory {
3739
plugin: IPlugin;
@@ -45,7 +47,7 @@ export class NewInputFieldFactory {
4547
this.checkRenderChildTypeAllowed(type, renderChildType);
4648
}
4749

48-
// Skipped: Date, Time, Image Suggester
50+
// Skipped: Date, Time, List Suggester
4951

5052
if (type === InputFieldType.TOGGLE) {
5153
return new ToggleIPF(renderChild);
@@ -81,6 +83,8 @@ export class NewInputFieldFactory {
8183
return new ImageSuggesterIPF(renderChild);
8284
} else if (type === InputFieldType.LIST) {
8385
return new ListIPF(renderChild);
86+
} else if (type === InputFieldType.LIST_SUGGESTER) {
87+
return new ListSuggesterIPF(renderChild);
8488
}
8589

8690
return undefined;

src/inputFields/_new/fields/ImageSuggester/ImageSuggesterComponent.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<style>
2424
.mb-image-suggest-input {
2525
background: var(--background-secondary);
26-
border-radius: var(--meta-bind-plugin-border-radius);
27-
border: var(--meta-bind-plugin-border-width) solid var(--background-modifier-border);
26+
border-radius: var(--mb-border-radius);
27+
border: var(--mb-border-width) solid var(--background-modifier-border);
2828
padding: var(--size-4-2);
2929
width: 100%;
3030
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<script lang='ts'>
2+
import { MBLiteral } from '../../../../utils/Utils';
3+
import {
4+
OptionInputFieldArgument
5+
} from '../../../../fieldArguments/inputFieldArguments/arguments/OptionInputFieldArgument';
6+
import { Button, TextInput } from 'obsidian-svelte';
7+
import Icon from '../../../../utils/Icon.svelte';
8+
import { isMdLink, parseMdLink } from '../../../../parsers/MarkdownLinkParser';
9+
import LinkComponent from '../../../../utils/LinkComponent.svelte';
10+
11+
export let value: MBLiteral[];
12+
export let showSuggester: () => void;
13+
export let onValueChange: (value: MBLiteral[]) => void;
14+
15+
export function setValue(v: MBLiteral[]): void {
16+
value = v;
17+
}
18+
19+
export function addValue(v: MBLiteral): void {
20+
value.push(v);
21+
22+
value = value;
23+
}
24+
25+
function remove(i: number) {
26+
value.splice(i, 1);
27+
// call with copy of array
28+
onValueChange(value);
29+
// tell svelte to update
30+
value = value;
31+
}
32+
33+
function suggest(event: MouseEvent): void {
34+
// don't fire the event if user clicked on the link
35+
if (!(event.target instanceof HTMLAnchorElement)) {
36+
showSuggester();
37+
}
38+
}
39+
40+
function suggestKey(event: KeyboardEvent): void {
41+
if (event.key === ' ') {
42+
showSuggester();
43+
}
44+
}
45+
</script>
46+
47+
<div class='mb-list-items'>
48+
{#each value as entry, i}
49+
<div class='mb-list-item'>
50+
{#if isMdLink(`${entry}`)}
51+
<span>
52+
<LinkComponent mdLink={parseMdLink(`${entry}`)}></LinkComponent>
53+
</span>
54+
{:else}
55+
<span>{entry}</span>
56+
{/if}
57+
<Button on:click={() => remove(i)}>
58+
<Icon iconName='x' />
59+
</Button>
60+
</div>
61+
{:else}
62+
<span class='mb-list-empty'>Empty</span>
63+
{/each}
64+
</div>
65+
<div class='mb-list-input'>
66+
<div
67+
class='mb-suggest-input'
68+
on:click={suggest}
69+
on:keydown={suggestKey}
70+
role='button'
71+
tabindex='0'>
72+
<div class='mb-suggest-text'>
73+
<span>Add Element...</span>
74+
</div>
75+
<Icon iconName='list' />
76+
</div>
77+
</div>
78+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NewAbstractInputField } from '../../NewAbstractInputField';
2+
import { type MBLiteral, parseUnknownToLiteralArray } from '../../../../utils/Utils';
3+
import { type InputFieldMDRC } from '../../../../renderChildren/InputFieldMDRC';
4+
import { type SvelteComponent } from 'svelte';
5+
import ListSuggesterComponent from './ListSuggesterComponent.svelte';
6+
import { openSuggesterModalForInputField } from '../Suggester/SuggesterHelper';
7+
8+
export class ListSuggesterIPF extends NewAbstractInputField<MBLiteral[], MBLiteral[]> {
9+
constructor(renderChild: InputFieldMDRC) {
10+
super(renderChild);
11+
}
12+
13+
protected filterValue(value: unknown): MBLiteral[] | undefined {
14+
return parseUnknownToLiteralArray(value);
15+
}
16+
17+
protected getFallbackDefaultValue(): MBLiteral[] {
18+
return [];
19+
}
20+
21+
protected getSvelteComponent(): typeof SvelteComponent {
22+
return ListSuggesterComponent;
23+
}
24+
25+
protected rawMapValue(value: MBLiteral[]): MBLiteral[] {
26+
return value;
27+
}
28+
29+
protected rawReverseMapValue(value: MBLiteral[]): MBLiteral[] | undefined {
30+
return value;
31+
}
32+
33+
protected getMountArgs(): Record<string, unknown> {
34+
return {
35+
showSuggester: () => this.openModal(),
36+
};
37+
}
38+
39+
openModal(): void {
40+
openSuggesterModalForInputField(this, selected => {
41+
const value = this.getInternalValue();
42+
value.push(selected.value);
43+
this.setInternalValue(value);
44+
});
45+
}
46+
}

src/inputFields/_new/fields/Suggester/SuggesterComponent.svelte

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,13 @@
4646
}
4747
</script>
4848

49-
<style>
50-
.suggest-input {
51-
background: var(--background-secondary);
52-
border-radius: var(--mb-border-radius);
53-
border: var(--mb-border-width) solid var(--background-modifier-border);
54-
padding: 5px 5px 5px 7px;
55-
cursor: pointer;
56-
position: relative;
57-
color: var(--text-normal);
58-
display: inline-flex;
59-
align-items: center;
60-
gap: 5px;
61-
}
62-
63-
.suggest-text {
64-
display: inline-block;
65-
}
66-
</style>
67-
6849
<div
69-
class='suggest-input'
50+
class='mb-suggest-input'
7051
on:click={openSuggester}
7152
on:keydown={openSuggesterOnKey}
7253
role='button'
7354
tabindex='0'>
74-
<div class='suggest-text'>
55+
<div class='mb-suggest-text'>
7556
{#if isLink}
7657
<LinkComponent bind:mdLink={mdLink}></LinkComponent>
7758
{:else}

src/inputFields/_new/fields/Suggester/SuggesterHelper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Notice } from 'obsidian';
66
import { InputFieldArgumentType } from '../../../../parsers/inputFieldParser/InputFieldConfigs';
77
import { SuggesterInputModal } from './SuggesterInputModal';
88
import { type SuggesterIPF } from './SuggesterIPF';
9+
import { ListSuggesterIPF } from '../ListSuggester/ListSuggesterIPF';
910

1011
export class SuggesterOption<T> {
1112
value: T;
@@ -64,7 +65,7 @@ export function getSuggesterOptions(
6465
return options;
6566
}
6667

67-
export function getSuggesterOptionsForInputField(inputField: SuggesterIPF): SuggesterOption<MBLiteral>[] {
68+
export function getSuggesterOptionsForInputField(inputField: SuggesterIPF | ListSuggesterIPF): SuggesterOption<MBLiteral>[] {
6869
const app = inputField.renderChild.plugin.app;
6970
const dv = getAPI(app);
7071
const optionArgs = inputField.renderChild.getArguments(InputFieldArgumentType.OPTION);
@@ -74,6 +75,9 @@ export function getSuggesterOptionsForInputField(inputField: SuggesterIPF): Sugg
7475
return getSuggesterOptions(dv, inputField.renderChild.filePath, optionArgs, optionQueryArgs, useLinksArgs === undefined || useLinksArgs.value);
7576
}
7677

77-
export function openSuggesterModalForInputField(inputField: SuggesterIPF, selectCallback: (selected: SuggesterOption<MBLiteral>) => void): void {
78+
export function openSuggesterModalForInputField(
79+
inputField: SuggesterIPF | ListSuggesterIPF,
80+
selectCallback: (selected: SuggesterOption<MBLiteral>) => void,
81+
): void {
7882
new SuggesterInputModal(inputField.renderChild.plugin.app, getSuggesterOptionsForInputField(inputField), selectCallback).open();
7983
}

src/inputFields/fields/listSuggest/ListSuggestInput.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<script src='ListSuggestInputField.ts'></script>
12
<script lang='ts'>
23
import Icon from '../../../utils/Icon.svelte';
34
import { Button } from 'obsidian-svelte';

styles.css

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ div.mb-view-wrapper {
6161
/* Text Input */
6262
.mb-content-limit-indicator {
6363
color: var(--text-muted);
64-
font-size: var(--font-ui-small);
64+
font-size: var(--font-ui-small);
6565
}
6666

6767
.mb-content-limit-indicator-overflow {
@@ -145,6 +145,24 @@ div.mb-view-wrapper {
145145
flex-grow: 1;
146146
}
147147

148+
/* Suggester Input */
149+
.mb-suggest-input {
150+
background: var(--background-secondary);
151+
border-radius: var(--mb-border-radius);
152+
border: var(--mb-border-width) solid var(--background-modifier-border);
153+
padding: 5px 5px 5px 7px;
154+
cursor: pointer;
155+
position: relative;
156+
color: var(--text-normal);
157+
display: inline-flex;
158+
align-items: center;
159+
gap: 5px;
160+
}
161+
162+
.mb-suggest-text {
163+
display: inline-block;
164+
}
165+
148166
/* Card */
149167
.mb-card {
150168
padding: var(--size-4-2);

0 commit comments

Comments
 (0)