@mochabug/adapt-astro 1.0.0-rc3 → 1.0.0-rc30
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 +3 -3
- package/src/Adapt.astro +83 -91
- package/src/AdaptCap.astro +96 -0
- package/src/index.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mochabug/adapt-astro",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc30",
|
|
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-
|
|
34
|
+
"@mochabug/adapt-web": "^1.0.0-rc53"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/Adapt.astro
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
---
|
|
2
|
-
import type { AdaptWebClientOptions } from "@mochabug/adapt-web";
|
|
2
|
+
import type { AdaptWebClientOptions, ForkDisplay } from "@mochabug/adapt-web";
|
|
3
3
|
|
|
4
|
-
export interface Props
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
| "forkDisplay"
|
|
17
|
+
| "darkMode"
|
|
18
|
+
| "autoResizing"
|
|
19
|
+
| "onSession"
|
|
20
|
+
| "onOutput"
|
|
21
|
+
| "onForkActive"
|
|
22
|
+
| "classNames"
|
|
23
|
+
> {
|
|
21
24
|
/** CSS class name for the container */
|
|
22
25
|
class?: string;
|
|
23
26
|
/** Inline styles for the container */
|
|
@@ -30,108 +33,97 @@ const {
|
|
|
30
33
|
authToken,
|
|
31
34
|
transmitter,
|
|
32
35
|
signals,
|
|
36
|
+
challengeToken,
|
|
37
|
+
requiresChallenge = false,
|
|
38
|
+
capWidgetOptions,
|
|
33
39
|
inheritToken,
|
|
34
40
|
inheritFrom,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
onSession,
|
|
39
|
-
onOutput,
|
|
41
|
+
forkDisplay,
|
|
42
|
+
darkMode = false,
|
|
43
|
+
autoResizing = false,
|
|
40
44
|
classNames,
|
|
41
45
|
class: className,
|
|
42
46
|
style,
|
|
43
47
|
} = Astro.props;
|
|
44
48
|
|
|
49
|
+
const forkDisplayMode = forkDisplay?.mode ?? "side-by-side";
|
|
50
|
+
const sideBySideSplit = forkDisplay?.mode === "side-by-side" ? (forkDisplay.split ?? 50) : 50;
|
|
51
|
+
const dialogBackdropClose = forkDisplay?.mode === "dialog" && forkDisplay.backdropClose;
|
|
52
|
+
const dialogResizeToContent = forkDisplay?.mode === "dialog" && forkDisplay.resizeToContent;
|
|
53
|
+
|
|
45
54
|
const containerId = `adapt-${Math.random().toString(36).slice(2, 11)}`;
|
|
46
55
|
---
|
|
47
56
|
|
|
48
|
-
<
|
|
57
|
+
<adapt-automation
|
|
49
58
|
id={containerId}
|
|
50
59
|
class={className}
|
|
51
60
|
style={style}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
automation-id={id}
|
|
62
|
+
session-token={sessionToken}
|
|
63
|
+
auth-token={authToken}
|
|
64
|
+
transmitter={transmitter}
|
|
65
|
+
challenge-token={challengeToken}
|
|
66
|
+
requires-challenge={requiresChallenge ? "" : undefined}
|
|
67
|
+
inherit-token={inheritToken}
|
|
68
|
+
fork-display-mode={forkDisplayMode}
|
|
69
|
+
side-by-side-split={String(sideBySideSplit)}
|
|
70
|
+
dialog-backdrop-close={dialogBackdropClose ? "" : undefined}
|
|
71
|
+
dialog-resize-to-content={dialogResizeToContent ? "" : undefined}
|
|
72
|
+
dark-mode={darkMode ? "" : undefined}
|
|
73
|
+
auto-resizing={autoResizing ? "" : undefined}
|
|
56
74
|
data-adapt-signals={signals ? JSON.stringify(signals) : undefined}
|
|
57
|
-
data-adapt-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
data-adapt-
|
|
61
|
-
|
|
75
|
+
data-adapt-cap-widget-options={capWidgetOptions
|
|
76
|
+
? JSON.stringify(capWidgetOptions)
|
|
77
|
+
: undefined}
|
|
78
|
+
data-adapt-inherit-from={inheritFrom
|
|
79
|
+
? JSON.stringify(inheritFrom)
|
|
80
|
+
: undefined}
|
|
62
81
|
data-adapt-class-names={classNames ? JSON.stringify(classNames) : undefined}
|
|
63
|
-
|
|
82
|
+
>
|
|
83
|
+
</adapt-automation>
|
|
64
84
|
|
|
65
85
|
<script>
|
|
66
|
-
import {
|
|
86
|
+
import { AdaptAutomationElement } from "@mochabug/adapt-web";
|
|
67
87
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (!id) return;
|
|
88
|
+
// Ensure custom element is registered
|
|
89
|
+
void AdaptAutomationElement;
|
|
71
90
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
91
|
+
function initAll() {
|
|
92
|
+
document
|
|
93
|
+
.querySelectorAll<AdaptAutomationElement>("adapt-automation")
|
|
94
|
+
.forEach((el) => {
|
|
95
|
+
// Set non-serializable properties from data attributes
|
|
96
|
+
const signalsStr = el.dataset.adaptSignals;
|
|
97
|
+
if (signalsStr) {
|
|
98
|
+
el.signals = JSON.parse(signalsStr);
|
|
99
|
+
}
|
|
75
100
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
authToken: container.dataset.adaptAuthToken,
|
|
81
|
-
transmitter: container.dataset.adaptTransmitter,
|
|
82
|
-
signals: signalsStr ? JSON.parse(signalsStr) : undefined,
|
|
83
|
-
inheritToken: container.dataset.adaptInheritToken,
|
|
84
|
-
inheritFrom: inheritFromStr ? JSON.parse(inheritFromStr) : undefined,
|
|
85
|
-
forkDisplayMode: container.dataset.adaptForkDisplayMode as "side-by-side" | "dialog",
|
|
86
|
-
sideBySideSplit: Number(container.dataset.adaptSideBySideSplit) || 50,
|
|
87
|
-
dialogBackdropClose: container.dataset.adaptDialogBackdropClose === "true",
|
|
88
|
-
classNames: classNamesStr ? JSON.parse(classNamesStr) : undefined,
|
|
89
|
-
// onSession and onOutput callbacks can be attached via custom events:
|
|
90
|
-
// container.addEventListener('adapt:session', (e) => { ... })
|
|
91
|
-
// container.addEventListener('adapt:output', (e) => { ... })
|
|
92
|
-
onSession: (status, fork) => {
|
|
93
|
-
container.dispatchEvent(new CustomEvent('adapt:session', {
|
|
94
|
-
detail: { status, fork },
|
|
95
|
-
bubbles: true
|
|
96
|
-
}));
|
|
97
|
-
},
|
|
98
|
-
onOutput: (output) => {
|
|
99
|
-
container.dispatchEvent(new CustomEvent('adapt:output', {
|
|
100
|
-
detail: { output },
|
|
101
|
-
bubbles: true
|
|
102
|
-
}));
|
|
103
|
-
},
|
|
104
|
-
});
|
|
101
|
+
const capStr = el.dataset.adaptCapWidgetOptions;
|
|
102
|
+
if (capStr) {
|
|
103
|
+
el.capWidgetOptions = JSON.parse(capStr);
|
|
104
|
+
}
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
const inheritFromStr = el.dataset.adaptInheritFrom;
|
|
107
|
+
if (inheritFromStr) {
|
|
108
|
+
el.inheritFrom = JSON.parse(inheritFromStr);
|
|
109
|
+
}
|
|
109
110
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
delete (container as any).__adaptClient;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
111
|
+
const classNamesStr = el.dataset.adaptClassNames;
|
|
112
|
+
if (classNamesStr) {
|
|
113
|
+
el.classNames = JSON.parse(classNamesStr);
|
|
114
|
+
}
|
|
117
115
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (!(container as any).__adaptClient) {
|
|
122
|
-
initAdapt(container);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
116
|
+
// Dispatch events for callbacks (users can addEventListener on the element)
|
|
117
|
+
// onSession and onOutput are handled automatically via adapt-session and adapt-output CustomEvents
|
|
118
|
+
});
|
|
125
119
|
}
|
|
126
120
|
|
|
127
121
|
// Initial load
|
|
128
122
|
initAll();
|
|
129
123
|
|
|
130
|
-
// Handle Astro View Transitions
|
|
124
|
+
// Handle Astro View Transitions
|
|
131
125
|
document.addEventListener("astro:page-load", initAll);
|
|
132
126
|
|
|
133
127
|
// Cleanup before page swap (View Transitions)
|
|
134
|
-
|
|
135
|
-
document.querySelectorAll<HTMLElement>("[data-adapt-id]").forEach(cleanupAdapt);
|
|
136
|
-
});
|
|
128
|
+
// The custom element handles its own cleanup in disconnectedCallback
|
|
137
129
|
</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,5 +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 everything from web
|
|
4
|
+
// Re-export everything from web (includes AdaptAutomationElement, AdaptCapElement)
|
|
5
5
|
export * from "@mochabug/adapt-web";
|