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