@1agh/maude 2.0.2 → 2.0.3

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.
@@ -7,6 +7,10 @@
7
7
  // `save` returned in this run of the app
8
8
  // copyPng(blob) → navigator.clipboard, then `clipboard_write_png` (Q13: WKWebView
9
9
  // refuses ClipboardItem outside some gestures)
10
+ // copyText(text) → navigator.clipboard (≤ 800 ms), then `clipboard_write_text` (2.0.3:
11
+ // a click inside the canvas iframe leaves the
12
+ // shell without user activation; WKWebView
13
+ // refuses, or leaves the promise pending)
10
14
  //
11
15
  // The bytes of a save never pass through the page: Rust fetches the job from this project's own server by
12
16
  // id. `saveTo` is ExportSpecV2's (`ask` | `remembered` | `downloads`; absent = ask). Rust is the gate (the
@@ -25,8 +29,14 @@ export const SAVE_COMMANDS = Object.freeze({
25
29
  save: 'save_export',
26
30
  reveal: 'reveal_export',
27
31
  copyPng: 'clipboard_write_png',
32
+ copyText: 'clipboard_write_text',
28
33
  });
29
34
 
35
+ /** Mirrors export_save.rs `MAX_CLIPBOARD_TEXT` (UTF-8 bytes). */
36
+ export const MAX_CLIPBOARD_TEXT = 64 * 1024;
37
+ /** How long `navigator.clipboard.writeText` may stay pending before the shell writes it instead. */
38
+ export const BROWSER_COPY_TIMEOUT_MS = 800;
39
+
30
40
  export const SAVE_TO = Object.freeze(['ask', 'remembered', 'downloads']);
31
41
 
32
42
  /** Mirrors export_save.rs `MAX_CLIPBOARD_PNG`. */
@@ -79,6 +89,7 @@ export function nativeSave(win = typeof window !== 'undefined' ? window : null,
79
89
  const download = opts.download ?? ((jobId, filename) => browserDownload(win, jobId, filename));
80
90
  const clipboard = opts.clipboard ?? win?.navigator?.clipboard ?? null;
81
91
  const Item = opts.ClipboardItem ?? win?.ClipboardItem ?? null;
92
+ const copyTimeoutMs = opts.copyTimeoutMs ?? BROWSER_COPY_TIMEOUT_MS;
82
93
 
83
94
  return {
84
95
  available: !!invoke,
@@ -156,5 +167,38 @@ export function nativeSave(win = typeof window !== 'undefined' ? window : null,
156
167
  return { ok: false, reason: 'refused', error: words(e) };
157
168
  }
158
169
  },
170
+
171
+ /** Plain text. `navigator.clipboard` first, raced against a short timeout; then the shell. */
172
+ async copyText(text) {
173
+ const s = typeof text === 'string' ? text : '';
174
+ if (!s || s.includes('\0'))
175
+ return { ok: false, reason: 'refused', error: "There's nothing to copy." };
176
+ if (new TextEncoder().encode(s).length > MAX_CLIPBOARD_TEXT) {
177
+ return { ok: false, reason: 'refused', error: 'That text is too long to copy.' };
178
+ }
179
+ if (clipboard?.writeText) {
180
+ let timer = null;
181
+ try {
182
+ const done = await Promise.race([
183
+ Promise.resolve(clipboard.writeText(s)).then(() => true),
184
+ new Promise((res) => {
185
+ timer = setTimeout(() => res(false), copyTimeoutMs);
186
+ }),
187
+ ]);
188
+ if (done) return { ok: true, via: 'browser' };
189
+ } catch {
190
+ // refused outside a gesture it trusts — the shell writes it instead
191
+ } finally {
192
+ if (timer !== null) clearTimeout(timer);
193
+ }
194
+ }
195
+ if (!invoke) return { ok: false, reason: 'refused', error: "Couldn't copy the text." };
196
+ try {
197
+ await invoke(SAVE_COMMANDS.copyText, { text: s });
198
+ return { ok: true, via: 'shell' };
199
+ } catch (e) {
200
+ return { ok: false, reason: 'refused', error: words(e) };
201
+ }
202
+ },
159
203
  };
160
204
  }