@cocreate/state 1.21.0 → 1.22.0

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,15 @@
1
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-state/compare/v1.21.0...v1.22.0) (2024-04-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improved state handeling and history api ([1b26d24](https://github.com/CoCreate-app/CoCreate-state/commit/1b26d24670d01020df4f2f054ee3dd100a3f5643))
7
+
8
+
9
+ ### Features
10
+
11
+ * popstate event handling ([df2432b](https://github.com/CoCreate-app/CoCreate-state/commit/df2432bc5090ef2942218d8ddb652f321e2c246c))
12
+
1
13
  # [1.21.0](https://github.com/CoCreate-app/CoCreate-state/compare/v1.20.1...v1.21.0) (2024-04-28)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/state",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
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
@@ -54,12 +54,14 @@ function _setAttributeValues(el, attrValues) {
54
54
 
55
55
  delete attrValues['overwrite']
56
56
 
57
- Object.keys(attrValues).forEach(key => {
58
- if (attrValues[key] === '$back')
59
- window.history.back()
60
- else if (attrValues[key] === '$forward')
61
- window.history.forward()
62
- else {
57
+ for (const key of Object.keys(attrValues)) {
58
+ if (attrValues[key] === '$back') {
59
+ window.history.back();
60
+ break;
61
+ } else if (attrValues[key] === '$forward') {
62
+ window.history.forward();
63
+ break;
64
+ } else {
63
65
  _setAttributeValue(el, key, attrValues[key], isOverwrite);
64
66
  _setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
65
67
  if (key == 'array' || key == 'object' || key == 'name') {
@@ -73,7 +75,7 @@ function _setAttributeValues(el, attrValues) {
73
75
  _setAttributeValue(el, 'template', attrValues[key], isOverwrite);
74
76
  }
75
77
  }
76
- });
78
+ }
77
79
  }
78
80
 
79
81
  async function _setAttributeValue(element, attribute, value, isOverwrite) {
@@ -103,15 +105,23 @@ async function stateAttributes(element) {
103
105
  elements.push(...nestedElements)
104
106
  }
105
107
 
108
+ let pushState = true
106
109
  for (let i = 0; i < elements.length; i++) {
107
110
  let attrValues = await _getAttributeValues(elements[i]);
111
+ if (attrValues.src === '$back' || attrValues.src === '$forward')
112
+ pushState = false
108
113
  let state_to = elements[i].getAttribute('state_to');
109
114
  Object.assign(statedAttributes, { [`${state_to}`]: attrValues });
110
115
  _getStateId(attrValues, state_to);
111
116
  }
112
117
  statedAttributes = JSON.stringify(statedAttributes)
113
- if (!element.closest('href')) {
114
- history.pushState({ statedAttributes, title: '', url: '' }, '', location.href);
118
+ if (pushState && !element.closest('href')) {
119
+ // TODO: Handle $title when adding statedAttributes
120
+ let title = element.getAttribute('title') || document.title || '';
121
+
122
+ history.pushState({ statedAttributes, title, url: location.href }, title, location.href);
123
+ } else {
124
+ console.log('pushState: ', pushState)
115
125
  }
116
126
 
117
127
  localStorage.setItem('statedAttributes', statedAttributes);
@@ -147,6 +157,31 @@ function _getStateId(attrValues, state_to) {
147
157
  _setAttributeValues(element, attrValues);
148
158
  }
149
159
 
160
+ window.onload = function () {
161
+ if (!history.state || history.state.url !== location.href) {
162
+ const statedAttributes = localStorage.getItem('statedAttributes') || '';
163
+ history.replaceState({ statedAttributes, url: location.href, title: document.title }, document.title, location.href);
164
+ }
165
+ };
166
+
167
+ window.addEventListener('popstate', function (event) {
168
+ if (event.state) {
169
+ if (event.state.statedAttributes) {
170
+ localStorage.setItem('statedAttributes', event.state.statedAttributes);
171
+ let elements = document.querySelectorAll('[state_id]')
172
+ initElements(elements)
173
+ }
174
+
175
+ if (event.state.url && event.state.url !== window.location.href) {
176
+ if (event.state.title)
177
+ sessionStorage.setItem('currentPageTitle', event.state.title); // Store title in session storage
178
+
179
+ location.href = event.state.url; // Navigate if the URL is different
180
+ } else if (event.state.title) {
181
+ document.title = event.state.title; // Update the title if provided
182
+ }
183
+ }
184
+ });
150
185
 
151
186
  observer.init({
152
187
  name: 'CoCreateState',