Browse Source

feat: /template command

Tienson Qin 7 months ago
parent
commit
1aeef85e4a

+ 1 - 1
deps/db/src/logseq/db/frontend/schema.cljs

@@ -37,7 +37,7 @@
          (map (juxt :major :minor)
          (map (juxt :major :minor)
               [(parse-schema-version x) (parse-schema-version y)])))
               [(parse-schema-version x) (parse-schema-version y)])))
 
 
-(def version (parse-schema-version "64.2"))
+(def version (parse-schema-version "64.3"))
 
 
 (defn major-version
 (defn major-version
   "Return a number.
   "Return a number.

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

@@ -561,7 +561,7 @@
 (defn- macro->block
 (defn- macro->block
   "macro: {:name \"\" arguments [\"\"]}"
   "macro: {:name \"\" arguments [\"\"]}"
   [macro]
   [macro]
-  {:block/uuid (random-uuid)
+  {:block/uuid (common-uuid/gen-uuid)
    :block/type "macro"
    :block/type "macro"
    :block/properties {:logseq.macro-name (:name macro)
    :block/properties {:logseq.macro-name (:name macro)
                       :logseq.macro-arguments (:arguments macro)}})
                       :logseq.macro-arguments (:arguments macro)}})

+ 2 - 1
deps/graph-parser/src/logseq/graph_parser/db.cljs

@@ -4,6 +4,7 @@
             [clojure.string :as string]
             [clojure.string :as string]
             [datascript.core :as d]
             [datascript.core :as d]
             [logseq.common.util :as common-util]
             [logseq.common.util :as common-util]
+            [logseq.common.uuid :as common-uuid]
             [logseq.db :as ldb]
             [logseq.db :as ldb]
             [logseq.db.file-based.schema :as file-schema]))
             [logseq.db.file-based.schema :as file-schema]))
 
 
@@ -23,7 +24,7 @@
   [title]
   [title]
   {:block/name (string/lower-case title)
   {:block/name (string/lower-case title)
    :block/title title
    :block/title title
-   :block/uuid (random-uuid)
+   :block/uuid (common-uuid/gen-uuid)
    :block/type "page"})
    :block/type "page"})
 
 
 (def built-in-pages
 (def built-in-pages

+ 2 - 1
deps/outliner/src/logseq/outliner/core.cljs

@@ -5,6 +5,7 @@
             [datascript.core :as d]
             [datascript.core :as d]
             [datascript.impl.entity :as de :refer [Entity]]
             [datascript.impl.entity :as de :refer [Entity]]
             [logseq.common.util :as common-util]
             [logseq.common.util :as common-util]
+            [logseq.common.uuid :as common-uuid]
             [logseq.db :as ldb]
             [logseq.db :as ldb]
             [logseq.db.common.order :as db-order]
             [logseq.db.common.order :as db-order]
             [logseq.db.file-based.schema :as file-schema]
             [logseq.db.file-based.schema :as file-schema]
@@ -503,7 +504,7 @@
         uuids (zipmap block-uuids
         uuids (zipmap block-uuids
                       (if keep-uuid?
                       (if keep-uuid?
                         block-uuids
                         block-uuids
-                        (repeatedly random-uuid)))
+                        (repeatedly common-uuid/gen-uuid)))
         uuids (if (and (not keep-uuid?) replace-empty-target?)
         uuids (if (and (not keep-uuid?) replace-empty-target?)
                 (assoc uuids (:block/uuid (first blocks)) (:block/uuid target-block))
                 (assoc uuids (:block/uuid (first blocks)) (:block/uuid target-block))
                 uuids)
                 uuids)

+ 3 - 4
src/main/frontend/commands.cljs

@@ -435,10 +435,9 @@
           "Upload file types like image, pdf, docx, etc.)"
           "Upload file types like image, pdf, docx, etc.)"
           :icon/upload])
           :icon/upload])
 
 
-       (when-not db?
-         ["Template" [[:editor/input command-trigger nil]
-                      [:editor/search-template]] "Insert a created template here"
-          :icon/template])
+       ["Template" [[:editor/input command-trigger nil]
+                    [:editor/search-template]] "Insert a created template here"
+        :icon/template]
 
 
        ["Embed HTML " (->inline "html") "" :icon/htmlEmbed]
        ["Embed HTML " (->inline "html") "" :icon/htmlEmbed]
 
 

+ 4 - 3
src/main/frontend/components/editor.cljs

@@ -352,7 +352,8 @@
 
 
 (rum/defc template-search-aux
 (rum/defc template-search-aux
   [id q]
   [id q]
-  (let [[matched-templates set-matched-templates!] (rum/use-state nil)]
+  (let [db-based? (config/db-based-graph?)
+        [matched-templates set-matched-templates!] (rum/use-state nil)]
     (hooks/use-effect! (fn []
     (hooks/use-effect! (fn []
                          (p/let [result (editor-handler/<get-matched-templates q)]
                          (p/let [result (editor-handler/<get-matched-templates q)]
                            (set-matched-templates! result)))
                            (set-matched-templates! result)))
@@ -362,8 +363,8 @@
      {:on-chosen   (editor-handler/template-on-chosen-handler id)
      {:on-chosen   (editor-handler/template-on-chosen-handler id)
       :on-enter    (fn [_state] (state/clear-editor-action!))
       :on-enter    (fn [_state] (state/clear-editor-action!))
       :empty-placeholder [:div.text-gray-500.px-4.py-2.text-sm "Search for a template"]
       :empty-placeholder [:div.text-gray-500.px-4.py-2.text-sm "Search for a template"]
-      :item-render (fn [[template _block-db-id]]
-                     template)
+      :item-render (fn [template]
+                     (if db-based? (:block/title template) (:template template)))
       :class       "black"})))
       :class       "black"})))
 
 
 (rum/defc template-search < rum/reactive
 (rum/defc template-search < rum/reactive

+ 61 - 60
src/main/frontend/handler/editor.cljs

@@ -2217,73 +2217,74 @@
   ([element-id db-id {:keys [target] :as opts}]
   ([element-id db-id {:keys [target] :as opts}]
    (let [repo (state/get-current-repo)
    (let [repo (state/get-current-repo)
          db? (config/db-based-graph? repo)]
          db? (config/db-based-graph? repo)]
-     (when-not db?
-       (p/let [block (if (integer? db-id)
-                       (db-async/<pull repo db-id)
-                       (db-async/<get-template-by-name (name db-id)))
-               block (when (:block/uuid block)
-                       (db-async/<get-block repo (:block/uuid block)
-                                            {:children? true
-                                             :nested-children? true}))]
-         (when (:db/id block)
-           (let [journal? (ldb/journal? target)
-                 target (or target (state/get-edit-block))
-                 format (get block :block/format :markdown)
-                 block-uuid (:block/uuid block)
-                 template-including-parent? (not (false? (:template-including-parent (:block/properties block))))
-                 blocks (db/get-block-and-children repo block-uuid)
-                 sorted-blocks (cons
+     (p/let [block (if (integer? db-id)
+                     (db-async/<pull repo db-id)
+                     (db-async/<get-template-by-name (name db-id)))
+             block (when (:block/uuid block)
+                     (db-async/<get-block repo (:block/uuid block)
+                                          {:children? true
+                                           :nested-children? true}))]
+       (when (:db/id block)
+         (let [journal? (ldb/journal? target)
+               target (or target (state/get-edit-block))
+               format (get block :block/format :markdown)
+               block-uuid (:block/uuid block)
+               template-including-parent? (not (false? (:template-including-parent (:block/properties block))))
+               blocks (db/get-block-and-children repo block-uuid)
+               sorted-blocks (if db?
+                               blocks
+                               (cons
                                 (-> (first blocks)
                                 (-> (first blocks)
                                     (update :block/properties-text-values dissoc :template)
                                     (update :block/properties-text-values dissoc :template)
                                     (update :block/properties-order (fn [keys]
                                     (update :block/properties-order (fn [keys]
                                                                       (vec (remove #{:template} keys)))))
                                                                       (vec (remove #{:template} keys)))))
-                                (rest blocks))
-                 blocks (if template-including-parent?
-                          sorted-blocks
-                          (drop 1 sorted-blocks))]
-             (when element-id
-               (insert-command! element-id "" format {:end-pattern commands/command-trigger}))
-             (let [exclude-properties [:id :template :template-including-parent]
-                   content-update-fn (fn [content]
-                                       (->> content
-                                            (property-file/remove-property-when-file-based repo format "template")
-                                            (property-file/remove-property-when-file-based repo format "template-including-parent")
-                                            template/resolve-dynamic-template!))
-                   page (if (:block/name block) block
-                            (when target (:block/page (db/entity (:db/id target)))))
-                   blocks' (map (fn [block]
-                                  (paste-block-cleanup repo block page exclude-properties format content-update-fn false))
-                                blocks)
-                   sibling? (:sibling? opts)
-                   sibling?' (cond
-                               (some? sibling?)
-                               sibling?
-
-                               (db/has-children? (:block/uuid target))
-                               false
-
-                               :else
-                               true)]
-               (when (seq blocks')
-                 (try
-                   (p/let [result (ui-outliner-tx/transact!
-                                   {:outliner-op :insert-blocks
-                                    :created-from-journal-template? journal?}
-                                   (when-not (string/blank? (state/get-edit-content))
-                                     (save-current-block!))
-                                   (outliner-op/insert-blocks! blocks' target
-                                                               (assoc opts :sibling? sibling?')))]
-                     (when result (edit-last-block-after-inserted! (ldb/read-transit-str result))))
-
-                   (catch :default ^js/Error e
-                     (notification/show!
-                      [:p.content
-                       (util/format "Template insert error: %s" (.-message e))]
-                      :error))))))))))))
+                                (rest blocks)))
+               blocks (if (and (not db?) template-including-parent?)
+                        sorted-blocks
+                        (drop 1 sorted-blocks))]
+           (when element-id
+             (insert-command! element-id "" format {:end-pattern commands/command-trigger}))
+           (let [exclude-properties [:id :template :template-including-parent]
+                 content-update-fn (fn [content]
+                                     (->> content
+                                          (property-file/remove-property-when-file-based repo format "template")
+                                          (property-file/remove-property-when-file-based repo format "template-including-parent")
+                                          template/resolve-dynamic-template!))
+                 page (if (:block/name block) block
+                          (when target (:block/page (db/entity (:db/id target)))))
+                 blocks' (map (fn [block]
+                                (paste-block-cleanup repo block page exclude-properties format content-update-fn false))
+                              blocks)
+                 sibling? (:sibling? opts)
+                 sibling?' (cond
+                             (some? sibling?)
+                             sibling?
+
+                             (db/has-children? (:block/uuid target))
+                             false
+
+                             :else
+                             true)]
+             (when (seq blocks')
+               (try
+                 (p/let [result (ui-outliner-tx/transact!
+                                 {:outliner-op :insert-blocks
+                                  :created-from-journal-template? journal?}
+                                 (when-not (string/blank? (state/get-edit-content))
+                                   (save-current-block!))
+                                 (outliner-op/insert-blocks! (map #(assoc (into {} %) :db/id (:db/id %)) blocks') target
+                                                             (assoc opts :sibling? sibling?')))]
+                   (when result (edit-last-block-after-inserted! (ldb/read-transit-str result))))
+
+                 (catch :default ^js/Error e
+                   (notification/show!
+                    [:p.content
+                     (util/format "Template insert error: %s" (.-message e))]
+                    :error)))))))))))
 
 
 (defn template-on-chosen-handler
 (defn template-on-chosen-handler
   [element-id]
   [element-id]
-  (fn [[_template template-block] _click?]
+  (fn [template-block]
     (when-let [db-id (:db/id template-block)]
     (when-let [db-id (:db/id template-block)]
       (insert-template! element-id db-id
       (insert-template! element-id db-id
                         {:replace-empty-target? true}))))
                         {:replace-empty-target? true}))))

+ 34 - 28
src/main/frontend/search.cljs

@@ -2,19 +2,20 @@
   "Provides search functionality for a number of features including Cmd-K
   "Provides search functionality for a number of features including Cmd-K
   search. Most of these fns depend on the search protocol"
   search. Most of these fns depend on the search protocol"
   (:require [clojure.string :as string]
   (:require [clojure.string :as string]
+            [datascript.core :as d]
+            [frontend.common.search-fuzzy :as fuzzy]
+            [frontend.config :as config]
+            [frontend.db :as db]
+            [frontend.db.async :as db-async]
+            [frontend.db.model :as db-model]
+            [frontend.db.utils :as db-utils]
             [frontend.search.agency :as search-agency]
             [frontend.search.agency :as search-agency]
             [frontend.search.protocol :as protocol]
             [frontend.search.protocol :as protocol]
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util :as util]
-            [promesa.core :as p]
-            [frontend.common.search-fuzzy :as fuzzy]
             [logseq.common.config :as common-config]
             [logseq.common.config :as common-config]
-            [frontend.db.async :as db-async]
-            [frontend.db :as db]
-            [frontend.db.model :as db-model]
-            [frontend.db.utils :as db-utils]
             [logseq.db :as ldb]
             [logseq.db :as ldb]
-            [datascript.core :as d]))
+            [promesa.core :as p]))
 
 
 (def fuzzy-search fuzzy/fuzzy-search)
 (def fuzzy-search fuzzy/fuzzy-search)
 
 
@@ -35,15 +36,15 @@
   ([q limit]
   ([q limit]
    (when-let [repo (state/get-current-repo)]
    (when-let [repo (state/get-current-repo)]
      (let [q (fuzzy/clean-str q)]
      (let [q (fuzzy/clean-str q)]
-      (when-not (string/blank? q)
-        (p/let [mldoc-exts (set (map name common-config/mldoc-support-formats))
-                result (db-async/<get-files repo)
-                files (->> result
-                           (map first)
-                           (remove (fn [file]
-                                     (mldoc-exts (util/get-file-ext file)))))]
-          (when (seq files)
-            (fuzzy/fuzzy-search files q :limit limit))))))))
+       (when-not (string/blank? q)
+         (p/let [mldoc-exts (set (map name common-config/mldoc-support-formats))
+                 result (db-async/<get-files repo)
+                 files (->> result
+                            (map first)
+                            (remove (fn [file]
+                                      (mldoc-exts (util/get-file-ext file)))))]
+           (when (seq files)
+             (fuzzy/fuzzy-search files q :limit limit))))))))
 
 
 (defn template-search
 (defn template-search
   ([q]
   ([q]
@@ -51,11 +52,16 @@
   ([q limit]
   ([q limit]
    (when-let [repo (state/get-current-repo)]
    (when-let [repo (state/get-current-repo)]
      (when q
      (when q
-       (p/let [q (fuzzy/clean-str q)
-               templates (db-async/<get-all-templates repo)]
-         (when (seq templates)
-           (let [result (fuzzy/fuzzy-search (keys templates) q {:limit limit})]
-             (vec (select-keys templates result)))))))))
+       (let [db-based? (config/db-based-graph?)]
+         (p/let [q (fuzzy/clean-str q)
+                 templates (if db-based?
+                             (db-async/<get-tag-objects repo (:db/id (db/entity :logseq.class/Template)))
+                             (p/let [result (db-async/<get-all-templates repo)]
+                               (vals result)))]
+           (when (seq templates)
+             (let [extract-fn (if db-based? :block/title :template)]
+               (fuzzy/fuzzy-search templates q {:limit limit
+                                                :extract-fn extract-fn})))))))))
 
 
 (defn property-search
 (defn property-search
   ([q]
   ([q]
@@ -78,13 +84,13 @@
   ([property q limit]
   ([property q limit]
    (when-let [repo (state/get-current-repo)]
    (when-let [repo (state/get-current-repo)]
      (when q
      (when q
-      (p/let [q (fuzzy/clean-str q)
-              result (db-async/<file-get-property-values repo (keyword property))]
-        (when (seq result)
-          (if (string/blank? q)
-            result
-            (let [result (fuzzy/fuzzy-search result q :limit limit)]
-              (vec result)))))))))
+       (p/let [q (fuzzy/clean-str q)
+               result (db-async/<file-get-property-values repo (keyword property))]
+         (when (seq result)
+           (if (string/blank? q)
+             result
+             (let [result (fuzzy/fuzzy-search result q :limit limit)]
+               (vec result)))))))))
 
 
 (defn rebuild-indices!
 (defn rebuild-indices!
   ([]
   ([]