Bladeren bron

fix: [[]] auto complete support all kinds of blocks

Tienson Qin 1 jaar geleden
bovenliggende
commit
3d8cc3df08
3 gewijzigde bestanden met toevoegingen van 48 en 49 verwijderingen
  1. 27 34
      src/main/frontend/components/editor.cljs
  2. 5 3
      src/main/frontend/handler/editor.cljs
  3. 16 12
      src/main/frontend/handler/page.cljs

+ 27 - 34
src/main/frontend/components/editor.cljs

@@ -140,50 +140,43 @@
                          (p/let [result (editor-handler/<get-matched-pages q)]
                            (set-matched-pages! result))))
                      [q])
-    (let [matched-pages (cond
-                          (contains? (set (map util/page-name-sanity-lc matched-pages))
-                                     (util/page-name-sanity-lc (string/trim q)))  ;; if there's a page name fully matched
-                          (sort-by (fn [m] [(count m) m]) matched-pages)
-
-                          (string/blank? q)
-                          nil
-
-                          (empty? matched-pages)
-                          (when-not (db/page-exists? q)
-                            (if db-tag?
-                              (concat [(str (t :new-class) " " q)
-                                       (str (t :new-page) " " q)]
-                                      matched-pages)
-                              (cons (str (t :new-page) " " q)
-                                    matched-pages)))
-
+    (let [matched-pages (when-not (string/blank? q)
                           ;; reorder, shortest and starts-with first.
-                          :else
-                          (let [matched-pages (remove nil? matched-pages)
-                                matched-pages (sort-by
-                                               (fn [m]
-                                                 [(not (gstring/caseInsensitiveStartsWith m q)) (count m) m])
-                                               matched-pages)]
-                            (if (gstring/caseInsensitiveStartsWith (first matched-pages) q)
+                          (let [matched-pages
+                                (->> matched-pages
+                                     (sort-by
+                                      (fn [block]
+                                        (let [m (:block/title block)]
+                                          [(not (gstring/caseInsensitiveStartsWith m q)) (count m) m]))))
+                                matched-pages-with-new-page
+                                (fn [partial-matched-pages]
+                                  (if (or (db/page-exists? q)
+                                          (some (fn [p] (= (string/lower-case q)
+                                                           (string/lower-case (:block/title p)))) matched-pages))
+                                    partial-matched-pages
+                                    (if db-tag?
+                                      (concat [{:block/title (str (t :new-class) " " q)}
+                                               {:block/title (str (t :new-page) " " q)}]
+                                              partial-matched-pages)
+                                      (cons {:block/title (str (t :new-page) " " q)}
+                                            partial-matched-pages))))]
+                            (if (and (seq matched-pages)
+                                     (gstring/caseInsensitiveStartsWith (:block/title (first matched-pages)) q))
                               (cons (first matched-pages)
-                                    (if (db/page-exists? q)
-                                      (rest matched-pages)
-                                      (cons (str (t :new-page) " " q) (rest matched-pages))))
-                              (if (db/page-exists? q)
-                                matched-pages
-                                (cons (str (t :new-page) " " q) matched-pages)))))]
+                                    (matched-pages-with-new-page (rest matched-pages)))
+                              (matched-pages-with-new-page matched-pages))))]
       (ui/auto-complete
        matched-pages
        {:on-chosen   (page-on-chosen-handler embed? input id q pos format)
         :on-enter    (fn []
                        (page-handler/page-not-exists-handler input id q current-pos))
-        :item-render (fn [page-name _chosen?]
+        :item-render (fn [block _chosen?]
                        [:div.flex
-                        (when (db-model/whiteboard-page? page-name) [:span.mr-1 (ui/icon "whiteboard" {:extension? true})])
-                        (search-handler/highlight-exact-query page-name q)])
+                        (when (db-model/whiteboard-page? block) [:span.mr-1 (ui/icon "whiteboard" {:extension? true})])
+                        (search-handler/highlight-exact-query (:block/title block) q)])
         :empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 (if db-tag?
                                                                    "Search for a page or a class"
-                                                                   "Search for a page")]
+                                                                   "Search for a block")]
         :class       "black"}))))
 
 (rum/defc page-search < rum/reactive

+ 5 - 3
src/main/frontend/handler/editor.cljs

@@ -1644,15 +1644,17 @@
   [q]
   (p/let [block (state/get-edit-block)
           editing-page-id (and block
-                            (when-let [page-id (:db/id (:block/page block))]
-                              (:block/uuid (db/entity page-id))))
+                               (when-let [page-id (:db/id (:block/page block))]
+                                 (:block/uuid (db/entity page-id))))
           pages (search/block-search (state/get-current-repo) q {:built-in? false
                                                                  :enable-snippet? false})]
     (->> (if editing-page-id
            ;; To prevent self references
            (remove (fn [p] (= editing-page-id (:block/uuid p))) pages)
            pages)
-         (map :block/title))))
+         (keep (fn [b]
+                 (when-let [id (:block/uuid b)]
+                   (db/entity [:block/uuid id])))))))
 
 (defn get-matched-blocks
   [q block-id]

+ 16 - 12
src/main/frontend/handler/page.cljs

@@ -42,7 +42,9 @@
             [logseq.graph-parser.db :as gp-db]
             [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.modules.outliner.op :as outliner-op]
-            [frontend.handler.property.util :as pu]))
+            [frontend.handler.property.util :as pu]
+            [datascript.impl.entity :as de]
+            [logseq.common.util.block-ref :as block-ref]))
 
 (def <create! page-common-handler/<create!)
 (def <delete! page-common-handler/<delete!)
@@ -333,10 +335,11 @@
              (common-util/safe-subs edit-content pos current-pos)))
         db-based? (config/db-based-graph? (state/get-current-repo))]
     (if hashtag?
-      (fn [chosen e]
+      (fn [chosen-result e]
         (util/stop e)
         (state/clear-editor-action!)
-        (let [class? (and db-based? hashtag?
+        (let [chosen (:block/title chosen-result)
+              class? (and db-based? hashtag?
                           (or (string/includes? chosen (str (t :new-class) " "))
                               (ldb/class? (db/get-page chosen))))
               chosen (-> chosen
@@ -358,16 +361,14 @@
              (let [tag (string/trim chosen)
                    edit-block (state/get-edit-block)
                    get-page-fn (if class? db/get-case-page db/get-page)]
-               (when (and (not (string/blank? tag)) (:block/uuid edit-block))
-                 (p/let [tag-entity (get-page-fn tag)
-                         _ (prn :debug :class? class?)
-                         _ (when-not tag-entity
+               (when (:block/uuid edit-block)
+                 (p/let [_ (when-not (de/entity? chosen-result) ; page not exists yet
                              (if class?
                                (<create-class! tag {:redirect? false
                                                     :create-first-block? false})
                                (<create! tag {:redirect? false
                                               :create-first-block? false})))
-                         tag-entity (get-page-fn tag)]
+                         tag-entity (or (when (de/entity? chosen-result) chosen-result) (get-page-fn tag))]
                    (when class?
                      (add-tag (state/get-current-repo) (:block/uuid edit-block) tag-entity))))))
            (editor-handler/insert-command! id
@@ -378,13 +379,16 @@
                                             :command :page-ref})
 
            (when input (.focus input)))))
-      (fn [chosen e]
+      (fn [chosen-result e]
         (util/stop e)
         (state/clear-editor-action!)
-        (let [chosen' (string/replace-first chosen (str (t :new-page) " ") "")
-              page-ref-text (get-page-ref-text chosen')]
+        (let [chosen (:block/title chosen-result)
+              chosen' (string/replace-first chosen (str (t :new-page) " ") "")
+              ref-text (if (and (de/entity? chosen-result) (not (ldb/page? chosen-result)))
+                         (block-ref/->block-ref (:block/uuid chosen-result))
+                         (get-page-ref-text chosen'))]
           (editor-handler/insert-command! id
-                                          page-ref-text
+                                          ref-text
                                           format
                                           {:last-pattern (str page-ref/left-brackets (if (editor-handler/get-selected-text) "" q))
                                            :end-pattern page-ref/right-brackets