@mochabug/adapt-astro 1.0.0-rc2 → 1.0.0-rc20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mochabug/adapt-astro",
3
- "version": "1.0.0-rc2",
3
+ "version": "1.0.0-rc20",
4
4
  "description": "Astro component for Adapt automation platform",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "scripts": {
19
19
  "build": "echo 'Astro component distributes source - no build needed'",
20
- "sample": "cd sample && npm install && npm run dev"
20
+ "sample": "npm run build && cd sample && npm install && npm run dev"
21
21
  },
22
22
  "keywords": [
23
23
  "adapt",
@@ -31,6 +31,6 @@
31
31
  "astro": "^3.0.0 || ^4.0.0 || ^5.0.0"
32
32
  },
33
33
  "dependencies": {
34
- "@mochabug/adapt-web": "^1.0.0-rc23"
34
+ "@mochabug/adapt-web": "^1.0.0-rc42"
35
35
  }
36
36
  }
package/src/Adapt.astro CHANGED
@@ -1,19 +1,27 @@
1
1
  ---
2
2
  import type { AdaptWebClientOptions } from "@mochabug/adapt-web";
3
3
 
4
- export interface Props
5
- extends Pick<
6
- AdaptWebClientOptions,
7
- | "id"
8
- | "sessionToken"
9
- | "authToken"
10
- | "inheritToken"
11
- | "inheritFrom"
12
- | "forkDisplayMode"
13
- | "sideBySideSplit"
14
- | "dialogBackdropClose"
15
- | "classNames"
16
- > {
4
+ export interface Props extends Pick<
5
+ AdaptWebClientOptions,
6
+ | "id"
7
+ | "sessionToken"
8
+ | "authToken"
9
+ | "transmitter"
10
+ | "signals"
11
+ | "challengeToken"
12
+ | "requiresChallenge"
13
+ | "capWidgetOptions"
14
+ | "inheritToken"
15
+ | "inheritFrom"
16
+ | "forkDisplayMode"
17
+ | "sideBySideSplit"
18
+ | "dialogBackdropClose"
19
+ | "darkMode"
20
+ | "autoResizing"
21
+ | "onSession"
22
+ | "onOutput"
23
+ | "classNames"
24
+ > {
17
25
  /** CSS class name for the container */
18
26
  class?: string;
19
27
  /** Inline styles for the container */
@@ -24,11 +32,18 @@ const {
24
32
  id,
25
33
  sessionToken,
26
34
  authToken,
35
+ transmitter,
36
+ signals,
37
+ challengeToken,
38
+ requiresChallenge = false,
39
+ capWidgetOptions,
27
40
  inheritToken,
28
41
  inheritFrom,
29
42
  forkDisplayMode = "side-by-side",
30
43
  sideBySideSplit = 50,
31
44
  dialogBackdropClose = false,
45
+ darkMode = false,
46
+ autoResizing = false,
32
47
  classNames,
33
48
  class: className,
34
49
  style,
@@ -37,73 +52,71 @@ const {
37
52
  const containerId = `adapt-${Math.random().toString(36).slice(2, 11)}`;
38
53
  ---
39
54
 
40
- <div
55
+ <adapt-automation
41
56
  id={containerId}
42
57
  class={className}
43
58
  style={style}
44
- data-adapt-id={id}
45
- data-adapt-session-token={sessionToken}
46
- data-adapt-auth-token={authToken}
47
- data-adapt-inherit-token={inheritToken}
59
+ automation-id={id}
60
+ session-token={sessionToken}
61
+ auth-token={authToken}
62
+ transmitter={transmitter}
63
+ challenge-token={challengeToken}
64
+ requires-challenge={requiresChallenge ? "" : undefined}
65
+ inherit-token={inheritToken}
66
+ fork-display-mode={forkDisplayMode}
67
+ side-by-side-split={String(sideBySideSplit)}
68
+ dialog-backdrop-close={dialogBackdropClose ? "" : undefined}
69
+ dark-mode={darkMode ? "" : undefined}
70
+ auto-resizing={autoResizing ? "" : undefined}
71
+ data-adapt-signals={signals ? JSON.stringify(signals) : undefined}
72
+ data-adapt-cap-widget-options={capWidgetOptions ? JSON.stringify(capWidgetOptions) : undefined}
48
73
  data-adapt-inherit-from={inheritFrom ? JSON.stringify(inheritFrom) : undefined}
49
- data-adapt-fork-display-mode={forkDisplayMode}
50
- data-adapt-side-by-side-split={sideBySideSplit}
51
- data-adapt-dialog-backdrop-close={dialogBackdropClose}
52
74
  data-adapt-class-names={classNames ? JSON.stringify(classNames) : undefined}
53
- ></div>
75
+ >
76
+ </adapt-automation>
54
77
 
55
78
  <script>
56
- import { AdaptWebClient } from "@mochabug/adapt-web";
79
+ import { AdaptAutomationElement } from "@mochabug/adapt-web";
57
80
 
58
- function initAdapt(container: HTMLElement) {
59
- const id = container.dataset.adaptId;
60
- if (!id) return;
81
+ // Ensure custom element is registered
82
+ void AdaptAutomationElement;
61
83
 
62
- const inheritFromStr = container.dataset.adaptInheritFrom;
63
- const classNamesStr = container.dataset.adaptClassNames;
84
+ function initAll() {
85
+ document
86
+ .querySelectorAll<AdaptAutomationElement>("adapt-automation")
87
+ .forEach((el) => {
88
+ // Set non-serializable properties from data attributes
89
+ const signalsStr = el.dataset.adaptSignals;
90
+ if (signalsStr) {
91
+ el.signals = JSON.parse(signalsStr);
92
+ }
64
93
 
65
- const client = new AdaptWebClient({
66
- container: container.id,
67
- id,
68
- sessionToken: container.dataset.adaptSessionToken,
69
- authToken: container.dataset.adaptAuthToken,
70
- inheritToken: container.dataset.adaptInheritToken,
71
- inheritFrom: inheritFromStr ? JSON.parse(inheritFromStr) : undefined,
72
- forkDisplayMode: container.dataset.adaptForkDisplayMode as "side-by-side" | "dialog",
73
- sideBySideSplit: Number(container.dataset.adaptSideBySideSplit) || 50,
74
- dialogBackdropClose: container.dataset.adaptDialogBackdropClose === "true",
75
- classNames: classNamesStr ? JSON.parse(classNamesStr) : undefined,
76
- });
94
+ const capStr = el.dataset.adaptCapWidgetOptions;
95
+ if (capStr) {
96
+ el.capWidgetOptions = JSON.parse(capStr);
97
+ }
77
98
 
78
- // Store reference for cleanup
79
- (container as any).__adaptClient = client;
80
- }
99
+ const inheritFromStr = el.dataset.adaptInheritFrom;
100
+ if (inheritFromStr) {
101
+ el.inheritFrom = JSON.parse(inheritFromStr);
102
+ }
81
103
 
82
- function cleanupAdapt(container: HTMLElement) {
83
- const client = (container as any).__adaptClient;
84
- if (client) {
85
- client.destroy();
86
- delete (container as any).__adaptClient;
87
- }
88
- }
104
+ const classNamesStr = el.dataset.adaptClassNames;
105
+ if (classNamesStr) {
106
+ el.classNames = JSON.parse(classNamesStr);
107
+ }
89
108
 
90
- // Initialize all Adapt containers on page load
91
- function initAll() {
92
- document.querySelectorAll<HTMLElement>("[data-adapt-id]").forEach((container) => {
93
- if (!(container as any).__adaptClient) {
94
- initAdapt(container);
95
- }
96
- });
109
+ // Dispatch events for callbacks (users can addEventListener on the element)
110
+ // onSession and onOutput are handled automatically via adapt-session and adapt-output CustomEvents
111
+ });
97
112
  }
98
113
 
99
114
  // Initial load
100
115
  initAll();
101
116
 
102
- // Handle Astro View Transitions (page navigation without full reload)
117
+ // Handle Astro View Transitions
103
118
  document.addEventListener("astro:page-load", initAll);
104
119
 
105
120
  // Cleanup before page swap (View Transitions)
106
- document.addEventListener("astro:before-swap", () => {
107
- document.querySelectorAll<HTMLElement>("[data-adapt-id]").forEach(cleanupAdapt);
108
- });
121
+ // The custom element handles its own cleanup in disconnectedCallback
109
122
  </script>
@@ -0,0 +1,96 @@
1
+ ---
2
+ import type { CapWidgetI18n } from "@mochabug/adapt-web";
3
+
4
+ export interface Props {
5
+ /** Automation ID for challenge endpoints */
6
+ automationId: string;
7
+ /** Number of workers for Cap.js */
8
+ workerCount?: number;
9
+ /** Custom labels for Cap.js widget */
10
+ i18n?: CapWidgetI18n;
11
+ /** Enable dark mode styling */
12
+ darkMode?: boolean;
13
+ /** CSS class name for the container */
14
+ class?: string;
15
+ /** Inline styles for the container */
16
+ style?: string;
17
+ }
18
+
19
+ const {
20
+ automationId,
21
+ workerCount,
22
+ i18n,
23
+ darkMode = false,
24
+ class: className,
25
+ style,
26
+ } = Astro.props;
27
+
28
+ const containerId = `adapt-cap-${Math.random().toString(36).slice(2, 11)}`;
29
+ ---
30
+
31
+ <!--
32
+ AdaptCap Astro component for proof-of-work challenges.
33
+
34
+ Since the client object is only available at runtime, you must initialize it
35
+ via JavaScript after the element is in the DOM:
36
+
37
+ ```js
38
+ import { createConnectClient } from "@mochabug/adapt-core/connect";
39
+ import { AdaptCapElement } from "@mochabug/adapt-web";
40
+
41
+ const rawClient = createConnectClient({ id: "my-automation" });
42
+
43
+ // Find the element and set the client property
44
+ const capEl = document.querySelector('adapt-cap') as AdaptCapElement;
45
+ capEl.client = rawClient;
46
+
47
+ // Listen for solve event
48
+ capEl.addEventListener('adapt-cap-solve', (e) => {
49
+ const { token, expires } = (e as CustomEvent).detail;
50
+ // Use token with client.run({ challengeToken: token })
51
+ });
52
+ ```
53
+ -->
54
+ <adapt-cap
55
+ id={containerId}
56
+ class={className}
57
+ style={style}
58
+ automation-id={automationId}
59
+ dark-mode={darkMode ? "" : undefined}
60
+ worker-count={workerCount !== undefined ? String(workerCount) : undefined}
61
+ data-adapt-cap-i18n={i18n ? JSON.stringify(i18n) : undefined}
62
+ >
63
+ </adapt-cap>
64
+
65
+ <script>
66
+ import { AdaptCapElement } from "@mochabug/adapt-web";
67
+ import type { AutomationClient } from "@mochabug/adapt-core";
68
+
69
+ // Ensure custom element is registered
70
+ void AdaptCapElement;
71
+
72
+ function initAll() {
73
+ document
74
+ .querySelectorAll<AdaptCapElement>("adapt-cap")
75
+ .forEach((el) => {
76
+ // Set i18n from data attribute if present
77
+ const i18nStr = el.dataset.adaptCapI18n;
78
+ if (i18nStr) {
79
+ el.i18n = JSON.parse(i18nStr);
80
+ }
81
+
82
+ // Listen for init event with client (legacy pattern for Astro)
83
+ el.addEventListener("adapt-cap:init", ((
84
+ e: CustomEvent<{ client: AutomationClient }>
85
+ ) => {
86
+ el.client = e.detail.client;
87
+ }) as EventListener);
88
+ });
89
+ }
90
+
91
+ // Initial setup
92
+ initAll();
93
+
94
+ // Handle Astro View Transitions
95
+ document.addEventListener("astro:page-load", initAll);
96
+ </script>
package/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  // Import the component directly from the .astro file
2
2
  // Usage: import Adapt from '@mochabug/adapt-astro/Adapt.astro';
3
3
 
4
- // Re-export types for convenience
5
- export type { Output, StatusJson } from "@mochabug/adapt-core";
6
- export type { AdaptWebClientOptions } from "@mochabug/adapt-web";
4
+ // Re-export everything from web (includes AdaptAutomationElement, AdaptCapElement)
5
+ export * from "@mochabug/adapt-web";