2020-08-13 15:02:15 +02:00
|
|
|
import {
|
|
|
|
makePropsSafe,
|
|
|
|
getBuiltin,
|
|
|
|
} from "components/userInterface/pagesParsing/createProps"
|
2020-06-01 13:12:25 +02:00
|
|
|
import api from "./api"
|
2020-06-02 12:11:53 +02:00
|
|
|
import { generate_screen_css } from "./generate_css"
|
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
|
|
|
|
|
|
|
export const selectComponent = (state, component) => {
|
|
|
|
const componentDef = component._component.startsWith("##")
|
|
|
|
? component
|
|
|
|
: state.components[component._component]
|
|
|
|
state.currentComponentInfo = makePropsSafe(componentDef, component)
|
|
|
|
state.currentView = "component"
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getParent = (rootProps, child) => {
|
|
|
|
let parent
|
|
|
|
walkProps(rootProps, (p, breakWalk) => {
|
|
|
|
if (
|
|
|
|
p._children &&
|
|
|
|
(p._children.includes(child) || p._children.some(c => c._id === child))
|
|
|
|
) {
|
|
|
|
parent = p
|
|
|
|
breakWalk()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
export const saveCurrentPreviewItem = s =>
|
|
|
|
s.currentFrontEndType === "page"
|
|
|
|
? savePage(s)
|
|
|
|
: saveScreenApi(s.currentPreviewItem, s)
|
|
|
|
|
|
|
|
export const savePage = async s => {
|
2020-10-12 15:31:17 +02:00
|
|
|
const pageName = s.currentPageName || "main"
|
|
|
|
const page = s.pages[pageName]
|
2020-06-01 13:12:25 +02:00
|
|
|
await api.post(`/_builder/api/${s.appId}/pages/${s.currentPageName}`, {
|
|
|
|
page: { componentLibraries: s.pages.componentLibraries, ...page },
|
|
|
|
uiFunctions: s.currentPageFunctions,
|
|
|
|
screens: page._screens,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const saveScreenApi = (screen, s) => {
|
|
|
|
api
|
|
|
|
.post(`/_builder/api/${s.appId}/pages/${s.currentPageName}/screen`, screen)
|
|
|
|
.then(() => savePage(s))
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:15:17 +02:00
|
|
|
export const renameCurrentScreen = (newname, state) => {
|
2020-06-15 17:01:24 +02:00
|
|
|
const oldname = state.currentPreviewItem.props._instanceName
|
|
|
|
state.currentPreviewItem.props._instanceName = newname
|
|
|
|
|
2020-06-12 13:15:17 +02:00
|
|
|
api.patch(
|
|
|
|
`/_builder/api/${state.appId}/pages/${state.currentPageName}/screen`,
|
|
|
|
{
|
|
|
|
oldname,
|
|
|
|
newname,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:12:25 +02:00
|
|
|
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-10-16 00:20:56 +02:00
|
|
|
export const regenerateCssForScreen = screen => {
|
|
|
|
screen._css = generate_screen_css([screen.props])
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:16:30 +02:00
|
|
|
export const regenerateCssForCurrentScreen = state => {
|
2020-10-16 00:20:56 +02:00
|
|
|
regenerateCssForScreen(state.currentPreviewItem)
|
2020-06-02 12:11:53 +02:00
|
|
|
return state
|
|
|
|
}
|
2020-08-12 17:28:19 +02:00
|
|
|
|
2020-10-09 12:58:46 +02:00
|
|
|
export const generateNewIdsForComponent = (c, state, changeName = true) =>
|
2020-08-12 17:28:19 +02:00
|
|
|
walkProps(c, p => {
|
|
|
|
p._id = uuid()
|
2020-10-09 12:58:46 +02:00
|
|
|
if (changeName) p._instanceName = getNewComponentName(p._component, 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]
|