浏览代码

refactor: move block/page/template searches to editor handler

Tienson Qin 4 年之前
父节点
当前提交
b0f48a4244

+ 6 - 97
src/main/frontend/components/editor.cljs

@@ -32,7 +32,6 @@
             [medley.core :as medley]
             [medley.core :as medley]
             [cljs-drag-n-drop.core :as dnd]
             [cljs-drag-n-drop.core :as dnd]
             [frontend.text :as text]
             [frontend.text :as text]
-            [frontend.template :as template]
             [frontend.date :as date]
             [frontend.date :as date]
             [frontend.handler.notification :as notification]
             [frontend.handler.notification :as notification]
             ["/frontend/utils" :as utils]))
             ["/frontend/utils" :as utils]))
@@ -93,45 +92,11 @@
                  (when (> (count edit-content) current-pos)
                  (when (> (count edit-content) current-pos)
                    (util/safe-subs edit-content pos current-pos)))
                    (util/safe-subs edit-content pos current-pos)))
               matched-pages (when-not (string/blank? q)
               matched-pages (when-not (string/blank? q)
-                              (editor-handler/get-matched-pages q))
-              chosen-handler (if (state/sub :editor/show-page-search-hashtag?)
-                               (fn [chosen _click?]
-                                 (state/set-editor-show-page-search! false)
-                                 (let [chosen (if (re-find #"\s+" chosen)
-                                                (util/format "[[%s]]" chosen)
-                                                chosen)]
-                                   (editor-handler/insert-command! id
-                                                                   (str "#" chosen)
-                                                                   format
-                                                                   {:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})))
-                               (fn [chosen _click?]
-                                 (state/set-editor-show-page-search! false)
-                                 (let [page-ref-text (page-handler/get-page-ref-text chosen)]
-                                   (editor-handler/insert-command! id
-                                                                   page-ref-text
-                                                                   format
-                                                                   {:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
-                                                                    :postfix-fn   (fn [s] (util/replace-first "]]" s ""))}))))
-              non-exist-page-handler (fn [_state]
-                                       (state/set-editor-show-page-search! false)
-                                       (if (state/org-mode-file-link? (state/get-current-repo))
-                                         (let [page-ref-text (page-handler/get-page-ref-text q)
-                                               value (gobj/get input "value")
-                                               old-page-ref (util/format "[[%s]]" q)
-                                               new-value (string/replace value
-                                                                         old-page-ref
-                                                                         page-ref-text)]
-                                           (state/set-edit-content! id new-value)
-                                           (let [new-pos (+ current-pos
-                                                            (- (count page-ref-text)
-                                                               (count old-page-ref))
-                                                            2)]
-                                             (util/move-cursor-to input new-pos)))
-                                         (util/cursor-move-forward input 2)))]
+                              (editor-handler/get-matched-pages q))]
           (ui/auto-complete
           (ui/auto-complete
            matched-pages
            matched-pages
-           {:on-chosen chosen-handler
-            :on-enter  non-exist-page-handler
+           {:on-chosen (page-handler/on-chosen-handler input id q pos format)
+            :on-enter #(page-handler/page-not-exists-handler input id q current-pos)
             :empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a page"]
             :empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a page"]
             :class     "black"}))))))
             :class     "black"}))))))
 
 
@@ -155,27 +120,8 @@
           matched-blocks (when-not (string/blank? q)
           matched-blocks (when-not (string/blank? q)
                            (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
                            (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
       (when input
       (when input
-        (let [chosen-handler (fn [chosen _click?]
-                               (state/set-editor-show-block-search! false)
-                               (let [uuid-string (str (:block/uuid chosen))]
-
-                                 ;; block reference
-                                 (editor-handler/insert-command! id
-                                                                 (util/format "((%s))" uuid-string)
-                                                                 format
-                                                                 {:last-pattern (str "((" (if @editor-handler/*selected-text "" q))
-                                                                  :postfix-fn   (fn [s] (util/replace-first "))" s ""))})
-
-                                 ;; Save it so it'll be parsed correctly in the future
-                                 (editor-handler/set-block-property! (:block/uuid chosen)
-                                                                     "ID"
-                                                                     uuid-string)
-
-                                 (when-let [input (gdom/getElement id)]
-                                   (.focus input))))
-              non-exist-block-handler (fn [_state]
-                                        (state/set-editor-show-block-search! false)
-                                        (util/cursor-move-forward input 2))]
+        (let [chosen-handler (editor-handler/block-on-chosen-handler input id q format)
+              non-exist-block-handler (editor-handler/block-non-exist-handler input)]
           (ui/auto-complete
           (ui/auto-complete
            matched-blocks
            matched-blocks
            {:on-chosen   chosen-handler
            {:on-chosen   chosen-handler
@@ -200,48 +146,11 @@
                    (subs edit-content pos current-pos))
                    (subs edit-content pos current-pos))
                  "")
                  "")
               matched-templates (editor-handler/get-matched-templates q)
               matched-templates (editor-handler/get-matched-templates q)
-              chosen-handler (fn [[template db-id] _click?]
-                               (if-let [block (db/entity db-id)]
-                                 (let [new-level (:block/level edit-block)
-                                       properties (:block/properties block)
-                                       block-uuid (:block/uuid block)
-                                       including-parent? (not= (get properties "including-parent") "false")
-                                       template-parent-level (:block/level block)
-                                       pattern (config/get-block-pattern format)
-                                       content
-                                       (block-handler/get-block-full-content
-                                        (state/get-current-repo)
-                                        (:block/uuid block)
-                                        (fn [{:block/keys [uuid level content properties] :as block}]
-                                          (let [parent? (= uuid block-uuid)
-                                                ignore-parent? (and parent? (not including-parent?))]
-                                            (if ignore-parent?
-                                              ""
-                                              (let [new-level (+ new-level
-                                                                 (- level template-parent-level
-                                                                    (if (not including-parent?) 1 0)))
-                                                    properties' (dissoc (into {} properties) "id" "custom_id" "template" "including-parent")]
-                                                (-> content
-                                                   (string/replace-first (apply str (repeat level pattern))
-                                                                         (apply str (repeat new-level pattern)))
-                                                   text/remove-properties!
-                                                   (text/rejoin-properties properties')))))))
-                                       content (if (string/includes? (string/trim edit-content) "\n")
-                                                 content
-                                                 (text/remove-level-spaces content format))
-                                       content (template/resolve-dynamic-template! content)]
-                                   (state/set-editor-show-template-search! false)
-                                   (editor-handler/insert-command! id
-                                                                   content
-                                                                   format
-                                                                   {})))
-                               (when-let [input (gdom/getElement id)]
-                                 (.focus input)))
               non-exist-handler (fn [_state]
               non-exist-handler (fn [_state]
                                   (state/set-editor-show-template-search! false))]
                                   (state/set-editor-show-template-search! false))]
           (ui/auto-complete
           (ui/auto-complete
            matched-templates
            matched-templates
-           {:on-chosen   chosen-handler
+           {:on-chosen   (editor-handler/template-on-chosen-handler input id q format edit-block edit-content)
             :on-enter    non-exist-handler
             :on-enter    non-exist-handler
             :empty-div   [:div.text-gray-500.pl-4.pr-4 "Search for a template"]
             :empty-div   [:div.text-gray-500.pl-4.pr-4 "Search for a template"]
             :item-render (fn [[template _block-db-id]]
             :item-render (fn [[template _block-db-id]]

+ 66 - 0
src/main/frontend/handler/editor.cljs

@@ -12,6 +12,7 @@
             [frontend.handler.notification :as notification]
             [frontend.handler.notification :as notification]
             [frontend.handler.draw :as draw]
             [frontend.handler.draw :as draw]
             [frontend.handler.expand :as expand]
             [frontend.handler.expand :as expand]
+            [frontend.handler.block :as block-handler]
             [frontend.format.mldoc :as mldoc]
             [frontend.format.mldoc :as mldoc]
             [frontend.format :as format]
             [frontend.format :as format]
             [frontend.format.block :as block]
             [frontend.format.block :as block]
@@ -44,6 +45,7 @@
             [frontend.text :as text]
             [frontend.text :as text]
             [frontend.date :as date]
             [frontend.date :as date]
             [frontend.handler.repeated :as repeated]
             [frontend.handler.repeated :as repeated]
+            [frontend.template :as template]
             [clojure.core.async :as async]
             [clojure.core.async :as async]
             [lambdaisland.glogi :as log]))
             [lambdaisland.glogi :as log]))
 
 
@@ -2283,3 +2285,67 @@
           (reset! commands/*angle-bracket-caret-pos (util/get-caret-pos input))
           (reset! commands/*angle-bracket-caret-pos (util/get-caret-pos input))
           (reset! commands/*show-block-commands true))
           (reset! commands/*show-block-commands true))
         nil))))
         nil))))
+
+(defn block-on-chosen-handler
+  [input id q format]
+  (fn [chosen _click?]
+    (state/set-editor-show-block-search! false)
+    (let [uuid-string (str (:block/uuid chosen))]
+
+      ;; block reference
+      (insert-command! id
+                       (util/format "((%s))" uuid-string)
+                       format
+                       {:last-pattern (str "((" (if @*selected-text "" q))
+                        :postfix-fn   (fn [s] (util/replace-first "))" s ""))})
+
+      ;; Save it so it'll be parsed correctly in the future
+      (set-block-property! (:block/uuid chosen)
+                           "ID"
+                           uuid-string)
+
+      (when-let [input (gdom/getElement id)]
+        (.focus input)))))
+
+(defn block-non-exist-handler
+  [input]
+  (fn []
+    (state/set-editor-show-block-search! false)
+    (util/cursor-move-forward input 2)))
+
+(defn template-on-chosen-handler
+  [input id q format edit-block edit-content]
+  (fn [[template db-id] _click?]
+    (if-let [block (db/entity db-id)]
+      (let [new-level (:block/level edit-block)
+            properties (:block/properties block)
+            block-uuid (:block/uuid block)
+            including-parent? (not= (get properties "including-parent") "false")
+            template-parent-level (:block/level block)
+            pattern (config/get-block-pattern format)
+            content
+            (block-handler/get-block-full-content
+             (state/get-current-repo)
+             (:block/uuid block)
+             (fn [{:block/keys [uuid level content properties] :as block}]
+               (let [parent? (= uuid block-uuid)
+                     ignore-parent? (and parent? (not including-parent?))]
+                 (if ignore-parent?
+                   ""
+                   (let [new-level (+ new-level
+                                      (- level template-parent-level
+                                         (if (not including-parent?) 1 0)))
+                         properties' (dissoc (into {} properties) "id" "custom_id" "template" "including-parent")]
+                     (-> content
+                         (string/replace-first (apply str (repeat level pattern))
+                                               (apply str (repeat new-level pattern)))
+                         text/remove-properties!
+                         (text/rejoin-properties properties')))))))
+            content (if (string/includes? (string/trim edit-content) "\n")
+                      content
+                      (text/remove-level-spaces content format))
+            content (template/resolve-dynamic-template! content)]
+        (state/set-editor-show-template-search! false)
+        (insert-command! id content format {})))
+    (when-let [input (gdom/getElement id)]
+      (.focus input))))

+ 51 - 1
src/main/frontend/handler/page.cljs

@@ -25,7 +25,8 @@
             [lambdaisland.glogi :as log]
             [lambdaisland.glogi :as log]
             [frontend.format.mldoc :as mldoc]
             [frontend.format.mldoc :as mldoc]
             [cljs-time.core :as t]
             [cljs-time.core :as t]
-            [cljs-time.coerce :as tc]))
+            [cljs-time.coerce :as tc]
+            [goog.object :as gobj]))
 
 
 (defn- get-directory
 (defn- get-directory
   [journal?]
   [journal?]
@@ -519,3 +520,52 @@
   [page-name]
   [page-name]
   (when page-name
   (when page-name
     (db/entity [:page/name page-name])))
     (db/entity [:page/name page-name])))
+
+;; Editor
+(defn page-not-exists-handler
+  [input id q current-pos]
+  (state/set-editor-show-page-search! false)
+  (if (state/org-mode-file-link? (state/get-current-repo))
+    (let [page-ref-text (get-page-ref-text q)
+          value (gobj/get input "value")
+          old-page-ref (util/format "[[%s]]" q)
+          new-value (string/replace value
+                                    old-page-ref
+                                    page-ref-text)]
+      (state/set-edit-content! id new-value)
+      (let [new-pos (+ current-pos
+                       (- (count page-ref-text)
+                          (count old-page-ref))
+                       2)]
+        (util/move-cursor-to input new-pos)))
+    (util/cursor-move-forward input 2)))
+
+(defn on-chosen-handler
+  [input id q pos format]
+  (let [current-pos (:pos (util/get-caret-pos input))
+        edit-content (state/sub [:editor/content id])
+        edit-block (state/sub :editor/block)
+        q (or
+           @editor-handler/*selected-text
+           (when (state/sub :editor/show-page-search-hashtag?)
+             (util/safe-subs edit-content pos current-pos))
+           (when (> (count edit-content) current-pos)
+             (util/safe-subs edit-content pos current-pos)))]
+    (if (state/sub :editor/show-page-search-hashtag?)
+      (fn [chosen _click?]
+        (state/set-editor-show-page-search! false)
+        (let [chosen (if (re-find #"\s+" chosen)
+                       (util/format "[[%s]]" chosen)
+                       chosen)]
+          (editor-handler/insert-command! id
+                                          (str "#" chosen)
+                                          format
+                                          {:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})))
+      (fn [chosen _click?]
+        (state/set-editor-show-page-search! false)
+        (let [page-ref-text (get-page-ref-text chosen)]
+          (editor-handler/insert-command! id
+                                          page-ref-text
+                                          format
+                                          {:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
+                                           :postfix-fn   (fn [s] (util/replace-first "]]" s ""))}))))))

+ 1 - 1
src/main/frontend/ui.css

@@ -62,7 +62,7 @@
   }
   }
 
 
   &-overlay div {
   &-overlay div {
-    background: var(--ls-quaternary-background-color);
+    background: var(--ls-tertiary-background-color);
   }
   }
 
 
   &-panel {
   &-panel {