@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Content Pilot
3
+ Copyright (c) 2019 Content Pilot
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,18 +1,44 @@
1
1
  # Content Pilot Page Tools
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@contentpilot/page-tools.svg)](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
- ### Installation
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
- `npm install @contentpilot/page-tools --save-dev`
17
+ ```javascript
18
+ import { PageTools, init } from "@contentpilot/page-tools";
19
+ ```
8
20
 
9
- ### Require JS file in task runner
21
+ Or include `./node_modules/@contentpilot/page-tools/index.js` as a plain
22
+ script.
10
23
 
11
- `./node_modules/@contentpilot/page-tools/index.js`
24
+ ## Usage
12
25
 
13
- ### Usage
26
+ Without jQuery, it auto-initializes on `.page-tools` on `DOMContentLoaded`.
27
+ With jQuery, nothing runs until you call `.cpPageTools()`.
14
28
 
15
- #### With jQuery (legacy)
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
- #### Without jQuery (ES6)
54
+ The 1.x options are unchanged. Sites using the jQuery plugin need no code
55
+ changes.
29
56
 
30
- Include `index.js` in your site's build/task-runner pipeline — it's a plain
31
- browser script, not a module to `import`/`require`.
57
+ ### Options
32
58
 
33
- ```javascript
34
- // Auto-initializes on elements with class "page-tools".
35
-
36
- // Or manually:
37
- const element = document.querySelector(".custom-page-tools");
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
- ### Publish to NPM
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. Commit with message pattern of `Release x.x.x`
51
- 3. Push to `main` branch to `origin`
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
@@ -0,0 +1,6 @@
1
+ // ESM entry: re-exports the API index.js exposes on window.
2
+ import "./index.js";
3
+
4
+ const { PageTools, init } = window.CpPageTools;
5
+
6
+ export { PageTools, init };
package/index.js CHANGED
@@ -1,133 +1,148 @@
1
1
  /**
2
- * Page Tools - Pure JavaScript implementation
2
+ * Page Tools
3
3
  * Set of page tools for users to print, email, share, etc.
4
4
  *
5
- * @version 2.0.0
6
- * @package Page Tools
7
- * @author Content Pilot, Dallas, Texas
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
- class PageTools {
14
- constructor(element, options = {}) {
15
- this.element = element;
16
- this.settings = {
17
- print: ".tool-print",
18
- email: ".tool-email",
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
- this.init();
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
- * Trigger page print via browser
28
- *
29
- * @since 1.0.0
30
- */
31
- setupPrint() {
32
- const printButtons = this.element.querySelectorAll(this.settings.print);
33
- printButtons.forEach((button) => {
34
- button.addEventListener("click", (e) => {
35
- e.preventDefault();
36
- window.print();
37
- return false;
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
- * Trigger email send with the page URL inside the content area
44
- *
45
- * @since 1.0.0
46
- */
47
- setupEmail() {
48
- const emailButtons = this.element.querySelectorAll(this.settings.email);
49
- emailButtons.forEach((button) => {
50
- button.addEventListener("click", (e) => {
51
- e.preventDefault();
52
- window.location.href = `mailto:?body=${window.location.href}`;
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
- * Trigger bookmark action
59
- *
60
- * @since 1.1.0
61
- */
62
- setupBookmark() {
63
- const bookmarkButtons = this.element.querySelectorAll(
64
- this.settings.bookmark
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
- bookmarkButtons.forEach((button) => {
68
- button.addEventListener("click", (e) => {
69
- if (window.sidebar && window.sidebar.addPanel) {
70
- // Firefox <23
71
- e.preventDefault();
72
- window.sidebar.addPanel(document.title, window.location.href, "");
73
- return false;
74
- } else if (window.external && "AddFavorite" in window.external) {
75
- // Internet Explorer
76
- e.preventDefault();
77
- window.external.AddFavorite(location.href, document.title);
78
- return false;
79
- } else if (
80
- (window.opera && window.print) ||
81
- (window.sidebar && !(window.sidebar instanceof Node))
82
- ) {
83
- // Opera <15 and Firefox >23
84
- /**
85
- * For Firefox <23 and Opera <15, no need for JS to add to bookmarks
86
- * The only thing needed is a `title` and a `rel="sidebar"`
87
- * To ensure that the bookmarked URL doesn't have a complementary `#` from our trigger's href
88
- * we force the current URL
89
- */
90
- button.setAttribute("rel", "sidebar");
91
- button.setAttribute("title", document.title);
92
- button.setAttribute("href", window.location.href);
93
- return true;
94
- } else {
95
- // 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
96
- e.preventDefault();
97
- const isMac = navigator.userAgent.toLowerCase().indexOf("mac") !== -1;
98
- alert(
99
- `Add this page to your bookmarks by pressing ${
100
- isMac ? "Command/Cmd" : "CTRL"
101
- } + D on your keyboard.`
102
- );
103
- return false;
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
- init() {
110
- this.setupPrint();
111
- this.setupEmail();
112
- this.setupBookmark();
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
- jQuery.fn.cpPageTools = function (options) {
119
- return this.each(function () {
120
- new PageTools(this, options);
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
- document.addEventListener("DOMContentLoaded", () => {
126
- // Only auto-init if jQuery plugin wasn't used.
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.2",
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
@@ -1,7 +0,0 @@
1
- {
2
- "semi": true,
3
- "singleQuote": false,
4
- "tabWidth": 2,
5
- "trailingComma": "es5",
6
- "printWidth": 80
7
- }
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
- ];