2020-02-10 16:51:09 +01:00
|
|
|
import { isString, isUndefined } from "lodash/fp"
|
2020-05-02 16:29:10 +02:00
|
|
|
import { TYPE_MAP } from "./types"
|
2020-02-03 10:24:25 +01:00
|
|
|
import { assign } from "lodash"
|
2020-04-01 12:55:21 +02:00
|
|
|
import { uuid } from "builderStore/uuid"
|
2019-08-14 23:11:59 +02:00
|
|
|
|
2020-05-03 12:33:20 +02:00
|
|
|
export const getBuiltin = _component => {
|
|
|
|
const { props } = createProps({ _component })
|
2020-02-18 11:32:00 +01:00
|
|
|
|
|
|
|
return {
|
2020-05-03 12:33:20 +02:00
|
|
|
_component,
|
|
|
|
name: "Screenslot",
|
2020-02-18 11:32:00 +01:00
|
|
|
props,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:41:44 +01:00
|
|
|
/**
|
|
|
|
* @param {object} componentDefinition - component definition from a component library
|
|
|
|
* @param {object} derivedFromProps - extra props derived from a components given props.
|
|
|
|
* @return {object} the fully created properties for the component, and any property parsing errors
|
|
|
|
*/
|
2020-01-18 00:06:42 +01:00
|
|
|
export const createProps = (componentDefinition, derivedFromProps) => {
|
2020-02-18 16:41:44 +01:00
|
|
|
const errorOccurred = (propName, error) => errors.push({ propName, error })
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const props = {
|
2020-02-10 16:51:09 +01:00
|
|
|
_id: uuid(),
|
2020-05-02 16:29:10 +02:00
|
|
|
_component: componentDefinition._component,
|
2020-05-22 16:30:29 +02:00
|
|
|
_styles: { normal: {}, hover: {}, active: {}, selected: {} },
|
2020-05-18 12:01:09 +02:00
|
|
|
_code: "",
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const errors = []
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-05-07 15:59:06 +02:00
|
|
|
if (!componentDefinition._component) {
|
2020-02-18 16:41:44 +01:00
|
|
|
errorOccurred("_component", "Component name not supplied")
|
2020-05-07 15:59:06 +02:00
|
|
|
}
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
for (let propName in componentDefinition.props) {
|
|
|
|
const parsedPropDef = parsePropDef(componentDefinition.props[propName])
|
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
if (parsedPropDef.error) {
|
2020-05-02 16:29:10 +02:00
|
|
|
errors.push({ propName, error: parsedPropDef.error })
|
2020-05-07 11:53:34 +02:00
|
|
|
} else {
|
2020-05-02 16:29:10 +02:00
|
|
|
props[propName] = parsedPropDef
|
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (derivedFromProps) {
|
|
|
|
assign(props, derivedFromProps)
|
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (componentDefinition.children !== false && isUndefined(props._children)) {
|
|
|
|
props._children = []
|
|
|
|
}
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return {
|
|
|
|
props,
|
|
|
|
errors,
|
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:51:45 +01:00
|
|
|
export const makePropsSafe = (componentDefinition, props) => {
|
|
|
|
const safeProps = createProps(componentDefinition, props).props
|
|
|
|
for (let propName in safeProps) {
|
|
|
|
props[propName] = safeProps[propName]
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let propName in props) {
|
|
|
|
if (safeProps[propName] === undefined) {
|
|
|
|
delete props[propName]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:08:51 +01:00
|
|
|
if (!props._styles) {
|
2020-05-22 16:30:29 +02:00
|
|
|
props._styles = { normal: {}, hover: {}, active: {}, selected: {} }
|
2020-02-20 18:08:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-14 12:51:45 +01:00
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:52:08 +02:00
|
|
|
const parsePropDef = propDef => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const error = message => ({ error: message, propDef })
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (isString(propDef)) {
|
2020-05-02 16:29:10 +02:00
|
|
|
if (!TYPE_MAP[propDef]) return error(`Type ${propDef} is not recognised.`)
|
2020-01-24 12:32:13 +01:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
return TYPE_MAP[propDef].default
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
const type = TYPE_MAP[propDef.type]
|
|
|
|
if (!type) return error(`Type ${propDef.type} is not recognised.`)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return propDef.default
|
2019-07-19 13:52:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 12:32:13 +01:00
|
|
|
export const arrayElementComponentName = (parentComponentName, arrayPropName) =>
|
2020-02-03 10:24:25 +01:00
|
|
|
`${parentComponentName}:${arrayPropName}`
|