diff --git a/src/lib/js/common/utils/index.mjs b/src/lib/js/common/utils/index.mjs index bc27d2d7..655f290d 100644 --- a/src/lib/js/common/utils/index.mjs +++ b/src/lib/js/common/utils/index.mjs @@ -5,6 +5,7 @@ import { CHILD_TYPE_MAP, ANIMATION_SPEED_SLOW, ANIMATION_SPEED_BASE, + DEFAULT_FORMDATA, } from '../../constants.js' import mergeWith from 'lodash/mergeWith.js' @@ -340,5 +341,4 @@ export function parseData(data = Object.create(null)) { return data } - -export const cleanFormData = formData => (formData ? clone(parseData(formData)) : {}) +export const cleanFormData = formData => (formData ? clone(parseData(formData)) : DEFAULT_FORMDATA()) diff --git a/src/lib/js/components/index.js b/src/lib/js/components/index.js index 25dc2348..bf19e80a 100644 --- a/src/lib/js/components/index.js +++ b/src/lib/js/components/index.js @@ -7,7 +7,7 @@ import RowsData from './rows/index.js' import ColumnsData from './columns/index.js' import FieldsData from './fields/index.js' import ExternalsData from './externals.js' -import { SESSION_FORMDATA_KEY, version } from '../constants.js' +import { DEFAULT_FORMDATA, SESSION_FORMDATA_KEY, version } from '../constants.js' export const Stages = StagesData export const Rows = RowsData @@ -16,23 +16,15 @@ export const Fields = FieldsData export const Controls = ControlsData export const Externals = ExternalsData -const defaultFormData = () => ({ - id: uuid(), - stages: { [uuid()]: {} }, - rows: {}, - columns: {}, - fields: {}, -}) - const getFormData = (formData, useSessionStorage = false) => { if (formData) { return clone(parseData(formData)) } if (useSessionStorage) { - return sessionStorage.get(SESSION_FORMDATA_KEY) || defaultFormData() + return sessionStorage.get(SESSION_FORMDATA_KEY) || DEFAULT_FORMDATA() } - return defaultFormData() + return DEFAULT_FORMDATA() } export class Components extends Data { diff --git a/src/lib/js/constants.js b/src/lib/js/constants.js index d2ec07d6..f5af9543 100644 --- a/src/lib/js/constants.js +++ b/src/lib/js/constants.js @@ -1,4 +1,5 @@ import pkg from '../../../package.json' with { type: 'json' } +import { uuid } from './common/utils/index.mjs' const isProd = import.meta.env?.PROD @@ -12,7 +13,9 @@ export const POLYFILLS = [ { name: 'mutationObserver', src: '//cdn.jsdelivr.net/npm/mutationobserver-shim/dist/mutationobserver.min.js' }, { name: 'fetch', src: 'https://unpkg.com/unfetch/polyfill' }, ] -export const SVG_SPRITE_URL = isProd ? `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/${formeoSpriteId}.svg` : `assets/img/${formeoSpriteId}.svg` +export const SVG_SPRITE_URL = isProd + ? `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/${formeoSpriteId}.svg` + : `assets/img/${formeoSpriteId}.svg` export const FALLBACK_SVG_SPRITE_URL = `https://draggable.github.io/formeo/assets/img/${formeoSpriteId}.svg` export const CSS_URL = `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/formeo.min.css` export const FALLBACK_CSS_URL = 'https://draggable.github.io/formeo/assets/css/formeo.min.css' @@ -199,3 +202,11 @@ export const UUID_REGEXP = /(\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4} export const bsColRegExp = /\bcol-\w+-\d+/g export const iconPrefix = 'f-i-' + +export const DEFAULT_FORMDATA = () => ({ + id: uuid(), + stages: { [uuid()]: {} }, + rows: {}, + columns: {}, + fields: {}, +})