Skip to content

Commit 749e24d

Browse files
committed
wip
1 parent 6c60bd4 commit 749e24d

File tree

1 file changed

+76
-46
lines changed
  • ghcjs/miso-functora/src/Functora/Miso/Widgets

1 file changed

+76
-46
lines changed

ghcjs/miso-functora/src/Functora/Miso/Widgets/Field.hs

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import qualified Functora.Miso.Widgets.Qr as Qr
2727
import qualified Functora.Miso.Widgets.Select as Select
2828
import qualified Language.Javascript.JSaddle as JS
2929
import qualified Miso.String as MS
30+
import Type.Reflection
3031

3132
data Args model action t f = Args
3233
{ argsModel :: model,
@@ -120,6 +121,9 @@ data ModalWidget' model where
120121
ModalWidget' model
121122

122123
field ::
124+
forall model action t.
125+
( Typeable t
126+
) =>
123127
Full model action t Unique ->
124128
Opts model action ->
125129
[View action]
@@ -152,27 +156,31 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
152156
)
153157
(optsLabel opts)
154158
( ( if typ == FieldTypeImage
155-
then
156-
join
157-
. maybeToList
158-
$ fmap
159-
( \src ->
160-
if null src
161-
then mempty
162-
else
163-
[ img_
164-
( loading_ "lazy"
165-
: src_ src
166-
: optsExtraAttributes opts
167-
),
168-
br_ mempty
169-
]
170-
)
171-
$ st
172-
^? cloneTraversal
173-
optic
174-
. #fieldInput
175-
. #uniqueValue
159+
then do
160+
out <- maybeToList $ st ^? cloneTraversal optic . #fieldOutput
161+
src <-
162+
case typeRep @t `eqTypeRep` typeRep @DynamicField of
163+
Nothing ->
164+
maybeToList
165+
$ st
166+
^? cloneTraversal optic
167+
. #fieldInput
168+
. #uniqueValue
169+
Just HRefl ->
170+
case out of
171+
DynamicFieldRfc2397 (Just ref) _ -> [ref]
172+
DynamicFieldRfc2397 {} -> mempty
173+
_ -> [inspectDynamicField out]
174+
if null src
175+
then mempty
176+
else
177+
[ img_
178+
( loading_ "lazy"
179+
: src_ src
180+
: optsExtraAttributes opts
181+
),
182+
br_ mempty
183+
]
176184
else
177185
singleton
178186
. input_
@@ -341,6 +349,8 @@ passwordField args opts =
341349
)
342350

343351
fieldIcon ::
352+
( Typeable t
353+
) =>
344354
Full model action t Unique ->
345355
Opts model action ->
346356
OptsWidget model action ->
@@ -590,7 +600,8 @@ selectTypeWidget args@Args {argsAction = action} optic =
590600
-- TODO : support optional copying widgets
591601
--
592602
fieldViewer ::
593-
( Foldable1 f
603+
( Typeable t,
604+
Foldable1 f
594605
) =>
595606
Opts model action ->
596607
Args model action t f ->
@@ -646,7 +657,9 @@ header txt =
646657
]
647658

648659
genericFieldViewer ::
649-
( Foldable1 f
660+
forall model action t f.
661+
( Typeable t,
662+
Foldable1 f
650663
) =>
651664
Opts model action ->
652665
Args model action t f ->
@@ -663,9 +676,21 @@ genericFieldViewer opts0 args widget =
663676
<> ( optsLeftRightViewer
664677
opts0
665678
( if typ == FieldTypeImage
666-
then
667-
[ img_ [loading_ "lazy", src_ input]
668-
]
679+
then do
680+
out <- maybeToList $ st ^? cloneTraversal optic . #fieldOutput
681+
src <-
682+
case typeRep @t `eqTypeRep` typeRep @DynamicField of
683+
Nothing -> [input]
684+
Just HRefl ->
685+
case out of
686+
DynamicFieldRfc2397 (Just ref) _ -> [ref]
687+
DynamicFieldRfc2397 {} -> mempty
688+
_ -> [inspectDynamicField out]
689+
if null src
690+
then mempty
691+
else
692+
[ img_ [loading_ "lazy", src_ src]
693+
]
669694
else
670695
[ widget
671696
$ truncateFieldViewer
@@ -790,6 +815,9 @@ expandDynamicField x =
790815
out = x ^. #fieldOutput
791816

792817
insertAction ::
818+
forall model action t.
819+
( Typeable t
820+
) =>
793821
Full model action t Unique ->
794822
((Maybe Unicode -> JSM ()) -> JSM ()) ->
795823
action
@@ -798,31 +826,33 @@ insertAction Full {fullArgs = args, fullParser = parser} selector =
798826
selector $ \case
799827
Nothing -> Jsm.popupText @Unicode "Failure!"
800828
Just inp -> do
801-
let next =
802-
prev
803-
& cloneTraversal optic
829+
let updateInput =
830+
cloneTraversal optic
804831
. #fieldInput
805832
. #uniqueValue
806833
.~ inp
807-
case next ^? cloneTraversal optic >>= parser of
834+
case updateInput prev ^? cloneTraversal optic >>= parser of
808835
Nothing -> Jsm.popupText @Unicode "Failure!"
809836
Just out ->
810-
emitter
811-
$ PureAndImpureUpdate
812-
( \st ->
813-
st
814-
& cloneTraversal optic
815-
. #fieldInput
816-
. #uniqueValue
817-
.~ inp
818-
& cloneTraversal optic
819-
. #fieldOutput
820-
.~ out
821-
)
822-
( do
823-
Jsm.popupText @Unicode "Success!"
824-
pure id
825-
)
837+
emitter . ImpureUpdate $ do
838+
Jsm.popupText @Unicode "Success!"
839+
let updateOutput = cloneTraversal optic . #fieldOutput .~ out
840+
case typeRep @t `eqTypeRep` typeRep @DynamicField of
841+
Nothing -> pure $ updateInput . updateOutput
842+
Just HRefl ->
843+
case out of
844+
DynamicFieldRfc2397 Just {} _ ->
845+
pure $ updateInput . updateOutput
846+
DynamicFieldRfc2397 Nothing val -> do
847+
ref <- Jsm.newBlobUrl inp
848+
pure
849+
$ updateInput
850+
. ( cloneTraversal optic
851+
. #fieldOutput
852+
.~ DynamicFieldRfc2397 (Just ref) val
853+
)
854+
_ ->
855+
pure $ updateInput . updateOutput
826856
pure id
827857
where
828858
prev = args ^. #argsModel

0 commit comments

Comments
 (0)