From 9837c96701f995788fb343a9156d2e58eb9bd7a3 Mon Sep 17 00:00:00 2001 From: Chris <7539871+paxperscientiam@users.noreply.github.com> Date: Fri, 21 May 2021 01:11:15 -0400 Subject: [PATCH 1/2] Config file for typechecking with typescript --- tsconfig.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tsconfig.json diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..fbbb1589 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "es3", + "module": "amd", + "allowJs": true, + "checkJs": true, + "noEmit": true, + "baseUrl": "./src" + }, + "include": ["./src/**/*"], + "verbose": true +} From cdd5c9e75657ac6d219b995dff18a718a17ef617 Mon Sep 17 00:00:00 2001 From: Chris <7539871+paxperscientiam@users.noreply.github.com> Date: Fri, 21 May 2021 19:01:09 -0400 Subject: [PATCH 2/2] add typings removed undesired comments. we are only documenting public API testing bump optional optionals done removed two junk files moved dependency to dev dependency --- jsconfig.json | 14 ++++++++++++++ package-lock.json | 6 ++++++ package.json | 7 +++++-- src/htmx.d.ts | 26 ++++++++++++++++++++++++++ src/htmx.js | 12 ++++++++++++ tsconfig.json | 12 ------------ 6 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 jsconfig.json create mode 100644 src/htmx.d.ts delete mode 100644 tsconfig.json 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 da3277d7..3df21717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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 482e4690..fac7f20d 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 f1b75801..65a5f0e3 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 @@ -150,9 +152,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; @@ -1205,6 +1209,7 @@ return (function () { function getWebSocketReconnectDelay(retryCount) { var delay = htmx.config.wsReconnectDelay; if (typeof delay === 'function') { + // @ts-ignore return delay(retryCount); } if (delay === 'full-jitter') { @@ -1598,9 +1603,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; @@ -2369,7 +2376,9 @@ return (function () { try { selectionInfo = { elt: activeElt, + // @ts-ignore start: activeElt ? activeElt.selectionStart : null, + // @ts-ignore end: activeElt ? activeElt.selectionEnd : null }; } catch (e) { @@ -2384,7 +2393,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(); @@ -2536,6 +2547,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/tsconfig.json b/tsconfig.json deleted file mode 100644 index fbbb1589..00000000 --- a/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "target": "es3", - "module": "amd", - "allowJs": true, - "checkJs": true, - "noEmit": true, - "baseUrl": "./src" - }, - "include": ["./src/**/*"], - "verbose": true -}