Fixing issue #4064 - making it possible to delete JSON fields.

This commit is contained in:
mike12345567 2022-01-18 15:24:42 +00:00
parent ee365da5ed
commit ae44eca764
1 changed files with 40 additions and 17 deletions

View File

@ -9,6 +9,7 @@
Select, Select,
Body, Body,
Layout, Layout,
ActionButton,
} from "@budibase/bbui" } from "@budibase/bbui"
import { onMount, createEventDispatcher } from "svelte" import { onMount, createEventDispatcher } from "svelte"
import { FIELDS } from "constants/backend" import { FIELDS } from "constants/backend"
@ -20,8 +21,8 @@
let dispatcher = createEventDispatcher() let dispatcher = createEventDispatcher()
let mode = "Form" let mode = "Form"
let fieldCount = 0 let fieldCount = 0
let fieldKeys = {}, let fieldKeys = [],
fieldTypes = {} fieldTypes = []
let keyValueOptions = [ let keyValueOptions = [
{ label: "String", value: FIELDS.STRING.type }, { label: "String", value: FIELDS.STRING.type },
{ label: "Number", value: FIELDS.NUMBER.type }, { label: "Number", value: FIELDS.NUMBER.type },
@ -50,27 +51,48 @@
if (!schema) { if (!schema) {
schema = {} schema = {}
} }
let i = 0 // find the entries which aren't in the list
for (let [key, value] of Object.entries(schema)) { const schemaEntries = Object.entries(schema).filter(
fieldKeys[i] = key ([key]) => !fieldKeys.includes(key)
fieldTypes[i] = value.type )
i++ for (let [key, value] of schemaEntries) {
fieldKeys.push(key)
fieldTypes.push(value.type)
} }
fieldCount = i fieldCount = fieldKeys.length
} }
function saveSchema() { function saveSchema() {
for (let i of Object.keys(fieldKeys)) { const newSchema = {}
const key = fieldKeys[i] for (let [index, key] of fieldKeys.entries()) {
// they were added to schema, rather than generated // they were added to schema, rather than generated
if (!schema[key]) { newSchema[key] = {
schema[key] = { ...schema[key],
type: fieldTypes[i], 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(() => { onMount(() => {
@ -97,6 +119,7 @@
getOptionValue={field => field.value} getOptionValue={field => field.value}
getOptionLabel={field => field.label} getOptionLabel={field => field.label}
/> />
<ActionButton icon="Close" quiet on:click={() => removeKey(i)} />
</div> </div>
{/each} {/each}
<div class:add-field-btn={fieldCount !== 0}> <div class:add-field-btn={fieldCount !== 0}>
@ -118,9 +141,9 @@
<style> <style>
.horizontal { .horizontal {
display: grid; display: grid;
grid-template-columns: 30% 1fr; grid-template-columns: 30% 1fr 40px;
grid-gap: var(--spacing-s); grid-gap: var(--spacing-s);
align-items: center; align-items: end;
} }
.add-field-btn { .add-field-btn {