formeo 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/formeo.es.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  /**
3
3
  formeo - https://formeo.io
4
- Version: 4.2.5
4
+ Version: 5.0.0
5
5
  Author: Draggable https://draggable.io
6
6
  */
7
7
 
@@ -432,7 +432,7 @@ if (window !== void 0) {
432
432
  window.SmartTooltip = SmartTooltip;
433
433
  }
434
434
  const name$1 = "formeo";
435
- const version$2 = "4.2.5";
435
+ const version$2 = "5.0.0";
436
436
  const pkg = {
437
437
  name: name$1,
438
438
  version: version$2
@@ -10714,11 +10714,25 @@ const Columns2 = columns;
10714
10714
  const Fields2 = fields;
10715
10715
  const Controls2 = Controls$2;
10716
10716
  const getFormData = (formData, useSessionStorage = false) => {
10717
- if (formData) {
10718
- return clone$1(parseData(formData));
10717
+ if (formData !== void 0 && formData !== null) {
10718
+ const parsed = parseData(formData);
10719
+ if (parsed && typeof parsed === "object") {
10720
+ const cloned = clone$1(parsed);
10721
+ return {
10722
+ id: cloned.id || DEFAULT_FORMDATA().id,
10723
+ stages: cloned.stages || DEFAULT_FORMDATA().stages,
10724
+ rows: cloned.rows || {},
10725
+ columns: cloned.columns || {},
10726
+ fields: cloned.fields || {}
10727
+ };
10728
+ }
10729
+ console.warn("Formeo: Invalid formData provided, using default");
10719
10730
  }
10720
10731
  if (useSessionStorage) {
10721
- return sessionStorage.get(SESSION_FORMDATA_KEY) || DEFAULT_FORMDATA();
10732
+ const sessionData = sessionStorage.get(SESSION_FORMDATA_KEY);
10733
+ if (sessionData) {
10734
+ return sessionData;
10735
+ }
10722
10736
  }
10723
10737
  return DEFAULT_FORMDATA();
10724
10738
  };
@@ -11122,7 +11136,18 @@ const defaults = {
11122
11136
  };
11123
11137
  }
11124
11138
  };
11139
+ const INIT_STATES = {
11140
+ CREATED: "created",
11141
+ LOADING_RESOURCES: "loading",
11142
+ INITIALIZING: "initializing",
11143
+ READY: "ready",
11144
+ ERROR: "error"
11145
+ };
11125
11146
  let FormeoEditor$1 = class FormeoEditor {
11147
+ #initState = INIT_STATES.CREATED;
11148
+ #initPromise = null;
11149
+ #lockedFormData = null;
11150
+ #dataLoadedOnce = false;
11126
11151
  /**
11127
11152
  * @param {Object} options formeo options
11128
11153
  * @param {String|Object} userFormData loaded formData
@@ -11137,7 +11162,9 @@ let FormeoEditor$1 = class FormeoEditor {
11137
11162
  this.opts = opts;
11138
11163
  dom.setOptions = opts;
11139
11164
  components.config = config;
11140
- this.userFormData = userFormData || formData;
11165
+ const providedData = userFormData || formData;
11166
+ this.#lockedFormData = providedData ? cleanFormData(providedData) : null;
11167
+ this.userFormData = this.#lockedFormData;
11141
11168
  this.Components = components;
11142
11169
  this.dom = dom;
11143
11170
  events.init({ debug, ...events$1 });
@@ -11152,7 +11179,9 @@ let FormeoEditor$1 = class FormeoEditor {
11152
11179
  return this.Components.formData;
11153
11180
  }
11154
11181
  set formData(data = {}) {
11155
- this.userFormData = cleanFormData(data);
11182
+ const cleaned = cleanFormData(data);
11183
+ this.#lockedFormData = cleaned;
11184
+ this.userFormData = cleaned;
11156
11185
  this.load(this.userFormData, this.opts);
11157
11186
  }
11158
11187
  loadData(data = {}) {
@@ -11166,7 +11195,9 @@ let FormeoEditor$1 = class FormeoEditor {
11166
11195
  * @return {void}
11167
11196
  */
11168
11197
  clear() {
11169
- this.userFormData = DEFAULT_FORMDATA();
11198
+ const defaultData = DEFAULT_FORMDATA();
11199
+ this.#lockedFormData = defaultData;
11200
+ this.userFormData = defaultData;
11170
11201
  this.Components.load(this.userFormData, this.opts);
11171
11202
  this.render();
11172
11203
  }
@@ -11176,6 +11207,7 @@ let FormeoEditor$1 = class FormeoEditor {
11176
11207
  */
11177
11208
  async loadResources() {
11178
11209
  document.removeEventListener("DOMContentLoaded", this.loadResources);
11210
+ this.#initState = INIT_STATES.LOADING_RESOURCES;
11179
11211
  const promises = [
11180
11212
  fetchIcons(this.opts.svgSprite),
11181
11213
  fetchFormeoStyle(this.opts.style),
@@ -11185,38 +11217,142 @@ let FormeoEditor$1 = class FormeoEditor {
11185
11217
  locale: globalThis.sessionStorage?.getItem(SESSION_LOCALE_KEY)
11186
11218
  })
11187
11219
  ].filter(Boolean);
11188
- await Promise.all(promises);
11189
- if (this.opts.allowEdit) {
11190
- this.init();
11220
+ try {
11221
+ await Promise.all(promises);
11222
+ if (this.opts.allowEdit) {
11223
+ this.init();
11224
+ }
11225
+ } catch (error) {
11226
+ this.#initState = INIT_STATES.ERROR;
11227
+ console.error("Failed to load resources:", error);
11228
+ throw error;
11191
11229
  }
11192
11230
  }
11193
11231
  /**
11194
11232
  * Formeo initializer
11195
- * @return {Object} References to formeo instance,
11233
+ * @return {Promise} References to formeo instance,
11196
11234
  * dom elements, actions events and more.
11197
11235
  */
11198
11236
  init() {
11199
- return Controls$2.init(this.opts.controls, this.opts.stickyControls).then((controls) => {
11237
+ if (this.#initState === INIT_STATES.INITIALIZING) {
11238
+ return this.#initPromise;
11239
+ }
11240
+ if (this.#initState === INIT_STATES.READY) {
11241
+ return this.#refreshUI();
11242
+ }
11243
+ this.#initState = INIT_STATES.INITIALIZING;
11244
+ this.#initPromise = Controls$2.init(this.opts.controls, this.opts.stickyControls).then((controls) => {
11200
11245
  this.controls = controls;
11201
- this.load(this.userFormData, this.opts);
11246
+ if (!this.#dataLoadedOnce) {
11247
+ this.#loadInitialData();
11248
+ this.#dataLoadedOnce = true;
11249
+ }
11202
11250
  this.formId = components.get("id");
11203
11251
  this.i18n = {
11204
- setLang: (formeoLocale) => {
11205
- globalThis.sessionStorage?.setItem(SESSION_LOCALE_KEY, formeoLocale);
11206
- const loadLang = mi18n.setCurrent(formeoLocale);
11207
- loadLang.then(() => {
11208
- this.init();
11209
- }, console.error);
11210
- }
11252
+ setLang: this.#setLanguage.bind(this)
11211
11253
  };
11254
+ this.render();
11255
+ this.#initState = INIT_STATES.READY;
11212
11256
  this.opts.onLoad?.(this);
11213
11257
  this.tooltipInstance = new SmartTooltip();
11258
+ return this;
11259
+ }).catch((error) => {
11260
+ this.#initState = INIT_STATES.ERROR;
11261
+ console.error("Failed to initialize editor:", error);
11262
+ throw error;
11214
11263
  });
11264
+ return this.#initPromise;
11265
+ }
11266
+ /**
11267
+ * Set language without reloading form data (fixes race condition)
11268
+ * @param {string} formeoLocale - locale code
11269
+ * @return {Promise}
11270
+ */
11271
+ async #setLanguage(formeoLocale) {
11272
+ globalThis.sessionStorage?.setItem(SESSION_LOCALE_KEY, formeoLocale);
11273
+ await mi18n.setCurrent(formeoLocale);
11274
+ await this.#refreshUI();
11275
+ }
11276
+ /**
11277
+ * Refresh UI without reloading data (used for language changes)
11278
+ * @return {Promise}
11279
+ */
11280
+ async #refreshUI() {
11281
+ this.controls = await Controls$2.init(this.opts.controls, this.opts.stickyControls);
11282
+ this.render();
11283
+ return this;
11284
+ }
11285
+ /**
11286
+ * Load initial data with proper priority
11287
+ */
11288
+ #loadInitialData() {
11289
+ const dataToLoad = this.#getDataWithPriority();
11290
+ this.Components.load(dataToLoad, this.opts);
11291
+ }
11292
+ /**
11293
+ * Get form data with proper priority:
11294
+ * 1. User-provided data (locked at construction)
11295
+ * 2. SessionStorage (if enabled)
11296
+ * 3. Default empty form
11297
+ * @return {Object} form data to load
11298
+ */
11299
+ #getDataWithPriority() {
11300
+ if (this.#lockedFormData) {
11301
+ return clone$1(this.#lockedFormData);
11302
+ }
11303
+ if (this.opts.sessionStorage) {
11304
+ const sessionData = sessionStorage.get(SESSION_FORMDATA_KEY);
11305
+ if (sessionData) {
11306
+ return sessionData;
11307
+ }
11308
+ }
11309
+ return DEFAULT_FORMDATA();
11215
11310
  }
11216
11311
  load(formData = this.userFormData, opts = this.opts) {
11217
11312
  this.Components.load(formData, opts);
11218
11313
  this.render();
11219
11314
  }
11315
+ /**
11316
+ * Get current initialization state
11317
+ * @return {string} current state
11318
+ */
11319
+ get initState() {
11320
+ return this.#initState;
11321
+ }
11322
+ /**
11323
+ * Check if the editor is ready
11324
+ * @return {boolean}
11325
+ */
11326
+ get isReady() {
11327
+ return this.#initState === INIT_STATES.READY;
11328
+ }
11329
+ /**
11330
+ * Wait for the editor to be ready
11331
+ * @return {Promise} resolves when editor is ready
11332
+ */
11333
+ async whenReady() {
11334
+ if (this.#initState === INIT_STATES.READY) {
11335
+ return this;
11336
+ }
11337
+ if (this.#initState === INIT_STATES.ERROR) {
11338
+ return Promise.reject(new Error("Editor initialization failed"));
11339
+ }
11340
+ if (this.#initPromise) {
11341
+ return this.#initPromise;
11342
+ }
11343
+ return new Promise((resolve, reject) => {
11344
+ const checkReady = () => {
11345
+ if (this.#initState === INIT_STATES.READY) {
11346
+ resolve(this);
11347
+ } else if (this.#initState === INIT_STATES.ERROR) {
11348
+ reject(new Error("Editor initialization failed"));
11349
+ } else {
11350
+ globalThis.requestAnimationFrame(checkReady);
11351
+ }
11352
+ };
11353
+ checkReady();
11354
+ });
11355
+ }
11220
11356
  /**
11221
11357
  * Render the formeo sections
11222
11358
  * @return {void}