Procházet zdrojové kódy

fix: typing a normal page as a tag silently changes its page type

User can choose to create either a page or a class with '#' now.
FIXES LOG-2742
Tienson Qin před 2 roky
rodič
revize
67fd609170

+ 2 - 4
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -349,7 +349,7 @@
          form))
      (concat title body))
     (swap! *refs #(remove string/blank? %))
-    (let [ref->map-fn (fn [*col tag?]
+    (let [ref->map-fn (fn [*col _tag?]
                         (let [col (remove string/blank? @*col)
                               children-pages (->> (mapcat (fn [p]
                                                             (let [p (if (map? p)
@@ -369,9 +369,7 @@
                              (let [macro? (and (map? item)
                                                (= "macro" (:type item)))]
                                (when-not macro?
-                                 (cond-> (page-name->map item with-id? db true date-formatter)
-                                   tag?
-                                   (assoc :block/type "class"))))) col)))]
+                                 (page-name->map item with-id? db true date-formatter)))) col)))]
       (assoc block
              :refs (ref->map-fn *refs false)
              :tags (ref->map-fn *structured-tags true)))))

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/test/docs_graph_helper.cljs

@@ -161,7 +161,7 @@
   ;; only increase over time as the docs graph rarely has deletions
   (testing "Counts"
     (is (= 303 (count files)) "Correct file count")
-    (is (= 64392 (count (d/datoms db :eavt))) "Correct datoms count")
+    (is (= 64376 (count (d/datoms db :eavt))) "Correct datoms count")
 
     (is (= 5866
            (ffirst

+ 19 - 37
src/main/frontend/components/editor.cljs

@@ -2,7 +2,6 @@
   (:require [clojure.string :as string]
             [frontend.commands :as commands
              :refer [*first-command-group *matched-block-commands *matched-commands]]
-            [frontend.components.block :as block]
             [frontend.components.datetime :as datetime-comp]
             [frontend.components.svg :as svg]
             [frontend.components.search :as search]
@@ -101,9 +100,6 @@
                                                       :command :block-commands}))
         :class     "black"}))))
 
-(defn- in-sidebar? [el]
-  (not (.contains (.getElementById js/document "left-container") el)))
-
 (defn- page-on-chosen-handler
   [embed? input id q pos format]
   (if embed?
@@ -143,7 +139,6 @@
         (when input
           (let [current-pos (cursor/pos input)
                 edit-content (state/sub-edit-content)
-                sidebar? (in-sidebar? input)
                 q (or
                    (editor-handler/get-selected-text)
                    (when (= action :page-search-hashtag)
@@ -151,11 +146,9 @@
                    (when (> (count edit-content) current-pos)
                      (gp-util/safe-subs edit-content pos current-pos))
                    "")
-                matched-pages (if db-tag?
-                                (editor-handler/get-matched-classes q)
-                                ;; FIXME: display refed pages recentedly or frequencyly used
-                                (when-not (string/blank? q)
-                                  (editor-handler/get-matched-pages q)))
+                ;; FIXME: display refed pages recentedly or frequencyly used
+                matched-pages (when-not (string/blank? q)
+                                (editor-handler/get-matched-pages q))
                 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
@@ -163,16 +156,18 @@
                                            [(count m) m])
                                          matched-pages)
 
-                                (and (string/blank? q) (not db-tag?))
+                                (string/blank? q)
                                 nil
 
-                                (and (string/blank? q) db-tag?)
-                                matched-pages
-
                                 (empty? matched-pages)
-                                (cons q matched-pages)
-
-                               ;; reorder, shortest and starts-with first.
+                                (when-not (db/page-exists? q)
+                                  (if db-tag?
+                                    (concat [(str (t :new-page) " " q)
+                                             (str (t :new-class) " " q)]
+                                            matched-pages)
+                                    (cons (str (t :new-page) " " q) matched-pages)))
+
+                                ;; reorder, shortest and starts-with first.
                                 :else
                                 (let [matched-pages (remove nil? matched-pages)
                                       matched-pages (sort-by
@@ -196,27 +191,14 @@
              (ui/auto-complete
               matched-pages
               {:on-chosen   (page-on-chosen-handler embed? input id q pos format)
-               :on-enter    #(page-handler/page-not-exists-handler input id q current-pos)
-               :item-render (fn [page-name chosen?]
-                              [:div.preview-trigger-wrapper
-                               (block/page-preview-trigger
-                                {:children
-                                 [:div.flex
-                                  (when (db-model/whiteboard-page? page-name) [:span.mr-1 (ui/icon "whiteboard" {:extension? true})])
-                                  [:div.flex.space-x-1
-                                   [:div (when-not (db/page-exists? page-name)
-                                           (if db-tag?
-                                             (t :new-class)
-                                             (t :new-page)))]
-                                   (search-handler/highlight-exact-query page-name q)]]
-                                 :open?           chosen?
-                                 :manual?         true
-                                 :fixed-position? true
-                                 :tippy-distance  24
-                                 :tippy-position  (if sidebar? "left" "right")}
-                                page-name)])
+               :on-enter    (fn []
+                              (page-handler/page-not-exists-handler input id q current-pos))
+               :item-render (fn [page-name _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)])
                :empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 (if db-tag?
-                                                                          "Search for a class"
+                                                                          "Search for a page or a class"
                                                                           "Search for a page")]
                :class       "black"})]))))))
 

+ 1 - 3
src/main/frontend/db/react.cljs

@@ -12,9 +12,7 @@
             [frontend.util :as util :refer [react]]
             [cljs.spec.alpha :as s]
             [clojure.core.async :as async]
-            [clojure.set :as set]
-            [logseq.db.sqlite.util :as sqlite-util]
-            [clojure.string :as string]))
+            [clojure.set :as set]))
 
 ;;; keywords specs for reactive query, used by `react/q` calls
 ;; ::block

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

@@ -1635,12 +1635,13 @@
       (remove (fn [p] (= (util/page-name-sanity-lc p) editing-page)) pages)
       pages)))
 
-(defn get-matched-classes
-  "Return matched class names"
-  [q]
-  (let [classes (->> (db-model/get-all-classes (state/get-current-repo))
-                     (map first))]
-    (search/fuzzy-search classes q {:limit 100})))
+(comment
+  (defn get-matched-classes
+   "Return matched class names"
+   [q]
+   (let [classes (->> (db-model/get-all-classes (state/get-current-repo))
+                      (map first))]
+     (search/fuzzy-search classes q {:limit 100}))))
 
 (defn get-matched-blocks
   [q block-id]

+ 14 - 6
src/main/frontend/handler/page.cljs

@@ -35,7 +35,8 @@
             [promesa.core :as p]
             [logseq.common.path :as path]
             [frontend.handler.property.util :as pu]
-            [electron.ipc :as ipc]))
+            [electron.ipc :as ipc]
+            [frontend.context.i18n :refer [t]]))
 
 (def create! page-common-handler/create!)
 (def delete! page-common-handler/delete!)
@@ -209,7 +210,11 @@
       (fn [chosen e]
         (util/stop e)
         (state/clear-editor-action!)
-        (let [wrapped? (= page-ref/left-brackets (gp-util/safe-subs edit-content (- pos 2) pos))
+        (let [class? (string/starts-with? chosen (t :new-class))
+              chosen (-> chosen
+                         (string/replace-first (str (t :new-class) " ") "")
+                         (string/replace-first (str (t :new-page) " ") ""))
+              wrapped? (= page-ref/left-brackets (gp-util/safe-subs edit-content (- pos 2) pos))
               wrapped-tag (if (and (util/safe-re-find #"\s+" chosen) (not wrapped?))
                             (page-ref/->page-ref chosen)
                             chosen)
@@ -228,10 +233,13 @@
                   (when-not tag-entity
                     (create! tag {:redirect? false
                                   :create-first-block? false
-                                  :class? true}))
-                  (let [tag-entity (or tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)]))]
-                    (db/transact! [[:db/add [:block/uuid (:block/uuid edit-block)] :block/tags (:db/id tag-entity)]
-                                   [:db/add [:block/uuid (:block/uuid edit-block)] :block/refs (:db/id tag-entity)]]))))))
+                                  :class? class?}))
+                  (when class?
+                    (let [repo (state/get-current-repo)
+                          tag-entity (or tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)]))
+                          tx-data [[:db/add [:block/uuid (:block/uuid edit-block)] :block/tags (:db/id tag-entity)]
+                                   [:db/add [:block/uuid (:block/uuid edit-block)] :block/refs (:db/id tag-entity)]]]
+                      (db/transact! repo tx-data {:outliner-op :save-block})))))))
 
           (editor-handler/insert-command! id
                                           (str "#" wrapped-tag)

+ 7 - 4
src/main/frontend/modules/outliner/core.cljs

@@ -150,8 +150,8 @@
                                         (:block/macros block-entity)))))))
 
 (defn- create-linked-page-when-save
-  [txs-state block-entity m structured-tags?]
-  (if structured-tags?
+  [txs-state block-entity m tags-has-class?]
+  (if tags-has-class?
     (let [content (state/get-edit-content)
           linked-page (some-> content mldoc/extract-plain)
           sanity-linked-page (some-> linked-page util/page-name-sanity-lc)
@@ -304,7 +304,10 @@
           eid (or (:db/id (:data this))
                   (when-let [block-uuid (:block/uuid (:data this))] [:block/uuid block-uuid]))
           block-entity (db/entity eid)
-          structured-tags? (and db-based? (seq (:block/tags m)))]
+          tags-has-class? (and db-based?
+                               (some (fn [tag]
+                                       (contains? (:block/type (db/entity [:block/uuid (:block/uuid tag)])) "class"))
+                                     (:block/tags m)))]
       (when eid
         ;; Retract attributes to prepare for tx which rewrites block attributes
         (let [retract-attributes (when db-based?
@@ -332,7 +335,7 @@
         (swap! txs-state conj
                (dissoc m :db/other-tx)))
 
-      (create-linked-page-when-save txs-state block-entity m structured-tags?)
+      (create-linked-page-when-save txs-state block-entity m tags-has-class?)
 
       (rebuild-refs repo txs-state block-entity m)
 

+ 1 - 1
src/test/frontend/handler/repo_conversion_test.cljs

@@ -98,7 +98,7 @@
   ;; only increase over time as the docs graph rarely has deletions
   (testing "Counts"
     (is (= 211 (count files)) "Correct file count")
-    (is (= 39293 (count (d/datoms db :eavt))) "Correct datoms count")
+    (is (= 39262 (count (d/datoms db :eavt))) "Correct datoms count")
 
     (is (= 3600
            (ffirst