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