-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskpane.html
More file actions
403 lines (370 loc) · 11.1 KB
/
taskpane.html
File metadata and controls
403 lines (370 loc) · 11.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!doctype html>
<html lang="da">
<head>
<meta charset="utf-8" />
<title>Martha Reader til Word</title>
<!-- Office.js -->
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
<!-- React + Babel (CDN, så du ikke behøver build) -->
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- Martha klient -->
<script src="marthaClient.js"></script>
<style>
body {
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: #111;
}
.martha-root { padding: 12px; }
.martha-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.martha-sub {
margin: 0;
font-size: 0.85rem;
opacity: 0.7;
}
.martha-brand {
display: flex;
flex-direction: column;
align-items: center;
font-size: 0.8rem;
}
.martha-brand img {
height: 32px;
margin-bottom: 4px;
}
.martha-section {
margin-top: 12px;
padding-top: 8px;
border-top: 1px solid #eee;
}
.martha-btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 8px 10px;
margin: 4px 4px 0 0;
border-radius: 8px;
border: none;
cursor: pointer;
font-size: 0.9rem;
background: #e4e4e4;
}
.martha-btn.primary {
background: #0078d4;
color: #fff;
}
.martha-btn.subtle {
background: #f5f5f5;
}
.martha-btn:disabled {
opacity: 0.5;
cursor: default;
}
.martha-player {
display: flex;
gap: 6px;
margin-bottom: 4px;
}
.martha-status {
font-size: 0.8rem;
opacity: 0.7;
}
.martha-preview {
max-height: 120px;
overflow: auto;
margin-top: 6px;
background: #fafafa;
border-radius: 6px;
padding: 6px;
font-size: 0.8rem;
}
.martha-preview pre {
margin: 0;
white-space: pre-wrap;
}
/* Samtykke-overlay */
.martha-consent-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.65);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
}
.martha-consent-box {
background: #fff;
color: #111;
max-width: 420px;
padding: 18px;
border-radius: 12px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
font-size: 0.9rem;
}
.martha-consent-box h2 {
margin-top: 0;
}
.martha-consent-actions {
margin-top: 12px;
display: flex;
justify-content: flex-end;
gap: 8px;
}
.martha-consent-actions button {
padding: 6px 10px;
border-radius: 8px;
border: none;
cursor: pointer;
}
.martha-consent-actions button:first-child {
background: #0078d4;
color: #fff;
}
.martha-consent-actions button:last-child {
background: #f2f2f2;
}
</style>
</head>
<body>
<div id="root"></div>
<!-- React-kode -->
<script type="text/babel">
const { useState, useEffect, useRef } = React;
const CONSENT_KEY = "marthaWordConsentAccepted";
function App() {
const [hasConsent, setHasConsent] = useState(null); // null = ikke loadet endnu
const [status, setStatus] = useState("Klar.");
const [isBusy, setIsBusy] = useState(false);
const [isPlaying, setIsPlaying] = useState(false);
const [currentText, setCurrentText] = useState("");
const audioRef = useRef(null);
useEffect(() => {
try {
const stored = window.localStorage.getItem(CONSENT_KEY);
setHasConsent(stored === "true");
} catch {
setHasConsent(false);
}
}, []);
useEffect(() => {
return () => {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
}
};
}, []);
function ensureConsent() {
if (!hasConsent) {
setStatus("Du skal acceptere samtykke for at bruge Martha.");
return false;
}
return true;
}
function handleAcceptConsent() {
window.localStorage.setItem(CONSENT_KEY, "true");
setHasConsent(true);
}
function handleDeclineConsent() {
window.localStorage.removeItem(CONSENT_KEY);
setHasConsent(false);
setStatus("Samtykke ikke givet – oplæsning er slået fra.");
}
async function getSelectionText() {
return Word.run(async (context) => {
const sel = context.document.getSelection();
sel.load("text");
await context.sync();
return sel.text || "";
});
}
async function getWholeDocumentText() {
return Word.run(async (context) => {
const body = context.document.body;
body.load("text");
await context.sync();
return body.text || "";
});
}
async function prepareAndPlay(text) {
if (!text.trim()) {
setStatus("Der blev ikke fundet tekst.");
return;
}
if (!ensureConsent()) return;
setIsBusy(true);
setStatus("Kontakter Martha…");
setCurrentText(text);
try {
const resp = await window.marthaClient.requestMarthaTTS(text);
const audioUrl = window.marthaClient.buildMarthaAudioUrl(resp.fn);
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
}
const audio = new Audio(audioUrl);
audio.playbackRate = 1.0;
audio.addEventListener("ended", () => {
setIsPlaying(false);
setStatus("Færdig.");
});
audio.addEventListener("error", () => {
setStatus("Kunne ikke afspille lydfilen.");
setIsPlaying(false);
});
audioRef.current = audio;
await audio.play();
setIsPlaying(true);
setStatus("Afspiller…");
} catch (err) {
console.error(err);
setStatus(err && err.message ? err.message : "Der opstod en fejl ved kald til Martha.");
setIsPlaying(false);
} finally {
setIsBusy(false);
}
}
async function onReadSelection() {
setIsBusy(true);
setStatus("Henter markeret tekst…");
try {
const txt = await getSelectionText();
await prepareAndPlay(txt);
} finally {
setIsBusy(false);
}
}
async function onReadDocument() {
setIsBusy(true);
setStatus("Henter dokumenttekst…");
try {
const txt = await getWholeDocumentText();
await prepareAndPlay(txt);
} finally {
setIsBusy(false);
}
}
async function onTogglePlayPause() {
const audio = audioRef.current;
if (!audio) return;
if (audio.paused) {
await audio.play();
setIsPlaying(true);
setStatus("Afspiller…");
} else {
audio.pause();
setIsPlaying(false);
setStatus("Sat på pause.");
}
}
function onStop() {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current.currentTime = 0;
}
setIsPlaying(false);
setStatus("Stoppet.");
}
function onResetConsent() {
window.localStorage.removeItem(CONSENT_KEY);
setHasConsent(false);
setStatus("Samtykke nulstillet.");
}
return (
<div className="martha-root">
{hasConsent === false && (
<div className="martha-consent-overlay">
<div className="martha-consent-box">
<h2>Beskyttelse af dine data</h2>
<p>
<b>Martha Læser</b> bruger tekst-til-tale fra{" "}
<a href="https://oqaasileriffik.gl" target="_blank" rel="noreferrer">
Oqaasileriffik.gl
</a>.
Kun den tekst, du beder om at få læst højt, sendes midlertidigt
til deres server.
</p>
<p>
Der gemmes ingen personoplysninger, og intet bruges til statistik
eller reklame. Ved at trykke <b>Accepter</b> giver du samtykke til,
at den valgte tekst må sendes til Martha-tjenesten.
</p>
<div className="martha-consent-actions">
<button onClick={handleAcceptConsent}>Accepter</button>
<button onClick={handleDeclineConsent}>Afvis</button>
</div>
</div>
</div>
)}
<header className="martha-header">
<div>
<h1>Martha Reader til Word</h1>
<p className="martha-sub">Læs markeret tekst eller hele dokumentet højt.</p>
</div>
<div className="martha-brand">
<img
src="https://oqaasileriffik.gl/wp-content/uploads/2021/04/knot2.png"
alt="Martha"
/>
<span>Martha TTS</span>
</div>
</header>
<section className="martha-section">
<h2>Læs fra dokumentet</h2>
<button onClick={onReadSelection} disabled={isBusy} className="martha-btn primary">
🔊 Læs markeret tekst
</button>
<button onClick={onReadDocument} disabled={isBusy} className="martha-btn">
📄 Læs hele dokumentet
</button>
</section>
<section className="martha-section">
<h2>Afspilning</h2>
<div className="martha-player">
<button
onClick={onTogglePlayPause}
disabled={!audioRef.current}
className="martha-btn"
>
{isPlaying ? "⏸ Pause" : "▶ Afspil"}
</button>
<button
onClick={onStop}
disabled={!audioRef.current}
className="martha-btn"
>
⏹ Stop
</button>
</div>
<p className="martha-status">Status: {status}</p>
{currentText && (
<details className="martha-preview">
<summary>Vis tekst der læses</summary>
<pre>{currentText}</pre>
</details>
)}
</section>
<section className="martha-section">
<h2>Indstillinger</h2>
<button onClick={onResetConsent} className="martha-btn subtle">
Nulstil samtykke
</button>
</section>
</div>
);
}
Office.onReady(() => {
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
});
</script>
</body>
</html>