Route Soma clipboard reset through timers

active-uix
dev 5 months ago
parent 248152263c
commit ad62a440d3

@ -5,7 +5,7 @@ import { Soma } from '../../core/soma.svelte';
import { CLIPBOARD_LANGS } from './langs'; import { CLIPBOARD_LANGS } from './langs';
import { clipboardMorfo } from '../../../morfo/components/clipboard'; import { clipboardMorfo } from '../../../morfo/components/clipboard';
import type { SomaRuntime, SomaRuntimePart } from '../../runtime.svelte'; import type { SomaRuntime, SomaRuntimePart } from '../../runtime.svelte';
import type { TimerHandle } from '$timer';
// ── Root provider ────────────────────────────────────────────────────────── // ── Root provider ──────────────────────────────────────────────────────────
@ -37,7 +37,7 @@ export class ClipboardProvider {
} }
copied = $state(false); copied = $state(false);
private resetTimer: ReturnType<typeof setTimeout> | null = null; private resetTimer: TimerHandle | null = null;
private constructor(opts: ClipboardOpts) { private constructor(opts: ClipboardOpts) {
this.opts = opts; this.opts = opts;
@ -56,9 +56,7 @@ export class ClipboardProvider {
}); });
$effect(() => { $effect(() => {
return () => { return () => this.clearResetTimer();
if (this.resetTimer) clearTimeout(this.resetTimer);
};
}); });
} }
@ -79,17 +77,33 @@ export class ClipboardProvider {
this.copied = true; this.copied = true;
this.opts.onCopy.current?.(value); this.opts.onCopy.current?.(value);
if (this.resetTimer) clearTimeout(this.resetTimer); this.clearResetTimer();
this.resetTimer = setTimeout(() => { this.resetTimer = this.soma.uix.timers.schedule(
this.copied = false; this.resetTimerKey,
this.resetTimer = null; this.opts.timeout.current,
}, this.opts.timeout.current); () => {
this.copied = false;
this.resetTimer = null;
},
{ replace: true, meta: { component: 'clipboard', action: 'reset-copied' } }
);
} catch (err) { } catch (err) {
this.opts.onError.current?.(err); this.opts.onError.current?.(err);
throw err; throw err;
} }
}; };
private clearResetTimer() {
if (this.resetTimer) {
this.resetTimer.cancel();
this.resetTimer = null;
}
}
private get resetTimerKey() {
return `soma:clipboard:${this.opts.id.current}:reset`;
}
readonly snippetProps = $derived.by(() => ({ readonly snippetProps = $derived.by(() => ({
value: this.opts.value.current, value: this.opts.value.current,
copied: this.copied, copied: this.copied,

Loading…
Cancel
Save

Powered by TurnKey Linux.