-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-host-example.html
More file actions
206 lines (195 loc) · 6.62 KB
/
remote-host-example.html
File metadata and controls
206 lines (195 loc) · 6.62 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>DataLab-Web — Remote control demo</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 0;
display: flex;
height: 100vh;
}
#panel {
width: 360px;
padding: 12px;
border-right: 1px solid #ccc;
display: flex;
flex-direction: column;
gap: 8px;
overflow-y: auto;
}
#panel h1 {
font-size: 16px;
margin: 0 0 4px;
}
#panel button {
padding: 6px 10px;
cursor: pointer;
}
#panel button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
#log {
flex: 1;
font-family: ui-monospace, Menlo, monospace;
font-size: 12px;
background: #111;
color: #d4d4d4;
padding: 8px;
overflow: auto;
white-space: pre-wrap;
border: 1px solid #333;
}
#frame-host {
flex: 1;
}
iframe {
width: 100%;
height: 100%;
border: 0;
}
.row {
display: flex;
gap: 6px;
}
.row > * {
flex: 1;
}
.status {
font-size: 12px;
color: #555;
}
.status.ok {
color: #2e7d32;
}
.status.err {
color: #c62828;
}
</style>
</head>
<body>
<div id="panel">
<h1>Remote control demo</h1>
<div id="status" class="status">Booting…</div>
<div class="row">
<button id="btn-version" disabled>get_version</button>
<button id="btn-list" disabled>list_signals</button>
</div>
<button id="btn-add-signal" disabled>add_signal (sine)</button>
<button id="btn-add-image" disabled>add_image (gradient)</button>
<div class="row">
<button id="btn-fft" disabled>calc(fft) on selection</button>
<button id="btn-get" disabled>get_signal_xy(current)</button>
</div>
<button id="btn-remove" disabled>delete_object(current)</button>
<button id="btn-reset" disabled>reset_all</button>
<div id="log" aria-live="polite"></div>
</div>
<div id="frame-host">
<iframe
id="dlw"
title="DataLab-Web"
src="/index.html?allowedOrigins=ORIGIN_PLACEHOLDER"
></iframe>
</div>
<script type="module">
// The SDK is consumed via Vite's dev server (TS on the fly) so this
// demo page can be used unmodified for both manual testing and the
// Playwright E2E spec.
import { DataLabWebClient } from "/packages/sdk/src/index.ts";
const log = document.getElementById("log");
const status = document.getElementById("status");
const iframe = document.getElementById("dlw");
// The iframe whitelists this host's origin. Patched in at load time
// so the same file works whether served from localhost:5173 or any
// other deployment.
iframe.src = iframe.src.replace(
"ORIGIN_PLACEHOLDER",
encodeURIComponent(window.location.origin),
);
function logLine(kind, text) {
const ts = new Date().toISOString().slice(11, 23);
log.textContent += `[${ts}] ${kind} ${text}\n`;
log.scrollTop = log.scrollHeight;
}
const client = new DataLabWebClient(iframe, {
targetOrigin: new URL(iframe.src).origin,
});
// Expose for Playwright (`page.evaluate(() => window.__client.…)`).
window.__client = client;
const buttons = {
version: document.getElementById("btn-version"),
list: document.getElementById("btn-list"),
addSignal: document.getElementById("btn-add-signal"),
addImage: document.getElementById("btn-add-image"),
fft: document.getElementById("btn-fft"),
get: document.getElementById("btn-get"),
remove: document.getElementById("btn-remove"),
reset: document.getElementById("btn-reset"),
};
function setEnabled(enabled) {
for (const btn of Object.values(buttons)) btn.disabled = !enabled;
}
async function wrap(label, fn) {
try {
logLine("→", label);
const result = await fn();
logLine("←", `${label} → ${JSON.stringify(result)?.slice(0, 200)}`);
return result;
} catch (err) {
logLine("✗", `${label} → ${err.message ?? err}`);
throw err;
}
}
buttons.version.onclick = () =>
wrap("get_version", () => client.getVersion());
buttons.list.onclick = () =>
wrap("list_signals", () => client.listSignals());
buttons.addSignal.onclick = async () => {
const N = 256;
const xs = Array.from({ length: N }, (_, i) => i / N);
const ys = xs.map((x) => Math.sin(2 * Math.PI * 5 * x));
await wrap("add_signal", () => client.addSignal("Sine 5 Hz", xs, ys));
};
buttons.addImage.onclick = async () => {
const N = 32;
const data = Array.from({ length: N }, (_, i) =>
Array.from({ length: N }, (_, j) => i + j),
);
await wrap("add_image", () => client.addImage("Gradient", data));
};
// Operate on whatever the user has selected inside the iframe.
// ``client.calc(...)`` with ``sources=null`` already defaults to
// the current selection on the DataLab-Web side.
buttons.fft.onclick = () => wrap("calc(fft)", () => client.calc("fft"));
buttons.get.onclick = async () => {
const sel = await client.getSelection();
if (sel.length === 0) return logLine("!", "no current selection");
return wrap("get_signal_xy", () => client.getSignalXY(sel[0]));
};
buttons.remove.onclick = async () => {
const sel = await client.getSelection();
if (sel.length === 0) return logLine("!", "no current selection");
return wrap("delete_object", () => client.deleteObject(sel[0]));
};
buttons.reset.onclick = () => wrap("reset_all", () => client.resetAll());
(async () => {
try {
status.textContent = "Waiting for DataLab-Web to be ready…";
const version = await client.ready(180_000);
status.textContent = `Ready — DataLab-Web v${version}`;
status.className = "status ok";
setEnabled(true);
// Marker for E2E: a test waits on this to know the demo is wired.
document.body.dataset.remoteReady = "1";
} catch (err) {
status.textContent = `Failed: ${err.message ?? err}`;
status.className = "status err";
logLine("✗", `ready: ${err.message ?? err}`);
}
})();
</script>
</body>
</html>