Skip to content

Commit 7c072e6

Browse files
committed
xlsx wip
1 parent 6adafd6 commit 7c072e6

File tree

2 files changed

+96
-27
lines changed

2 files changed

+96
-27
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ menu st =
9393
[ ("min-width", "0")
9494
],
9595
onClick . PushUpdate . Instant . EffectUpdate $ do
96-
imgs <- Jsm.fetchBlobUris $ st ^. #modelState
96+
let doc = st ^. #modelState
97+
imgs <- Jsm.fetchBlobUris doc
9798
Jsm.saveFile Xlsx.xlsxFile Xlsx.xlsxMime
9899
. from @BL.ByteString @ByteString
99-
$ Xlsx.newXlsx imgs
100+
$ Xlsx.newXlsx doc imgs
100101
]
101102
[ icon Icon.IconDownload
102103
],

ghcjs/delivery-calculator/src/App/Xlsx.hs

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,118 @@
1-
{-# OPTIONS_GHC -Wno-deprecations #-}
2-
{-# OPTIONS_GHC -Wno-x-partial #-}
3-
41
module App.Xlsx
52
( newXlsx,
63
xlsxFile,
74
xlsxMime,
85
)
96
where
107

8+
import App.Types
119
import Codec.Xlsx
1210
import qualified Data.ByteString.Lazy as BL
1311
import qualified Data.Map as Map
14-
import Functora.Miso.Prelude
12+
import Functora.Miso.Prelude hiding ((^.), _Just)
1513
import Lens.Micro hiding (each, to)
16-
import qualified Prelude
1714

18-
newXlsx :: Map Unicode Rfc2397 -> BL.ByteString
19-
newXlsx imgs = xlsx
15+
newXlsx :: St Unique -> Map Unicode Rfc2397 -> BL.ByteString
16+
newXlsx st imgs = xlsx
2017
where
2118
xlsx =
2219
fromXlsx 0
2320
$ def
24-
& atSheet "List1" ?~ sheet
21+
& atSheet "Delivery Calculator" ?~ sheet
2522
sheet =
2623
def
27-
& cellValueAt (1, 2) ?~ CellDouble 42.0
28-
& cellValueAt (3, 2) ?~ CellText "foo"
29-
& #wsDrawing ?~ drawing (Prelude.head $ Map.elems imgs)
24+
& #wsDrawing ?~ Drawing mempty
25+
& addHeader st
26+
& flip
27+
(foldl $ addRow imgs)
28+
( zip [2 ..]
29+
$ fmap
30+
(^.. #assetFieldPairs . each . #fieldPairValue)
31+
(st ^. #stAssets)
32+
)
33+
34+
addHeader :: St Unique -> Worksheet -> Worksheet
35+
addHeader st sheet =
36+
case sortOn length headers of
37+
[] -> sheet
38+
(rowVal : _) ->
39+
foldl
40+
( \acc (colIdx, colVal) ->
41+
acc
42+
& cellValueAt (1, colIdx) ?~ CellText colVal
43+
)
44+
sheet
45+
$ zip [1 ..] rowVal
46+
where
47+
headers :: [[Unicode]]
48+
headers =
49+
fmap
50+
( ^..
51+
#assetFieldPairs
52+
. each
53+
. #fieldPairKey
54+
. #fieldOutput
55+
)
56+
$ stAssets st
57+
58+
addRow ::
59+
Map Unicode Rfc2397 ->
60+
Worksheet ->
61+
(RowIndex, [Field DynamicField Unique]) ->
62+
Worksheet
63+
addRow imgs sheet (rowIdx, rowVal) =
64+
foldl
65+
( \acc (colIdx, colVal) ->
66+
addCol imgs acc rowIdx colIdx colVal
67+
)
68+
sheet
69+
$ zip [1 ..] rowVal
70+
71+
addCol ::
72+
Map Unicode Rfc2397 ->
73+
Worksheet ->
74+
RowIndex ->
75+
ColumnIndex ->
76+
Field DynamicField Unique ->
77+
Worksheet
78+
addCol imgs sheet rowIdx colIdx field =
79+
if fieldType field /= FieldTypeImage
80+
then
81+
sheet
82+
& cellValueAt (rowIdx, colIdx)
83+
?~ CellText txt
84+
else case Map.lookup txt imgs of
85+
--
86+
-- TODO : handle img link
87+
--
88+
Nothing ->
89+
sheet
90+
& cellValueAt (rowIdx, colIdx)
91+
?~ CellText txt
92+
Just img ->
93+
sheet
94+
& #wsDrawing . _Just %~ \case
95+
Drawing xs ->
96+
Drawing $ newImg rowIdx colIdx (length xs) img : xs
97+
where
98+
txt = field ^. #fieldInput . #uniqueValue
3099

31-
drawing :: Rfc2397 -> Drawing
32-
drawing rfc2397 = Drawing [anchor1]
100+
newImg :: RowIndex -> ColumnIndex -> Int -> Rfc2397 -> Anchor FileInfo a
101+
newImg (RowIndex rowIdx) (ColumnIndex colIdx) imgIdx rfc2397 =
102+
Anchor
103+
{ anchAnchoring =
104+
TwoCellAnchor
105+
{ tcaFrom = unqMarker (colIdx - 1, 0) (rowIdx - 1, 0),
106+
tcaTo = unqMarker (colIdx, 0) (rowIdx, 0),
107+
tcaEditAs = EditAsTwoCell
108+
},
109+
anchObject = obj,
110+
anchClientData = def
111+
}
33112
where
34-
anchor1 =
35-
Anchor
36-
{ anchAnchoring =
37-
TwoCellAnchor
38-
{ tcaFrom = unqMarker (1, 0) (1, 0),
39-
tcaTo = unqMarker (5, 0) (13, 0),
40-
tcaEditAs = EditAsTwoCell
41-
},
42-
anchObject = obj,
43-
anchClientData = def
44-
}
45113
obj =
46114
picture
47-
(DrawingElementId 0)
115+
(DrawingElementId imgIdx)
48116
FileInfo
49117
{ fiFilename = "img",
50118
fiContentType = decodeUtf8 $ rfc2397Mime rfc2397,

0 commit comments

Comments
 (0)