Skip to content

Commit 780a934

Browse files
committed
fix error with button builder
1 parent 8af9c34 commit 780a934

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

exampleVault/Input Fields/List.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ list4:
1919
list5:
2020
- "[[Other/Example Notes/Example Note with Embeds.md|Example Note with Embeds]]"
2121
- "[[Other/Example Notes/Example Note with Callouts.md|Example Note with Callouts]]"
22-
list6:
22+
list6:
23+
- as
2324
---
2425

2526
### List

packages/core/src/modals/modalContents/buttonBuilder/ButtonBuilderModalComponent.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@
5252
buttonMountable?.unmount();
5353
if (buttonEl) {
5454
DomHelpers.empty(buttonEl);
55-
buttonMountable = new ButtonField(plugin, buttonConfig, '', RenderChildType.BLOCK, undefined, false, true);
55+
buttonMountable = new ButtonField(
56+
plugin,
57+
$state.snapshot(buttonConfig),
58+
'',
59+
RenderChildType.BLOCK,
60+
undefined,
61+
false,
62+
true,
63+
);
5664
buttonMountable.mount(buttonEl);
5765
}
5866
});

packages/obsidian/src/cm6/Cm6_Util.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export enum MB_WidgetType {
88
HIGHLIGHT = 'highlight',
99
}
1010

11+
export interface MB_WidgetSpec {
12+
mb_widgetType: MB_WidgetType;
13+
mb_unload?: () => void;
14+
}
15+
1116
export class Cm6_Util {
1217
/**
1318
* Checks if a selection overlaps with a given range.
@@ -78,6 +83,14 @@ export class Cm6_Util {
7883
return exists;
7984
}
8085

86+
/**
87+
* Checks if a decoration of a given type exists in a given range.
88+
*
89+
* @param decorations
90+
* @param widgetType
91+
* @param from
92+
* @param to
93+
*/
8194
static existsDecorationOfTypeBetween(
8295
decorations: DecorationSet,
8396
widgetType: MB_WidgetType,
@@ -86,8 +99,8 @@ export class Cm6_Util {
8699
): boolean {
87100
let exists = false;
88101
decorations.between(from, to, (_from, _to, decoration) => {
89-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
90-
if (decoration.spec.mb_widgetType === widgetType) {
102+
const spec = decoration.spec as MB_WidgetSpec;
103+
if (spec.mb_widgetType === widgetType) {
91104
exists = true;
92105
}
93106
});

packages/obsidian/src/cm6/Cm6_ViewPlugin.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { summary } from 'itertools-ts/es';
77
import type { TFile } from 'obsidian';
88
import { Component, editorLivePreviewField } from 'obsidian';
99
import type { InlineFieldType } from 'packages/core/src/config/APIConfigs';
10-
import { Cm6_Util, MB_WidgetType } from 'packages/obsidian/src/cm6/Cm6_Util';
10+
import type {MB_WidgetSpec} from 'packages/obsidian/src/cm6/Cm6_Util';
11+
import { Cm6_Util, MB_WidgetType } from 'packages/obsidian/src/cm6/Cm6_Util';
1112
import type MetaBindPlugin from 'packages/obsidian/src/main';
1213

1314
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -68,6 +69,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
6869
*/
6970
updateWidgets(view: EditorView): void {
7071
// remove all decorations that are not visible and call unload manually
72+
// this is needed because otherwise some decorations are not unloaded correctly
7173
this.decorations = this.decorations.update({
7274
filter: (fromA, toA, decoration) => {
7375
const inVisibleRange = summary.anyMatch(view.visibleRanges, range =>
@@ -76,11 +78,12 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
7678

7779
if (inVisibleRange) {
7880
return true;
79-
}
81+
} else {
82+
const spec = decoration.spec as MB_WidgetSpec;
8083

81-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
82-
decoration.spec.mb_unload?.();
83-
return false;
84+
spec.mb_unload?.();
85+
return false;
86+
}
8487
},
8588
});
8689

@@ -135,17 +138,13 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
135138
filterFrom: from,
136139
filterTo: to,
137140
filter: (_from, _to, decoration) => {
138-
if (widgetTypeToKeep) {
139-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
140-
const widgetType = decoration.spec.mb_widgetType;
141+
const spec = decoration.spec as MB_WidgetSpec;
141142

142-
if (widgetType === widgetTypeToKeep) {
143-
return true;
144-
}
143+
if (widgetTypeToKeep && spec.mb_widgetType === widgetTypeToKeep) {
144+
return true;
145145
}
146146

147-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
148-
decoration.spec.mb_unload?.();
147+
spec.mb_unload?.();
149148
return false;
150149
},
151150
});
@@ -171,11 +170,12 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
171170
const from = node.from - 1;
172171
const to = node.to + 1;
173172

174-
// check if the decoration already exists and only add it if it does not exist
173+
// we check if there already is a decoration of the same type in the range
175174
if (Cm6_Util.existsDecorationOfTypeBetween(this.decorations, widgetType, from, to)) {
176175
return;
177176
}
178177

178+
// we can only render widgets if we have a current file
179179
const currentFile = Cm6_Util.getCurrentFile(view);
180180
if (!currentFile) {
181181
return;
@@ -189,6 +189,7 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
189189
currentFile,
190190
);
191191
const newDecorations = Array.isArray(newDecoration) ? newDecoration : [newDecoration];
192+
// the render widget function might return an empty array if the widget is not supposed to be rendered
192193
if (newDecorations.length === 0) {
193194
return;
194195
}
@@ -221,15 +222,22 @@ export function createMarkdownRenderChildWidgetEditorPlugin(plugin: MetaBindPlug
221222
// node is inline code
222223
if (props.has('inline-code') && !props.has('formatting')) {
223224
// check for selection or cursor overlap
224-
const selection = view.state.selection;
225-
const hasSelectionOverlap = Cm6_Util.checkSelectionOverlap(selection, node.from - 1, node.to + 1);
225+
const hasSelectionOverlap = Cm6_Util.checkSelectionOverlap(
226+
view.state.selection,
227+
node.from - 1,
228+
node.to + 1,
229+
);
226230
const content = this.readNode(view, node.from, node.to);
227231
const isLivePreview = this.isLivePreview(view.state);
232+
// if we are in live preview mode, we only render the widget if there is no selection overlap
233+
// otherwise the user has it's cursor within the bounds of the code for the field and we do syntax highlighting
234+
// if we are not in live preview, so in source mode, we always do syntax highlighting
235+
const shouldRenderField = !hasSelectionOverlap && isLivePreview;
228236

229237
return {
230-
shouldRender: !hasSelectionOverlap && isLivePreview,
231-
shouldHighlight:
232-
(hasSelectionOverlap || !isLivePreview) && plugin.settings.enableSyntaxHighlighting,
238+
shouldRender: shouldRenderField,
239+
// we need to also check that the user has highlighting enabled in the settings
240+
shouldHighlight: !shouldRenderField && plugin.settings.enableSyntaxHighlighting,
233241
content: content.content,
234242
widgetType: content.widgetType,
235243
};

0 commit comments

Comments
 (0)