@contentpilot/page-tools 2.0.2 → 2.1.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/LICENSE +1 -1
- package/README.md +47 -23
- package/index.esm.js +6 -0
- package/index.js +128 -113
- package/package.json +15 -3
- package/.github/workflows/npm-publish.yml +0 -29
- package/.prettierrc +0 -7
- package/eslint.config.js +0 -28
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,18 +1,44 @@
|
|
|
1
1
|
# Content Pilot Page Tools
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@contentpilot/page-tools)
|
|
4
|
+
|
|
3
5
|
Set of page tools for users to print, email, share, etc.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install the package:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @contentpilot/page-tools --save-dev
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Import the module:
|
|
6
16
|
|
|
7
|
-
|
|
17
|
+
```javascript
|
|
18
|
+
import { PageTools, init } from "@contentpilot/page-tools";
|
|
19
|
+
```
|
|
8
20
|
|
|
9
|
-
|
|
21
|
+
Or include `./node_modules/@contentpilot/page-tools/index.js` as a plain
|
|
22
|
+
script.
|
|
10
23
|
|
|
11
|
-
|
|
24
|
+
## Usage
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
Without jQuery, it auto-initializes on `.page-tools` on `DOMContentLoaded`.
|
|
27
|
+
With jQuery, nothing runs until you call `.cpPageTools()`.
|
|
14
28
|
|
|
15
|
-
|
|
29
|
+
**Initialize:**
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { init } from "@contentpilot/page-tools";
|
|
33
|
+
|
|
34
|
+
init(".page-tools", {
|
|
35
|
+
print: ".tool-print",
|
|
36
|
+
email: ".tool-email",
|
|
37
|
+
bookmark: ".tool-bookmark",
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**jQuery Compatibility:**
|
|
16
42
|
|
|
17
43
|
```javascript
|
|
18
44
|
$(".page-tools").cpPageTools();
|
|
@@ -25,27 +51,25 @@ $(".page-tools").cpPageTools({
|
|
|
25
51
|
});
|
|
26
52
|
```
|
|
27
53
|
|
|
28
|
-
|
|
54
|
+
The 1.x options are unchanged. Sites using the jQuery plugin need no code
|
|
55
|
+
changes.
|
|
29
56
|
|
|
30
|
-
|
|
31
|
-
browser script, not a module to `import`/`require`.
|
|
57
|
+
### Options
|
|
32
58
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
new PageTools(element, {
|
|
39
|
-
print: ".custom-print-btn",
|
|
40
|
-
email: ".custom-email-btn",
|
|
41
|
-
bookmark: ".custom-bookmark-btn",
|
|
42
|
-
});
|
|
43
|
-
```
|
|
59
|
+
Selectors are scoped to the container element.
|
|
60
|
+
|
|
61
|
+
- `print` - Print button selector (default `.tool-print`)
|
|
62
|
+
- `email` - Email button selector (default `.tool-email`)
|
|
63
|
+
- `bookmark` - Bookmark button selector (default `.tool-bookmark`)
|
|
44
64
|
|
|
45
|
-
|
|
65
|
+
## Publish to NPM
|
|
46
66
|
|
|
47
67
|
To update this repository on NPM using the GitHub action,
|
|
48
68
|
|
|
49
69
|
1. Bump the version number in `package.json`
|
|
50
|
-
2.
|
|
51
|
-
3.
|
|
70
|
+
2. Open a PR titled `Release x.x.x`
|
|
71
|
+
3. Squash and merge it once approved
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
package/index.esm.js
ADDED
package/index.js
CHANGED
|
@@ -1,133 +1,148 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Page Tools
|
|
2
|
+
* Page Tools
|
|
3
3
|
* Set of page tools for users to print, email, share, etc.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @link https://www.contentpilot.com
|
|
9
|
-
* @copyright Copyright © 2018 Content Pilot
|
|
10
|
-
* @license GPL-2.0+
|
|
5
|
+
* @author Content Pilot
|
|
6
|
+
* @copyright Copyright (c) 2019 Content Pilot
|
|
7
|
+
* @license MIT
|
|
11
8
|
*/
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
bookmark: ".tool-bookmark",
|
|
20
|
-
...options,
|
|
21
|
-
};
|
|
10
|
+
// Plain script, no leaked globals; bundlers get index.esm.js.
|
|
11
|
+
(() => {
|
|
12
|
+
class PageTools {
|
|
13
|
+
constructor(element, options = {}) {
|
|
14
|
+
if (element.dataset.cpPageToolsInit) return;
|
|
15
|
+
element.dataset.cpPageToolsInit = "true";
|
|
22
16
|
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
this.element = element;
|
|
18
|
+
this.settings = {
|
|
19
|
+
print: ".tool-print",
|
|
20
|
+
email: ".tool-email",
|
|
21
|
+
bookmark: ".tool-bookmark",
|
|
22
|
+
...options,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
this.init();
|
|
26
|
+
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Trigger page print via browser
|
|
30
|
+
*
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
setupPrint() {
|
|
34
|
+
const printButtons = this.element.querySelectorAll(this.settings.print);
|
|
35
|
+
printButtons.forEach((button) => {
|
|
36
|
+
button.addEventListener("click", (e) => {
|
|
37
|
+
e.preventDefault();
|
|
38
|
+
e.stopPropagation();
|
|
39
|
+
window.print();
|
|
40
|
+
return false;
|
|
41
|
+
});
|
|
38
42
|
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
43
|
+
}
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Trigger email send with the page URL inside the content area
|
|
47
|
+
*
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
setupEmail() {
|
|
51
|
+
const emailButtons = this.element.querySelectorAll(this.settings.email);
|
|
52
|
+
emailButtons.forEach((button) => {
|
|
53
|
+
button.addEventListener("click", (e) => {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
window.location.href = `mailto:?body=${window.location.href}`;
|
|
56
|
+
});
|
|
53
57
|
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
58
|
+
}
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Trigger bookmark action
|
|
62
|
+
*
|
|
63
|
+
* @since 1.1.0
|
|
64
|
+
*/
|
|
65
|
+
setupBookmark() {
|
|
66
|
+
const bookmarkButtons = this.element.querySelectorAll(
|
|
67
|
+
this.settings.bookmark
|
|
68
|
+
);
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
70
|
+
bookmarkButtons.forEach((button) => {
|
|
71
|
+
button.addEventListener("click", (e) => {
|
|
72
|
+
if (window.sidebar && window.sidebar.addPanel) {
|
|
73
|
+
// Firefox <23
|
|
74
|
+
e.preventDefault();
|
|
75
|
+
e.stopPropagation();
|
|
76
|
+
window.sidebar.addPanel(document.title, window.location.href, "");
|
|
77
|
+
return false;
|
|
78
|
+
} else if (window.external && "AddFavorite" in window.external) {
|
|
79
|
+
// Internet Explorer
|
|
80
|
+
e.preventDefault();
|
|
81
|
+
e.stopPropagation();
|
|
82
|
+
window.external.AddFavorite(location.href, document.title);
|
|
83
|
+
return false;
|
|
84
|
+
} else if (
|
|
85
|
+
(window.opera && window.print) ||
|
|
86
|
+
(window.sidebar && !(window.sidebar instanceof Node))
|
|
87
|
+
) {
|
|
88
|
+
// Opera <15 and Firefox >23
|
|
89
|
+
/**
|
|
90
|
+
* For Firefox <23 and Opera <15, no need for JS to add to bookmarks
|
|
91
|
+
* The only thing needed is a `title` and a `rel="sidebar"`
|
|
92
|
+
* To ensure that the bookmarked URL doesn't have a complementary `#` from our trigger's href
|
|
93
|
+
* we force the current URL
|
|
94
|
+
*/
|
|
95
|
+
button.setAttribute("rel", "sidebar");
|
|
96
|
+
button.setAttribute("title", document.title);
|
|
97
|
+
button.setAttribute("href", window.location.href);
|
|
98
|
+
return true;
|
|
99
|
+
} else {
|
|
100
|
+
// For the other browsers (mainly WebKit) we use a simple alert to inform users that they can add to bookmarks with ctrl+D/cmd+D
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
e.stopPropagation();
|
|
103
|
+
const isMac =
|
|
104
|
+
navigator.userAgent.toLowerCase().indexOf("mac") !== -1;
|
|
105
|
+
alert(
|
|
106
|
+
`Add this page to your bookmarks by pressing ${
|
|
107
|
+
isMac ? "Command/Cmd" : "CTRL"
|
|
108
|
+
} + D on your keyboard.`
|
|
109
|
+
);
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
105
113
|
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
114
|
+
}
|
|
108
115
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
init() {
|
|
117
|
+
this.setupPrint();
|
|
118
|
+
this.setupEmail();
|
|
119
|
+
this.setupBookmark();
|
|
120
|
+
}
|
|
113
121
|
}
|
|
114
|
-
}
|
|
115
122
|
|
|
116
|
-
// jQuery plugin wrapper for backwards compatibility.
|
|
117
|
-
if (typeof jQuery !== "undefined") {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
123
|
+
// jQuery plugin wrapper for backwards compatibility.
|
|
124
|
+
if (typeof jQuery !== "undefined") {
|
|
125
|
+
jQuery.fn.cpPageTools = function (options) {
|
|
126
|
+
return this.each(function () {
|
|
127
|
+
new PageTools(this, options);
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
}
|
|
124
131
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (typeof jQuery === "undefined") {
|
|
128
|
-
const pageToolsElements = document.querySelectorAll(".page-tools");
|
|
132
|
+
function init(selector = ".page-tools", options = {}) {
|
|
133
|
+
const pageToolsElements = document.querySelectorAll(selector);
|
|
129
134
|
pageToolsElements.forEach((element) => {
|
|
130
|
-
new PageTools(element);
|
|
135
|
+
new PageTools(element, options);
|
|
131
136
|
});
|
|
132
137
|
}
|
|
133
|
-
|
|
138
|
+
|
|
139
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
140
|
+
// Only auto-init if jQuery plugin wasn't used.
|
|
141
|
+
if (typeof jQuery === "undefined") {
|
|
142
|
+
init();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Exposed for index.esm.js.
|
|
147
|
+
window.CpPageTools = { PageTools, init };
|
|
148
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentpilot/page-tools",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Set of page tools for users to print, email, share, etc.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"module": "index.esm.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./index.esm.js",
|
|
10
|
+
"default": "./index.js"
|
|
11
|
+
},
|
|
12
|
+
"./index.js": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"index.esm.js"
|
|
17
|
+
],
|
|
6
18
|
"type": "module",
|
|
7
19
|
"scripts": {
|
|
8
|
-
"lint": "eslint index.js",
|
|
9
|
-
"lint:fix": "eslint index.js --fix",
|
|
20
|
+
"lint": "eslint index.js index.esm.js",
|
|
21
|
+
"lint:fix": "eslint index.js index.esm.js --fix",
|
|
10
22
|
"format": "prettier --write .",
|
|
11
23
|
"format:check": "prettier --check ."
|
|
12
24
|
},
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
name: npm-publish
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches:
|
|
5
|
-
- main
|
|
6
|
-
jobs:
|
|
7
|
-
npm-publish:
|
|
8
|
-
name: npm-publish
|
|
9
|
-
runs-on: ubuntu-latest
|
|
10
|
-
if: contains(github.event.head_commit.message, 'Release')
|
|
11
|
-
steps:
|
|
12
|
-
- name: Checkout repository
|
|
13
|
-
uses: actions/checkout@v4
|
|
14
|
-
- name: Setup Node.js
|
|
15
|
-
uses: actions/setup-node@v4
|
|
16
|
-
with:
|
|
17
|
-
node-version: "20.x"
|
|
18
|
-
- name: Publish to NPM
|
|
19
|
-
uses: JS-DevTools/npm-publish@v3
|
|
20
|
-
with:
|
|
21
|
-
package: "./package.json"
|
|
22
|
-
access: public
|
|
23
|
-
tag: latest
|
|
24
|
-
token: ${{ secrets.NPM_AUTH_TOKEN }}
|
|
25
|
-
- name: Create Git tag
|
|
26
|
-
run: |
|
|
27
|
-
VERSION=$(node -p "require('./package.json').version")
|
|
28
|
-
git tag "$VERSION"
|
|
29
|
-
git push origin "$VERSION"
|
package/.prettierrc
DELETED
package/eslint.config.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import js from "@eslint/js";
|
|
2
|
-
import prettier from "eslint-config-prettier";
|
|
3
|
-
|
|
4
|
-
export default [
|
|
5
|
-
js.configs.recommended,
|
|
6
|
-
prettier,
|
|
7
|
-
{
|
|
8
|
-
languageOptions: {
|
|
9
|
-
ecmaVersion: "latest",
|
|
10
|
-
sourceType: "module",
|
|
11
|
-
globals: {
|
|
12
|
-
window: "readonly",
|
|
13
|
-
document: "readonly",
|
|
14
|
-
navigator: "readonly",
|
|
15
|
-
alert: "readonly",
|
|
16
|
-
location: "readonly",
|
|
17
|
-
Node: "readonly",
|
|
18
|
-
jQuery: "readonly",
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
rules: {
|
|
22
|
-
"no-unused-vars": "warn",
|
|
23
|
-
"no-console": "warn",
|
|
24
|
-
"prefer-const": "error",
|
|
25
|
-
"no-var": "error",
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
];
|