From ae44eca7645a8435cfe53cc132f2807f330d21a2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 18 Jan 2022 15:24:42 +0000 Subject: [PATCH] Fixing issue #4064 - making it possible to delete JSON fields. --- .../DataTable/modals/JSONSchemaModal.svelte | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/JSONSchemaModal.svelte b/packages/builder/src/components/backend/DataTable/modals/JSONSchemaModal.svelte index a575d0c288..1d0266100b 100644 --- a/packages/builder/src/components/backend/DataTable/modals/JSONSchemaModal.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/JSONSchemaModal.svelte @@ -9,6 +9,7 @@ Select, Body, Layout, + ActionButton, } from "@budibase/bbui" import { onMount, createEventDispatcher } from "svelte" import { FIELDS } from "constants/backend" @@ -20,8 +21,8 @@ let dispatcher = createEventDispatcher() let mode = "Form" let fieldCount = 0 - let fieldKeys = {}, - fieldTypes = {} + let fieldKeys = [], + fieldTypes = [] let keyValueOptions = [ { label: "String", value: FIELDS.STRING.type }, { label: "Number", value: FIELDS.NUMBER.type }, @@ -50,27 +51,48 @@ if (!schema) { schema = {} } - let i = 0 - for (let [key, value] of Object.entries(schema)) { - fieldKeys[i] = key - fieldTypes[i] = value.type - i++ + // find the entries which aren't in the list + const schemaEntries = Object.entries(schema).filter( + ([key]) => !fieldKeys.includes(key) + ) + for (let [key, value] of schemaEntries) { + fieldKeys.push(key) + fieldTypes.push(value.type) } - fieldCount = i + fieldCount = fieldKeys.length } function saveSchema() { - for (let i of Object.keys(fieldKeys)) { - const key = fieldKeys[i] + const newSchema = {} + for (let [index, key] of fieldKeys.entries()) { // they were added to schema, rather than generated - if (!schema[key]) { - schema[key] = { - type: fieldTypes[i], - } + newSchema[key] = { + ...schema[key], + type: fieldTypes[index], } } + dispatcher("save", { schema: newSchema, json }) + schema = newSchema + } - dispatcher("save", { schema, json }) + function removeKey(index) { + const keyToRemove = fieldKeys[index] + if (fieldKeys[index + 1] != null) { + fieldKeys[index] = fieldKeys[index + 1] + fieldTypes[index] = fieldTypes[index + 1] + } + fieldKeys.splice(index, 1) + fieldTypes.splice(index, 1) + fieldCount-- + if (json) { + try { + const parsed = JSON.parse(json) + delete parsed[keyToRemove] + json = JSON.stringify(parsed, null, 2) + } catch (err) { + // json not valid, ignore + } + } } onMount(() => { @@ -97,6 +119,7 @@ getOptionValue={field => field.value} getOptionLabel={field => field.label} /> + removeKey(i)} /> {/each}
@@ -118,9 +141,9 @@