Skip to content

Commit 89320d6

Browse files
committed
field image file input
1 parent 3ef40ac commit 89320d6

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,10 @@ syncInputs st = do
379379
elExist <- ghcjsPure $ JS.isTruthy el
380380
when elExist $ do
381381
elActive <- JS.strictEqual el act
382-
unless elActive $ el ^. JS.jss ("value" :: Unicode) (txt ^. #uniqueValue)
382+
typ <- (el ! ("type" :: Unicode)) >>= JS.fromJSVal
383+
unless (elActive || typ == Just ("file" :: Unicode))
384+
$ el
385+
^. JS.jss ("value" :: Unicode) (txt ^. #uniqueValue)
383386
pure txt
384387

385388
evalModel :: (MonadThrow m, MonadUnliftIO m) => Model -> m (Model -> Model)

ghcjs/miso-functora/js/main.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ export async function insertStorage(key, value) {
3030
return await Preferences.set({ key: key, value: value });
3131
}
3232

33+
export async function compressImage(quality, prevImage) {
34+
const nextImage = await new Promise((resolve, reject) => {
35+
new Compressor(prevImage, {
36+
quality: quality,
37+
mimeType: "image/jpeg",
38+
success: resolve,
39+
error: reject,
40+
});
41+
});
42+
return nextImage;
43+
}
44+
3345
export async function selectClipboard() {
3446
const { value } = await Clipboard.read();
3547
try {
3648
const { buffer: u8a, typeFull: mime } = dataUriToBuffer(value);
3749
let blob = new Blob([u8a], { type: mime });
3850
if (mime.startsWith("image")) {
39-
blob = await new Promise((resolve, reject) => {
40-
new Compressor(blob, {
41-
quality: 0.1,
42-
mimeType: "image/jpeg",
43-
success: resolve,
44-
error: reject,
45-
});
46-
});
51+
blob = await compressImage(1, blob);
4752
}
4853
return URL.createObjectURL(blob);
4954
} catch (e) {
@@ -118,13 +123,14 @@ export function isNativePlatform() {
118123
export async function fetchUrlAsRfc2397(url) {
119124
const imgResp = await fetch(url);
120125
const imgBlob = await imgResp.blob();
126+
const imgComp = await compressImage(0.2, imgBlob);
121127
const rfc2397 = await new Promise((resolve, reject) => {
122128
var fr = new FileReader();
123129
fr.onload = () => {
124130
resolve(fr.result);
125131
};
126132
fr.onerror = reject;
127-
fr.readAsDataURL(imgBlob);
133+
fr.readAsDataURL(imgComp);
128134
});
129135
const utf8Encode = new TextEncoder();
130136
const ab = utf8Encode.encode(rfc2397).buffer;

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/Widgets/Field.hs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import qualified Functora.Miso.Widgets.Dialog as Dialog
2525
import qualified Functora.Miso.Widgets.Icon as Icon
2626
import qualified Functora.Miso.Widgets.Qr as Qr
2727
import qualified Functora.Miso.Widgets.Select as Select
28+
import Language.Javascript.JSaddle ((!))
2829
import qualified Language.Javascript.JSaddle as JS
2930
import qualified Miso.String as MS
3031

@@ -166,7 +167,13 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
166167
$ catMaybes
167168
[ Just $ type_ "file",
168169
Just $ accept_ "image/*",
169-
Just $ onInput onInputAction,
170+
Just $ onInput onInputFileAction,
171+
Just
172+
. id_
173+
. either impureThrow id
174+
. decodeUtf8Strict
175+
. unTagged
176+
$ htmlUid uid,
170177
fmap required_
171178
$ st
172179
^? cloneTraversal optic
@@ -299,6 +306,45 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
299306
& cloneTraversal optic
300307
. #fieldOutput
301308
%~ maybe id (const . id) (getOutput next)
309+
onInputFileAction =
310+
const . fromMaybe action (optsOnInputAction opts) . ImpureUpdate $ do
311+
el <-
312+
getElementById
313+
. either impureThrow id
314+
. decodeUtf8Strict
315+
. unTagged
316+
$ htmlUid uid
317+
elExist <- ghcjsPure $ JS.isTruthy el
318+
if not elExist
319+
then pure id
320+
else do
321+
file <-
322+
el JS.! ("files" :: Unicode) JS.!! 0
323+
link <-
324+
JS.global
325+
! ("URL" :: Unicode)
326+
^. JS.js1 ("createObjectURL" :: Unicode) file
327+
murl <-
328+
JS.fromJSVal link
329+
case murl of
330+
Nothing -> pure id
331+
Just (url :: Unicode) -> do
332+
let next =
333+
st
334+
& cloneTraversal optic
335+
. #fieldInput
336+
. #uniqueValue
337+
.~ url
338+
pure
339+
$ ( cloneTraversal optic
340+
. #fieldInput
341+
. #uniqueValue
342+
.~ url
343+
)
344+
. ( cloneTraversal optic
345+
. #fieldOutput
346+
%~ maybe id (const . id) (getOutput next)
347+
)
302348

303349
ratioField ::
304350
Args model action Rational Unique ->

0 commit comments

Comments
 (0)