Browse Source

fix: remove editor-handler/*selected-text

This is a follow-up fix on #7705
Tienson Qin 2 years ago
parent
commit
32b83cb3b5

+ 17 - 16
src/main/frontend/components/editor.cljs

@@ -98,7 +98,6 @@
   (not (.contains (.getElementById js/document "left-container") el)))
 
 (rum/defc page-search < rum/reactive
-  {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
   "Embedded page searching popup"
   [id format]
   (let [action (state/sub :editor/action)]
@@ -110,7 +109,7 @@
                 edit-content (or (state/sub [:editor/content id]) "")
                 sidebar? (in-sidebar? input)
                 q (or
-                   @editor-handler/*selected-text
+                   (editor-handler/get-selected-text)
                    (when (= action :page-search-hashtag)
                      (gp-util/safe-subs edit-content pos current-pos))
                    (when (> (count edit-content) current-pos)
@@ -162,20 +161,25 @@
               :empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 "Search for a page"]
               :class       "black"})))))))
 
+(defn- search-blocks!
+  [state result]
+  (let [[edit-block _ _ q] (:rum/args state)]
+    (p/let [matched-blocks (when-not (string/blank? q)
+                             (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
+      (reset! result matched-blocks))))
+
 (rum/defcs block-search-auto-complete < rum/reactive
   {:init (fn [state]
-           (assoc state ::result (atom nil)))
+           (let [result (atom nil)]
+             (search-blocks! state result)
+             (assoc state ::result result)))
    :did-update (fn [state]
-                 (let [result (::result state)
-                       [edit-block _ _ q] (:rum/args state)]
-                   (p/let [matched-blocks (when-not (string/blank? q)
-                                            (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
-                     (reset! result matched-blocks)))
+                 (search-blocks! state (::result state))
                  state)}
-  [state _edit-block input id q format]
+  [state _edit-block input id q format selected-text]
   (let [result (->> (rum/react (get state ::result))
                     (remove (fn [b] (string/blank? (:block/content (db-model/query-block-by-uuid (:block/uuid b)))))))
-        chosen-handler (editor-handler/block-on-chosen-handler input id q format)
+        chosen-handler (editor-handler/block-on-chosen-handler id q format selected-text)
         non-exist-block-handler (editor-handler/block-non-exist-handler input)]
     (ui/auto-complete
      result
@@ -195,7 +199,6 @@
 
 (rum/defcs block-search < rum/reactive
   {:will-unmount (fn [state]
-                   (reset! editor-handler/*selected-text nil)
                    (state/clear-search-result!)
                    state)}
   [state id _format]
@@ -206,15 +209,15 @@
           current-pos (cursor/pos input)
           edit-content (state/sub [:editor/content id])
           edit-block (state/get-edit-block)
+          selected-text (editor-handler/get-selected-text)
           q (or
-             @editor-handler/*selected-text
+             selected-text
              (when (> (count edit-content) current-pos)
                (subs edit-content pos current-pos)))]
       (when input
-        (block-search-auto-complete edit-block input id q format)))))
+        (block-search-auto-complete edit-block input id q format selected-text)))))
 
 (rum/defc template-search < rum/reactive
-  {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
   [id _format]
   (let [pos (state/get-editor-last-pos)
         input (gdom/getElement id)]
@@ -238,7 +241,6 @@
           :class       "black"})))))
 
 (rum/defc property-search < rum/reactive
-  {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
   [id]
   (let [input (gdom/getElement id)]
     (when input
@@ -258,7 +260,6 @@
           :class       "black"})))))
 
 (rum/defc property-value-search < rum/reactive
-  {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
   [id]
   (let [property (:property (state/get-editor-action-data))
         input (gdom/getElement id)]

+ 12 - 6
src/main/frontend/handler/editor.cljs

@@ -58,7 +58,6 @@
 
 (defonce *asset-uploading? (atom false))
 (defonce *asset-uploading-process (atom 0))
-(defonce *selected-text (atom nil))
 
 (defn get-selection-and-format
   []
@@ -1524,7 +1523,6 @@
         value (str prefix postfix)
         input (gdom/getElement input-id)]
     (when value
-      (when-not (string/blank? selected) (reset! *selected-text selected))
       (let [[prefix _pos] (commands/simple-replace! input-id value selected
                                                     {:backward-pos (count postfix)
                                                      :check-fn (fn [new-value prefix-pos]
@@ -1535,12 +1533,14 @@
           (= prefix page-ref/left-brackets)
           (do
             (commands/handle-step [:editor/search-page])
-            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
+            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)
+                                            :selected selected}))
 
           (= prefix block-ref/left-parens)
           (do
             (commands/handle-step [:editor/search-block :reference])
-            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})))))))
+            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)
+                                            :selected selected})))))))
 
 (defn surround-by?
   [input before end]
@@ -1848,8 +1848,14 @@
       :else
       nil)))
 
+(defn get-selected-text
+  []
+  (let [text (:selected (state/get-editor-action-data))]
+    (when-not (string/blank? text)
+      text)))
+
 (defn block-on-chosen-handler
-  [_input id q format]
+  [id q format selected-text]
   (fn [chosen _click?]
     (state/clear-editor-action!)
     (let [uuid-string (str (:block/uuid chosen))]
@@ -1858,7 +1864,7 @@
       (insert-command! id
                        (block-ref/->block-ref uuid-string)
                        format
-                       {:last-pattern (str block-ref/left-parens (if @*selected-text "" q))
+                       {:last-pattern (str block-ref/left-parens (if selected-text "" q))
                         :end-pattern block-ref/right-parens
                         :postfix-fn   (fn [s] (util/replace-first block-ref/right-parens s ""))
                         :forward-pos 3

+ 3 - 3
src/main/frontend/handler/page.cljs

@@ -754,7 +754,7 @@
         action (state/get-editor-action)
         hashtag? (= action :page-search-hashtag)
         q (or
-           @editor-handler/*selected-text
+           (editor-handler/get-selected-text)
            (when hashtag?
              (gp-util/safe-subs edit-content pos current-pos))
            (when (> (count edit-content) current-pos)
@@ -770,7 +770,7 @@
               chosen (if (and (util/safe-re-find #"\s+" chosen) (not wrapped?))
                        (page-ref/->page-ref chosen)
                        chosen)
-              q (if @editor-handler/*selected-text "" q)
+              q (if (editor-handler/get-selected-text) "" q)
               last-pattern (if wrapped?
                              q
                              (if (= \# (first q))
@@ -793,7 +793,7 @@
           (editor-handler/insert-command! id
                                           page-ref-text
                                           format
-                                          {:last-pattern (str page-ref/left-brackets (if @editor-handler/*selected-text "" q))
+                                          {:last-pattern (str page-ref/left-brackets (if (editor-handler/get-selected-text) "" q))
                                            :end-pattern page-ref/right-brackets
                                            :postfix-fn   (fn [s] (util/replace-first page-ref/right-brackets s ""))
                                            :command :page-ref}))))))