Skip to content

Commit 25abf6e

Browse files
committed
using cap sharing plugin
1 parent 0f37add commit 25abf6e

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

ghcjs/currency-converter/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ in rec {
9090
${./static}/web2.js -o $out/web2.js --compress --mangle --source-map
9191
${functora-pkgs.terser}/bin/terser \
9292
${./static}/web3.js -o $out/web3.js --compress --mangle --source-map
93+
${functora-pkgs.terser}/bin/terser \
94+
${./static}/web4.js -o $out/web4.js --compress --mangle --source-map
9395
${functora-pkgs.terser}/bin/terser \
9496
${./static}/main.js -o $out/main.js --compress --mangle --source-map
9597
${functora-pkgs.html-minifier}/bin/html-minifier \

ghcjs/currency-converter/package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/currency-converter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@capacitor/core": "^6.0.0",
1313
"@capacitor/ios": "^6.0.0",
1414
"@capacitor/preferences": "^6.0.1",
15+
"@capacitor/share": "^6.0.1",
1516
"capacitor-webview-print": "^6.0.1"
1617
},
1718
"devDependencies": {

ghcjs/currency-converter/src/App/Misc.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ copyIntoClipboard :: (Show a, Data a) => Model -> a -> JSM ()
7373
copyIntoClipboard st x = do
7474
let txt = inspect x
7575
unless (txt == mempty) $ do
76-
clip <-
77-
JS.global JS.! ("navigator" :: MisoString) JS.! ("clipboard" :: MisoString)
78-
prom <- clip ^. JS.js1 ("writeText" :: MisoString) txt
76+
prom <- JS.global ^. JS.js1 ("shareText" :: MisoString) txt
7977
success <- JS.function $ \_ _ _ -> textPopup @MisoString st "Copied!"
8078
failure <- JS.function $ \_ _ _ -> textPopup @MisoString st "Failed to copy!"
8179
void $ prom ^. JS.js2 ("then" :: MisoString) success failure

ghcjs/currency-converter/src/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ runApp app = do
9292
("web.js" : _) -> staticApp (defaultWebAppSettings "static") req
9393
("web2.js" : _) -> staticApp (defaultWebAppSettings "static") req
9494
("web3.js" : _) -> staticApp (defaultWebAppSettings "static") req
95+
("web4.js" : _) -> staticApp (defaultWebAppSettings "static") req
9596
("main.js" : _) -> staticApp (defaultWebAppSettings "static") req
9697
("site.webmanifest" : _) -> staticApp (defaultWebAppSettings "static") req
9798
_ -> JS.jsaddleAppWithJs (JS.jsaddleJs False <> js) req
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { WebviewPrint } from "capacitor-webview-print";
22
import { Preferences } from "@capacitor/preferences";
33
import { Browser } from "@capacitor/browser";
4+
import { Share } from "@capacitor/share";
45

56
async function printCurrentPage(name) {
6-
await WebviewPrint.print({ name: name });
7+
return await WebviewPrint.print({ name: name });
78
}
89

910
async function selectStorage(key) {
@@ -12,18 +13,28 @@ async function selectStorage(key) {
1213
}
1314

1415
async function insertStorage(key, value) {
15-
await Preferences.set({ key: key, value: value });
16+
return await Preferences.set({ key: key, value: value });
1617
}
1718

1819
async function openBrowserPage(url) {
1920
try {
20-
await Browser.open({ url: url });
21+
return await Browser.open({ url: url });
2122
} catch (e) {
22-
window.open(url, "_blank").focus();
23+
return window.open(url, "_blank").focus();
24+
}
25+
}
26+
27+
async function shareText(text) {
28+
const { value } = await Share.canShare();
29+
if (value) {
30+
return await Share.share({ text: text });
31+
} else {
32+
return await navigator.clipboard.writeText(text);
2333
}
2434
}
2535

2636
globalThis.printCurrentPage = printCurrentPage;
2737
globalThis.selectStorage = selectStorage;
2838
globalThis.insertStorage = insertStorage;
2939
globalThis.openBrowserPage = openBrowserPage;
40+
globalThis.shareText = shareText;

ghcjs/currency-converter/static/main.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,27 +582,39 @@ const Preferences = registerPlugin("Preferences", {
582582
const Browser = registerPlugin("Browser", {
583583
web: () => __vitePreload(() => import("./web3.js"), true ? [] : void 0).then((m) => new m.BrowserWeb())
584584
});
585+
const Share = registerPlugin("Share", {
586+
web: () => __vitePreload(() => import("./web4.js"), true ? [] : void 0).then((m) => new m.ShareWeb())
587+
});
585588
async function printCurrentPage(name) {
586-
await WebviewPrint.print({ name });
589+
return await WebviewPrint.print({ name });
587590
}
588591
async function selectStorage(key) {
589592
const { value } = await Preferences.get({ key });
590593
return value;
591594
}
592595
async function insertStorage(key, value) {
593-
await Preferences.set({ key, value });
596+
return await Preferences.set({ key, value });
594597
}
595598
async function openBrowserPage(url) {
596599
try {
597-
await Browser.open({ url });
600+
return await Browser.open({ url });
598601
} catch (e) {
599-
window.open(url, "_blank").focus();
602+
return window.open(url, "_blank").focus();
603+
}
604+
}
605+
async function shareText(text) {
606+
const { value } = await Share.canShare();
607+
if (value) {
608+
return await Share.share({ text });
609+
} else {
610+
return await navigator.clipboard.writeText(text);
600611
}
601612
}
602613
globalThis.printCurrentPage = printCurrentPage;
603614
globalThis.selectStorage = selectStorage;
604615
globalThis.insertStorage = insertStorage;
605616
globalThis.openBrowserPage = openBrowserPage;
617+
globalThis.shareText = shareText;
606618
export {
607619
WebPlugin as W
608620
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { W as WebPlugin } from "./main.js";
2+
class ShareWeb extends WebPlugin {
3+
async canShare() {
4+
if (typeof navigator === "undefined" || !navigator.share) {
5+
return { value: false };
6+
} else {
7+
return { value: true };
8+
}
9+
}
10+
async share(options) {
11+
if (typeof navigator === "undefined" || !navigator.share) {
12+
throw this.unavailable("Share API not available in this browser");
13+
}
14+
await navigator.share({
15+
title: options.title,
16+
text: options.text,
17+
url: options.url
18+
});
19+
return {};
20+
}
21+
}
22+
export {
23+
ShareWeb
24+
};

0 commit comments

Comments
 (0)