|
| 1 | +module ImagePreviewsHalogenHooks.Main where |
| 2 | + |
| 3 | +import Prelude hiding (top) |
| 4 | + |
| 5 | +import CSS (alignItems, backgroundImage, backgroundPosition, backgroundRepeat, backgroundSize, border, borderRadius, column, contain, dashed, display, displayNone, flex, flexDirection, gray, height, justifyContent, margin, noRepeat, padding, placed, purple, px, sideCenter, solid, url, width) |
| 6 | +import CSS as CSS |
| 7 | +import CSS.Common (center) |
| 8 | +import DOM.HTML.Indexed.InputAcceptType (mediaType) |
| 9 | +import DOM.HTML.Indexed.InputType (InputType(..)) |
| 10 | +import Data.Array (snoc) |
| 11 | +import Data.Maybe (Maybe(..), maybe) |
| 12 | +import Data.MediaType (MediaType(..)) |
| 13 | +import Data.Traversable (for, sequence_) |
| 14 | +import Data.Tuple.Nested ((/\)) |
| 15 | +import Effect (Effect) |
| 16 | +import Effect.Class (class MonadEffect, liftEffect) |
| 17 | +import Effect.Ref (Ref) |
| 18 | +import Effect.Ref as Ref |
| 19 | +import Halogen (ClassName(..)) |
| 20 | +import Halogen as H |
| 21 | +import Halogen.Aff as HA |
| 22 | +import Halogen.HTML as HH |
| 23 | +import Halogen.HTML.CSS as HC |
| 24 | +import Halogen.HTML.Events as HE |
| 25 | +import Halogen.HTML.Properties as HP |
| 26 | +import Halogen.Hooks (HookM, StateToken) |
| 27 | +import Halogen.Hooks as Hooks |
| 28 | +import Halogen.Hooks.Extra.Actions.Events (preventDefault) |
| 29 | +import Halogen.VDom.Driver (runUI) |
| 30 | +import Web.File.File (File, toBlob) |
| 31 | +import Web.File.FileList as FileList |
| 32 | +import Web.File.Url (createObjectURL, revokeObjectURL) |
| 33 | +import Web.HTML.Event.DataTransfer as DataTransfer |
| 34 | +import Web.HTML.Event.DragEvent (dataTransfer) |
| 35 | +import Web.HTML.Event.DragEvent as DragEvent |
| 36 | + |
| 37 | +main :: Effect Unit |
| 38 | +main = |
| 39 | + HA.runHalogenAff do |
| 40 | + body <- HA.awaitBody |
| 41 | + void $ runUI hookComponent unit body |
| 42 | + |
| 43 | +hookComponent |
| 44 | + :: forall unusedQuery unusedInput unusedOutput anyMonad |
| 45 | + . MonadEffect anyMonad |
| 46 | + => H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad |
| 47 | +hookComponent = Hooks.component \_ _ -> Hooks.do |
| 48 | + hover /\ hoverIdx <- Hooks.useState false |
| 49 | + _ /\ ref <- Hooks.useRef [] |
| 50 | + urls /\ urlsIdx <- Hooks.useState [] |
| 51 | + Hooks.pure $ |
| 52 | + HH.div |
| 53 | + [ HC.style do |
| 54 | + border dashed (px 6.0) $ if hover then purple else CSS.fromInt 0xcccccc |
| 55 | + borderRadius (px 20.0) (px 20.0) (px 20.0) (px 20.0) |
| 56 | + width (px 480.0) |
| 57 | + height (px 100.0) |
| 58 | + margin (px 100.0) (px 100.0) (px 100.0) (px 100.0) |
| 59 | + padding (px 40.0) (px 40.0) (px 40.0) (px 40.0) |
| 60 | + display flex |
| 61 | + flexDirection column |
| 62 | + justifyContent center |
| 63 | + alignItems center |
| 64 | + , HE.onDragEnter \e -> Just do |
| 65 | + preventDefault DragEvent.toEvent e |
| 66 | + Hooks.put hoverIdx true |
| 67 | + , HE.onDragOver \e -> Just do |
| 68 | + preventDefault DragEvent.toEvent e |
| 69 | + Hooks.put hoverIdx true |
| 70 | + , HE.onDragLeave \e -> Just do |
| 71 | + preventDefault DragEvent.toEvent e |
| 72 | + Hooks.put hoverIdx false |
| 73 | + , HE.onDrop \e -> Just do |
| 74 | + preventDefault DragEvent.toEvent e |
| 75 | + let |
| 76 | + mbFileList = DataTransfer.files $ dataTransfer e |
| 77 | + fileArray = maybe [] FileList.items mbFileList |
| 78 | + putFileUrls ref urlsIdx fileArray |
| 79 | + ] |
| 80 | + -- Note: Elm uses a button that, when clicked, will do the following: |
| 81 | + -- 1. create an input element |
| 82 | + -- 2. add it to the DOM |
| 83 | + -- 3. create a mouse event |
| 84 | + -- 4. dispatch the mouse event to the input element |
| 85 | + -- 5. (implication) file dialogue appears |
| 86 | + -- 6. user selects a file |
| 87 | + -- 7. input event handler runs a callback using user's selected file |
| 88 | + -- 8. input element is removed from DOM |
| 89 | + -- |
| 90 | + -- The approach used below is based on this SO answer: |
| 91 | + -- https://stackoverflow.com/a/47094148 |
| 92 | + [ HH.label |
| 93 | + [ HP.for "file-input" ] |
| 94 | + [ HH.div |
| 95 | + -- simulate button-like appearance |
| 96 | + [ HC.style do |
| 97 | + margin (px 4.0) (px 4.0) (px 4.0) (px 4.0) |
| 98 | + border solid (px 2.0) gray |
| 99 | + borderRadius (px 20.0) (px 20.0) (px 20.0) (px 20.0) |
| 100 | + padding (px 20.0) (px 20.0) (px 20.0) (px 20.0) |
| 101 | + , HP.class_ $ ClassName "otherCssNotInPurescript-Css" |
| 102 | + ] |
| 103 | + [ HH.text "Upload images" ] |
| 104 | + ] |
| 105 | + , HH.input |
| 106 | + [ HP.id_ "file-input" |
| 107 | + , HC.style $ display displayNone |
| 108 | + , HP.type_ InputFile |
| 109 | + , HP.accept $ mediaType $ MediaType "images/*" |
| 110 | + , HP.multiple true |
| 111 | + , HE.onFileUpload \fileArray -> Just do |
| 112 | + putFileUrls ref urlsIdx fileArray |
| 113 | + ] |
| 114 | + , HH.div |
| 115 | + [ HC.style do |
| 116 | + display flex |
| 117 | + alignItems center |
| 118 | + height (px 60.0) |
| 119 | + padding (px 20.0) (px 20.0) (px 20.0) (px 20.0) |
| 120 | + ] |
| 121 | + (urls <#> \fileUrl -> |
| 122 | + HH.div |
| 123 | + [ HC.style do |
| 124 | + width (px 60.0) |
| 125 | + height (px 60.0) |
| 126 | + backgroundImage $ url fileUrl |
| 127 | + backgroundPosition $ placed sideCenter sideCenter |
| 128 | + backgroundRepeat noRepeat |
| 129 | + backgroundSize contain |
| 130 | + ] |
| 131 | + [] |
| 132 | + ) |
| 133 | + ] |
| 134 | + where |
| 135 | + putFileUrls |
| 136 | + :: Ref (Array (Effect Unit)) |
| 137 | + -> StateToken (Array String) |
| 138 | + -> Array File |
| 139 | + -> HookM anyMonad Unit |
| 140 | + putFileUrls arrayRef idx files = do |
| 141 | + -- revoke all prior object urls |
| 142 | + -- by running their effects and ignoring the result |
| 143 | + liftEffect do |
| 144 | + arrayOfRemovePriorObjectUrls <- Ref.read arrayRef |
| 145 | + sequence_ arrayOfRemovePriorObjectUrls |
| 146 | + |
| 147 | + arrayOfUrls <- for files \file -> do |
| 148 | + liftEffect do |
| 149 | + -- create the object url |
| 150 | + urlString <- createObjectURL (toBlob file) |
| 151 | + |
| 152 | + -- create an effect that, when run, will revoke the url |
| 153 | + -- and clean up memory |
| 154 | + let |
| 155 | + revokeUrl :: Effect Unit |
| 156 | + revokeUrl = revokeObjectURL urlString |
| 157 | + -- add that effect to our mutable reference |
| 158 | + Ref.modify_ (\arr -> arr `snoc` revokeUrl) arrayRef |
| 159 | + |
| 160 | + -- now return the original url |
| 161 | + pure urlString |
| 162 | + Hooks.put idx arrayOfUrls |
0 commit comments