astro-altcha 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # `astro-altcha`
2
2
 
3
+ > **Warning** Early development. Expect bugs and frequent breaking changes.
4
+
3
5
  This is an [Astro integration](https://docs.astro.build/en/guides/integrations-guide/) that sets up altcha for Astro.
4
6
 
5
7
  ## Usage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-altcha",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Altcha integration for Astro",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -32,4 +32,52 @@ const {
32
32
  />
33
33
  </form>
34
34
 
35
- <script src="./altcha-form-script.ts" />
35
+ <script>
36
+ import type { WidgetAttributes, WidgetMethods } from "altcha/types";
37
+ import type { AstroAltchaHydrationStrategy } from "astro-altcha/types";
38
+
39
+ const altchaWidgets = document.querySelectorAll("altcha-widget");
40
+
41
+ for (const widget of altchaWidgets) {
42
+ if (widget.getAttribute("data-astro-altcha") !== "astro-altcha-form-widget") {
43
+ continue;
44
+ }
45
+
46
+ const form = widget.closest("form");
47
+ if (!form) {
48
+ throw new Error("Widget must be placed inside a form element");
49
+ }
50
+
51
+ const config = form.querySelector("script");
52
+ if (config?.getAttribute("data-astro-altcha") !== "config") continue;
53
+
54
+ const {
55
+ hydrationStrategy,
56
+ debug,
57
+ }: {
58
+ hydrationStrategy: AstroAltchaHydrationStrategy;
59
+ debug: boolean;
60
+ } = JSON.parse(config.innerText);
61
+ if (hydrationStrategy === "onload") {
62
+ await loadAltcha(form, widget, debug);
63
+ } else {
64
+ form.addEventListener("focusin", () => loadAltcha(form, widget, debug), {
65
+ once: true,
66
+ });
67
+ }
68
+ }
69
+
70
+ async function loadAltcha(
71
+ form: HTMLFormElement,
72
+ widget: WidgetAttributes & WidgetMethods & HTMLElement,
73
+ debug: boolean,
74
+ ) {
75
+ await import("altcha");
76
+ widget.configure({ debug });
77
+
78
+ const configureEvent = new CustomEvent("astro-altcha:init", {
79
+ detail: { widget },
80
+ });
81
+ form.dispatchEvent(configureEvent);
82
+ }
83
+ </script>
@@ -1 +0,0 @@
1
- export {}
@@ -1,21 +0,0 @@
1
- //#region src/components/altcha-form-script.ts
2
- const altchaWidgets = document.querySelectorAll("altcha-widget");
3
- for (const widget of altchaWidgets) {
4
- if (widget.getAttribute("data-astro-altcha") !== "astro-altcha-form-widget") continue;
5
- const form = widget.closest("form");
6
- if (!form) throw new Error("Widget must be placed inside a form element");
7
- const config = form.querySelector("script");
8
- if (config?.getAttribute("data-astro-altcha") !== "config") continue;
9
- const { hydrationStrategy, debug } = JSON.parse(config.innerText);
10
- if (hydrationStrategy === "onload") await loadAltcha(form, widget, debug);
11
- else form.addEventListener("focusin", () => loadAltcha(form, widget, debug), { once: true });
12
- }
13
- async function loadAltcha(form, widget, debug) {
14
- await import("altcha");
15
- widget.configure({ debug });
16
- const configureEvent = new CustomEvent("astro-altcha:init", { detail: { widget } });
17
- form.dispatchEvent(configureEvent);
18
- }
19
-
20
- //#endregion
21
- export { };
@@ -1,47 +0,0 @@
1
- import type { WidgetAttributes, WidgetMethods } from "altcha/types";
2
- import type { AstroAltchaHydrationStrategy } from "astro-altcha/types";
3
-
4
- const altchaWidgets = document.querySelectorAll("altcha-widget");
5
-
6
- for (const widget of altchaWidgets) {
7
- if (widget.getAttribute("data-astro-altcha") !== "astro-altcha-form-widget") {
8
- continue;
9
- }
10
-
11
- const form = widget.closest("form");
12
- if (!form) {
13
- throw new Error("Widget must be placed inside a form element");
14
- }
15
-
16
- const config = form.querySelector("script");
17
- if (config?.getAttribute("data-astro-altcha") !== "config") continue;
18
-
19
- const {
20
- hydrationStrategy,
21
- debug,
22
- }: {
23
- hydrationStrategy: AstroAltchaHydrationStrategy;
24
- debug: boolean;
25
- } = JSON.parse(config.innerText);
26
- if (hydrationStrategy === "onload") {
27
- await loadAltcha(form, widget, debug);
28
- } else {
29
- form.addEventListener("focusin", () => loadAltcha(form, widget, debug), {
30
- once: true,
31
- });
32
- }
33
- }
34
-
35
- async function loadAltcha(
36
- form: HTMLFormElement,
37
- widget: WidgetAttributes & WidgetMethods & HTMLElement,
38
- debug: boolean,
39
- ) {
40
- await import("altcha");
41
- widget.configure({ debug });
42
-
43
- const configureEvent = new CustomEvent("astro-altcha:init", {
44
- detail: { widget },
45
- });
46
- form.dispatchEvent(configureEvent);
47
- }