@cocreate/state 1.20.1 → 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 +19 -0
- package/package.json +1 -1
- package/src/index.js +56 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
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
|
+
|
|
13
|
+
# [1.21.0](https://github.com/CoCreate-app/CoCreate-state/compare/v1.20.1...v1.21.0) (2024-04-28)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* $back and $forward operators for history control ([ffdeb5d](https://github.com/CoCreate-app/CoCreate-state/commit/ffdeb5d8d5331e51cbcd97043369f5eef1331c35))
|
|
19
|
+
|
|
1
20
|
## [1.20.1](https://github.com/CoCreate-app/CoCreate-state/compare/v1.20.0...v1.20.1) (2024-02-20)
|
|
2
21
|
|
|
3
22
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/state",
|
|
3
|
-
"version": "1.
|
|
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,20 +54,28 @@ function _setAttributeValues(el, attrValues) {
|
|
|
54
54
|
|
|
55
55
|
delete attrValues['overwrite']
|
|
56
56
|
|
|
57
|
-
Object.keys(attrValues)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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 {
|
|
65
|
+
_setAttributeValue(el, key, attrValues[key], isOverwrite);
|
|
66
|
+
_setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
|
|
67
|
+
if (key == 'array' || key == 'object' || key == 'name') {
|
|
68
|
+
_setAttributeValue(el, `fetch-${key}`, attrValues[key], isOverwrite);
|
|
69
|
+
_setAttributeValue(el, `state-fetch-${key}`, attrValues[key], isOverwrite);
|
|
70
|
+
}
|
|
71
|
+
if (key == 'template') {
|
|
72
|
+
_setAttributeValue(el, 'template_id', attrValues[key], isOverwrite);
|
|
73
|
+
}
|
|
74
|
+
if (key == 'template_id') {
|
|
75
|
+
_setAttributeValue(el, 'template', attrValues[key], isOverwrite);
|
|
76
|
+
}
|
|
63
77
|
}
|
|
64
|
-
|
|
65
|
-
_setAttributeValue(el, 'template_id', attrValues[key], isOverwrite);
|
|
66
|
-
}
|
|
67
|
-
if (key == 'template_id') {
|
|
68
|
-
_setAttributeValue(el, 'template', attrValues[key], isOverwrite);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
78
|
+
}
|
|
71
79
|
}
|
|
72
80
|
|
|
73
81
|
async function _setAttributeValue(element, attribute, value, isOverwrite) {
|
|
@@ -97,15 +105,23 @@ async function stateAttributes(element) {
|
|
|
97
105
|
elements.push(...nestedElements)
|
|
98
106
|
}
|
|
99
107
|
|
|
108
|
+
let pushState = true
|
|
100
109
|
for (let i = 0; i < elements.length; i++) {
|
|
101
110
|
let attrValues = await _getAttributeValues(elements[i]);
|
|
111
|
+
if (attrValues.src === '$back' || attrValues.src === '$forward')
|
|
112
|
+
pushState = false
|
|
102
113
|
let state_to = elements[i].getAttribute('state_to');
|
|
103
114
|
Object.assign(statedAttributes, { [`${state_to}`]: attrValues });
|
|
104
115
|
_getStateId(attrValues, state_to);
|
|
105
116
|
}
|
|
106
117
|
statedAttributes = JSON.stringify(statedAttributes)
|
|
107
|
-
if (!element.closest('href')) {
|
|
108
|
-
|
|
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)
|
|
109
125
|
}
|
|
110
126
|
|
|
111
127
|
localStorage.setItem('statedAttributes', statedAttributes);
|
|
@@ -141,6 +157,31 @@ function _getStateId(attrValues, state_to) {
|
|
|
141
157
|
_setAttributeValues(element, attrValues);
|
|
142
158
|
}
|
|
143
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
|
+
});
|
|
144
185
|
|
|
145
186
|
observer.init({
|
|
146
187
|
name: 'CoCreateState',
|