aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-11-28 16:24:45 -0800
committerAdam Soltys <[email protected]>2024-11-28 16:24:45 -0800
commit99055df277e94ec560d01f2b85bca5bc92849ad8 (patch)
tree1682080d8c1c39d2d46d8318b5cea6a5040a6aba
parent92aaa392926982d95845d5482842b1a46719dafe (diff)
downloadcoinos-ui-99055df277e94ec560d01f2b85bca5bc92849ad8.tar.gz
coinos-ui-99055df277e94ec560d01f2b85bca5bc92849ad8.zip
fix numpad
-rw-r--r--src/components/Numpad.svelte56
-rw-r--r--src/service-worker.js67
2 files changed, 66 insertions, 57 deletions
diff --git a/src/components/Numpad.svelte b/src/components/Numpad.svelte
index f694679a..85f4b07f 100644
--- a/src/components/Numpad.svelte
+++ b/src/components/Numpad.svelte
@@ -74,14 +74,20 @@
const handleInput = (value) => {
if (value === "<") html = html.substr(0, html.length - 1);
else html += value;
- input();
+ input(null, true);
};
- $: html = $fiat ? amountFiat : s(amount);
+ $: html = $fiat ? amountFiat : (amount || 0).toString();
let prev = "";
- let input = (e) => {
+ let input = (e, numpad = false) => {
+ let d = decimal.replace(".", "\\.");
+
+ if (!$fiat && html.includes(decimal)) {
+ html = html.replace(new RegExp(d), "");
+ }
+
if (prev === "0" && html !== `0${decimal}`) {
prev = "";
html = html.replace("0", "");
@@ -107,7 +113,6 @@
i = html.length;
}
- let d = decimal.replace(".", "\\.");
let clean = html
.substr(0, 15)
.replace(new RegExp(`[^0-9${d}]+`, "g"), "")
@@ -120,51 +125,46 @@
i = html.length;
}
if (amount > sats && html.length > prev.length) {
- html = prev;
+ html = html.substr(0, html.length - 1);
+ i = html.length;
warning($t("user.receive.lessThan1BTCWarning"));
- i--;
}
if ($fiat) {
amountFiat = html;
} else {
- amount = parseInt(html.replace(/,/g, ""));
+ amount = parseInt(html);
}
if (!amount) amount = null;
- setTimeout(() => {
- if (!$fiat) {
- let p = prev.split(",")[0].length;
- if (html.length > prev.length && p === 3) i++;
- if (html.length < prev.length && p === 1 && i > 1) i--;
- }
+ if (!numpad) {
+ setTimeout(() => {
+ let node = element.childNodes[0];
+ let range = document.createRange();
- let node = element.childNodes[0];
- let range = document.createRange();
+ if (node) {
+ if (i > node.length) i = node.length;
+ range.setStart(node, i);
+ range.setEnd(node, i);
- if (node) {
- if (i > node.length) i = node.length;
- range.setStart(node, i);
- range.setEnd(node, i);
+ let sel = getSelection();
+ sel.removeAllRanges();
+ sel.addRange(range);
+ node.selectionStart = node.selectionEnd;
+ }
- let sel = getSelection();
- sel.removeAllRanges();
- sel.addRange(range);
- }
-
- prev = html.toString();
- }, 0);
+ prev = html.toString();
+ }, 0);
+ }
};
- let selecting;
let select = (e) => {
let range = document.createRange();
range.selectNodeContents(e.target);
let sel = getSelection();
sel.removeAllRanges();
sel.addRange(range);
- selecting = true;
};
let blur = () => {
diff --git a/src/service-worker.js b/src/service-worker.js
index 488b36b4..80a5ead3 100644
--- a/src/service-worker.js
+++ b/src/service-worker.js
@@ -31,48 +31,57 @@ self.addEventListener("activate", (event) => {
});
self.addEventListener("fetch", (event) => {
- // ignore POST requests etc
+ // Ignore non-GET requests
if (event.request.method !== "GET") return;
async function respond() {
const url = new URL(event.request.url);
const cache = await caches.open(CACHE);
- // `build`/`files` can always be served from the cache
- if (ASSETS.includes(url.pathname)) {
- const response = await cache.match(url.pathname);
+ // Check for a cached response
+ const cachedResponse = await cache.match(event.request);
- if (response) {
- return response;
- }
- }
+ // If cached, attempt a conditional request with `If-None-Match`
+ if (cachedResponse) {
+ const etag = cachedResponse.headers.get("ETag");
+ const fetchOptions = etag
+ ? {
+ headers: {
+ "If-None-Match": etag,
+ },
+ }
+ : {};
- // for everything else, try the network first, but
- // fall back to the cache if we're offline
- try {
- const response = await fetch(event.request);
+ try {
+ const networkResponse = await fetch(event.request, fetchOptions);
- // if we're offline, fetch can return a value that is not a Response
- // instead of throwing - and we can't pass this non-Response to respondWith
- if (!(response instanceof Response)) {
- throw new Error("invalid response from fetch");
- }
+ if (networkResponse.status === 200) {
+ // Update cache and return the new response
+ cache.put(event.request, networkResponse.clone());
+ return networkResponse;
+ }
- if (response.status === 200) {
- cache.put(event.request, response.clone());
+ if (networkResponse.status === 304) {
+ // Return cached response if not modified
+ return cachedResponse;
+ }
+ } catch (err) {
+ console.error("Network error:", err);
+ // Fall back to cached response if the network fails
+ return cachedResponse;
}
+ }
- return response;
- } catch (err) {
- const response = await cache.match(event.request);
-
- if (response) {
- return response;
+ // If no cache, fetch the resource and store it if successful
+ try {
+ const networkResponse = await fetch(event.request);
+ if (networkResponse.status === 200) {
+ cache.put(event.request, networkResponse.clone());
}
-
- // if there's no cache, then just error out
- // as there is nothing we can do to respond to this request
- throw err;
+ return networkResponse;
+ } catch (err) {
+ console.error("Fetch failed and no cache available:", err);
+ throw err; // No fallback available
}
}