diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 00000000..f93419d4 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es5", + "noEmit": true, + "checkJs": true, + "jsx": "react", + "strict": false, + "lib": [ "dom" ] + }, + "include": [ + "./src/htmx.js" + ], + "verbose": true +} diff --git a/package-lock.json b/package-lock.json index d0e7473f..3df21717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "htmx.org", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5363,9 +5363,9 @@ } }, "postcss": { - "version": "7.0.35", - "resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.35.tgz", - "integrity": "sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==", + "version": "7.0.36", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-7.0.36.tgz", + "integrity": "sha512-BebJSIUMwJHRH0HAQoxN4u1CN86glsrwsW0q7T+/m44eXOUAxSNdHRkNZPYz5vVUbg17hFgOQDE7fZk7li3pZw==", "dev": true, "requires": { "chalk": "^2.4.2", @@ -7239,6 +7239,12 @@ "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", "dev": true }, + "typescript": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz", + "integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==", + "dev": true + }, "ua-parser-js": { "version": "0.7.28", "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.28.tgz", diff --git a/package.json b/package.json index 7bbeb037..71d695b9 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,11 @@ "dist/*.js.gz" ], "main": "dist/htmx.min.js", + "types": "src/htmx.d.ts", "unpkg": "dist/htmx.min.js", "scripts": { "test": "mocha-chrome test/index.html", + "test-types": "tsc --project ./jsconfig.json", "dist": "cp -r src/* dist/ && npm run-script uglify && gzip -k -f dist/htmx.min.js > dist/htmx.min.js.gz && exit", "www": "node scripts/www.js", "uglify": "uglifyjs -m eval -o dist/htmx.min.js dist/htmx.js" @@ -38,8 +40,9 @@ "fs-extra": "^9.1.0", "mocha": "^7.2.0", "mocha-chrome": "^2.2.0", + "sass": "^1.34.0", "sinon": "^9.2.4", - "uglify-js": "^3.13.7", - "sass": "^1.34.0" + "typescript": "^4.3.5", + "uglify-js": "^3.13.7" } } diff --git a/src/htmx.d.ts b/src/htmx.d.ts new file mode 100644 index 00000000..b964e3f7 --- /dev/null +++ b/src/htmx.d.ts @@ -0,0 +1,26 @@ +export interface HtmxApi { + config?: HtmxConfig + logger?: (a: HTMLElement, b: Event, c: any) => void | null +} + +export interface HtmxConfig { + historyEnabled?: boolean; + historyCacheSize?: number; + refreshOnHistoryMiss?: boolean; + defaultSwapStyle?: 'innerHTML' | string; + defaultSwapDelay?: number; + defaultSettleDelay?: number; + includeIndicatorStyles?: boolean; + indicatorClass?: 'htmx-indicator' | string; + requestClass?: 'htmx-request' | string; + settlingClass?: 'htmx-settling' | string; + swappingClass?: 'htmx-swapping' | string; + allowEval?: boolean; + attributesToSettle?: ["class", "style", "width", "height"] | string[]; + withCredentials?: boolean; + wsReconnectDelay?: 'full-jitter' | string; + disableSelector?: "[hx-disable], [data-hx-disable]" | string; + useTemplateFragments?: boolean; +} + +export declare var htmx: HtmxApi diff --git a/src/htmx.js b/src/htmx.js index 4c6bef38..7b5b67c6 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1,7 +1,9 @@ //AMD insanity (function (root, factory) { + //@ts-ignore if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. + //@ts-ignore define([], factory); } else { // Browser globals @@ -153,9 +155,11 @@ return (function () { var responseNode = responseDoc.body; while (depth > 0) { depth--; + // @ts-ignore responseNode = responseNode.firstChild; } if (responseNode == null) { + // @ts-ignore responseNode = getDocument().createDocumentFragment(); } return responseNode; @@ -1221,6 +1225,7 @@ return (function () { function getWebSocketReconnectDelay(retryCount) { var delay = htmx.config.wsReconnectDelay; if (typeof delay === 'function') { + // @ts-ignore return delay(retryCount); } if (delay === 'full-jitter') { @@ -1638,9 +1643,11 @@ return (function () { if (this.status >= 200 && this.status < 400) { triggerEvent(getDocument().body, "htmx:historyCacheMissLoad", details); var fragment = makeFragment(this.response); + // @ts-ignore fragment = fragment.querySelector('[hx-history-elt],[data-hx-history-elt]') || fragment; var historyElement = getHistoryElement(); var settleInfo = makeSettleInfo(historyElement); + // @ts-ignore swapInnerHTML(historyElement, fragment, settleInfo) settleImmediately(settleInfo.tasks); currentPathForHistory = path; @@ -2465,7 +2472,9 @@ return (function () { try { selectionInfo = { elt: activeElt, + // @ts-ignore start: activeElt ? activeElt.selectionStart : null, + // @ts-ignore end: activeElt ? activeElt.selectionEnd : null }; } catch (e) { @@ -2480,7 +2489,9 @@ return (function () { selectionInfo.elt.id) { var newActiveElt = document.getElementById(selectionInfo.elt.id); if (newActiveElt) { + // @ts-ignore if (selectionInfo.start && newActiveElt.setSelectionRange) { + // @ts-ignore newActiveElt.setSelectionRange(selectionInfo.start, selectionInfo.end); } newActiveElt.focus(); @@ -2632,6 +2643,7 @@ return (function () { function getMetaConfig() { var element = getDocument().querySelector('meta[name="htmx-config"]'); if (element) { + // @ts-ignore return parseJSON(element.content); } else { return null; diff --git a/www/_includes/layout.njk b/www/_includes/layout.njk index c2338be1..d65db806 100644 --- a/www/_includes/layout.njk +++ b/www/_includes/layout.njk @@ -28,7 +28,7 @@