@@ -27,6 +27,7 @@ import qualified Functora.Miso.Widgets.Qr as Qr
27
27
import qualified Functora.Miso.Widgets.Select as Select
28
28
import qualified Language.Javascript.JSaddle as JS
29
29
import qualified Miso.String as MS
30
+ import Type.Reflection
30
31
31
32
data Args model action t f = Args
32
33
{ argsModel :: model ,
@@ -120,6 +121,9 @@ data ModalWidget' model where
120
121
ModalWidget' model
121
122
122
123
field ::
124
+ forall model action t .
125
+ ( Typeable t
126
+ ) =>
123
127
Full model action t Unique ->
124
128
Opts model action ->
125
129
[View action ]
@@ -152,27 +156,31 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
152
156
)
153
157
(optsLabel opts)
154
158
( ( 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
+ ]
176
184
else
177
185
singleton
178
186
. input_
@@ -341,6 +349,8 @@ passwordField args opts =
341
349
)
342
350
343
351
fieldIcon ::
352
+ ( Typeable t
353
+ ) =>
344
354
Full model action t Unique ->
345
355
Opts model action ->
346
356
OptsWidget model action ->
@@ -590,7 +600,8 @@ selectTypeWidget args@Args {argsAction = action} optic =
590
600
-- TODO : support optional copying widgets
591
601
--
592
602
fieldViewer ::
593
- ( Foldable1 f
603
+ ( Typeable t ,
604
+ Foldable1 f
594
605
) =>
595
606
Opts model action ->
596
607
Args model action t f ->
@@ -646,7 +657,9 @@ header txt =
646
657
]
647
658
648
659
genericFieldViewer ::
649
- ( Foldable1 f
660
+ forall model action t f .
661
+ ( Typeable t ,
662
+ Foldable1 f
650
663
) =>
651
664
Opts model action ->
652
665
Args model action t f ->
@@ -663,9 +676,21 @@ genericFieldViewer opts0 args widget =
663
676
<> ( optsLeftRightViewer
664
677
opts0
665
678
( 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
+ ]
669
694
else
670
695
[ widget
671
696
$ truncateFieldViewer
@@ -790,6 +815,9 @@ expandDynamicField x =
790
815
out = x ^. # fieldOutput
791
816
792
817
insertAction ::
818
+ forall model action t .
819
+ ( Typeable t
820
+ ) =>
793
821
Full model action t Unique ->
794
822
((Maybe Unicode -> JSM () ) -> JSM () ) ->
795
823
action
@@ -798,31 +826,33 @@ insertAction Full {fullArgs = args, fullParser = parser} selector =
798
826
selector $ \ case
799
827
Nothing -> Jsm. popupText @ Unicode " Failure!"
800
828
Just inp -> do
801
- let next =
802
- prev
803
- & cloneTraversal optic
829
+ let updateInput =
830
+ cloneTraversal optic
804
831
. # fieldInput
805
832
. # uniqueValue
806
833
.~ inp
807
- case next ^? cloneTraversal optic >>= parser of
834
+ case updateInput prev ^? cloneTraversal optic >>= parser of
808
835
Nothing -> Jsm. popupText @ Unicode " Failure!"
809
836
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
826
856
pure id
827
857
where
828
858
prev = args ^. # argsModel
0 commit comments