Skip to content

Commit 88c8628

Browse files
committed
miso-capa wip
1 parent 99bd624 commit 88c8628

36 files changed

+10364
-0
lines changed

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ if os(wasi)
103103
pub/selective/*.cabal
104104
pub/universum/*.cabal
105105
pub/xlsx/*.cabal
106+
ghcjs/miso-capa/*.cabal
106107
ghcjs/miso-widgets/*.cabal
107108
ghcjs/miso-components/*.cabal
108109
ghcjs/delivery-calculator/*.cabal

ghcjs/miso-capa/.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use nix

ghcjs/miso-capa/.ghcid

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--restart=.ghcid
2+
--restart=miso-widgets.cabal
3+
--restart=cabal.config
4+
--restart=cabal.project
5+
--restart=static
6+
--restart=README.md

ghcjs/miso-capa/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.direnv
2+
result
3+
dist-newstyle
4+
node_modules
5+
dist
6+
android
7+
capacitor-geckoview/capacitor/build

ghcjs/miso-capa/.ignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.stack-work
2+
dist-newstyle
3+
capacitor-geckoview
4+
package-lock.json
5+
*\.min\.*

ghcjs/miso-capa/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
Copyright (c) 2024 Functora
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21+
OR OTHER DEALINGS IN THE SOFTWARE.
22+

ghcjs/miso-capa/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Sample Miso-JSaddle application
2+
3+
4+
It's possible to build miso applications with `ghcid`, `miso` and `jsaddle`. This can enable a faster workflow due to hot reloading of the code.
5+
6+
This application (sample-app-jsaddle) serves as an example of development w/ GHC, and releases with GHCJS.
7+
8+
To take advantage of the hot reload code features, we recommend running the following command (see below) in a shell. (This will invoke `ghcid` for you).
9+
10+
## Dev
11+
```bash
12+
nix-shell --run reload
13+
```
14+
15+
You should now be able to open your browser to `http://localhost:8080` and see your working application. Subsequent edits of the code should cause a live update of the website at that address.
16+
17+
To build the application w/ GHCJS, execute the below command.
18+
19+
## Build w/ GHCJS
20+
```bash
21+
nix-build -A release
22+
```
23+
24+
## Dev with `stack`
25+
26+
In order to build `miso` w/ `jsaddle` support, it is necessary to remove the existing `miso` package first.
27+
28+
```bash
29+
stack exec -- ghc-pkg unregister --force miso
30+
```
31+
32+
Enable the `jsaddle` flag by adding the following to your project's `package.yaml` file, then call `stack build`.
33+
34+
```yaml
35+
flags:
36+
miso:
37+
jsaddle: true
38+
```
39+
40+
## Add external javascript file
41+
42+
First download the external javascript file (`your-file.js`) to your project directory.
43+
Then add `bytestring` to `build-depends` in `app.cabal`.
44+
In your `Main.hs` you need to change the implementation of `runApp` from this:
45+
```
46+
runApp f =
47+
Warp.runSettings (Warp.setPort 8080 (Warp.setTimeout 3600 Warp.defaultSettings)) =<<
48+
JSaddle.jsaddleOr defaultConnectionOptions (f >> syncPoint) JSaddle.jsaddleApp
49+
```
50+
to this:
51+
```
52+
runApp f = do
53+
bString <- B.readFile "your-file.js"
54+
jSaddle <- JSaddle.jsaddleOr defaultConnectionOptions (f >> syncPoint) (JSaddle.jsaddleAppWithJs (B.append (JSaddle.jsaddleJs False) bString))
55+
Warp.runSettings (Warp.setPort 8081 (Warp.setTimeout 3600 Warp.defaultSettings)) jSaddle
56+
```
57+
Now you should be able to use `your-file.js` in jsaddle.

ghcjs/miso-capa/default.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let
2+
# functora = ./..;
3+
functora = fetchTarball {
4+
url = "https://github.com/functora/functora.github.io/archive/03df5e6d39d0fdc31a53cc350e1125118b4a12ca.tar.gz";
5+
sha256 = "0pad1981bpnl1szvlhgl00ai9ikz9nsl4yv6mxxv0q46j10bdchz";
6+
};
7+
functora-miso = import "${functora}/pub/miso/default.nix" {
8+
overlays = import ../overlays.nix {inherit functora;};
9+
};
10+
pkgs = functora-miso.pkgs;
11+
doCheck = pkgs.haskell.lib.doCheck;
12+
in rec {
13+
inherit pkgs functora;
14+
source = pkgs.nix-gitignore.gitignoreSourcePure ./.gitignore ./.;
15+
dev = doCheck (pkgs.haskell.packages.ghc865.callCabal2nix "miso-capa" source {
16+
miso = functora-miso.miso-jsaddle;
17+
});
18+
miso-capa = pkgs.haskell.packages.ghcjs86.callCabal2nix "miso-capa" source {};
19+
vsn = miso-capa.passthru.version;
20+
}

ghcjs/miso-capa/js/main.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { defineCustomElements } from "@ionic/pwa-elements/loader";
2+
import { Filesystem, Directory } from "@capacitor/filesystem";
3+
import { WebviewPrint } from "capacitor-webview-print";
4+
import { Preferences } from "@capacitor/preferences";
5+
import { Clipboard } from "@capacitor/clipboard";
6+
import { Browser } from "@capacitor/browser";
7+
import { Share } from "@capacitor/share";
8+
import { Toast } from "@capacitor/toast";
9+
import { Capacitor } from "@capacitor/core";
10+
import { Html5Qrcode } from "html5-qrcode";
11+
import { saveAs } from "file-saver";
12+
import {
13+
CapacitorBarcodeScanner,
14+
CapacitorBarcodeScannerTypeHint,
15+
} from "@capacitor/barcode-scanner";
16+
17+
export async function printCurrentPage(name) {
18+
return await WebviewPrint.print({ name: name });
19+
}
20+
21+
export async function selectStorage(key) {
22+
const { value } = await Preferences.get({ key: key });
23+
return value;
24+
}
25+
26+
export async function insertStorage(key, value) {
27+
return await Preferences.set({ key: key, value: value });
28+
}
29+
30+
export async function selectClipboard() {
31+
const { value } = await Clipboard.read();
32+
return value;
33+
}
34+
35+
export async function openBrowserPage(url) {
36+
try {
37+
return await Browser.open({ url: url, windowName: "_blank" });
38+
} catch (e) {
39+
return window.open(url, "_blank").focus();
40+
}
41+
}
42+
43+
export async function shareText(text) {
44+
const { value } = await Share.canShare();
45+
if (value) {
46+
return await Share.share({ text: text });
47+
} else {
48+
return await navigator.clipboard.writeText(text);
49+
}
50+
}
51+
52+
export async function popupText(text) {
53+
return await Toast.show({ text: text });
54+
}
55+
56+
export async function selectBarcode() {
57+
if (!Capacitor.isNativePlatform()) {
58+
const devices = await Html5Qrcode.getCameras();
59+
if (!(devices && devices.length)) {
60+
throw new Error("Camera not found!");
61+
}
62+
}
63+
const { ScanResult } = await CapacitorBarcodeScanner.scanBarcode({
64+
hint: CapacitorBarcodeScannerTypeHint.ALL,
65+
});
66+
return ScanResult;
67+
}
68+
69+
export async function saveFile(name, mime, bs) {
70+
const u8a = Uint8Array.from(bs);
71+
if (Capacitor.isNativePlatform()) {
72+
const b64 = btoa(String.fromCharCode.apply(null, u8a));
73+
const { uri } = await Filesystem.writeFile({
74+
path: name,
75+
data: b64,
76+
directory: Directory.Documents,
77+
});
78+
return uri;
79+
} else {
80+
const blob = new Blob([u8a, { type: mime }]);
81+
await saveAs(blob, name);
82+
return null;
83+
}
84+
}
85+
86+
export async function shareFiles(files) {
87+
if (Capacitor.isNativePlatform()) {
88+
const { value } = await Share.share({ files: files });
89+
return value;
90+
} else {
91+
return null;
92+
}
93+
}
94+
95+
export function isNativePlatform() {
96+
return Capacitor.isNativePlatform();
97+
}
98+
99+
defineCustomElements(window);

ghcjs/miso-capa/js/main.min.js

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

0 commit comments

Comments
 (0)