wip copy paste action
2 files changed, 84 insertions(+), 65 deletions(-)

M elm/TsEditor.elm
M tsview/tsview_templates/tseditor.html
M elm/TsEditor.elm +54 -47
@@ 55,7 55,6 @@ type Msg
     | InputChanged String String
     | SaveEditedData
     | GotEditedData (Result Http.Error String)
-    | BasicInput String String
     | Paste PasteType
 
 

          
@@ 123,10 122,6 @@ process raw =
         [raw]
 
 
-injectInTable : List String -> String
-injectInTable values = Maybe.withDefault "" (List.head values)
-
-
 geteditor : Model -> Bool -> Cmd Msg
 geteditor model atidate =
     let

          
@@ 199,12 194,12 @@ update msg model =
 
         InputChanged date value ->
             let
-                newEntry = Dict.update
+                newtimeSeries = Dict.update
                     date (updateEntry value) model.horizonModel.timeSeries
                 newHorizonModel = model.horizonModel
             in
             U.nocmd { model
-                | horizonModel = { newHorizonModel | timeSeries = newEntry }
+                | horizonModel = { newHorizonModel | timeSeries = newtimeSeries }
                 }
 
         SaveEditedData ->

          
@@ 226,16 221,50 @@ update msg model =
         GotMetadata (Err err) ->
             doerr "gotmeta http" <| U.unwraperror err
 
-        BasicInput index pasted -> (
-            { model | contentCell = injectInTable (process pasted)
-                    , indexToInsert = Just index}
-            , Cmd.none )
-        Paste payload -> (
-            { model | rawPasted = payload.text
-                    , processedPasted = process payload.text
-                    , contentCell = injectInTable (process payload.text)
-                    , indexToInsert = Just payload.index}
-            , Cmd.none)
+        Paste payload ->
+            let
+                newtimeSeries = getPastedDict model payload
+                newHorizonModel = model.horizonModel
+            in
+            U.nocmd { model
+                | horizonModel = { newHorizonModel | timeSeries = newtimeSeries }
+                }
+
+
+parseCopyPastedData : String -> String
+parseCopyPastedData value =
+    if Maybe.isJust (String.toFloat value) then
+        value
+    else
+        ""
+
+
+getPastedDict : Model -> PasteType -> Dict String Entry
+getPastedDict model payload =
+    let
+        newValues = List.map parseCopyPastedData (process payload.text)
+        _  = Debug.log "newValues" newValues
+        firstIndex = Maybe.withDefault
+            0
+            (Maybe.map
+                    (\entry -> entry.index)
+                    (Dict.get (payload.index) model.horizonModel.timeSeries))
+        listIndex = List.range
+                        firstIndex (firstIndex + (List.length newValues) - 1)
+        listDates = Dict.keys
+                        (Dict.filter
+                        (\_ value -> List.member value.index listIndex)
+                        model.horizonModel.timeSeries)
+        copyPastedDict = Dict.fromList
+            (List.map2
+                Tuple.pair listDates newValues)
+    in Dict.merge
+        (\_ _ dict -> dict)
+        (\key _ value dict -> Dict.update key (updateEntry value) dict)
+        (\_ _ dict -> dict)
+        model.horizonModel.timeSeries
+        copyPastedDict
+        model.horizonModel.timeSeries
 
 
 updateEntry : String -> Maybe Entry -> Maybe Entry

          
@@ 390,11 419,13 @@ viewRow ( date, entry ) =
         , H.td
             [ ]
             [ H.input
-                [ HA.placeholder "enter your value"
+                [ HA.class "pasteble"
+                , HA.placeholder "enter your value"
                 , HA.type_ "number"
                 , HA.value data
                 , HE.onInput (InputChanged date)
-                , HA.id date
+                , HA.attribute "index" date
+                , onPaste Paste
                 ]
                 [ ]
             ]

          
@@ 415,29 446,6 @@ viewPlotData model =
         ]
 
 
-viewCopyPast : Model -> H.Html Msg
-viewCopyPast model =
-    H.div []
-    [
-    H.table []
-          [ H.tr []
-               [ H.td [] [H.text "2024-01-01"]
-               , H.td [HA.style "width" "150px"]
-                   [ H.input
-                        [ HA.id "my-input"
-                        , HA.attribute "index" "42"
-                        , onPaste Paste
-                        , HE.onInput (BasicInput "42")
-                        , HA.value model.contentCell]
-                        [] ]]]
-   , H.div [] [H.text ("Pasted raw = " ++ model.rawPasted)]
-   , H.div []  (List.map (\ cell -> H.div [] [H.text cell]) model.processedPasted)
-   , H.div [] [H.text ("content " ++ model.contentCell)]
-  , H.div [] [H.text ( "The pasted values should be inserted from the index:  "
-                   ++ Maybe.withDefault "Nothing to paste yet" model.indexToInsert ) ]
-    ]
-
-
 view : Model -> H.Html Msg
 view model =
     H.div

          
@@ 451,7 459,6 @@ view model =
                 , viewEditedRow model
                 ]
             ]
-        , viewCopyPast model
         ]
 
 

          
@@ 482,6 489,9 @@ editTable model =
                     (Dict.toList model.horizonModel.timeSeries)
                 )
             ]
+        , H.node "eval-js"
+            [ HA.attribute "myjs" ("applyCopyPaste();") ]
+            [ ]
         ]
 
 

          
@@ 532,7 542,4 @@ main =
                , view = view
                , update = update
                , subscriptions = \model -> Sub.none
-               }
-
-
-
+               }
  No newline at end of file

          
M tsview/tsview_templates/tseditor.html +30 -18
@@ 49,25 49,37 @@ 
                 }
             });
 
-            const inputs = document.querySelectorAll("#ts_table td:nth-child(2) input");
-            const ids = Array.from(inputs).map(toto => toto.id);
-            console.log(ids);
-
+        function applyCopyPaste () {
+            var myinputs = document.getElementsByClassName("pasteble")
+            for (var i = 0; i<myinputs.length; i++){
+                let myinput = myinputs[i]
+                console.log("in the loop")
+                myinput.onpaste = e => {
+                    e.preventDefault();
+                    const clipboardData = e.clipboardData || window.clipboardData;
+                    const text = clipboardData.getData('text') || "";
+                    const index = myinput.getAttribute('index')
+                    const newEvent = new CustomEvent("pastewithdata", {
+                        detail: {
+                            text: text,
+                            index: index
+                        }
+                    });
+                    console.log("newEvent", newEvent)
+                    myinput.dispatchEvent(newEvent);
+                    return false;
+                };
+            };}
 
-        // myinput.onpaste = e => {
-        //     e.preventDefault();
-        //     const clipboardData = e.clipboardData || window.clipboardData;
-        //     const text = clipboardData.getData('text') || "";
-        //     const index = myinput.getAttribute('index')
-        //     const newEvent = new CustomEvent("pastewithdata", {
-        //         detail: {
-        //             text: text,
-        //             index: index
-        //         }
-        //     });
-        //     myinput.dispatchEvent(newEvent);
-        //     return false;
-        // };
+        class EvalJs extends HTMLElement {
+            static get observedAttributes() {
+                return ['myjs'];
+            }
+            attributeChangedCallback(name, oldValue, newValue) {
+            eval(newValue);
+            }
+        }
+        window.customElements.define('eval-js', EvalJs);
         </script>
     </body>
 </html>
  No newline at end of file