Browse Source

chore: mv file-handler dependent namespaces

that are now obviously file graph specific

- mv save-tree! as it calls file-handler/alter-files via events
- mv roam import which called file-handler/alter-files
- mv git component which is only enabled for file graphs
- fixed handler.code which allowed db graphs to call file graph code for
  an edge case
Gabriel Horner 8 months ago
parent
commit
885c06f1fb

+ 0 - 89
src/main/frontend/common/file/core.cljs

@@ -2,34 +2,13 @@
   "Save file to disk. Used by both file and DB graphs and shared
    by worker and frontend namespaces"
   (:require [clojure.string :as string]
-            [frontend.common.file.util :as wfu]
             [logseq.graph-parser.property :as gp-property]
-            [logseq.common.path :as path]
             [datascript.core :as d]
             [logseq.db :as ldb]
-            [logseq.common.date :as common-date]
             [logseq.db.frontend.content :as db-content]
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.outliner.tree :as otree]))
 
-(defonce *writes (atom {}))
-(defonce *request-id (atom 0))
-
-(defn conj-page-write!
-  [page-id]
-  (let [request-id (swap! *request-id inc)]
-    (swap! *writes assoc request-id page-id)
-    request-id))
-
-(defn dissoc-request!
-  [request-id]
-  (when-let [page-id (get @*writes request-id)]
-    (let [old-page-request-ids (keep (fn [[r p]]
-                                       (when (and (= p page-id) (<= r request-id))
-                                         r)) @*writes)]
-      (when (seq old-page-request-ids)
-        (swap! *writes (fn [x] (apply dissoc x old-page-request-ids)))))))
-
 (defn- indented-block-content
   [content spaces-tabs]
   (let [lines (string/split-lines content)]
@@ -128,74 +107,6 @@
   [repo db tree opts context]
   (->> (tree->file-content-aux repo db tree opts context) (string/join "\n")))
 
-(defn- transact-file-tx-if-not-exists!
-  [conn page-block ok-handler context]
-  (when (:block/name page-block)
-    (let [format (name (get page-block :block/format (:preferred-format context)))
-          date-formatter (:date-formatter context)
-          title (string/capitalize (:block/name page-block))
-          whiteboard-page? (ldb/whiteboard? page-block)
-          format (if whiteboard-page? "edn" format)
-          journal-page? (common-date/valid-journal-title? title date-formatter)
-          journal-title (common-date/normalize-journal-title title date-formatter)
-          journal-page? (and journal-page? (not (string/blank? journal-title)))
-          filename (if journal-page?
-                     (common-date/date->file-name journal-title (:journal-file-name-format context))
-                     (-> (or (:block/title page-block) (:block/name page-block))
-                         wfu/file-name-sanity))
-          sub-dir (cond
-                    journal-page?    (:journals-directory context)
-                    whiteboard-page? (:whiteboards-directory context)
-                    :else            (:pages-directory context))
-          ext (if (= format "markdown") "md" format)
-          file-rpath (path/path-join sub-dir (str filename "." ext))
-          file {:file/path file-rpath}
-          tx [{:file/path file-rpath}
-              {:block/name (:block/name page-block)
-               :block/file file}]]
-      (ldb/transact! conn tx)
-      (when ok-handler (ok-handler)))))
-
-(defn- remove-transit-ids [block] (dissoc block :db/id :block/file))
-
-(defn- save-tree-aux!
-  [repo db page-block tree blocks-just-deleted? context request-id]
-  (let [page-block (d/pull db '[*] (:db/id page-block))
-        init-level 1
-        file-db-id (-> page-block :block/file :db/id)
-        file-path (-> (d/entity db file-db-id) :file/path)
-        result (if (and (string? file-path) (not-empty file-path))
-                 (let [new-content (if (ldb/whiteboard? page-block)
-                                     (->
-                                      (wfu/ugly-pr-str {:blocks tree
-                                                        :pages (list (remove-transit-ids page-block))})
-                                      (string/triml))
-                                     (tree->file-content repo db tree {:init-level init-level} context))]
-                   (when-not (and (string/blank? new-content) (not blocks-just-deleted?))
-                     (let [files [[file-path new-content]]]
-                       (when (seq files)
-                         (let [page-id (:db/id page-block)]
-                           (wfu/post-message :write-files {:request-id request-id
-                                                           :page-id page-id
-                                                           :repo repo
-                                                           :files files})
-                           :sent)))))
-                 ;; In e2e tests, "card" page in db has no :file/path
-                 (js/console.error "File path from page-block is not valid" page-block tree))]
-    (when-not (= :sent result)          ; page may not exists now
-      (dissoc-request! request-id))))
-
-(defn save-tree!
-  [repo conn page-block tree blocks-just-deleted? context request-id]
-  {:pre [(map? page-block)]}
-  (when repo
-    (let [ok-handler #(save-tree-aux! repo @conn page-block tree blocks-just-deleted? context request-id)
-          file (or (:block/file page-block)
-                   (when-let [page-id (:db/id (:block/page page-block))]
-                     (:block/file (d/entity @conn page-id))))]
-      (if file
-        (ok-handler)
-        (transact-file-tx-if-not-exists! conn page-block ok-handler context)))))
 
 (defn block->content
   "Converts a block including its children (recursively) to plain-text."

+ 1 - 1
src/main/frontend/components/git.cljs → src/main/frontend/components/file_based/git.cljs

@@ -1,4 +1,4 @@
-(ns frontend.components.git
+(ns frontend.components.file-based.git
   (:require [clojure.string :as string]
             [frontend.handler.file-based.file :as file-handler]
             [frontend.handler.shell :as shell]

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

@@ -10,6 +10,7 @@
             [frontend.context.i18n :refer [t]]
             [frontend.db :as db]
             [frontend.fs :as fs]
+            [frontend.handler.file-based.import :as file-import-handler]
             [frontend.handler.db-based.editor :as db-editor-handler]
             [frontend.handler.import :as import-handler]
             [frontend.handler.notification :as notification]
@@ -59,7 +60,7 @@
           (set! (.-onload reader)
                 (fn [e]
                   (let [text (.. e -target -result)]
-                    (import-handler/import-from-roam-json!
+                    (file-import-handler/import-from-roam-json!
                      text
                      #(do
                         (state/set-state! :graph/importing nil)

+ 2 - 1
src/main/frontend/handler/code.cljs

@@ -49,7 +49,8 @@
                  (config/db-based-graph? repo))
             (db-editor-handler/save-file! (:file-path config) value)
 
-            (not-empty (:file-path config))
+            (and (not-empty (:file-path config))
+                 (not (config/db-based-graph? repo)))
             (let [path (:file-path config)
                   repo-dir (config/get-repo-dir repo)
                   rpath (when (string/starts-with? path repo-dir)

+ 1 - 1
src/main/frontend/handler/events.cljs

@@ -15,7 +15,7 @@
             [frontend.components.diff :as diff]
             [frontend.components.encryption :as encryption]
             [frontend.components.file-sync :as file-sync]
-            [frontend.components.git :as git-component]
+            [frontend.components.file-based.git :as git-component]
             [frontend.components.plugins :as plugin]
             [frontend.components.property.dialog :as property-dialog]
             [frontend.components.repo :as repo]

+ 70 - 0
src/main/frontend/handler/file_based/import.cljs

@@ -0,0 +1,70 @@
+(ns frontend.handler.file-based.import
+  "Handles file-graph specific imports"
+  (:require [clojure.string :as string]
+            [frontend.config :as config]
+            [frontend.db :as db]
+            [frontend.date :as date]
+            [frontend.external :as external]
+            [frontend.handler.file-based.file :as file-handler]
+            [frontend.handler.file-based.repo :as file-repo-handler]
+            [frontend.state :as state]
+            [frontend.util :as util]
+            [logseq.common.util :as common-util]
+            [logseq.common.util.date-time :as date-time-util]))
+
+
+(defn index-files!
+  "Create file structure, then parse into DB (client only)"
+  [repo files finish-handler]
+  (let [titles (->> files
+                    (map :title)
+                    (remove nil?))
+        files (map (fn [file]
+                     (let [title (:title file)
+                           journal? (date/valid-journal-title? title)]
+                       (when-let [text (:text file)]
+                         (let [title (or
+                                      (when journal?
+                                        (date/journal-title->default title))
+                                      (string/replace title "/" "-"))
+                               title (-> (common-util/page-name-sanity title)
+                                         (string/replace "\n" " "))
+                               path (str (if journal?
+                                           (config/get-journals-directory)
+                                           (config/get-pages-directory))
+                                         "/"
+                                         title
+                                         ".md")]
+                           {:file/path path
+                            :file/content text}))))
+                   files)
+        files (remove nil? files)]
+    (file-repo-handler/parse-files-and-load-to-db! repo files nil)
+    (let [files (->> (map (fn [{:file/keys [path content]}] (when path [path content])) files)
+                     (remove nil?))]
+      (file-handler/alter-files repo files {:add-history? false
+                                            :update-db? false
+                                            :update-status? false
+                                            :finish-handler finish-handler}))
+    (let [journal-pages-tx (let [titles (filter date/normalize-journal-title titles)]
+                             (map
+                              (fn [title]
+                                (let [day (date/journal-title->int title)
+                                      journal-title (date-time-util/int->journal-title day (state/get-date-formatter))]
+                                  (when journal-title
+                                    (let [page-name (util/page-name-sanity-lc journal-title)]
+                                      {:block/name page-name
+                                       :block/type "journal"
+                                       :block/journal-day day}))))
+                              titles))]
+      (when (seq journal-pages-tx)
+        (db/transact! repo journal-pages-tx)))))
+
+;; TODO: replace files with page blocks transaction
+(defn import-from-roam-json!
+  [data finished-ok-handler]
+  (when-let [repo (state/get-current-repo)]
+    (let [files (external/to-markdown-files :roam data {})]
+      (index-files! repo files
+                    (fn []
+                      (finished-ok-handler))))))

+ 0 - 62
src/main/frontend/handler/import.cljs

@@ -6,23 +6,17 @@
             [clojure.string :as string]
             [clojure.walk :as walk]
             [frontend.config :as config]
-            [frontend.date :as date]
             [frontend.db :as db]
             [frontend.db.async :as db-async]
-            [frontend.external :as external]
             [frontend.format.block :as block]
             [frontend.format.mldoc :as mldoc]
             [frontend.handler.editor :as editor]
-            [frontend.handler.file-based.file :as file-handler]
-            [frontend.handler.file-based.repo :as file-repo-handler]
             [frontend.handler.notification :as notification]
             [frontend.handler.page :as page-handler]
             [frontend.handler.repo :as repo-handler]
             [frontend.persist-db :as persist-db]
             [frontend.state :as state]
             [frontend.util :as util]
-            [logseq.common.util :as common-util]
-            [logseq.common.util.date-time :as date-time-util]
             [logseq.db :as ldb]
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.graph-parser.mldoc :as gp-mldoc]
@@ -30,62 +24,6 @@
             [medley.core :as medley]
             [promesa.core :as p]))
 
-(defn index-files!
-  "Create file structure, then parse into DB (client only)"
-  [repo files finish-handler]
-  (let [titles (->> files
-                    (map :title)
-                    (remove nil?))
-        files (map (fn [file]
-                     (let [title (:title file)
-                           journal? (date/valid-journal-title? title)]
-                       (when-let [text (:text file)]
-                         (let [title (or
-                                      (when journal?
-                                        (date/journal-title->default title))
-                                      (string/replace title "/" "-"))
-                               title (-> (common-util/page-name-sanity title)
-                                         (string/replace "\n" " "))
-                               path (str (if journal?
-                                           (config/get-journals-directory)
-                                           (config/get-pages-directory))
-                                         "/"
-                                         title
-                                         ".md")]
-                           {:file/path path
-                            :file/content text}))))
-                   files)
-        files (remove nil? files)]
-    (file-repo-handler/parse-files-and-load-to-db! repo files nil)
-    (let [files (->> (map (fn [{:file/keys [path content]}] (when path [path content])) files)
-                     (remove nil?))]
-      (file-handler/alter-files repo files {:add-history? false
-                                            :update-db? false
-                                            :update-status? false
-                                            :finish-handler finish-handler}))
-    (let [journal-pages-tx (let [titles (filter date/normalize-journal-title titles)]
-                             (map
-                              (fn [title]
-                                (let [day (date/journal-title->int title)
-                                      journal-title (date-time-util/int->journal-title day (state/get-date-formatter))]
-                                  (when journal-title
-                                    (let [page-name (util/page-name-sanity-lc journal-title)]
-                                      {:block/name page-name
-                                       :block/type "journal"
-                                       :block/journal-day day}))))
-                              titles))]
-      (when (seq journal-pages-tx)
-        (db/transact! repo journal-pages-tx)))))
-
-;; TODO: replace files with page blocks transaction
-(defn import-from-roam-json!
-  [data finished-ok-handler]
-  (when-let [repo (state/get-current-repo)]
-    (let [files (external/to-markdown-files :roam data {})]
-      (index-files! repo files
-                    (fn []
-                      (finished-ok-handler))))))
-
 ;;; import OPML files
 (defn import-from-opml!
   [data finished-ok-handler]

+ 102 - 16
src/main/frontend/worker/file.cljs

@@ -1,25 +1,42 @@
 (ns frontend.worker.file
   "Save pages to files for file-based graphs"
-  (:require [clojure.core.async :as async]
-            [clojure.string :as string]
-            [clojure.set :as set]
-            [frontend.common.file.core :as common-file]
-            [logseq.outliner.tree :as otree]
-            [lambdaisland.glogi :as log]
+  (:require [cljs-time.coerce :as tc]
             [cljs-time.core :as t]
-            [cljs-time.coerce :as tc]
-            [frontend.worker.util :as worker-util]
-            [frontend.common.async-util :as async-util]
+            [clojure.core.async :as async]
+            [clojure.set :as set]
+            [clojure.string :as string]
             [datascript.core :as d]
-            [logseq.db :as ldb]
-            [malli.core :as m]
+            [frontend.common.async-util :as async-util]
+            [frontend.common.file.core :as common-file]
+            [frontend.common.file.util :as wfu]
             [frontend.worker.state :as worker-state]
+            [frontend.worker.util :as worker-util]
             [goog.object :as gobj]
-            [logseq.common.util :as common-util]))
+            [lambdaisland.glogi :as log]
+            [logseq.common.date :as common-date]
+            [logseq.common.path :as path]
+            [logseq.common.util :as common-util]
+            [logseq.db :as ldb]
+            [logseq.outliner.tree :as otree]
+            [malli.core :as m]))
+
+(defonce *writes (atom {}))
+(defonce *request-id (atom 0))
+
+(defn- conj-page-write!
+  [page-id]
+  (let [request-id (swap! *request-id inc)]
+    (swap! *writes assoc request-id page-id)
+    request-id))
 
-(def *writes common-file/*writes)
-(def dissoc-request! common-file/dissoc-request!)
-(def conj-page-write! common-file/conj-page-write!)
+(defn- dissoc-request!
+  [request-id]
+  (when-let [page-id (get @*writes request-id)]
+    (let [old-page-request-ids (keep (fn [[r p]]
+                                       (when (and (= p page-id) (<= r request-id))
+                                         r)) @*writes)]
+      (when (seq old-page-request-ids)
+        (swap! *writes (fn [x] (apply dissoc x old-page-request-ids)))))))
 
 (defonce file-writes-chan
   (let [coercer (m/coercer [:catn
@@ -58,6 +75,75 @@
             :block/parent) ;; these are auto-generated for whiteboard shapes
     (dissoc block :db/id :block/page)))
 
+(defn- transact-file-tx-if-not-exists!
+  [conn page-block ok-handler context]
+  (when (:block/name page-block)
+    (let [format (name (get page-block :block/format (:preferred-format context)))
+          date-formatter (:date-formatter context)
+          title (string/capitalize (:block/name page-block))
+          whiteboard-page? (ldb/whiteboard? page-block)
+          format (if whiteboard-page? "edn" format)
+          journal-page? (common-date/valid-journal-title? title date-formatter)
+          journal-title (common-date/normalize-journal-title title date-formatter)
+          journal-page? (and journal-page? (not (string/blank? journal-title)))
+          filename (if journal-page?
+                     (common-date/date->file-name journal-title (:journal-file-name-format context))
+                     (-> (or (:block/title page-block) (:block/name page-block))
+                         wfu/file-name-sanity))
+          sub-dir (cond
+                    journal-page?    (:journals-directory context)
+                    whiteboard-page? (:whiteboards-directory context)
+                    :else            (:pages-directory context))
+          ext (if (= format "markdown") "md" format)
+          file-rpath (path/path-join sub-dir (str filename "." ext))
+          file {:file/path file-rpath}
+          tx [{:file/path file-rpath}
+              {:block/name (:block/name page-block)
+               :block/file file}]]
+      (ldb/transact! conn tx)
+      (when ok-handler (ok-handler)))))
+
+(defn- remove-transit-ids [block] (dissoc block :db/id :block/file))
+
+(defn- save-tree-aux!
+  [repo db page-block tree blocks-just-deleted? context request-id]
+  (let [page-block (d/pull db '[*] (:db/id page-block))
+        init-level 1
+        file-db-id (-> page-block :block/file :db/id)
+        file-path (-> (d/entity db file-db-id) :file/path)
+        result (if (and (string? file-path) (not-empty file-path))
+                 (let [new-content (if (ldb/whiteboard? page-block)
+                                     (->
+                                      (wfu/ugly-pr-str {:blocks tree
+                                                        :pages (list (remove-transit-ids page-block))})
+                                      (string/triml))
+                                     (common-file/tree->file-content repo db tree {:init-level init-level} context))]
+                   (when-not (and (string/blank? new-content) (not blocks-just-deleted?))
+                     (let [files [[file-path new-content]]]
+                       (when (seq files)
+                         (let [page-id (:db/id page-block)]
+                           (wfu/post-message :write-files {:request-id request-id
+                                                           :page-id page-id
+                                                           :repo repo
+                                                           :files files})
+                           :sent)))))
+                 ;; In e2e tests, "card" page in db has no :file/path
+                 (js/console.error "File path from page-block is not valid" page-block tree))]
+    (when-not (= :sent result)          ; page may not exists now
+      (dissoc-request! request-id))))
+
+(defn save-tree!
+  [repo conn page-block tree blocks-just-deleted? context request-id]
+  {:pre [(map? page-block)]}
+  (when repo
+    (let [ok-handler #(save-tree-aux! repo @conn page-block tree blocks-just-deleted? context request-id)
+          file (or (:block/file page-block)
+                   (when-let [page-id (:db/id (:block/page page-block))]
+                     (:block/file (d/entity @conn page-id))))]
+      (if file
+        (ok-handler)
+        (transact-file-tx-if-not-exists! conn page-block ok-handler context)))))
+
 (defn do-write-file!
   [repo conn page-db-id outliner-op context request-id]
   (let [page-block (d/pull @conn '[*] page-db-id)
@@ -81,7 +167,7 @@
             (let [tree-or-blocks (if whiteboard? blocks
                                      (otree/blocks->vec-tree repo @conn blocks (:db/id page-block)))]
               (if page-block
-                (common-file/save-tree! repo conn page-block tree-or-blocks blocks-just-deleted? context request-id)
+                (save-tree! repo conn page-block tree-or-blocks blocks-just-deleted? context request-id)
                 (do
                   (js/console.error (str "can't find page id: " page-db-id))
                   (dissoc-request! request-id)))))))