@cocreate/state 1.18.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.
@@ -0,0 +1,361 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <title>CoCreate-state Documentation | CoCreateJS</title>
8
+ <link
9
+ rel="icon"
10
+ type="image/png"
11
+ sizes="32x32"
12
+ href="https://cocreate.app/images/favicon.ico" />
13
+ <meta
14
+ name="description"
15
+ content="Data attributes which can be used for components in CoCreate framework combined as a group of attributes that dynamically change attributes &amp; fetch custom data" />
16
+ <meta
17
+ name="keywords"
18
+ content="helper classes, utility classes, css framework, css library, inline style classes" />
19
+ <meta name="robots" content="index,follow" />
20
+ <meta
21
+ property="og:image"
22
+ content="https://cdn.cocreate.app/docs/state.png" />
23
+
24
+ <link
25
+ rel="stylesheet"
26
+ href="../index.css"
27
+ array="files"
28
+ object="60888216117c640e7596303f"
29
+ key="src"
30
+ type="text/css"
31
+ save="true" />
32
+ <link rel="manifest" href="/manifest.webmanifest" />
33
+ </head>
34
+
35
+ <body>
36
+ <!-- Navbar -->
37
+ <nav
38
+ class="width:100% display:flex align-items:center background:transparent padding-top:10px padding-bottom:10px"
39
+ content_id="content"
40
+ scroll="sticky-nav,"
41
+ scroll-up="5"
42
+ scroll-down="5"
43
+ path="../"
44
+ src="../components/navbar.html"></nav>
45
+ <sidenav
46
+ id="menuL"
47
+ class="position:fixed top:0px left:0px overflow:hidden background:whitesmoke height:100vh width:0px width:300px@xl"
48
+ resizable
49
+ resize-selector="[content_id='content']"
50
+ resize-property="margin-left"
51
+ resize-value="width">
52
+ <menu
53
+ array="files"
54
+ object="603717b07de7fb350ae9fec8"
55
+ key="src"></menu>
56
+ <div resize="right"></div>
57
+ </sidenav>
58
+ <main
59
+ class="padding-top:15px padding:15px@lg@xl"
60
+ content_id="content"
61
+ id="cocreate-state">
62
+ <div
63
+ class="display:flex flex-wrap:wrap justify-content:space-between position:relative margin:10px">
64
+ <div class="display:flex align-items:center">
65
+ <h2>CoCreate-state</h2>
66
+ </div>
67
+ <div
68
+ class="display:flex align-items:center font-size:20px position:absolute right:0 padding:5px background:white">
69
+ <div
70
+ class="display:flex align-items:center transition:0.3s padding-left:10px"
71
+ share-network="true"
72
+ share-text="Data attributes which can be used for components in CoCreate framework combined as a group of attributes that dynamically change attributes &amp; fetch custom data"
73
+ share-title="CoCreate-state"
74
+ share-height="600"
75
+ share-width="700"
76
+ share-media="https://cdn.cocreate.app/docs/state.png"
77
+ hover="display:block!important"
78
+ hover-selector=".social-networks">
79
+ <div class="display:none social-networks">
80
+ <a
81
+ class="margin-right:15px"
82
+ share-network="twitter"
83
+ title="Share on twitter"
84
+ ><i
85
+ class="height:20px fill:#505050"
86
+ src="/assets/svg/twitter.svg"></i
87
+ ></a>
88
+ <a
89
+ class="margin-right:15px"
90
+ share-network="facebook"
91
+ title="Share on Facebook"
92
+ ><i
93
+ class="height:20px fill:#505050"
94
+ src="/assets/svg/facebook.svg"></i
95
+ ></a>
96
+ <a
97
+ class="margin-right:15px"
98
+ share-network="instagram"
99
+ title="Share on instagram"
100
+ ><i
101
+ class="height:20px fill:#505050"
102
+ src="/assets/svg/instagram.svg"></i
103
+ ></a>
104
+ </div>
105
+ <a
106
+ class="margin-right:15px"
107
+ share-network="share"
108
+ title="Share on share"
109
+ ><i
110
+ class="height:20px fill:#505050"
111
+ src="/assets/svg/share-alt.svg"></i
112
+ ></a>
113
+ </div>
114
+ <a
115
+ href="https://github.com/CoCreate-app/CoCreate-state"
116
+ target="_blank"
117
+ class="margin-right:15px"
118
+ ><i
119
+ class="height:20px fill:#505050"
120
+ src="/assets/svg/github.svg"></i
121
+ ></a>
122
+ </div>
123
+ </div>
124
+ <h1
125
+ class="max-width:500px margin:20px_10px line-height:1.5 font-size:16px font-weight:100">
126
+ Data attributes which can be used for components in CoCreate
127
+ framework combined as a group of attributes that dynamically
128
+ change attributes &amp; fetch custom data.
129
+ </h1>
130
+ <div id="state-section" class="display:flex flex-wrap:wrap">
131
+ <div
132
+ class="flex-grow:1 width:400px width:300px@xs padding:0px_10px margin-top:60px">
133
+ <div
134
+ id="state-install"
135
+ class="border-bottom:1px_solid_lightgrey"
136
+ scroll
137
+ scroll-intersect="color:dodgerblue"
138
+ scroll-selector="#state-install-section">
139
+ <span
140
+ class="display:flex align-items:center width:fit-content"
141
+ hover="display:block!important"
142
+ hover-selector="[href='#state-install']">
143
+ <h2 class="padding:5px_0px">Install</h2>
144
+ <a
145
+ class="margin-left:10px display:none"
146
+ href="#state-install"
147
+ ><i
148
+ class="height:20px fill:#505050"
149
+ src="/assets/svg/link.svg"></i
150
+ ></a>
151
+ </span>
152
+ </div>
153
+ <pre><code class="language-bash">npm i @cocreate/state</code></pre>
154
+ <p class="padding:10px_0px line-height:1.5">
155
+ Or you can use cdn link:
156
+ </p>
157
+ <pre><code class="language-html">&lt;script&gt;https://cdn.cocreate.app/state/latest/CoCreate-state.min.js&lt;/script&gt;</code></pre>
158
+
159
+ <div
160
+ id="state-usage"
161
+ class="margin-top:80px border-bottom:1px_solid_lightgrey"
162
+ scroll
163
+ scroll-intersect="color:dodgerblue"
164
+ scroll-selector="#state-usage-section">
165
+ <span
166
+ class="display:flex align-items:center width:fit-content"
167
+ hover="display:block!important"
168
+ hover-selector="[href='#state-usage']">
169
+ <h2 class="padding:5px_0px">Usage</h2>
170
+ <a
171
+ class="margin-left:10px display:none"
172
+ href="#state-usage"
173
+ ><i
174
+ class="height:20px fill:#505050"
175
+ src="/assets/svg/link.svg"></i
176
+ ></a>
177
+ </span>
178
+ </div>
179
+ <div class="">
180
+ <p class="padding:10px_0px line-height:1.5">
181
+ This is state usage
182
+ </p>
183
+
184
+ <div class="flex-grow:1 min-width:300px width:100%">
185
+ <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;</code></pre>
186
+ </div>
187
+ <p class="padding:10px_0px line-height:1.5">
188
+ This is state usage
189
+ </p>
190
+ <p class="padding:10px_0px line-height:1.5">
191
+ This is state usage
192
+ </p>
193
+ </div>
194
+ <div
195
+ id="state-attributes"
196
+ class="margin-top:80px border-bottom:1px_solid_lightgrey"
197
+ scroll
198
+ scroll-intersect="color:dodgerblue"
199
+ scroll-selector="#state-attributes-section">
200
+ <span
201
+ class="display:flex align-items:center width:fit-content"
202
+ hover="display:block!important"
203
+ hover-selector="[href='#state-attributes']">
204
+ <h2 class="padding:5px_0px">Attributes</h2>
205
+ <a
206
+ class="margin-left:10px display:none"
207
+ href="#state-attributes"
208
+ ><i
209
+ class="height:20px fill:#505050"
210
+ src="/assets/svg/link.svg"></i
211
+ ></a>
212
+ </span>
213
+ </div>
214
+ <ul class="list-style-type:none">
215
+ <li
216
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
217
+ <h4>
218
+ <span>state</span>
219
+ <span class="cocreate-badge success"
220
+ >string</span
221
+ >
222
+ <span class="cocreate-badge warning"
223
+ >optional</span
224
+ >
225
+ </h4>
226
+ <p>state-attribute</p>
227
+ </li>
228
+ <li
229
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
230
+ <h4>
231
+ <span>state</span>
232
+ <span class="cocreate-badge success"
233
+ >string</span
234
+ >
235
+ <span class="cocreate-badge warning"
236
+ >optional</span
237
+ >
238
+ </h4>
239
+ <p>state-attribute</p>
240
+ </li>
241
+ </ul>
242
+ </div>
243
+
244
+ <div
245
+ class="flex-grow:1 width:300px padding:0px_10px margin-top:60px border-bottom:1px_solid_lightgrey">
246
+ <!-- SandBox -->
247
+ <div
248
+ id="state-demo"
249
+ class="border-bottom:1px_solid_lightgrey"
250
+ scroll
251
+ scroll-intersect="color:dodgerblue"
252
+ scroll-selector="#state-demo-section">
253
+ <span
254
+ class="display:flex align-items:center width:fit-content"
255
+ hover="display:block!important"
256
+ hover-selector="[href='#state-demo']">
257
+ <h2 class="padding:5px_0px">Demo</h2>
258
+ <a
259
+ class="margin-left:10px display:none"
260
+ href="#state-demo"
261
+ ><i
262
+ class="height:20px fill:#505050"
263
+ src="/assets/svg/link.svg"></i
264
+ ></a>
265
+ </span>
266
+ </div>
267
+ <div
268
+ class="position:sticky top:0 padding:15px_0px height:100vh">
269
+ <!-- SandBox -->
270
+ <div
271
+ class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px"
272
+ id="playground">
273
+ <div
274
+ id="demo-code"
275
+ resizable
276
+ class="position:relative height:50%">
277
+ <textarea
278
+ type="code"
279
+ lang="html"
280
+ array="demos"
281
+ object=""
282
+ key="demo"
283
+ save="false"
284
+ id="demo"
285
+ class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
286
+ <div
287
+ resize="bottom"
288
+ class="background:lightgrey"></div>
289
+ </div>
290
+
291
+ <div
292
+ id="demo-preview"
293
+ class="position:relative overflow:auto background-color:white">
294
+ <div class="demopreview padding:20px"></div>
295
+ </div>
296
+
297
+ <div
298
+ class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
299
+ <a
300
+ class="margin-right:10px"
301
+ id="eye"
302
+ show="#eye-slash"
303
+ hide="#eye, #demo-preview"
304
+ toggle="code-height"
305
+ toggle-selector="#demo-code"
306
+ ><i
307
+ class="height:18px fill:#505050"
308
+ src="/assets/svg/eye.svg"></i
309
+ ></a>
310
+ <a
311
+ class="margin-right:10px"
312
+ hidden
313
+ id="eye-slash"
314
+ show="#eye, #demo-preview"
315
+ hide="#eye-slash"
316
+ toggle="code-height"
317
+ toggle-selector="#demo-code"
318
+ ><i
319
+ class="height:20px fill:#505050"
320
+ src="/assets/svg/eye-slash.svg"></i
321
+ ></a>
322
+ <a
323
+ class="margin-right:10px"
324
+ id="code"
325
+ show="#code-slash"
326
+ hide="#code, #demo-code"
327
+ ><i
328
+ class="height:20px fill:#505050"
329
+ src="/assets/svg/code.svg"></i
330
+ ></a>
331
+ <a
332
+ class="margin-right:10px"
333
+ hidden
334
+ id="code-slash"
335
+ show="#code, #demo-code"
336
+ hide="#code-slash"
337
+ ><i
338
+ class="display:flex height:18px fill:#505050"
339
+ src="/assets/svg/code.svg"></i
340
+ ></a>
341
+ <a
342
+ class="margin-right:5px"
343
+ fullscreen
344
+ fullscreen-selector="#playground"></a>
345
+ </div>
346
+ </div>
347
+ <!-- End SandBox -->
348
+ </div>
349
+ </div>
350
+ </div>
351
+ <button
352
+ href="https://github.com/CoCreate-app/CoCreate-state/tree/master/docs/index.html?message=docs%3A%20describe%20your%20change..."
353
+ target="_blank"
354
+ class="display:flex justify-content:center align-items:center position:fixed bottom:15px right:15px height:50px width:50px border-radius:50% box-shadow:0px_2px_10px_0px_rgba(0,_0,_0,_0.4)">
355
+ <i class="height:20px" src="../assets/svg/pencil-alt.svg"></i>
356
+ </button>
357
+ </main>
358
+
359
+ <script src="https://CoCreate.app/dist/CoCreate.js"></script>
360
+ </body>
361
+ </html>
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@cocreate/state",
3
+ "version": "1.18.0",
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
+ "keywords": [
6
+ "state",
7
+ "cocreate",
8
+ "low-code-framework",
9
+ "no-code-framework",
10
+ "cocreatejs",
11
+ "cocreatejs-component",
12
+ "cocreate-framework",
13
+ "no-code",
14
+ "low-code",
15
+ "collaborative-framework",
16
+ "realtime",
17
+ "realtime-framework",
18
+ "collaboration",
19
+ "shared-editing",
20
+ "html5-framework",
21
+ "javascript-framework"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "start": "npx webpack --config webpack.config.js",
28
+ "build": "npx webpack --mode=production --config webpack.config.js",
29
+ "dev": "npx webpack --config webpack.config.js --watch",
30
+ "postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/CoCreate-app/CoCreate-state.git"
35
+ },
36
+ "author": "CoCreate LLC",
37
+ "license": "MIT",
38
+ "bugs": {
39
+ "url": "https://github.com/CoCreate-app/CoCreate-state/issues"
40
+ },
41
+ "homepage": "https://cocreate.app/docs/state",
42
+ "funding": {
43
+ "type": "GitHub Sponsors ❤",
44
+ "url": "https://github.com/sponsors/CoCreate-app"
45
+ },
46
+ "main": "./src/index.js",
47
+ "devDependencies": {
48
+ "@babel/core": "^7.9.6",
49
+ "@babel/preset-env": "^7.9.6",
50
+ "babel-loader": "^8.1.0",
51
+ "clean-webpack-plugin": "^3.0.0",
52
+ "file-loader": "^6.2.0",
53
+ "mini-css-extract-plugin": "^1.5.0",
54
+ "style-loader": "^3.3.1",
55
+ "terser-webpack-plugin": "^5.1.1",
56
+ "webpack": "^5.24.4",
57
+ "webpack-cli": "^4.5.0",
58
+ "webpack-log": "^3.0.1"
59
+ },
60
+ "dependencies": {
61
+ "@cocreate/actions": "^1.12.4",
62
+ "@cocreate/crud-client": "^1.29.0",
63
+ "@cocreate/element-prototype": "^1.13.2",
64
+ "@cocreate/local-storage": "^1.10.11",
65
+ "@cocreate/observer": "^1.12.0",
66
+ "@cocreate/uuid": "^1.7.6"
67
+ }
68
+ }
@@ -0,0 +1,22 @@
1
+ module.exports = {
2
+ dryRun: false,
3
+ branches: ["master"],
4
+ plugins: [
5
+ "@semantic-release/commit-analyzer",
6
+ "@semantic-release/release-notes-generator",
7
+ [
8
+ "@semantic-release/changelog",
9
+ {
10
+ changelogFile: "CHANGELOG.md",
11
+ },
12
+ ],
13
+ "@semantic-release/npm",
14
+ "@semantic-release/github",
15
+ [
16
+ "@semantic-release/git",
17
+ {
18
+ assets: ["CHANGELOG.md", "package.json"],
19
+ },
20
+ ],
21
+ ],
22
+ };
package/src/index.js ADDED
@@ -0,0 +1,166 @@
1
+ import '@cocreate/element-prototype';
2
+ import observer from '@cocreate/observer';
3
+ import action from '@cocreate/actions';
4
+ import uid from '@cocreate/uuid';
5
+ import localStorage from '@cocreate/local-storage';
6
+
7
+
8
+ function init() {
9
+ let elements = document.querySelectorAll('[state_id]');
10
+ initElements(elements);
11
+ window.addEventListener('storage', function (e) {
12
+ if (e.key == 'statedAttributes') {
13
+ elements = document.querySelectorAll('[state_id]:not([state-onstoragechange="false"])')
14
+ initElements(elements)
15
+ }
16
+ });
17
+ document.addEventListener('click', function (e) {
18
+ const target = e.target.closest('[state_to]');
19
+ if (target) {
20
+ if (target.closest('[actions*="state"]')) return;
21
+ stateAttributes(target);
22
+ }
23
+ });
24
+ }
25
+
26
+ function initElements(elements) {
27
+ for (let element of elements)
28
+ initElement(element);
29
+ }
30
+
31
+ function initElement(element) {
32
+ let state_id = element.getAttribute('state_id');
33
+ if (!state_id) return;
34
+
35
+ let statedAttributes = localStorage.getItem('statedAttributes');
36
+
37
+ if (!statedAttributes || statedAttributes.length == 0) return;
38
+ statedAttributes = JSON.parse(statedAttributes);
39
+
40
+ let attrValues = statedAttributes[`${state_id}`];
41
+ if (!attrValues) return;
42
+ _setAttributeValues(element, attrValues);
43
+ }
44
+
45
+ function _setAttributeValues(el, attrValues) {
46
+ let isOverwrite = el.getAttribute('state-overwrite');
47
+ if (isOverwrite === null || isOverwrite === undefined)
48
+ isOverwrite = attrValues['overwrite']
49
+
50
+ if (isOverwrite && isOverwrite != 'false' || isOverwrite === '')
51
+ isOverwrite = true;
52
+ else if (!isOverwrite || isOverwrite === 'false')
53
+ isOverwrite = false;
54
+
55
+ delete attrValues['overwrite']
56
+
57
+ Object.keys(attrValues).forEach(key => {
58
+ _setAttributeValue(el, key, attrValues[key], isOverwrite);
59
+ _setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
60
+ if (key == 'array' || key == 'object' || key == 'name') {
61
+ _setAttributeValue(el, `fetch-${key}`, attrValues[key], isOverwrite);
62
+ _setAttributeValue(el, `state-fetch-${key}`, attrValues[key], isOverwrite);
63
+ }
64
+ if (key == 'template') {
65
+ _setAttributeValue(el, 'template_id', attrValues[key], isOverwrite);
66
+ }
67
+ if (key == 'template_id') {
68
+ _setAttributeValue(el, 'template', attrValues[key], isOverwrite);
69
+ }
70
+ });
71
+ }
72
+
73
+ async function _setAttributeValue(element, attribute, value, isOverwrite) {
74
+ // TODO: if (value !== undefined)???
75
+ if (!element.getAttribute(attribute) || isOverwrite) {
76
+ if (attribute == 'value') {
77
+ if (element.value == '' || element.value && isOverwrite)
78
+ element.value = value;
79
+ else if (isOverwrite || element.hasAttribute('value') && !await element.getValue())
80
+ element.setValue(value)
81
+ } else if (element.hasAttribute(attribute) && value)
82
+ element.setAttribute(attribute, value);
83
+ }
84
+ }
85
+
86
+ async function stateAttributes(element) {
87
+ let statedAttributes = {};
88
+ let elements = []
89
+
90
+ let form = element.closest('form');
91
+ if (form) {
92
+ elements = form.querySelectorAll('[state_to]')
93
+ } else {
94
+ if (element.hasAttribute('state_to'))
95
+ elements.push(element)
96
+ let nestedElements = element.querySelectorAll('[state_to]')
97
+ elements.push(...nestedElements)
98
+ }
99
+
100
+ for (let i = 0; i < elements.length; i++) {
101
+ let attrValues = await _getAttributeValues(elements[i]);
102
+ let state_to = elements[i].getAttribute('state_to');
103
+ Object.assign(statedAttributes, { [`${state_to}`]: attrValues });
104
+ _getStateId(attrValues, state_to);
105
+ }
106
+
107
+ if (!element.closest('href')) {
108
+ let currentState = localStorage.getItem('statedAttributes');
109
+ if (currentState)
110
+ history.pushState({ statedAttributes: currentState, title: '', url: '' }, '', location.href);
111
+ }
112
+
113
+ localStorage.setItem('statedAttributes', JSON.stringify(statedAttributes));
114
+
115
+ document.dispatchEvent(new CustomEvent('stateEnd', {
116
+ detail: {}
117
+ }))
118
+
119
+ }
120
+
121
+ async function _getAttributeValues(element) {
122
+ let attributeValues = {};
123
+ let attributes = element.attributes;
124
+ for (let attribute of attributes) {
125
+ if (attribute.name.startsWith('state-')) {
126
+ if (attribute.value == '$uid')
127
+ Object.assign(attributeValues, { [`${attribute.name.substring(5)}`]: uid.generate(6) });
128
+ else if (attribute.name == 'state-value' && !attribute.value)
129
+ Object.assign(attributeValues, { value: await element.getvalue() });
130
+ else
131
+ Object.assign(attributeValues, { [`${attribute.name.substring(6)}`]: attribute.value });
132
+ }
133
+ }
134
+ if (element.value !== undefined && !element.hasAttribute('state-value') && element.getvalue)
135
+ Object.assign(attributeValues, { value: await element.getvalue() });
136
+
137
+ return attributeValues;
138
+ }
139
+
140
+ function _getStateId(attrValues, state_to) {
141
+ const elements = document.querySelectorAll(`[state_id="${state_to}"]`);
142
+ for (let element of elements)
143
+ _setAttributeValues(element, attrValues);
144
+ }
145
+
146
+
147
+ observer.init({
148
+ name: 'CoCreateState',
149
+ observe: ['addedNodes'],
150
+ target: '[state_id]',
151
+ callback: function (mutation) {
152
+ initElement(mutation.target);
153
+ }
154
+ });
155
+
156
+ action.init({
157
+ name: "state",
158
+ endEvent: "stateEnd",
159
+ callback: (data) => {
160
+ stateAttributes(data.element);
161
+ },
162
+ });
163
+
164
+ init();
165
+
166
+ export default { initElements, initElement, stateAttributes };