2020-11-25 18:56:09 +01:00
|
|
|
import { getBuiltin } from "components/userInterface/assetParsing/createProps"
|
2020-08-12 17:28:19 +02:00
|
|
|
import { uuid } from "./uuid"
|
2020-08-13 12:50:12 +02:00
|
|
|
import getNewComponentName from "./getNewComponentName"
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-12-07 16:27:46 +01:00
|
|
|
/**
|
|
|
|
* Find the parent component of the passed in child.
|
|
|
|
* @param {Object} rootProps - props to search for the parent in
|
|
|
|
* @param {String|Object} child - id of the child or the child itself to find the parent of
|
|
|
|
*/
|
|
|
|
export const findParent = (rootProps, child) => {
|
2020-06-01 13:12:25 +02:00
|
|
|
let parent
|
2020-12-01 17:22:06 +01:00
|
|
|
walkProps(rootProps, (props, breakWalk) => {
|
2020-06-01 13:12:25 +02:00
|
|
|
if (
|
2020-12-01 17:22:06 +01:00
|
|
|
props._children &&
|
|
|
|
(props._children.includes(child) ||
|
|
|
|
props._children.some(c => c._id === child))
|
2020-06-01 13:12:25 +02:00
|
|
|
) {
|
2020-12-01 17:22:06 +01:00
|
|
|
parent = props
|
2020-06-01 13:12:25 +02:00
|
|
|
breakWalk()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
export const walkProps = (props, action, cancelToken = null) => {
|
|
|
|
cancelToken = cancelToken || { cancelled: false }
|
|
|
|
action(props, () => {
|
|
|
|
cancelToken.cancelled = true
|
|
|
|
})
|
|
|
|
|
|
|
|
if (props._children) {
|
|
|
|
for (let child of props._children) {
|
|
|
|
if (cancelToken.cancelled) return
|
|
|
|
walkProps(child, action, cancelToken)
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-02 12:11:53 +02:00
|
|
|
|
2020-11-06 13:31:47 +01:00
|
|
|
export const generateNewIdsForComponent = (
|
|
|
|
component,
|
|
|
|
state,
|
|
|
|
changeName = true
|
|
|
|
) =>
|
2020-11-05 12:44:18 +01:00
|
|
|
walkProps(component, prop => {
|
|
|
|
prop._id = uuid()
|
|
|
|
if (changeName) prop._instanceName = getNewComponentName(prop, state)
|
2020-08-12 17:28:19 +02:00
|
|
|
})
|
2020-08-13 15:02:15 +02:00
|
|
|
|
|
|
|
export const getComponentDefinition = (state, name) =>
|
|
|
|
name.startsWith("##") ? getBuiltin(name) : state.components[name]
|
2020-10-17 19:20:06 +02:00
|
|
|
|
|
|
|
export const findChildComponentType = (node, typeToFind) => {
|
|
|
|
// Stop recursion if invalid props
|
|
|
|
if (!node || !typeToFind) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop recursion if this element matches
|
|
|
|
if (node._component === typeToFind) {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check if any children match
|
|
|
|
// Stop recursion if no valid children to process
|
|
|
|
const children = node._children || (node.props && node.props._children)
|
|
|
|
if (!children || !children.length) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse and check each child component
|
|
|
|
for (let child of children) {
|
|
|
|
const childResult = findChildComponentType(child, typeToFind)
|
|
|
|
if (childResult) {
|
|
|
|
return childResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here then no children were valid
|
|
|
|
return null
|
|
|
|
}
|