1
0
Fork 0
mirror of synced 2024-09-19 10:48:30 +12:00
budibase/packages/client/src/stores/builder.js
2022-06-15 11:07:46 +01:00

82 lines
2.1 KiB
JavaScript

import { writable, get } from "svelte/store"
import { devToolsStore } from "./devTools.js"
const dispatchEvent = (type, data = {}) => {
window.parent.postMessage({ type, data })
}
const createBuilderStore = () => {
const initialState = {
inBuilder: false,
layout: null,
screen: null,
selectedComponentId: null,
editMode: false,
previewId: null,
previewType: null,
selectedPath: [],
theme: null,
customTheme: null,
previewDevice: "desktop",
isDragging: false,
}
const store = writable(initialState)
const actions = {
selectComponent: id => {
if (id === get(store).selectedComponentId) {
return
}
store.update(state => ({
...state,
editMode: false,
selectedComponentId: id,
}))
devToolsStore.actions.setAllowSelection(false)
dispatchEvent("select-component", { id })
},
updateProp: (prop, value) => {
dispatchEvent("update-prop", { prop, value })
},
deleteComponent: id => {
dispatchEvent("delete-component", { id })
},
duplicateComponent: id => {
dispatchEvent("duplicate-component", { id })
},
notifyLoaded: () => {
dispatchEvent("preview-loaded")
},
setSelectedPath: path => {
store.update(state => ({ ...state, selectedPath: path }))
},
moveComponent: (componentId, destinationComponentId, mode) => {
dispatchEvent("move-component", {
componentId,
destinationComponentId,
mode,
})
},
setDragging: dragging => {
if (dragging === get(store).isDragging) {
return
}
store.update(state => ({ ...state, isDragging: dragging }))
},
setEditMode: enabled => {
if (enabled === get(store).editMode) {
return
}
store.update(state => ({ ...state, editMode: enabled }))
},
highlightSetting: setting => {
dispatchEvent("highlight-setting", { setting })
},
}
return {
...store,
set: state => store.set({ ...initialState, ...state }),
actions,
}
}
export const builderStore = createBuilderStore()