Browse Source

fix: batch copy page titles

Tienson Qin 11 months ago
parent
commit
68e4f641d8

+ 2 - 1
src/main/frontend/components/selection.cljs

@@ -11,7 +11,8 @@
   [& {:keys [on-cut on-copy selected-blocks hide-dots? button-border?]
   [& {:keys [on-cut on-copy selected-blocks hide-dots? button-border?]
       :or {on-cut #(editor-handler/cut-selection-blocks true)}}]
       :or {on-cut #(editor-handler/cut-selection-blocks true)}}]
   (let [on-copy (if (and selected-blocks (nil? on-copy))
   (let [on-copy (if (and selected-blocks (nil? on-copy))
-                  #(editor-handler/copy-selection-blocks true {:selected-blocks selected-blocks})
+                  #(editor-handler/copy-selection-blocks true {:selected-blocks selected-blocks
+                                                               :page-title-only? true})
                   (or on-copy #(editor-handler/copy-selection-blocks true)))
                   (or on-copy #(editor-handler/copy-selection-blocks true)))
         button-opts {:variant :outline
         button-opts {:variant :outline
                      :size :sm
                      :size :sm

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

@@ -924,14 +924,16 @@
   (block-handler/select-block! block-uuid))
   (block-handler/select-block! block-uuid))
 
 
 (defn- compose-copied-blocks-contents
 (defn- compose-copied-blocks-contents
-  [repo block-ids]
+  [repo block-ids & {:as opts}]
   (let [blocks (db-utils/pull-many repo '[*] (mapv (fn [id] [:block/uuid id]) block-ids))
   (let [blocks (db-utils/pull-many repo '[*] (mapv (fn [id] [:block/uuid id]) block-ids))
         top-level-block-uuids (->> (block-handler/get-top-level-blocks blocks)
         top-level-block-uuids (->> (block-handler/get-top-level-blocks blocks)
                                    (map :block/uuid))
                                    (map :block/uuid))
         content (export-text/export-blocks-as-markdown
         content (export-text/export-blocks-as-markdown
                  repo top-level-block-uuids
                  repo top-level-block-uuids
-                 {:indent-style (state/get-export-block-text-indent-style)
-                  :remove-options (set (state/get-export-block-text-remove-options))})]
+                 (merge
+                  opts
+                  {:indent-style (state/get-export-block-text-indent-style)
+                   :remove-options (set (state/get-export-block-text-remove-options))}))]
     [top-level-block-uuids content]))
     [top-level-block-uuids content]))
 
 
 (defn- get-all-blocks-by-ids
 (defn- get-all-blocks-by-ids
@@ -947,14 +949,14 @@
       result)))
       result)))
 
 
 (defn copy-selection-blocks
 (defn copy-selection-blocks
-  [html? & {:keys [selected-blocks]}]
+  [html? & {:keys [selected-blocks] :as opts}]
   (let [repo (state/get-current-repo)
   (let [repo (state/get-current-repo)
         blocks (seq (state/get-selection-blocks))
         blocks (seq (state/get-selection-blocks))
         ids (if blocks
         ids (if blocks
               (distinct (keep #(when-let [id (dom/attr % "blockid")]
               (distinct (keep #(when-let [id (dom/attr % "blockid")]
                                  (uuid id)) blocks))
                                  (uuid id)) blocks))
               (map :block/uuid selected-blocks))
               (map :block/uuid selected-blocks))
-        [top-level-block-uuids content] (compose-copied-blocks-contents repo ids)
+        [top-level-block-uuids content] (compose-copied-blocks-contents repo ids opts)
         block (db/entity [:block/uuid (first ids)])
         block (db/entity [:block/uuid (first ids)])
         db-based? (config/db-based-graph? repo)]
         db-based? (config/db-based-graph? repo)]
     (when block
     (when block

+ 11 - 5
src/main/frontend/handler/export/common.cljs

@@ -5,6 +5,7 @@
   (:refer-clojure :exclude [map filter mapcat concat remove])
   (:refer-clojure :exclude [map filter mapcat concat remove])
   (:require [cljs.core.match :refer [match]]
   (:require [cljs.core.match :refer [match]]
             [clojure.string :as string]
             [clojure.string :as string]
+            [frontend.common.file.core :as common-file]
             [frontend.db :as db]
             [frontend.db :as db]
             [frontend.format.mldoc :as mldoc]
             [frontend.format.mldoc :as mldoc]
             [frontend.modules.file.core :as outliner-file]
             [frontend.modules.file.core :as outliner-file]
@@ -12,11 +13,10 @@
             [frontend.persist-db.browser :as db-browser]
             [frontend.persist-db.browser :as db-browser]
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.util :as util :refer [concatv mapcatv removev]]
             [frontend.util :as util :refer [concatv mapcatv removev]]
-            [frontend.common.file.core :as common-file]
+            [logseq.db :as ldb]
             [malli.core :as m]
             [malli.core :as m]
             [malli.util :as mu]
             [malli.util :as mu]
-            [promesa.core :as p]
-            [logseq.db :as ldb]))
+            [promesa.core :as p]))
 
 
 ;;; TODO: split frontend.handler.export.text related states
 ;;; TODO: split frontend.handler.export.text related states
 (def ^:dynamic *state*
 (def ^:dynamic *state*
@@ -64,8 +64,14 @@
       (outliner-file/tree->file-content {:init-level init-level})))
       (outliner-file/tree->file-content {:init-level init-level})))
 
 
 (defn root-block-uuids->content
 (defn root-block-uuids->content
-  [repo root-block-uuids]
-  (let [contents (mapv #(get-blocks-contents repo %) root-block-uuids)]
+  [repo root-block-uuids & {:keys [page-title-only?]}]
+  (let [contents (mapv (fn [id]
+                         (if-let [page (and page-title-only?
+                                            (let [e (db/entity [:block/uuid id])]
+                                              (when (:block/name e)
+                                                e)))]
+                           (:block/title page)
+                           (get-blocks-contents repo id))) root-block-uuids)]
     (string/join "\n" (mapv string/trim-newline contents))))
     (string/join "\n" (mapv string/trim-newline contents))))
 
 
 (declare remove-block-ast-pos Properties-block-ast?)
 (declare remove-block-ast-pos Properties-block-ast?)

+ 3 - 2
src/main/frontend/handler/export/text.cljs

@@ -506,7 +506,8 @@
   "options:
   "options:
   :indent-style \"dashes\" | \"spaces\" | \"no-indent\"
   :indent-style \"dashes\" | \"spaces\" | \"no-indent\"
   :remove-options [:emphasis :page-ref :tag :property]
   :remove-options [:emphasis :page-ref :tag :property]
-  :other-options {:keep-only-level<=N int :newline-after-block bool}"
+  :other-options {:keep-only-level<=N int :newline-after-block bool}
+  :page-title-only? boolean"
   [repo root-block-uuids-or-page-uuid options]
   [repo root-block-uuids-or-page-uuid options]
   {:pre [(or (coll? root-block-uuids-or-page-uuid)
   {:pre [(or (coll? root-block-uuids-or-page-uuid)
              (uuid? root-block-uuids-or-page-uuid))]}
              (uuid? root-block-uuids-or-page-uuid))]}
@@ -517,7 +518,7 @@
            (if (uuid? root-block-uuids-or-page-uuid)
            (if (uuid? root-block-uuids-or-page-uuid)
              ;; page
              ;; page
              (common/get-page-content root-block-uuids-or-page-uuid)
              (common/get-page-content root-block-uuids-or-page-uuid)
-             (common/root-block-uuids->content repo root-block-uuids-or-page-uuid))
+             (common/root-block-uuids->content repo root-block-uuids-or-page-uuid options))
            first-block (and (coll? root-block-uuids-or-page-uuid)
            first-block (and (coll? root-block-uuids-or-page-uuid)
                             (db/entity [:block/uuid (first root-block-uuids-or-page-uuid)]))
                             (db/entity [:block/uuid (first root-block-uuids-or-page-uuid)]))
            format (get first-block :block/format :markdown)]
            format (get first-block :block/format :markdown)]