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
|
<script lang="ts">
import PhLightningFill from "virtual:icons/ph/lightning-fill";
import { onMount } from "svelte";
import Left from "$comp/Left.svelte";
import { f, s, focus, warning, sat, sats, localeMap } from "$lib/utils";
import { t } from "$lib/translations";
import { fiat as fiatStore } from "$lib/store";
// ---- Props (Svelte 5 runes) ----
let {
amount = $bindable(),
currency = "USD",
fiat = $bindable(),
element = $bindable(),
rate = $bindable(),
locale = undefined,
submit = $bindable(undefined),
amountFiat = $bindable(0),
} = $props();
// ---- Local state ----
let fiatDigits = $state("0"); // cents buffer in fiat mode
let html = $state("0");
// currency info as reactive state
let decimalChar = $state(".");
let symbol = $state("");
let position = $state("before");
function getCurrencyInfo(locale: any = "", currency: string) {
const l = typeof locale === "function" ? locale() : locale || localeMap[currency] || "en-US";
const region = l.length >= 2 ? l.slice(-2) : "";
const currencyDisplay = region && currency.startsWith(region) ? "narrowSymbol" : "symbol";
const formatter = new Intl.NumberFormat(l, {
style: "currency",
currency,
currencyDisplay,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const parts = formatter.formatToParts(123456.78);
const { value: decimal } = parts.find((p) => p.type === "decimal")!;
const currencyPart = parts.find((p) => p.type === "currency");
const sym = currencyPart ? currencyPart.value : "";
const firstNumberIndex = parts.findIndex((p) => p.type === "integer");
const currencyIndex = parts.findIndex((p) => p.type === "currency");
const pos = currencyIndex < firstNumberIndex ? "before" : "after";
return { symbol: sym, position: pos, decimal };
}
// reflect external prop/locale changes
$effect(() => {
const info = getCurrencyInfo(locale, currency);
decimalChar = info.decimal;
symbol = info.symbol;
position = info.position;
});
function formatFiatFromDigits(digits, dec = ".") {
let d = digits.replace(/\D+/g, "");
if (!d) d = "0";
if (d.length > 15) d = d.slice(0, 15);
if (d.length === 1) d = "0" + d;
if (d.length === 2) d = "0" + d;
const whole = d.slice(0, -2).replace(/^0+(?=\d)/, "") || "0";
const cents = d.slice(-2);
return `${whole}${dec}${cents}`;
}
function normalizeSats(str) {
const digits = (str ?? "").replace(/[^\d]/g, "");
const noLead = digits.replace(/^0+(?=\d)/, "");
return noLead || "0";
}
const updateByMode = () => {
if (fiat) {
const numericFiat = parseFloat((fiatDigits || "0").replace(/\D/g, "")) / 100;
amountFiat = isFinite(numericFiat) ? numericFiat.toFixed(2) : ("0.00" as any);
amount = numericFiat ? Math.round((numericFiat * sats) / rate) : 0;
} else {
// integer-only sats, already normalized
const a = parseInt((html || "0").replace(/[^\d]/g, "")) || 0;
amount = a || 0;
const fval = amount ? (amount * rate) / sats : 0;
amountFiat = fval ? fval.toFixed(2) : ("0.00" as any);
}
};
const pasted = async () => {
const txt = (await navigator.clipboard.readText()) ?? "";
if (fiat) {
const m = txt.replace(/[^\d.,]/g, "");
const normalized = m.replace(new RegExp(`[${decimalChar}]`, "g"), ".");
const val = parseFloat(normalized);
const cents = isFinite(val) ? Math.round(val * 100) : 0;
fiatDigits = Math.max(0, cents).toString();
html = formatFiatFromDigits(fiatDigits, decimalChar);
} else {
const clean = normalizeSats(txt);
html = clean;
element && (element.innerHTML = html);
}
input(null, true);
};
let prevHtml = $state("");
// (kept for completeness; no longer used in sats path)
function trimTrailingZeros(str) {
if (!str.includes(".")) return str;
let out = str.replace(/(\.\d*?[1-9])0+$/, "$1");
out = out.replace(/\.0*$/, "");
return out;
}
const input = (e, fromPad = false) => {
if (fiat) {
if (!fromPad) {
const raw = (element?.textContent ?? "").replace(/[^\d]/g, "");
fiatDigits = raw || "0";
}
const cents = parseInt(fiatDigits || "0") || 0;
const numericFiat = cents / 100;
const satsEstimate = Math.round((numericFiat * sats) / rate);
if (satsEstimate > sats && html.length > prevHtml.length) {
fiatDigits = fiatDigits.slice(0, -1) || "0";
warning($t("user.receive.lessThan1BTCWarning"));
}
html = formatFiatFromDigits(fiatDigits, decimalChar);
} else {
// integer-only sats, normalize leading zeros
let clean = normalizeSats(element?.textContent ?? "");
const maybe = parseInt(clean) || 0;
if (maybe > sats && clean.length > prevHtml.length) {
clean = normalizeSats(clean.slice(0, -1));
warning($t("user.receive.lessThan1BTCWarning"));
}
html = clean;
element && (element.innerHTML = html);
}
updateByMode();
if (!fromPad) {
queueMicrotask(() => {
const node = element?.childNodes?.[0];
if (!node) return;
const range = document.createRange();
const sel = getSelection();
const L = (node as any).length ?? node.textContent?.length ?? 0;
range.setStart(node, L);
range.setEnd(node, L);
sel?.removeAllRanges();
sel?.addRange(range);
});
}
prevHtml = html.toString();
};
const handleInput = (value: string) => {
if (fiat) {
if (value === arrow) {
fiatDigits = fiatDigits.length > 1 ? fiatDigits.slice(0, -1) : "0";
} else if (value === "00") {
fiatDigits = (fiatDigits === "0" ? "" : fiatDigits) + "00";
if (!fiatDigits) fiatDigits = "0";
} else {
fiatDigits = (fiatDigits === "0" ? "" : fiatDigits) + value;
}
html = formatFiatFromDigits(fiatDigits, decimalChar);
} else {
if (value === arrow) {
html = html.length > 1 ? html.slice(0, -1) : "0";
} else if (value === "00") {
// If current is "0", keep it "0"; otherwise append two zeros
html = html === "0" ? "0" : html + "00";
} else {
// If current is "0", replace with the digit; else append
html = html === "0" ? value : html + value;
}
// normalize away any accidental leading zeros
html = normalizeSats(html);
element && (element.innerHTML = html);
}
input(null, true);
};
const select = (e: any) => {
const range = document.createRange();
range.selectNodeContents(e.target);
const sel = getSelection();
sel?.removeAllRanges();
sel?.addRange(range);
};
const blur = () => {
if (!html) html = fiat ? formatFiatFromDigits("0", decimalChar) : "0";
};
const keydown = (e) => e.key === "Enter" && (e.preventDefault() || submit?.click?.());
// CHANGED: make swap set html for the new mode immediately and keep DOM in sync
const swap = () => {
if (fiat) {
// going -> sats
fiat = false;
html = normalizeSats((amount || 0).toString());
element && (element.innerHTML = html);
} else {
// going -> fiat
const cents = Math.round((parseFloat(String(amountFiat) || "0") || 0) * 100);
fiatDigits = Math.max(0, cents).toString();
fiat = true;
html = formatFiatFromDigits(fiatDigits, decimalChar);
element && (element.innerHTML = html);
}
updateByMode();
prevHtml = html.toString();
// Move caret to end after swap
queueMicrotask(() => {
const node = element?.childNodes?.[0];
if (!node) return;
const range = document.createRange();
const sel = getSelection();
const L = (node as any).length ?? node.textContent?.length ?? 0;
range.setStart(node, L);
range.setEnd(node, L);
sel?.removeAllRanges();
sel?.addRange(range);
});
$fiatStore = fiat;
};
const arrow = "<";
const numPad = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "00", "0", arrow];
onMount(() => {
fiat = $fiatStore;
if (fiat) {
const cents = Math.round((parseFloat(String(amountFiat) || "0") || 0) * 100);
fiatDigits = Math.max(0, cents).toString();
html = formatFiatFromDigits(fiatDigits, decimalChar); // keep html in sync on mount
} else {
html = normalizeSats((amount || 0).toString());
}
element && (element.innerHTML = html);
updateByMode();
});
</script>
<div class="flex justify-center items-center">
<div class="space-y-5 w-full">
<div class="text-center">
<div class="text-5xl md:text-6xl font-semibold tracking-widest flex justify-center">
<button
type="button"
class="my-auto cursor-pointer select-none"
class:text-5xl={!fiat}
aria-label="Swap currency display"
onclick={(e) => { e.preventDefault(); e.stopPropagation(); swap(); }}
>
{#if fiat}
{#if position === "before"}{symbol}{/if}
{:else}
<PhLightningFill width="48" class="text-yellow-300" />
{/if}
</button>
<!-- contenteditable input -->
<div
use:focus
role="textbox"
tabindex="0"
aria-label="Amount input"
aria-multiline="false"
contenteditable
bind:innerHTML={html}
inputmode={fiat ? "decimal" : "numeric"}
enterkeyhint="done"
autocapitalize="off"
autocorrect="off"
spellcheck="false"
onpaste={pasted}
onfocus={(e) => {
select(e);
}}
onblur={blur}
oninput={input}
onkeydown={keydown}
class="outline-none my-auto"
bind:this={element}
></div>
{#if fiat && position === "after"}
<button
type="button"
class="cursor-pointer select-none"
aria-label="Swap currency display"
onclick={(e) => { e.preventDefault(); e.stopPropagation(); swap(); }}
>{symbol}</button>
{/if}
</div>
</div>
<div class="grid grid-cols-3 gap-2 w-full mx-auto">
{#each numPad as value}
{#if value === arrow}
<button
type="button"
class="btn"
aria-label="Backspace"
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
handleInput(value);
}}
>
<Left />
</button>
{:else}
<button
type="button"
class="btn"
aria-label={`Key ${value}`}
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
handleInput(value);
}}
>
{value}
</button>
{/if}
{/each}
</div>
</div>
</div>
|