Skip to content

Commit 28b17b0

Browse files
committed
fetchUrlAsRfc2397
1 parent 16852e9 commit 28b17b0

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

ghcjs/delivery-calculator/delivery-calculator.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ common pkg
1616
hs-source-dirs: src
1717
js-sources: static/app.js
1818
other-modules:
19+
App.Jsm
1920
App.Types
2021
App.Widgets.Asset
2122
App.Widgets.Fav
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module App.Jsm (fetchBlobUris) where
2+
3+
import App.Types
4+
import qualified Data.Generics as Syb
5+
import qualified Data.Map as Map
6+
import qualified Functora.Miso.Jsm as Jsm
7+
import Functora.Miso.Prelude
8+
import qualified Text.URI as URI
9+
10+
fetchBlobUris :: (Data a) => a -> JSM a
11+
fetchBlobUris st = do
12+
vars <-
13+
forM blobUris $ \uri -> do
14+
var <- newEmptyMVar
15+
Jsm.fetchUrlAsRfc2397 uri $ liftIO . putMVar var . fmap (uri,)
16+
pure var
17+
vals <-
18+
fmap (fromList . catMaybes)
19+
. forM vars
20+
$ liftIO
21+
. takeMVar
22+
pure
23+
$ Syb.everywhere
24+
( Syb.mkT $ \(x :: Field DynamicField Unique) ->
25+
case Map.lookup (x ^. #fieldInput . #uniqueValue) vals of
26+
Just val
27+
| fieldType x == FieldTypeImage ->
28+
x
29+
& #fieldInput
30+
. #uniqueValue
31+
.~ val
32+
& #fieldOutput
33+
.~ DynamicFieldText val
34+
_ -> x
35+
)
36+
st
37+
where
38+
blobUris =
39+
nubOrd
40+
. filter (\x -> URI.mkScheme "blob" == (URI.mkURI x >>= URI.uriScheme))
41+
. fmap (^. #fieldInput . #uniqueValue)
42+
$ Syb.listify
43+
(\(x :: Field DynamicField Unique) -> fieldType x == FieldTypeImage)
44+
st

ghcjs/delivery-calculator/src/App/Widgets/Menu.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module App.Widgets.Menu
55
)
66
where
77

8+
import qualified App.Jsm as Jsm
89
import App.Types
910
import qualified App.Widgets.Fav as Fav
1011
import qualified App.Xlsx as Xlsx
@@ -90,16 +91,13 @@ menu st =
9091
style_
9192
[ ("min-width", "0")
9293
],
93-
onClick
94-
. PushUpdate
95-
. Instant
96-
. ImpureUpdate
97-
$ do
98-
Jsm.saveFile
99-
"delivery-calculator.xlsx"
100-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
101-
Xlsx.newXlsx
102-
pure id
94+
onClick . PushUpdate . Instant . EffectUpdate $ do
95+
res <- Jsm.fetchBlobUris $ st ^. #modelState . #stAssets
96+
consoleLog res
97+
Jsm.saveFile
98+
"delivery-calculator.xlsx"
99+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
100+
Xlsx.newXlsx
103101
]
104102
[ icon Icon.IconDownload
105103
],

ghcjs/miso-functora/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function selectClipboard() {
3232
const { value } = await Clipboard.read();
3333
try {
3434
const { buffer: u8a, typeFull: mime } = dataUriToBuffer(value);
35-
const blob = new Blob([u8a, { type: mime }]);
35+
const blob = new Blob([u8a], { type: mime });
3636
return URL.createObjectURL(blob);
3737
} catch (e) {
3838
return value;
@@ -84,7 +84,7 @@ export async function saveFile(name, mime, bs) {
8484
});
8585
return uri;
8686
} else {
87-
const blob = new Blob([u8a, { type: mime }]);
87+
const blob = new Blob([u8a], { type: mime });
8888
await saveAs(blob, name);
8989
return null;
9090
}

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

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

ghcjs/miso-functora/src/Functora/Miso/Jsm/Generic.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ popupText x = do
4040

4141
shareText :: (Show a, Data a) => a -> Update model
4242
shareText x =
43-
ImpureUpdate $ do
43+
EffectUpdate $ do
4444
let txt = inspect @Unicode x
4545
unless (txt == mempty) $ do
4646
pkg <- getPkg
4747
prom <- pkg ^. JS.js1 ("shareText" :: Unicode) txt
4848
success <- JS.function $ \_ _ _ -> popupText @Unicode "Copied!"
4949
failure <- JS.function $ \_ _ _ -> popupText @Unicode "Failed to copy!"
5050
void $ prom ^. JS.js2 ("then" :: Unicode) success failure
51-
pure id
5251

5352
moveUp :: ATraversal' model [item] -> Int -> Update model
5453
moveUp optic idx =
@@ -104,14 +103,13 @@ swapAt i j xs
104103

105104
openBrowserPage :: URI -> Update model
106105
openBrowserPage uri =
107-
ImpureUpdate $ do
106+
EffectUpdate $ do
108107
pkg <- getPkg
109108
void $ pkg ^. JS.js1 @Unicode "openBrowserPage" (URI.render uri)
110-
pure id
111109

112110
enterOrEscapeBlur :: Uid -> KeyCode -> Update model
113111
enterOrEscapeBlur uid (KeyCode code) =
114-
ImpureUpdate $ do
112+
EffectUpdate $ do
115113
let enterOrEscape = [13, 27] :: [Int]
116114
when (code `elem` enterOrEscape) $ do
117115
--
@@ -123,10 +121,9 @@ enterOrEscapeBlur uid (KeyCode code) =
123121
. unTagged
124122
$ "document.getElementById('"
125123
<> htmlUid uid
126-
<> "').getElementsByTagName('input')[0].blur();"
124+
<> "').blur();"
127125
void
128126
$ JS.eval res
129-
pure id
130127

131128
insertStorage :: (ToJSON a) => Unicode -> a -> JSM ()
132129
insertStorage key raw = do

pub/functora/src/prelude/Functora/Prelude.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ import Data.Sequences as X hiding
218218
group,
219219
groupBy,
220220
intersperse,
221-
isPrefixOf,
222221
permutations,
223222
replicate,
224223
replicateM,
@@ -295,6 +294,7 @@ import Universum as X hiding
295294
handleAny,
296295
inits,
297296
intercalate,
297+
isPrefixOf,
298298
lines,
299299
on,
300300
over,

0 commit comments

Comments
 (0)