@cocreate/pwa 1.18.2 → 1.19.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,10 @@
1
+ # [1.19.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.18.2...v1.19.0) (2024-03-18)
2
+
3
+
4
+ ### Features
5
+
6
+ * web share api using share, share-title, share-text, share-url ([b760a1f](https://github.com/CoCreate-app/CoCreate-pwa/commit/b760a1f5ae4b9376b4adac3b9456b706d83f9fbc))
7
+
1
8
  ## [1.18.2](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.18.1...v1.18.2) (2024-02-05)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.18.2",
3
+ "version": "1.19.0",
4
4
  "description": "A simple pwa component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "pwa",
package/src/index.js CHANGED
@@ -24,6 +24,7 @@
24
24
 
25
25
  import observer from '@cocreate/observer'
26
26
  import localStorage from '@cocreate/local-storage'
27
+ import './web-share'
27
28
 
28
29
 
29
30
  /**
package/src/web-share.js CHANGED
@@ -1 +1,82 @@
1
1
  // https://web.dev/web-share-target/
2
+ /********************************************************************************
3
+ * Copyright (C) 2023 CoCreate and Contributors.
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published
7
+ * by the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU Affero General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Affero General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ ********************************************************************************/
18
+
19
+ /**
20
+ * Commercial Licensing Information:
21
+ * For commercial use of this software without the copyleft provisions of the AGPLv3,
22
+ * you must obtain a commercial license from CoCreate LLC.
23
+ * For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
24
+ */
25
+
26
+ import Observer from '@cocreate/observer'
27
+ import Actions from '@cocreate/actions'
28
+
29
+ const selector = "[share]";
30
+
31
+ async function init(element) {
32
+ if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
33
+ element = [element]
34
+ else if (!element) {
35
+ element = document.querySelectorAll(selector)
36
+ }
37
+
38
+ for (let i = 0; i < element.length; i++)
39
+ element[i].addEventListener('click', function (e) {
40
+ share(e.target);
41
+ });
42
+
43
+ }
44
+
45
+ function share(element) {
46
+ if (navigator.share && element) {
47
+ let title = element.getAttribute('share-title') || document.title;
48
+ let text = element.getAttribute('share-text') || document.querySelector('meta[name="description"]')?.content || '';
49
+ let url = element.getAttribute('share-url') || window.location.href;
50
+
51
+ navigator.share({
52
+ title: title,
53
+ text: text,
54
+ url: url
55
+ })
56
+ .then(() => console.log('Successful share'))
57
+ .catch((error) => console.log('Error sharing', error));
58
+ } else {
59
+ console.log('Web Share API is not supported or element is not provided.');
60
+ }
61
+ }
62
+
63
+ Observer.init({
64
+ name: 'webshare',
65
+ observe: ['addedNodes'],
66
+ target: '[share]',
67
+ callback: (mutation) => {
68
+ init(mutation.target)
69
+ }
70
+ });
71
+
72
+ Actions.init({
73
+ name: "share",
74
+ callback: (action) => {
75
+ share(action.element);
76
+ }
77
+ })
78
+
79
+ init()
80
+
81
+ export default { init }
82
+