Update data binding generation to match how context is provided by components, respecting branching due to local context
This commit is contained in:
parent
41d32fdad1
commit
d6bf33bce7
|
@ -7,6 +7,9 @@ import {
|
||||||
findHBSBlocks,
|
findHBSBlocks,
|
||||||
} from "@budibase/string-templates"
|
} from "@budibase/string-templates"
|
||||||
import { capitalise } from "helpers"
|
import { capitalise } from "helpers"
|
||||||
|
import { Constants } from "@budibase/frontend-core"
|
||||||
|
|
||||||
|
const { ContextScopes } = Constants
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively searches for a specific component ID
|
* Recursively searches for a specific component ID
|
||||||
|
@ -263,11 +266,59 @@ export const getComponentName = component => {
|
||||||
if (component == null) {
|
if (component == null) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
const components = get(store)?.components || {}
|
const components = get(store)?.components || {}
|
||||||
const componentDefinition = components[component._component] || {}
|
const componentDefinition = components[component._component] || {}
|
||||||
const name =
|
return componentDefinition.friendlyName || componentDefinition.name || ""
|
||||||
componentDefinition.friendlyName || componentDefinition.name || ""
|
}
|
||||||
|
|
||||||
return name
|
/**
|
||||||
|
* Recurses through the component tree and builds a tree of contexts provided
|
||||||
|
* by components.
|
||||||
|
*/
|
||||||
|
export const buildContextTree = (
|
||||||
|
rootComponent,
|
||||||
|
tree = { root: [] },
|
||||||
|
currentBranch = "root"
|
||||||
|
) => {
|
||||||
|
// Sanity check
|
||||||
|
if (!rootComponent) {
|
||||||
|
return tree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process this component's contexts
|
||||||
|
const def = store.actions.components.getDefinition(rootComponent._component)
|
||||||
|
if (def?.context) {
|
||||||
|
tree[currentBranch].push(rootComponent._id)
|
||||||
|
const contexts = Array.isArray(def.context) ? def.context : [def.context]
|
||||||
|
|
||||||
|
// If we provide local context, start a new branch for our children
|
||||||
|
if (contexts.some(context => context.scope === ContextScopes.Local)) {
|
||||||
|
currentBranch = rootComponent._id
|
||||||
|
tree[rootComponent._id] = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process children
|
||||||
|
if (rootComponent._children) {
|
||||||
|
rootComponent._children.forEach(child => {
|
||||||
|
buildContextTree(child, tree, currentBranch)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return tree
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a lookup map of which content branch all components in a component
|
||||||
|
* tree are inside.
|
||||||
|
*/
|
||||||
|
export const buildContextTreeLookupMap = rootComponent => {
|
||||||
|
const tree = buildContextTree(rootComponent)
|
||||||
|
let map = {}
|
||||||
|
Object.entries(tree).forEach(([branch, ids]) => {
|
||||||
|
ids.forEach(id => {
|
||||||
|
map[id] = branch
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return map
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import {
|
import {
|
||||||
|
buildContextTree,
|
||||||
|
buildContextTreeLookupMap,
|
||||||
findAllComponents,
|
findAllComponents,
|
||||||
findAllMatchingComponents,
|
findAllMatchingComponents,
|
||||||
findComponent,
|
findComponent,
|
||||||
|
@ -20,11 +22,13 @@ import {
|
||||||
encodeJSBinding,
|
encodeJSBinding,
|
||||||
} from "@budibase/string-templates"
|
} from "@budibase/string-templates"
|
||||||
import { TableNames } from "../constants"
|
import { TableNames } from "../constants"
|
||||||
import { JSONUtils } from "@budibase/frontend-core"
|
import { JSONUtils, Constants } from "@budibase/frontend-core"
|
||||||
import ActionDefinitions from "components/design/settings/controls/ButtonActionEditor/manifest.json"
|
import ActionDefinitions from "components/design/settings/controls/ButtonActionEditor/manifest.json"
|
||||||
import { environment, licensing } from "stores/portal"
|
import { environment, licensing } from "stores/portal"
|
||||||
import { convertOldFieldFormat } from "components/design/settings/controls/FieldConfiguration/utils"
|
import { convertOldFieldFormat } from "components/design/settings/controls/FieldConfiguration/utils"
|
||||||
|
|
||||||
|
const { ContextScopes } = Constants
|
||||||
|
|
||||||
// Regex to match all instances of template strings
|
// Regex to match all instances of template strings
|
||||||
const CAPTURE_VAR_INSIDE_TEMPLATE = /{{([^}]+)}}/g
|
const CAPTURE_VAR_INSIDE_TEMPLATE = /{{([^}]+)}}/g
|
||||||
const CAPTURE_VAR_INSIDE_JS = /\$\("([^")]+)"\)/g
|
const CAPTURE_VAR_INSIDE_JS = /\$\("([^")]+)"\)/g
|
||||||
|
@ -214,20 +218,27 @@ export const getComponentContexts = (
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
let map = {}
|
let map = {}
|
||||||
|
const componentPath = findComponentPath(asset.props, componentId)
|
||||||
|
const componentPathIds = componentPath.map(component => component._id)
|
||||||
|
const contextTreeLookupMap = buildContextTreeLookupMap(asset.props)
|
||||||
|
|
||||||
// Processes all contexts exposed by a component
|
// Processes all contexts exposed by a component
|
||||||
const processContexts = scope => component => {
|
const processContexts = scope => component => {
|
||||||
const def = store.actions.components.getDefinition(component._component)
|
// Sanity check
|
||||||
|
const def = store.actions.components.getDefinition(component?._component)
|
||||||
if (!def?.context) {
|
if (!def?.context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!map[component._id]) {
|
|
||||||
map[component._id] = {
|
// Filter out global contexts not in the same branch.
|
||||||
component,
|
// Global contexts are only valid if their branch root is an ancestor of
|
||||||
definition: def,
|
// this component.
|
||||||
contexts: [],
|
const branch = contextTreeLookupMap[component._id]
|
||||||
}
|
if (branch !== "root" && !componentPathIds.includes(branch)) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process all contexts provided by this component
|
||||||
const contexts = Array.isArray(def.context) ? def.context : [def.context]
|
const contexts = Array.isArray(def.context) ? def.context : [def.context]
|
||||||
contexts.forEach(context => {
|
contexts.forEach(context => {
|
||||||
// Ensure type matches
|
// Ensure type matches
|
||||||
|
@ -235,7 +246,7 @@ export const getComponentContexts = (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Ensure scope matches
|
// Ensure scope matches
|
||||||
let contextScope = context.scope || "global"
|
let contextScope = context.scope || ContextScopes.Global
|
||||||
if (contextScope !== scope) {
|
if (contextScope !== scope) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -243,17 +254,23 @@ export const getComponentContexts = (
|
||||||
if (!isContextCompatibleWithComponent(context, component)) {
|
if (!isContextCompatibleWithComponent(context, component)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (!map[component._id]) {
|
||||||
|
map[component._id] = {
|
||||||
|
component,
|
||||||
|
definition: def,
|
||||||
|
contexts: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
map[component._id].contexts.push(context)
|
map[component._id].contexts.push(context)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process all global contexts
|
// Process all global contexts
|
||||||
const allComponents = findAllComponents(asset.props)
|
const allComponents = findAllComponents(asset.props)
|
||||||
allComponents.forEach(processContexts("global"))
|
allComponents.forEach(processContexts(ContextScopes.Global))
|
||||||
|
|
||||||
// Process all local contexts
|
// Process all local contexts in the immediate tree
|
||||||
const localComponents = findComponentPath(asset.props, componentId)
|
componentPath.forEach(processContexts(ContextScopes.Local))
|
||||||
localComponents.forEach(processContexts("local"))
|
|
||||||
|
|
||||||
// Exclude self if required
|
// Exclude self if required
|
||||||
if (!options?.includeSelf) {
|
if (!options?.includeSelf) {
|
||||||
|
|
|
@ -4720,7 +4720,8 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"context": {
|
"context": {
|
||||||
"type": "schema"
|
"type": "schema",
|
||||||
|
"scope": "local"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"daterangepicker": {
|
"daterangepicker": {
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
import { getContext } from "svelte"
|
import { getContext } from "svelte"
|
||||||
import Placeholder from "./Placeholder.svelte"
|
import Placeholder from "./Placeholder.svelte"
|
||||||
import Container from "./Container.svelte"
|
import Container from "./Container.svelte"
|
||||||
import { ContextScopes } from "constants"
|
|
||||||
|
const { Provider, ContextScopes } = getContext("sdk")
|
||||||
|
const component = getContext("component")
|
||||||
|
|
||||||
export let dataProvider
|
export let dataProvider
|
||||||
export let noRowsMessage
|
export let noRowsMessage
|
||||||
|
@ -12,9 +14,6 @@
|
||||||
export let gap
|
export let gap
|
||||||
export let scope = ContextScopes.Local
|
export let scope = ContextScopes.Local
|
||||||
|
|
||||||
const { Provider } = getContext("sdk")
|
|
||||||
const component = getContext("component")
|
|
||||||
|
|
||||||
$: rows = dataProvider?.rows ?? []
|
$: rows = dataProvider?.rows ?? []
|
||||||
$: loaded = dataProvider?.loaded ?? true
|
$: loaded = dataProvider?.loaded ?? true
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
<script>
|
<script>
|
||||||
import { getContext, setContext, onDestroy } from "svelte"
|
import { getContext, setContext, onDestroy } from "svelte"
|
||||||
import { dataSourceStore, createContextStore } from "stores"
|
import { dataSourceStore, createContextStore } from "stores"
|
||||||
import { ActionTypes, ContextScopes } from "constants"
|
import { ActionTypes } from "constants"
|
||||||
import { generate } from "shortid"
|
import { generate } from "shortid"
|
||||||
|
|
||||||
|
const { ContextScopes } = getContext("sdk")
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
export let actions
|
export let actions
|
||||||
export let key
|
export let key
|
||||||
|
|
|
@ -12,10 +12,5 @@ export const ActionTypes = {
|
||||||
ScrollTo: "ScrollTo",
|
ScrollTo: "ScrollTo",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ContextScopes = {
|
|
||||||
Local: "local",
|
|
||||||
Global: "global",
|
|
||||||
}
|
|
||||||
|
|
||||||
export const DNDPlaceholderID = "dnd-placeholder"
|
export const DNDPlaceholderID = "dnd-placeholder"
|
||||||
export const ScreenslotType = "screenslot"
|
export const ScreenslotType = "screenslot"
|
||||||
|
|
|
@ -23,12 +23,12 @@ import { getAction } from "utils/getAction"
|
||||||
import Provider from "components/context/Provider.svelte"
|
import Provider from "components/context/Provider.svelte"
|
||||||
import Block from "components/Block.svelte"
|
import Block from "components/Block.svelte"
|
||||||
import BlockComponent from "components/BlockComponent.svelte"
|
import BlockComponent from "components/BlockComponent.svelte"
|
||||||
import { ActionTypes, ContextScopes } from "./constants"
|
import { ActionTypes } from "./constants"
|
||||||
import { fetchDatasourceSchema } from "./utils/schema.js"
|
import { fetchDatasourceSchema } from "./utils/schema.js"
|
||||||
import { getAPIKey } from "./utils/api.js"
|
import { getAPIKey } from "./utils/api.js"
|
||||||
import { enrichButtonActions } from "./utils/buttonActions.js"
|
import { enrichButtonActions } from "./utils/buttonActions.js"
|
||||||
import { processStringSync, makePropSafe } from "@budibase/string-templates"
|
import { processStringSync, makePropSafe } from "@budibase/string-templates"
|
||||||
import { fetchData, LuceneUtils } from "@budibase/frontend-core"
|
import { fetchData, LuceneUtils, Constants } from "@budibase/frontend-core"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
API,
|
API,
|
||||||
|
@ -57,7 +57,7 @@ export default {
|
||||||
fetchDatasourceSchema,
|
fetchDatasourceSchema,
|
||||||
fetchData,
|
fetchData,
|
||||||
LuceneUtils,
|
LuceneUtils,
|
||||||
ContextScopes,
|
ContextScopes: Constants.ContextScopes,
|
||||||
getAPIKey,
|
getAPIKey,
|
||||||
enrichButtonActions,
|
enrichButtonActions,
|
||||||
processStringSync,
|
processStringSync,
|
||||||
|
|
|
@ -106,3 +106,8 @@ export const Themes = [
|
||||||
export const EventPublishType = {
|
export const EventPublishType = {
|
||||||
ENV_VAR_UPGRADE_PANEL_OPENED: "environment_variable_upgrade_panel_opened",
|
ENV_VAR_UPGRADE_PANEL_OPENED: "environment_variable_upgrade_panel_opened",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const ContextScopes = {
|
||||||
|
Local: "local",
|
||||||
|
Global: "global",
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue