Skip to content

Commit e5a448b

Browse files
committed
- Fix so that the Obsidian Data View Filter shows a value selector correctly.
1 parent e5353f3 commit e5a448b

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Rock.JavaScript.Obsidian/Framework/FieldTypes/dataViewsField.partial.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { defineAsyncComponent } from "@Obsidian/Utility/component";
3737
import { FieldTypeBase } from "./fieldType";
3838

3939
export const enum ConfigurationValueKey {
40-
EntityTypeName = "entityTypeName"
40+
EntityType = "entityType"
4141
}
4242

4343
// The edit component can be quite large, so load it only as needed.
@@ -69,4 +69,4 @@ export class DataViewsFieldType extends FieldTypeBase {
6969
public override isFilterable(): boolean {
7070
return false;
7171
}
72-
}
72+
}

Rock.JavaScript.Obsidian/Framework/FieldTypes/dataViewsFieldComponents.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ export const EditComponent = defineComponent({
1717
setup(props, { emit }) {
1818
const internalValue = ref({} as ListItemBag);
1919

20+
// The selected Entity Type.
2021
const entityTypeGuid = computed((): string | null | undefined => {
21-
const entityType = JSON.parse(props.configurationValues[ConfigurationValueKey.EntityTypeName] ?? "{}") as ListItemBag;
22+
const entityType = JSON.parse(props.configurationValues[ConfigurationValueKey.EntityType] ?? "{}") as ListItemBag;
2223
return entityType?.value;
2324
});
2425

2526
watch(() => props.modelValue, () => {
26-
internalValue.value = JSON.parse(props.modelValue || "{}");
27+
internalValue.value = JSON.parse(props.modelValue || "null");
2728
}, { immediate: true });
2829

2930
watch(() => internalValue.value, () => {
@@ -56,7 +57,31 @@ export const ConfigurationComponent = defineComponent({
5657
],
5758

5859
setup(props, { emit }) {
59-
const entityType = ref({} as ListItemBag);
60+
const entityType = ref<ListItemBag>();
61+
62+
// Compute a consistent entity type string value to compare against.
63+
const entityTypeStringFromProps = computed((): string => {
64+
const propsString = props.modelValue[ConfigurationValueKey.EntityType];
65+
if (!propsString) {
66+
return "{}";
67+
}
68+
69+
try {
70+
// It's possible for propsString to be "null", in which case we
71+
// want to default to an empty object for consistent comparison.
72+
return JSON.stringify(
73+
JSON.parse(propsString) ?? {}
74+
);
75+
}
76+
catch {
77+
return "{}";
78+
}
79+
});
80+
81+
// Compute a consistent entity type object value to compare against.
82+
const entityTypeFromProps = computed((): ListItemBag => {
83+
return JSON.parse(entityTypeStringFromProps.value);
84+
});
6085

6186
/**
6287
* Update the modelValue property if any value of the dictionary has
@@ -71,10 +96,10 @@ export const ConfigurationComponent = defineComponent({
7196

7297
// Construct the new value that will be emitted if it is different
7398
// than the current value.
74-
newValue[ConfigurationValueKey.EntityTypeName] = JSON.stringify(entityType.value ?? "");
99+
newValue[ConfigurationValueKey.EntityType] = JSON.stringify(entityType.value ?? {});
75100

76101
// Compare the new value and the old value.
77-
const anyValueChanged = newValue[ConfigurationValueKey.EntityTypeName] !== (props.modelValue[ConfigurationValueKey.EntityTypeName] ?? "");
102+
const anyValueChanged = newValue[ConfigurationValueKey.EntityType] !== entityTypeStringFromProps.value;
78103

79104
// If any value changed then emit the new model value.
80105
if (anyValueChanged) {
@@ -102,12 +127,12 @@ export const ConfigurationComponent = defineComponent({
102127
// Watch for changes coming in from the parent component and update our
103128
// data to match the new information.
104129
watch(() => [props.modelValue, props.configurationProperties], () => {
105-
entityType.value = JSON.parse(props.modelValue[ConfigurationValueKey.EntityTypeName] || "{}");
130+
entityType.value = entityTypeFromProps.value;
106131
}, {
107132
immediate: true
108133
});
109134

110-
watch(entityType, val => maybeUpdateConfiguration(ConfigurationValueKey.EntityTypeName, JSON.stringify(val ?? "")));
135+
watch(entityType, () => maybeUpdateConfiguration(ConfigurationValueKey.EntityType, JSON.stringify(entityType.value ?? {})));
111136

112137
return {
113138
entityType
@@ -119,4 +144,4 @@ export const ConfigurationComponent = defineComponent({
119144
<EntityTypePicker label="Entity Type" v-model="entityType" help="The type of entity to display data views for." />
120145
</div>
121146
`
122-
});
147+
});

0 commit comments

Comments
 (0)