@cocreate/state 1.24.1 → 1.24.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [1.24.2](https://github.com/CoCreate-app/CoCreate-state/compare/v1.24.1...v1.24.2) (2025-04-11)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * renamed statedAttributes to state in local storage. ([610f591](https://github.com/CoCreate-app/CoCreate-state/commit/610f5916069ee773ee4bc020430892c869088d0b))
7
+ * update observer observe param to type and and attributeName to attributeFilter ([42d8a89](https://github.com/CoCreate-app/CoCreate-state/commit/42d8a89ce2b32ee5d17b8a1498e40bd4503416a9))
8
+
1
9
  ## [1.24.1](https://github.com/CoCreate-app/CoCreate-state/compare/v1.24.0...v1.24.1) (2025-01-18)
2
10
 
3
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/state",
3
- "version": "1.24.1",
3
+ "version": "1.24.2",
4
4
  "description": "Data attributes which can be used for components in CoCreate framework combined as a group of attributes that dynamically change attributes & fetch custom data.",
5
5
  "keywords": [
6
6
  "state",
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "@cocreate/element-prototype";
2
- import observer from "@cocreate/observer";
3
- import action from "@cocreate/actions";
2
+ import Observer from "@cocreate/observer";
3
+ import Action from "@cocreate/actions";
4
4
  import uid from "@cocreate/uuid";
5
5
  import localStorage from "@cocreate/local-storage";
6
6
 
@@ -8,7 +8,7 @@ function init() {
8
8
  let elements = document.querySelectorAll("[state_id]");
9
9
  initElements(elements);
10
10
  window.addEventListener("storage", function (e) {
11
- if (e.key == "statedAttributes") {
11
+ if (e.key == "state") {
12
12
  elements = document.querySelectorAll(
13
13
  '[state_id]:not([state-onstoragechange="false"])'
14
14
  );
@@ -32,7 +32,7 @@ function initElement(element) {
32
32
  let state_id = element.getAttribute("state_id");
33
33
  if (!state_id) return;
34
34
 
35
- let statedAttributes = localStorage.getItem("statedAttributes");
35
+ let statedAttributes = localStorage.getItem("state");
36
36
 
37
37
  if (!statedAttributes || statedAttributes.length == 0) return;
38
38
  statedAttributes = JSON.parse(statedAttributes);
@@ -104,17 +104,14 @@ async function _setAttributeValue(element, attribute, value, isOverwrite) {
104
104
  async function stateAttributes(element) {
105
105
  let statedAttributes = {};
106
106
  try {
107
- const storedAttributes = localStorage.getItem("statedAttributes");
107
+ const storedAttributes = localStorage.getItem("state");
108
108
  if (storedAttributes) {
109
109
  statedAttributes = JSON.parse(storedAttributes);
110
110
  }
111
111
  } catch (error) {
112
- console.error(
113
- "Failed to parse statedAttributes from localStorage:",
114
- error
115
- );
116
- // Optionally reset `statedAttributes` in localStorage to ensure valid data:
117
- localStorage.setItem("statedAttributes", JSON.stringify({}));
112
+ console.error("Failed to parse state from localStorage:", error);
113
+ // Optionally reset `state` in localStorage to ensure valid data:
114
+ localStorage.setItem("state", JSON.stringify({}));
118
115
  }
119
116
  let elements = [];
120
117
 
@@ -159,10 +156,9 @@ async function stateAttributes(element) {
159
156
  );
160
157
  }
161
158
 
162
- if (!changeState)
163
- localStorage.setItem("statedAttributes", statedAttributes);
159
+ if (!changeState) localStorage.setItem("state", statedAttributes);
164
160
 
165
- document.dispatchEvent(
161
+ element.dispatchEvent(
166
162
  new CustomEvent("stateEnd", {
167
163
  detail: {}
168
164
  })
@@ -205,14 +201,14 @@ function _getStateId(attrValues, state_to) {
205
201
 
206
202
  window.onload = function () {
207
203
  if (!history.length) {
208
- const statedAttributes = localStorage.getItem("statedAttributes") || "";
204
+ const statedAttributes = localStorage.getItem("state") || "";
209
205
  history.replaceState(
210
206
  { statedAttributes, url: location.href, title: document.title },
211
207
  document.title,
212
208
  location.href
213
209
  );
214
210
  } else if (!history.state || history.state.url !== location.href) {
215
- const statedAttributes = localStorage.getItem("statedAttributes") || "";
211
+ const statedAttributes = localStorage.getItem("state") || "";
216
212
  history.pushState(
217
213
  { statedAttributes, url: location.href, title: document.title },
218
214
  document.title,
@@ -224,10 +220,7 @@ window.onload = function () {
224
220
  window.addEventListener("popstate", function (event) {
225
221
  if (event.state) {
226
222
  if (event.state.statedAttributes) {
227
- localStorage.setItem(
228
- "statedAttributes",
229
- event.state.statedAttributes
230
- );
223
+ localStorage.setItem("state", event.state.statedAttributes);
231
224
  let elements = document.querySelectorAll("[state_id]");
232
225
  initElements(elements);
233
226
  }
@@ -243,20 +236,20 @@ window.addEventListener("popstate", function (event) {
243
236
  }
244
237
  });
245
238
 
246
- observer.init({
239
+ Observer.init({
247
240
  name: "CoCreateState",
248
- observe: ["addedNodes"],
241
+ types: ["addedNodes"],
249
242
  selector: "[state_id]",
250
243
  callback: function (mutation) {
251
244
  initElement(mutation.target);
252
245
  }
253
246
  });
254
247
 
255
- action.init({
248
+ Action.init({
256
249
  name: "state",
257
250
  endEvent: "stateEnd",
258
- callback: (data) => {
259
- stateAttributes(data.element);
251
+ callback: (action) => {
252
+ stateAttributes(action.element);
260
253
  }
261
254
  });
262
255