2021-11-26 14:25:02 +01:00
|
|
|
import { writable, get } from "svelte/store"
|
2022-01-25 20:22:43 +01:00
|
|
|
import { API } from "api"
|
2022-02-24 16:36:21 +01:00
|
|
|
import { devToolsStore } from "./devTools.js"
|
2021-06-08 09:00:54 +02:00
|
|
|
|
2021-06-30 20:37:03 +02:00
|
|
|
const dispatchEvent = (type, data = {}) => {
|
2021-10-28 19:40:46 +02:00
|
|
|
window.parent.postMessage({ type, data })
|
2021-06-08 09:00:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
const createBuilderStore = () => {
|
|
|
|
const initialState = {
|
|
|
|
inBuilder: false,
|
2020-11-27 17:36:31 +01:00
|
|
|
layout: null,
|
2020-11-24 10:31:54 +01:00
|
|
|
screen: null,
|
2020-11-30 13:11:50 +01:00
|
|
|
selectedComponentId: null,
|
2021-10-28 13:43:31 +02:00
|
|
|
editMode: false,
|
2020-11-30 13:11:50 +01:00
|
|
|
previewId: null,
|
2021-01-06 11:11:56 +01:00
|
|
|
previewType: null,
|
2021-08-19 15:53:13 +02:00
|
|
|
selectedPath: [],
|
2021-09-08 10:40:25 +02:00
|
|
|
theme: null,
|
|
|
|
customTheme: null,
|
|
|
|
previewDevice: "desktop",
|
2021-09-17 15:17:50 +02:00
|
|
|
isDragging: false,
|
2021-01-06 11:11:56 +01:00
|
|
|
}
|
2021-11-26 14:25:02 +01:00
|
|
|
const store = writable(initialState)
|
2021-01-06 11:11:56 +01:00
|
|
|
const actions = {
|
2021-05-04 12:32:22 +02:00
|
|
|
selectComponent: id => {
|
2021-11-26 14:25:02 +01:00
|
|
|
if (id === get(store).selectedComponentId) {
|
2021-10-28 13:43:31 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-26 14:25:02 +01:00
|
|
|
store.update(state => ({
|
|
|
|
...state,
|
|
|
|
editMode: false,
|
|
|
|
selectedComponentId: id,
|
|
|
|
}))
|
2022-02-24 16:36:21 +01:00
|
|
|
devToolsStore.actions.setAllowSelection(false)
|
2021-06-23 15:21:37 +02:00
|
|
|
dispatchEvent("select-component", { id })
|
2021-01-06 11:11:56 +01:00
|
|
|
},
|
2021-06-08 09:00:54 +02:00
|
|
|
updateProp: (prop, value) => {
|
|
|
|
dispatchEvent("update-prop", { prop, value })
|
|
|
|
},
|
2021-06-23 15:21:37 +02:00
|
|
|
deleteComponent: id => {
|
|
|
|
dispatchEvent("delete-component", { id })
|
|
|
|
},
|
2022-03-07 15:05:26 +01:00
|
|
|
duplicateComponent: id => {
|
|
|
|
dispatchEvent("duplicate-component", { id })
|
2021-06-23 15:21:37 +02:00
|
|
|
},
|
2021-06-30 20:37:03 +02:00
|
|
|
notifyLoaded: () => {
|
|
|
|
dispatchEvent("preview-loaded")
|
|
|
|
},
|
2022-01-20 10:40:53 +01:00
|
|
|
pingEndUser: async () => {
|
|
|
|
try {
|
|
|
|
await API.pingEndUser()
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-30 16:03:57 +02:00
|
|
|
},
|
2021-08-19 15:53:13 +02:00
|
|
|
setSelectedPath: path => {
|
2021-11-26 14:25:02 +01:00
|
|
|
store.update(state => ({ ...state, selectedPath: path }))
|
2021-08-19 15:53:13 +02:00
|
|
|
},
|
2021-09-16 08:28:59 +02:00
|
|
|
moveComponent: (componentId, destinationComponentId, mode) => {
|
|
|
|
dispatchEvent("move-component", {
|
|
|
|
componentId,
|
|
|
|
destinationComponentId,
|
|
|
|
mode,
|
|
|
|
})
|
|
|
|
},
|
2021-09-17 15:17:50 +02:00
|
|
|
setDragging: dragging => {
|
2021-11-26 14:25:02 +01:00
|
|
|
if (dragging === get(store).isDragging) {
|
2021-11-16 14:35:20 +01:00
|
|
|
return
|
|
|
|
}
|
2021-11-26 14:25:02 +01:00
|
|
|
store.update(state => ({ ...state, isDragging: dragging }))
|
2021-10-28 13:43:31 +02:00
|
|
|
},
|
|
|
|
setEditMode: enabled => {
|
2021-11-26 14:25:02 +01:00
|
|
|
if (enabled === get(store).editMode) {
|
2021-11-16 14:35:20 +01:00
|
|
|
return
|
|
|
|
}
|
2021-11-26 14:25:02 +01:00
|
|
|
store.update(state => ({ ...state, editMode: enabled }))
|
2021-09-16 15:28:44 +02:00
|
|
|
},
|
2021-01-06 11:11:56 +01:00
|
|
|
}
|
|
|
|
return {
|
2021-11-26 14:25:02 +01:00
|
|
|
...store,
|
|
|
|
set: state => store.set({ ...initialState, ...state }),
|
2021-01-06 11:11:56 +01:00
|
|
|
actions,
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const builderStore = createBuilderStore()
|