-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
86 lines (71 loc) · 2.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
import type { HtmxExtension } from "htmx.org";
//
(function () {
let api: HtmxApi;
htmx.defineExtension("chunked-transfer", {
init: function (apiRef: HtmxApi) {
api = apiRef;
},
onEvent: function (name, evt) {
const elt = evt.target as Element;
if (name === "htmx:beforeRequest") {
const xhr = evt.detail.xhr as XMLHttpRequest;
xhr.onprogress = function () {
const is_chunked =
xhr.getResponseHeader("Transfer-Encoding") === "chunked";
if (!is_chunked) return;
let response = xhr.response as string;
api.withExtensions(elt, function (extension) {
if (!extension.transformResponse) return;
response = extension.transformResponse(response, xhr, elt);
});
var swapSpec = api.getSwapSpecification(elt);
var target = api.getTarget(elt);
var settleInfo = api.makeSettleInfo(elt);
api.selectAndSwap(
swapSpec.swapStyle,
target,
elt,
response,
settleInfo,
);
api.settleImmediately(settleInfo.tasks);
};
}
},
} as HtmxExtension & { init: (apiRef: any) => void });
})();
//
declare global {
var htmx: typeof import("htmx.org");
interface Window {
htmx: typeof import("htmx.org");
}
}
//
// Inspired by https://github.com/delaneyj/nothtmx2
interface HtmxApi {
defineExtension(name: string, extension: HtmxExtension): void;
getSwapSpecification(elt: Element): { swapStyle: string };
getTarget(elt: Element): Element;
makeSettleInfo(elt: Element): SettleInfo;
selectAndSwap(
swapStyle: string,
target: Element,
elt: Element,
responseText: string,
settleInfo: SettleInfo,
): void;
settleImmediately(tasks: Task[]): void;
withExtensions(
elt: Element,
callback: (extension: HtmxExtension) => void,
): void;
}
interface SettleInfo {
title?: string;
elts: Element[];
tasks: Task[];
}
export type Task = () => void;