Explorar el Código

enhance(ux): convert existing page to a tag when tagging a block

Tienson Qin hace 8 meses
padre
commit
e92ea7a771
Se han modificado 2 ficheros con 54 adiciones y 41 borrados
  1. 22 13
      src/main/frontend/components/editor.cljs
  2. 32 28
      src/main/frontend/handler/page.cljs

+ 22 - 13
src/main/frontend/components/editor.cljs

@@ -134,12 +134,15 @@
     (page-handler/on-chosen-handler input id pos format)))
 
 (defn- matched-pages-with-new-page [partial-matched-pages db-tag? q]
-  (if (or (db/page-exists? q (if db-tag?
-                               #{:logseq.class/Tag}
-                               ;; Page existence here should be the same as entity-util/page?.
-                                ;; Don't show 'New page' if a page has any of these tags
-                               db-class/page-classes))
-          (and db-tag? (some ldb/class? (:block/_alias (db/get-page q)))))
+  (if (or
+       (if db-tag?
+         (let [entity (db/get-page q)]
+           (and (ldb/internal-page? entity) (= (:block/title entity) q)))
+         ;; Page existence here should be the same as entity-util/page?.
+         ;; Don't show 'New page' if a page has any of these tags
+         (db/page-exists? q db-class/page-classes))
+
+       (and db-tag? (some ldb/class? (:block/_alias (db/get-page q)))))
     partial-matched-pages
     (if db-tag?
       (concat [{:block/title (str (t :new-tag) " " q)}]
@@ -154,13 +157,19 @@
         [matched-pages set-matched-pages!] (rum/use-state nil)
         search-f (fn []
                    (when-not (string/blank? q)
-                     (p/do!
-                      (db-async/<get-block (state/get-current-repo) q {:children? false})
-                      (p/let [result (if db-tag?
-                                       (editor-handler/get-matched-classes q)
-                                       (editor-handler/<get-matched-blocks q {:nlp-pages? true
-                                                                              :page-only? (not db-based?)}))]
-                        (set-matched-pages! result)))))]
+                     (p/let [block (db-async/<get-block (state/get-current-repo) q {:children? false})
+                             result (if db-tag?
+                                      (let [classes (editor-handler/get-matched-classes q)]
+                                        (if (and (ldb/internal-page? block)
+                                                 (= (:block/title block) q))
+                                          (cons {:block/title (util/format "Convert \"%s\" to tag" q)
+                                                 :db/id (:db/id block)
+                                                 :block/uuid (:block/uuid block)
+                                                 :convert-page-to-tag? true} classes)
+                                          classes))
+                                      (editor-handler/<get-matched-blocks q {:nlp-pages? true
+                                                                             :page-only? (not db-based?)}))]
+                       (set-matched-pages! result))))]
     (hooks/use-effect! search-f [(hooks/use-debounced-value q 150)])
 
     (let [matched-pages' (if (string/blank? q)

+ 32 - 28
src/main/frontend/handler/page.cljs

@@ -213,34 +213,38 @@
   (fn [chosen-result ^js e]
     (util/stop e)
     (state/clear-editor-action!)
-    (let [chosen-result (if (:block/uuid chosen-result)
-                          (db/entity [:block/uuid (:block/uuid chosen-result)])
-                          chosen-result)
-          target (first (:block/_alias chosen-result))
-          chosen-result (if (and target (not (ldb/class? chosen-result)) (ldb/class? target)) target chosen-result)
-          chosen (:block/title chosen-result)
-          class? (and db-based?
-                      (or (string/includes? chosen (str (t :new-tag) " "))
-                          (ldb/class? chosen-result)))
-          inline-tag? (and class? (= (.-identifier e) "auto-complete/meta-complete"))
-          chosen (-> chosen
-                     (string/replace-first (str (t :new-tag) " ") "")
-                     (string/replace-first (str (t :new-page) " ") ""))
-          wrapped? (= page-ref/left-brackets (common-util/safe-subs edit-content (- pos 2) pos))
-          chosen-last-part (if (text/namespace-page? chosen)
-                             (text/get-namespace-last-part chosen)
-                             chosen)
-          wrapped-tag (if (and (util/safe-re-find #"\s+" chosen-last-part) (not wrapped?))
-                        (ref/->page-ref chosen-last-part)
-                        chosen-last-part)
-          q (if (editor-handler/get-selected-text) "" q)
-          last-pattern (if wrapped?
-                         q
-                         (if (= \# (first q))
-                           (subs q 1)
-                           q))
-          last-pattern (str "#" (when wrapped? page-ref/left-brackets) last-pattern)
-          tag-in-page-auto-complete? (= page-ref/right-brackets (common-util/safe-subs edit-content current-pos (+ current-pos 2)))]
+    (p/let [_ (when (:convert-page-to-tag? chosen-result)
+                (let [entity (db/entity (:db/id chosen-result))]
+                  (when (and (ldb/page? entity) (not (ldb/class? entity)))
+                    (db-page-handler/convert-to-tag! entity))))
+            chosen-result (if (:block/uuid chosen-result)
+                            (db/entity [:block/uuid (:block/uuid chosen-result)])
+                            chosen-result)
+            target (first (:block/_alias chosen-result))
+            chosen-result (if (and target (not (ldb/class? chosen-result)) (ldb/class? target)) target chosen-result)
+            chosen (:block/title chosen-result)
+            class? (and db-based?
+                        (or (string/includes? chosen (str (t :new-tag) " "))
+                            (ldb/class? chosen-result)))
+            inline-tag? (and class? (= (.-identifier e) "auto-complete/meta-complete"))
+            chosen (-> chosen
+                       (string/replace-first (str (t :new-tag) " ") "")
+                       (string/replace-first (str (t :new-page) " ") ""))
+            wrapped? (= page-ref/left-brackets (common-util/safe-subs edit-content (- pos 2) pos))
+            chosen-last-part (if (text/namespace-page? chosen)
+                               (text/get-namespace-last-part chosen)
+                               chosen)
+            wrapped-tag (if (and (util/safe-re-find #"\s+" chosen-last-part) (not wrapped?))
+                          (ref/->page-ref chosen-last-part)
+                          chosen-last-part)
+            q (if (editor-handler/get-selected-text) "" q)
+            last-pattern (if wrapped?
+                           q
+                           (if (= \# (first q))
+                             (subs q 1)
+                             q))
+            last-pattern (str "#" (when wrapped? page-ref/left-brackets) last-pattern)
+            tag-in-page-auto-complete? (= page-ref/right-brackets (common-util/safe-subs edit-content current-pos (+ current-pos 2)))]
       (p/do!
        (editor-handler/insert-command! id
                                        (if (and class? (not inline-tag?)) "" (str "#" wrapped-tag))