UNPKG

nitropage

Version:

A free and open source, extensible visual page builder based on SolidStart.

95 lines (78 loc) 2.7 kB
import { createEffect, createSignal, untrack } from "solid-js"; import { produce, SetStoreFunction } from "solid-js/store"; import { Config, State } from "../../../../types"; /** * Initializes blueprint default values in project and presets */ export const createBlueprintDefaultsInitializer = ( config: Config, state: State, setState: SetStoreFunction<State>, postReconcileState: (nextState: State) => void, ) => { const [getBlueprintVersions, setBlueprintVersions] = createSignal<Record<string, number>>(); const setBlueprintDefaults = function (state: State) { const blueprintVersions = getBlueprintVersions(); if (!blueprintVersions) return; try { for (const blueprint of Object.keys(config.blueprints)) { const version = config.blueprints[blueprint].version; const newVersion = blueprintVersions[blueprint] !== version; if (!newVersion) { continue; } if (version) { blueprintVersions[blueprint] = version; } const dataItems = Object.entries(config.blueprints[blueprint].data); const presetIds = Object.keys(state.presets); const elementIds = Object.keys(state.elements); if (!state.blueprintDefaults.project[blueprint]) { state.blueprintDefaults.project[blueprint] = {}; } for (const [key, data] of dataItems) { if (!state.blueprintDefaults.project[blueprint][key]) { state.blueprintDefaults.project[blueprint][key] = { value: data.defaultValue(), }; } for (const elementId of elementIds) { const element = state.elements[elementId]; if (element.blueprintId !== blueprint) { continue; } if (element.data[key]) { continue; } state.elements[elementId].data[key] = { value: data.defaultValue(), override: false, }; } for (const presetId of presetIds) { const preset = state.presets[presetId]; if (preset.blueprint !== blueprint) { continue; } if (preset.data[key]) { continue; } state.presets[presetId].data[key] = { value: data.defaultValue(), override: false, }; } } } } catch (err) { console.error(err); } setBlueprintVersions(blueprintVersions); }; createEffect(() => { setState(produce(setBlueprintDefaults)); untrack(() => postReconcileState(state)); }); return setBlueprintVersions; };