Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

✨ During UI edit, strip out empty/defaults before save

Alicia Sykes747812971cc2
diff --git a/src/components/FormElements/SchemaForm.vue b/src/components/FormElements/SchemaForm.vue
index 2a8fe15e..fb53135e 100644
--- a/src/components/FormElements/SchemaForm.vue
+++ b/src/components/FormElements/SchemaForm.vue
@@ -7,7 +7,7 @@
<template v-else>
<JsonForms
:data="formData"
- :schema="schema"
+ :schema="renderSchema"
:uischema="uischema"
:renderers="renderers"
:validation-mode="validationMode"
@@ -43,6 +43,30 @@ addFormats(ajv);
* not gated behind focus (the vue-vanilla default). */
const jfConfig = Object.freeze({ showUnfocusedDescription: true });
+/* Strip oneOf/anyOf/allOf branches that are pure AJV constraints (e.g. just
+ * `{ required: [...] }`). vue-vanilla's OneOfRenderer otherwise tries to build
+ * a tab switcher from variants that have no renderable shape, leading to
+ * `indexedOneOfRenderInfos[selectedIndex] is undefined`. Renderable variants
+ * (those with properties/items/type/etc.) are kept intact. */
+const RENDERABLE_KEYS = ['properties', 'items', 'type', '$ref', 'enum', 'const'];
+const isRenderableVariant = (v) => v && typeof v === 'object'
+ && RENDERABLE_KEYS.some((k) => Object.prototype.hasOwnProperty.call(v, k));
+
+function sanitizeSchemaForRender(node) {
+ if (Array.isArray(node)) return node.map(sanitizeSchemaForRender);
+ if (!node || typeof node !== 'object') return node;
+ const out = {};
+ for (const [k, v] of Object.entries(node)) {
+ if ((k === 'oneOf' || k === 'anyOf' || k === 'allOf') && Array.isArray(v)) {
+ if (!v.some(isRenderableVariant)) continue;
+ out[k] = v.map(sanitizeSchemaForRender);
+ } else {
+ out[k] = sanitizeSchemaForRender(v);
+ }
+ }
+ return out;
+}
+
const dashyStyles = Object.fromEntries(
Object.entries(defaultStyles).map(([group, map]) => [
group,
@@ -77,6 +101,9 @@ export default {
fatalError: null,
};
},
+ computed: {
+ renderSchema() { return sanitizeSchemaForRender(this.schema); },
+ },
errorCaptured(err, _vm, info) {
/* Return false to stop Vue's default handler from also logging. */
this.fatalError = err?.message || 'unknown error';
diff --git a/src/components/InteractiveEditor/EditAppConfig.vue b/src/components/InteractiveEditor/EditAppConfig.vue
index 9bffac5e..e74f1ea7 100644
--- a/src/components/InteractiveEditor/EditAppConfig.vue
+++ b/src/components/InteractiveEditor/EditAppConfig.vue
@@ -32,6 +32,7 @@ import StoreKeys from '@/utils/StoreMutations';
import { modalNames } from '@/utils/config/defaults';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import safeClone from '@/utils/safeClone';
+import pruneSchemaDefaults from '@/utils/config/pruneSchemaDefaults';
import AccessError from '@/components/Configuration/AccessError';
import SaveCancelButtons from '@/components/InteractiveEditor/SaveCancelButtons';
@@ -59,7 +60,8 @@ export default {
},
saveToState() {
try {
- const patched = { ...this.$store.state.configSource, appConfig: this.formData };
+ const pruned = pruneSchemaDefaults(this.formData, this.schema);
+ const patched = { ...this.$store.state.configSource, appConfig: pruned };
this.$store.dispatch(StoreKeys.APPLY_EDITED_CONFIG, patched);
this.$store.commit(StoreKeys.SET_EDIT_MODE, true);
InfoHandler('App config updated', InfoKeys.EDITOR);
diff --git a/src/components/InteractiveEditor/EditMultiPages.vue b/src/components/InteractiveEditor/EditMultiPages.vue
index 1446fe21..d3890c1b 100644
--- a/src/components/InteractiveEditor/EditMultiPages.vue
+++ b/src/components/InteractiveEditor/EditMultiPages.vue
@@ -21,6 +21,7 @@ import StoreKeys from '@/utils/StoreMutations';
import { modalNames } from '@/utils/config/defaults';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import safeClone from '@/utils/safeClone';
+import pruneSchemaDefaults from '@/utils/config/pruneSchemaDefaults';
import SaveCancelButtons from '@/components/InteractiveEditor/SaveCancelButtons';
import AccessError from '@/components/Configuration/AccessError';
@@ -73,9 +74,11 @@ export default {
},
saveToState() {
try {
- this.$store.commit(StoreKeys.SET_PAGES, this.formData);
+ const pruned = pruneSchemaDefaults(this.formData, this.customSchema);
+ const pages = Array.isArray(pruned) ? pruned : [];
+ this.$store.commit(StoreKeys.SET_PAGES, pages);
this.$store.commit(StoreKeys.SET_EDIT_MODE, true);
- InfoHandler(`Pages list updated (${this.formData.length} page${this.formData.length === 1 ? '' : 's'})`, InfoKeys.EDITOR);
+ InfoHandler(`Pages list updated (${pages.length} page${pages.length === 1 ? '' : 's'})`, InfoKeys.EDITOR);
this.cancelEditing();
} catch (e) {
ErrorHandler('Failed to save pages list', e);
diff --git a/src/components/InteractiveEditor/EditPageInfo.vue b/src/components/InteractiveEditor/EditPageInfo.vue
index ba6d5ddf..8950b02e 100644
--- a/src/components/InteractiveEditor/EditPageInfo.vue
+++ b/src/components/InteractiveEditor/EditPageInfo.vue
@@ -21,6 +21,7 @@ import StoreKeys from '@/utils/StoreMutations';
import { modalNames } from '@/utils/config/defaults';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import safeClone from '@/utils/safeClone';
+import pruneSchemaDefaults from '@/utils/config/pruneSchemaDefaults';
import AccessError from '@/components/Configuration/AccessError';
import SaveCancelButtons from '@/components/InteractiveEditor/SaveCancelButtons';
@@ -46,7 +47,8 @@ export default {
},
saveToState() {
try {
- const patched = { ...this.$store.state.configSource, pageInfo: this.formData };
+ const pruned = pruneSchemaDefaults(this.formData, this.schema);
+ const patched = { ...this.$store.state.configSource, pageInfo: pruned };
this.$store.dispatch(StoreKeys.APPLY_EDITED_CONFIG, patched);
this.$store.commit(StoreKeys.SET_EDIT_MODE, true);
InfoHandler('Page info updated', InfoKeys.EDITOR);
diff --git a/src/components/InteractiveEditor/EditSection.vue b/src/components/InteractiveEditor/EditSection.vue
index 5aca4c2a..c90b1a97 100644
--- a/src/components/InteractiveEditor/EditSection.vue
+++ b/src/components/InteractiveEditor/EditSection.vue
@@ -22,6 +22,7 @@ import StoreKeys from '@/utils/StoreMutations';
import { modalNames } from '@/utils/config/defaults';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import safeClone from '@/utils/safeClone';
+import pruneSchemaDefaults from '@/utils/config/pruneSchemaDefaults';
import SaveCancelButtons from '@/components/InteractiveEditor/SaveCancelButtons';
import AccessError from '@/components/Configuration/AccessError';
@@ -82,7 +83,7 @@ export default {
saveSection() {
try {
/* Form only edits metadata, so preserve the live section's items array. */
- const payload = { ...this.sectionData };
+ const payload = pruneSchemaDefaults(this.sectionData, this.customSchema);
if (!this.isAddNew) {
const live = this.$store.getters.getSectionByIndex(this.sectionIndex);
if (live?.items) payload.items = live.items;
diff --git a/src/utils/config/pruneSchemaDefaults.js b/src/utils/config/pruneSchemaDefaults.js
new file mode 100644
index 00000000..04c3cefb
--- /dev/null
+++ b/src/utils/config/pruneSchemaDefaults.js
@@ -0,0 +1,50 @@
+/* Strip leaves that match the schema's `default` (or are empty) so saved YAML
+ * stays minimal. AJV's `useDefaults: true` in SchemaForm fills untouched
+ * fields with their schema defaults; without pruning they'd persist verbatim.
+ * Empty objects/arrays/strings are also dropped. */
+
+const DROP = Symbol('prune.drop');
+
+const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
+
+function deepEqualJson(a, b) {
+ if (a === b) return true;
+ if (typeof a !== typeof b) return false;
+ if (a === null || b === null) return false;
+ if (typeof a !== 'object') return false;
+ try { return JSON.stringify(a) === JSON.stringify(b); } catch { return false; }
+}
+
+function pruneNode(value, schema) {
+ if (schema && Object.prototype.hasOwnProperty.call(schema, 'default')
+ && deepEqualJson(value, schema.default)) {
+ return DROP;
+ }
+ if (Array.isArray(value)) {
+ const itemSchema = schema && schema.items;
+ const out = [];
+ for (const item of value) {
+ const pruned = pruneNode(item, itemSchema);
+ if (pruned !== DROP) out.push(pruned);
+ }
+ return out.length === 0 ? DROP : out;
+ }
+ if (isPlainObject(value)) {
+ const props = schema && schema.properties;
+ const out = {};
+ for (const [k, v] of Object.entries(value)) {
+ const childSchema = props && props[k];
+ const pruned = pruneNode(v, childSchema);
+ if (pruned !== DROP) out[k] = pruned;
+ }
+ return Object.keys(out).length === 0 ? DROP : out;
+ }
+ if (value === undefined || value === null || value === '') return DROP;
+ return value;
+}
+
+export default function pruneSchemaDefaults(data, schema) {
+ const result = pruneNode(data, schema);
+ if (result === DROP) return Array.isArray(data) ? [] : {};
+ return result;
+}