2020-11-24 12:02:10 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { cloneDeep } from "lodash/fp"
|
|
|
|
|
|
|
|
export const createDataStore = existingContext => {
|
2020-11-25 10:50:51 +01:00
|
|
|
const store = writable({ ...existingContext })
|
2020-11-24 12:02:10 +01:00
|
|
|
|
|
|
|
// Adds a context layer to the data context tree
|
|
|
|
const addContext = (row, componentId) => {
|
|
|
|
store.update(state => {
|
|
|
|
if (componentId) {
|
|
|
|
state[componentId] = row
|
2020-11-25 10:50:51 +01:00
|
|
|
state[`${componentId}_draft`] = cloneDeep(row)
|
|
|
|
state.closestComponentId = componentId
|
2020-11-24 12:02:10 +01:00
|
|
|
}
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2020-11-25 10:50:51 +01:00
|
|
|
update: store.update,
|
2020-11-24 12:02:10 +01:00
|
|
|
actions: { addContext },
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
|
|
|
|
export const dataStore = createDataStore()
|