diff --git a/lib/shareon.js b/lib/shareon.js index 20a7708..3a5d4e3 100644 --- a/lib/shareon.js +++ b/lib/shareon.js @@ -4,24 +4,20 @@ */ /** - * @typedef {{ + * Social networks with their URL building functions. + * + * The format of this object is a map of strings (social networks' names) to the + * respective URL building functions. Such functions takes an object with the + * metadata of the page and returns the built URL. + * + * @type {{ [network: string]: (d: { * url: string, * title?: string, * media?: string, * text?: string, * via?: string, - * fbAppId?: string, - * }} PublicationData - */ - -/** - * @typedef {function(PublicationData): string} UrlBuilder - */ - -/** - * List of social networks available to shareon - * - * @type {Record} + * fbAppId?: string + * }) => string}} */ const URL_BUILDERS = { facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}`, @@ -39,7 +35,14 @@ const URL_BUILDERS = { whatsapp: (d) => `https://wa.me/?text=${d.title}%0D%0A${d.url}${d.text ? `%0D%0A%0D%0A${d.text}` : ''}`, }; -const populate = () => { +/** + * Populates shareon buttons with correct URLs. + * + * This function generates the sharing URL for every share button and embeds + * them into the elements. For elements, it updates the "href" attribute. + * For others, it sets a "click" listener. + */ +const shareon = () => { const shareonContainers = document.getElementsByClassName('shareon'); // iterate over
@@ -115,6 +118,6 @@ const populate = () => { } }; -window.addEventListener('load', populate); +window.addEventListener('load', shareon); -export default populate; +export default shareon; diff --git a/package.json b/package.json index 12dc5aa..498ae16 100644 --- a/package.json +++ b/package.json @@ -21,24 +21,27 @@ "email": "nick@karamoff.dev", "url": "https://karamoff.dev" }, - "type": "module", - "main": "./shareon.legacy.js", - "module": "./shareon.js", + "type": "module", + "main": "shareon.legacy.js", + "module": "shareon.js", "exports": { - "import": "./shareon.js" + "import": "./shareon.js" }, + "types": "shareon.d.ts", "files": [ "shareon.css", "shareon.css.map", "shareon.js", "shareon.js.map", "shareon.legacy.js", - "shareon.legacy.js.map" + "shareon.legacy.js.map", + "shareon.d.ts" ], "scripts": { "build-js": "esbuild lib/shareon.js --format=esm --minify --outfile=shareon.js --sourcemap --target=es6", "build-legacy-js": "esbuild lib/shareon.js --format=iife --minify --outfile=shareon.legacy.js --sourcemap --target=es6 --global-name=shareon", "build-css": "postcss lib/shareon.css --map --output shareon.css", + "build-types": "tsc lib/shareon.js --declaration --allowJs --emitDeclarationOnly --outDir .", "build": "run-p build-*", "lint": "eslint lib", "release": "np", @@ -60,7 +63,8 @@ "postcss-calc": "^8.0.0", "postcss-cli": "^8.3.1", "postcss-css-variables": "^0.18.0", - "size-limit": "^5.0.1" + "size-limit": "^5.0.1", + "typescript": "^4.3.5" }, "size-limit": [ { diff --git a/yarn.lock b/yarn.lock index 22563b8..8ee1efa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6037,6 +6037,11 @@ typedarray@^0.0.6: resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= +typescript@^4.3.5: + version "4.3.5" + resolved "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4" + integrity sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA== + unbox-primitive@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz#085e215625ec3162574dc8859abee78a59b14471"