Skip to content

Commit f6e3cd3

Browse files
committed
progress bar input; inline select input; impoved arument parser error handling; various fixes
1 parent 7f812cb commit f6e3cd3

26 files changed

+527
-99
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
select: a
3+
---
4+
5+
`INPUT[inlineSelect(option(a), option(b)):select]`
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
progress1: -7
3+
---
4+
5+
```meta-bind
6+
INPUT[progressBar(minValue(-10), maxValue(3)):progress1]
7+
```
8+
9+
10+

src/MDRCManager.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { AbstractMDRC } from './renderChildren/AbstractMDRC';
22

33
export class MDRCManager {
4-
activeMDRCs: AbstractMDRC[];
4+
activeMDRCs: Map<string, AbstractMDRC>;
55

66
constructor() {
7-
this.activeMDRCs = [];
7+
this.activeMDRCs = new Map<string, AbstractMDRC>();
88
}
99

10-
unload() {
11-
for (const activeMDRC of this.activeMDRCs) {
12-
activeMDRC.unload();
10+
unload(): void {
11+
for (const mdrc of this.activeMDRCs.values()) {
12+
console.debug(`meta-bind | MDRCManager >> unregistered MDRC ${mdrc.uuid}`);
13+
mdrc.unload();
1314
}
1415
}
1516

1617
registerMDRC(mdrc: AbstractMDRC): void {
1718
console.debug(`meta-bind | MDRCManager >> registered MDRC ${mdrc.uuid}`);
18-
this.activeMDRCs.push(mdrc);
19+
this.activeMDRCs.set(mdrc.uuid, mdrc);
1920
}
2021

2122
unregisterMDRC(mdrc: AbstractMDRC): void {
2223
console.debug(`meta-bind | MDRCManager >> unregistered MDRC ${mdrc.uuid}`);
23-
this.activeMDRCs = this.activeMDRCs.filter(x => x.uuid !== mdrc.uuid);
24+
this.activeMDRCs.delete(mdrc.uuid);
2425
}
2526
}

src/api/API.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ export class API {
6262
throw new MetaBindParsingError(ErrorLevel.CRITICAL, 'failed to create input field declaration', `input field type '${inputFieldType}' is invalid`);
6363
}
6464

65+
const errorCollection = new ErrorCollection('InputFieldDeclaration');
66+
6567
return {
6668
declaration: undefined,
6769
fullDeclaration: undefined,
6870
inputFieldType: inputFieldType,
69-
argumentContainer: this.inputFieldParser.parseArguments(inputFieldType, inputFieldArguments),
71+
argumentContainer: this.inputFieldParser.parseArguments(inputFieldType, inputFieldArguments, errorCollection),
7072
isBound: false,
7173
bindTarget: '',
72-
errorCollection: new ErrorCollection('InputFieldDeclaration'),
74+
errorCollection: errorCollection,
7375
} as InputFieldDeclaration;
7476
}
7577

src/cm6/Cm6_Util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EditorSelection, EditorState } from '@codemirror/state';
22
import MetaBindPlugin from '../main';
3-
import { editorInfoField, TFile } from 'obsidian';
3+
import { Component, editorInfoField, TFile } from 'obsidian';
44
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';
55
import { InputFieldWidget, MarkdownRenderChildWidget, MBWidgetType, ViewFieldWidget } from './Cm6_Widgets';
66
import { DecorationSet, EditorView } from '@codemirror/view';
@@ -29,12 +29,13 @@ export class Cm6_Util {
2929
widgetType: MBWidgetType,
3030
content: string,
3131
filePath: string,
32+
component: Component,
3233
plugin: MetaBindPlugin
3334
): MarkdownRenderChildWidget<AbstractMDRC> | undefined {
3435
if (widgetType === MBWidgetType.INPUT_FIELD_WIDGET) {
35-
return new InputFieldWidget(content, filePath, plugin);
36+
return new InputFieldWidget(content, filePath, component, plugin);
3637
} else if (widgetType === MBWidgetType.VIEW_FIELD_WIDGET) {
37-
return new ViewFieldWidget(content, filePath, plugin);
38+
return new ViewFieldWidget(content, filePath, component, plugin);
3839
}
3940

4041
return undefined;

src/cm6/Cm6_ViewPlugin.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Decoration, DecorationSet, EditorView, ViewPlugin, ViewUpdate } from '@codemirror/view';
2-
import { Range } from '@codemirror/state';
2+
import { Range, RangeSet } from '@codemirror/state';
33
import { syntaxTree, tokenClassNodeProp } from '@codemirror/language';
44
import { SyntaxNode } from '@lezer/common';
55
import { Component, editorLivePreviewField, TFile } from 'obsidian';
66
import MetaBindPlugin from '../main';
7-
import { MBWidgetType } from './Cm6_Widgets';
7+
import { MarkdownRenderChildWidget, MBWidgetType } from './Cm6_Widgets';
88
import { Cm6_Util } from './Cm6_Util';
99

1010
export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlugin) {
@@ -19,10 +19,9 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
1919
this.decorations = this.renderWidgets(view) ?? Decoration.none;
2020
}
2121

22-
update(update: ViewUpdate) {
22+
update(update: ViewUpdate): void {
2323
// only activate in LP and not source mode
2424
if (!update.state.field(editorLivePreviewField)) {
25-
console.log('not LP');
2625
this.decorations = Decoration.none;
2726
return;
2827
}
@@ -40,7 +39,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
4039
}
4140
}
4241

43-
updateTree(view: EditorView) {
42+
updateTree(view: EditorView): void {
4443
for (const { from, to } of view.visibleRanges) {
4544
syntaxTree(view.state).iterate({
4645
from,
@@ -69,7 +68,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
6968
}
7069
}
7170

72-
removeDecoration(node: SyntaxNode) {
71+
removeDecoration(node: SyntaxNode): void {
7372
this.decorations.between(node.from - 1, node.to + 1, (from, to, value) => {
7473
this.decorations = this.decorations.update({
7574
filterFrom: from,
@@ -79,7 +78,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
7978
});
8079
}
8180

82-
addDecoration(node: SyntaxNode, view: EditorView, content: string, widgetType: MBWidgetType) {
81+
addDecoration(node: SyntaxNode, view: EditorView, content: string, widgetType: MBWidgetType): void {
8382
const from = node.from - 1;
8483
const to = node.to + 1;
8584

@@ -93,7 +92,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
9392
return;
9493
}
9594

96-
const newDecoration = this.renderWidget(node, widgetType, content, currentFile)?.value;
95+
const newDecoration: Decoration | undefined = this.renderWidget(node, widgetType, content, currentFile)?.value;
9796
if (!newDecoration) {
9897
return;
9998
}
@@ -145,10 +144,10 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
145144
};
146145
}
147146

148-
renderWidgets(view: EditorView) {
147+
renderWidgets(view: EditorView): RangeSet<Decoration> | undefined {
149148
const currentFile = Cm6_Util.getCurrentFile(view);
150149
if (!currentFile) {
151-
return;
150+
return undefined;
152151
}
153152

154153
const widgets: Range<Decoration>[] = [];
@@ -184,8 +183,8 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
184183
return Decoration.set(widgets, true);
185184
}
186185

187-
renderWidget(node: SyntaxNode, widgetType: MBWidgetType, content: string, currentFile: TFile) {
188-
const widget = Cm6_Util.constructMarkdownRenderChildWidget(widgetType, content, currentFile.path, plugin);
186+
renderWidget(node: SyntaxNode, widgetType: MBWidgetType, content: string, currentFile: TFile): Range<Decoration> | undefined {
187+
const widget = Cm6_Util.constructMarkdownRenderChildWidget(widgetType, content, currentFile.path, this.component, plugin);
189188
if (!widget) {
190189
return;
191190
}
@@ -195,7 +194,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
195194
}).range(node.from - 1, node.to + 1);
196195
}
197196

198-
destroy() {
197+
destroy(): void {
199198
this.component.unload();
200199
}
201200
},

src/cm6/Cm6_Widgets.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ import MetaBindPlugin from '../main';
33
import { AbstractMDRC } from '../renderChildren/AbstractMDRC';
44
import { ViewFieldMDRC } from '../renderChildren/ViewFieldMDRC';
55
import { InputFieldMDRC, RenderChildType } from '../renderChildren/InputFieldMDRC';
6+
import { Component } from 'obsidian';
67

78
export abstract class MarkdownRenderChildWidget<T extends AbstractMDRC> extends WidgetType {
89
content: string;
910
filePath: string;
11+
parentComponent: Component;
1012
plugin: MetaBindPlugin;
1113
renderChild?: T;
1214

13-
constructor(content: string, filePath: string, plugin: MetaBindPlugin) {
15+
constructor(content: string, filePath: string, component: Component, plugin: MetaBindPlugin) {
1416
super();
1517
this.content = content;
1618
this.filePath = filePath;
19+
this.parentComponent = component;
1720
this.plugin = plugin;
1821
}
1922

@@ -30,6 +33,8 @@ export abstract class MarkdownRenderChildWidget<T extends AbstractMDRC> extends
3033
this.renderChild = this.createRenderChild(div);
3134
this.renderChild.load();
3235

36+
this.parentComponent.addChild(this.renderChild);
37+
3338
return div;
3439
}
3540

src/inputFieldArguments/InputFieldArgumentContainer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export class InputFieldArgumentContainer {
1010
}
1111

1212
validate(): void {
13-
let map: Record<string, number> = {};
13+
const map: Record<string, number> = {};
1414
for (const inputFieldArgumentType of Object.values(InputFieldArgumentType)) {
1515
map[inputFieldArgumentType] = 0;
1616
}
1717

1818
for (const argument of this.arguments) {
1919
map[argument.identifier] += 1;
2020
if (map[argument.identifier] > 1 && !argument.allowMultiple) {
21-
throw new MetaBindParsingError(ErrorLevel.ERROR, 'failed to validate argument container', `argument '${argument.identifier}' does not allow duplicates`);
21+
throw new MetaBindParsingError(ErrorLevel.CRITICAL, 'failed to validate argument container', `argument '${argument.identifier}' does not allow duplicates`);
2222
}
2323
}
2424
}

src/inputFieldArguments/arguments/AddLabelsInputFieldArgument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputField
33

44
export class AddLabelsInputFieldArgument extends AbstractInputFieldArgument {
55
identifier: InputFieldArgumentType = InputFieldArgumentType.ADD_LABELS;
6-
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER];
6+
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER, InputFieldType.PROGRESS_BAR];
77
value: boolean = true;
88
requiresValue: boolean = false;
99
allowMultiple: boolean = false;

src/inputFieldArguments/arguments/MaxValueInputFieldArgument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ErrorLevel, MetaBindParsingError } from '../../utils/errors/MetaBindErr
44

55
export class MaxValueInputFieldArgument extends AbstractInputFieldArgument {
66
identifier: InputFieldArgumentType = InputFieldArgumentType.MAX_VALUE;
7-
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER];
7+
allowedInputFields: InputFieldType[] = [InputFieldType.SLIDER, InputFieldType.PROGRESS_BAR];
88
value: number = 100;
99
requiresValue: boolean = true;
1010
allowMultiple: boolean = false;

0 commit comments

Comments
 (0)