Fixing the transition between readable and runtime bindings.

This commit is contained in:
mike12345567 2021-01-26 15:59:28 +00:00
parent 75b0e49978
commit 6219a66604
1 changed files with 19 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import { makePropSafe } from "@budibase/string-templates"
// Regex to match all instances of template strings
const CAPTURE_VAR_INSIDE_TEMPLATE = /{{([^}]+)}}/g
const INVALID_BINDING = "{{ invalid binding }}"
/**
* Gets all bindable data context fields and instance fields.
@ -178,14 +179,19 @@ export function readableToRuntimeBinding(bindableProperties, textWithBindings) {
}
const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_TEMPLATE) || []
let result = textWithBindings
boundValues.forEach(boundValue => {
for (let boundValue of boundValues) {
const binding = bindableProperties.find(({readableBinding}) => {
return boundValue === `{{ ${readableBinding} }}`
return boundValue.includes(readableBinding)
})
let newBoundValue = INVALID_BINDING
if (binding) {
result = result.replace(boundValue, `{{ ${binding.runtimeBinding} }}`)
newBoundValue = boundValue.replace(binding.readableBinding, binding.runtimeBinding)
}
result = result.replace(
boundValue,
newBoundValue
)
}
})
return result
}
@ -198,15 +204,18 @@ export function runtimeToReadableBinding(bindableProperties, textWithBindings) {
}
const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_TEMPLATE) || []
let result = textWithBindings
boundValues.forEach(boundValue => {
for (let boundValue of boundValues) {
const binding = bindableProperties.find(({ runtimeBinding }) => {
return boundValue === `{{ ${runtimeBinding} }}`
return boundValue.includes(runtimeBinding)
})
// Show invalid bindings as invalid rather than a long ID
let newBoundValue = INVALID_BINDING
if (binding) {
newBoundValue = boundValue.replace(binding.runtimeBinding, binding.readableBinding)
}
result = result.replace(
boundValue,
`{{ ${binding?.readableBinding ?? "Invalid binding"} }}`
newBoundValue
)
})
}
return result
}