diff --git a/packages/builder/src/components/common/bindings/BindingPanel.svelte b/packages/builder/src/components/common/bindings/BindingPanel.svelte index f05f935226..49cbd434cf 100644 --- a/packages/builder/src/components/common/bindings/BindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/BindingPanel.svelte @@ -8,6 +8,7 @@ Tab, Body, Layout, + Button, } from "@budibase/bbui" import { createEventDispatcher, onMount } from "svelte" import { @@ -15,10 +16,15 @@ decodeJSBinding, encodeJSBinding, } from "@budibase/string-templates" - import { readableToRuntimeBinding } from "builderStore/dataBinding" + import { + readableToRuntimeBinding, + runtimeToReadableBinding, + } from "builderStore/dataBinding" import { handlebarsCompletions } from "constants/completions" import { addHBSBinding, addJSBinding } from "./utils" import CodeMirrorEditor from "components/common/CodeMirrorEditor.svelte" + import { convertToJS } from "@budibase/string-templates" + import { admin } from "stores/portal" const dispatch = createEventDispatcher() @@ -62,15 +68,24 @@ } } - // Adds a HBS helper to the expression - const addHelper = helper => { - hbsValue = addHBSBinding(hbsValue, getCaretPosition(), helper.text) - updateValue(hbsValue) + // Adds a JS/HBS helper to the expression + const addHelper = (helper, js) => { + let tempVal + const pos = getCaretPosition() + if (js) { + const decoded = decodeJSBinding(jsValue) + tempVal = jsValue = encodeJSBinding( + addJSBinding(decoded, pos, helper.text, { helper: true }) + ) + } else { + tempVal = hbsValue = addHBSBinding(hbsValue, pos, helper.text) + } + updateValue(tempVal) } // Adds a data binding to the expression - const addBinding = binding => { - if (usingJS) { + const addBinding = (binding, { forceJS } = {}) => { + if (usingJS || forceJS) { let js = decodeJSBinding(jsValue) js = addJSBinding(js, getCaretPosition(), binding.readableBinding) jsValue = encodeJSBinding(js) @@ -100,6 +115,26 @@ updateValue(jsValue) } + const convert = () => { + const runtime = readableToRuntimeBinding(bindings, hbsValue) + const runtimeJs = encodeJSBinding(convertToJS(runtime)) + jsValue = runtimeToReadableBinding(bindings, runtimeJs) + hbsValue = null + mode = "JavaScript" + addBinding("", { forceJS: true }) + } + + const getHelperExample = (helper, js) => { + let example = helper.example || "" + if (js) { + example = convertToJS(example).split("\n")[0].split("= ")[1] + if (example === "null;") { + example = "" + } + } + return example || "" + } + onMount(() => { valid = isValid(readableToRuntimeBinding(bindings, value)) }) @@ -135,18 +170,21 @@ {/if} {/each} - {#if filteredHelpers?.length && !usingJS} + {#if filteredHelpers?.length}
Helpers