Browse Source

fix: pasting does not replace a selected text

related to #5700
Tienson Qin 3 years ago
parent
commit
d81dcb2de3
2 changed files with 21 additions and 3 deletions
  1. 14 0
      src/main/frontend/commands.cljs
  2. 7 3
      src/main/frontend/handler/paste.cljs

+ 14 - 0
src/main/frontend/commands.cljs

@@ -438,6 +438,20 @@
     (state/set-block-content-and-last-pos! id new-value new-pos)
     (state/set-block-content-and-last-pos! id new-value new-pos)
     (cursor/move-cursor-to input new-pos)))
     (cursor/move-cursor-to input new-pos)))
 
 
+(defn delete-selection!
+  [id]
+  (let [input (gdom/getElement id)
+        edit-content (gobj/get input "value")
+        start (util/get-selection-start input)
+        end (util/get-selection-end input)]
+    (when-not (= start end)
+      (let [prefix (subs edit-content 0 start)
+            new-value (str prefix
+                           (subs edit-content end))
+            new-pos (count prefix)]
+        (state/set-block-content-and-last-pos! id new-value new-pos)
+        (cursor/move-cursor-to input new-pos)))))
+
 (defn get-matched-commands
 (defn get-matched-commands
   ([text]
   ([text]
    (get-matched-commands text @*initial-commands))
    (get-matched-commands text @*initial-commands))

+ 7 - 3
src/main/frontend/handler/paste.cljs

@@ -109,7 +109,11 @@
           (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
           (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
                 text (or (when-not (string/blank? html)
                 text (or (when-not (string/blank? html)
                            (html-parser/convert format html))
                            (html-parser/convert format html))
-                         text)]
+                         text)
+                input-id (state/get-edit-input-id)
+                replace-text-f (fn []
+                                 (commands/delete-selection! input-id)
+                                 (commands/simple-insert! input-id text nil))]
             (util/stop e)
             (util/stop e)
             (match [format
             (match [format
                     (nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
                     (nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
@@ -125,13 +129,13 @@
               (paste-segmented-text format text)
               (paste-segmented-text format text)
 
 
               [:markdown true _ true]
               [:markdown true _ true]
-              (commands/simple-insert! (state/get-edit-input-id) text nil)
+              (replace-text-f)
 
 
               [:org _ true false]
               [:org _ true false]
               (paste-segmented-text format text)
               (paste-segmented-text format text)
 
 
               [:org _ true true]
               [:org _ true true]
-              (commands/simple-insert! (state/get-edit-input-id) text nil))))))))
+              (replace-text-f))))))))
 
 
 (defn paste-text-in-one-block-at-point
 (defn paste-text-in-one-block-at-point
   []
   []