Browse Source

fix: cannot paste at middle of some text

related to #5501
Tienson Qin 3 years ago
parent
commit
8bd278ea44

+ 16 - 13
src/main/frontend/handler/editor.cljs

@@ -2043,18 +2043,21 @@
                         (db/entity [:block/name (util/page-name-sanity-lc id)]))]
       (= (:block/uuid entity) (tree/-get-parent-id current-node)))))
 
-(defn- insert
-  [insertion]
-  (when-not (auto-complete?)
-    (let [^js input (state/get-input)
-          selected-start (util/get-selection-start input)
-          selected-end (util/get-selection-end input)
-          value (.-value input)
-          s1 (subs value 0 selected-start)
-          s2 (subs value selected-end)]
-      (state/set-edit-content! (state/get-edit-input-id)
-                               (str s1 insertion s2))
-      (cursor/move-cursor-to input (+ selected-start (count insertion))))))
+(defn insert
+  ([insertion]
+   (insert insertion false))
+  ([insertion auto-complete-enabled?]
+   (when (or auto-complete-enabled?
+             (not (auto-complete?)))
+     (let [^js input (state/get-input)
+           selected-start (util/get-selection-start input)
+           selected-end (util/get-selection-end input)
+           value (.-value input)
+           s1 (subs value 0 selected-start)
+           s2 (subs value selected-end)]
+       (state/set-edit-content! (state/get-edit-input-id)
+                                (str s1 insertion s2))
+       (cursor/move-cursor-to input (+ selected-start (count insertion)))))))
 
 (defn- keydown-new-line
   []
@@ -2949,7 +2952,7 @@
        (let [data (if (gp-util/url? clipboard-data)
                         (wrap-macro-url clipboard-data)
                         clipboard-data)]
-             (state/append-current-edit-content! data))))
+         (insert data true))))
    (fn [error]
      (js/console.error error))))
 

+ 3 - 3
src/main/frontend/mobile/intent.cljs

@@ -52,7 +52,7 @@
                    (string/replace "{text}" (or text ""))
                    (string/replace "{url}" (or url "")))]
     (if (state/get-edit-block)
-      (state/append-current-edit-content! values)
+      (editor-handler/insert values)
       (editor-handler/api-insert-new-block! values {:page page
                                                     :edit-block? false
                                                     :replace-empty-target? true}))))
@@ -102,7 +102,7 @@
           format (db/get-page-format page)
           content (embed-asset-file url format)]
     (if (state/get-edit-block)
-      (state/append-current-edit-content! content)
+      (editor-handler/insert content)
       (editor-handler/api-insert-new-block! content {:page page
                                                      :edit-block? false
                                                      :replace-empty-target? true}))))
@@ -129,7 +129,7 @@
                       ". We will look into it soon."]
                      :warning false))]
     (if (state/get-edit-block)
-      (state/append-current-edit-content! content)
+      (editor-handler/insert content)
       (editor-handler/api-insert-new-block! content {:page page
                                                      :edit-block? false
                                                      :replace-empty-target? true}))))

+ 1 - 1
src/main/frontend/mobile/record.cljs

@@ -63,7 +63,7 @@
                       {:edit-block? false
                        :replace-empty-target? true})]
     (if edit-block
-      (state/append-current-edit-content! file-link)
+      (editor-handler/insert file-link)
       (editor-handler/api-insert-new-block! file-link args))))
 
 (defn stop-recording []

+ 0 - 15
src/main/frontend/state.cljs

@@ -567,21 +567,6 @@
   []
   (sub [:editor/content (get-edit-input-id)]))
 
-(defn append-current-edit-content!
-  [append-text]
-  (when-not (string/blank? append-text)
-    (when-let [input-id (get-edit-input-id)]
-      (when-let [input (gdom/getElement input-id)]
-        (let [value (gobj/get input "value")
-              new-value (if (or (string/blank? value)
-                                (= (last value) " ")
-                                (= (last value) "\n"))
-                          (str value append-text)
-                          (str value "\n" append-text))]
-          (util/set-change-value input new-value)
-          (update-state! :editor/content (fn [m]
-                                           (assoc m input-id new-value))))))))
-
 (defn get-cursor-range
   []
   (:cursor-range @state))