Sfoglia il codice sorgente

wip save/load pages as edn

Peng Xiao 3 anni fa
parent
commit
e0296b90ee

+ 6 - 4
src/main/frontend/modules/file/core.cljs

@@ -144,10 +144,12 @@
 
 
 (defn save-tree-aux!
 (defn save-tree-aux!
   [page-block tree]
   [page-block tree]
-  (let [page-block (db/pull (:db/id page-block))
-        new-content (tree->file-content tree {:init-level init-level})
-        file-db-id (-> page-block :block/file :db/id)
-        file-path (-> (db-utils/entity file-db-id) :file/path)
+  (let [page-block (db/pull '[* {:block/file [:file/path]}] (:db/id page-block))
+        file-path (get-in page-block [:block/file :file/path])
+        edn? (string/ends-with? file-path ".edn")
+        new-content (if edn?
+                      (util/pp-str {:blocks tree :pages (list page-block)})
+                      (tree->file-content tree {:init-level init-level}))
         _ (assert (string? file-path) "File path should satisfy string?")
         _ (assert (string? file-path) "File path should satisfy string?")
         ;; FIXME: name conflicts between multiple graphs
         ;; FIXME: name conflicts between multiple graphs
         files [[file-path new-content]]]
         files [[file-path new-content]]]

+ 20 - 7
src/main/frontend/modules/outliner/file.cljs

@@ -21,18 +21,31 @@
   []
   []
   (empty? @write-chan-batch-buf))
   (empty? @write-chan-batch-buf))
 
 
+(def blocks-pull-keys-with-persisted-ids
+  '[:block/uuid
+    :block/format
+    :block/content
+    :block/unordered
+    {:block/page      [:block/name :block/uuid]}
+    {:block/left      [:block/name :block/uuid]}
+    {:block/parent    [:block/name :block/uuid]}
+    {:block/path-refs [:block/name :block/uuid]}])
+
 (defn do-write-file!
 (defn do-write-file!
   [repo page-db-id]
   [repo page-db-id]
-  (let [page-block (db/pull repo '[*] page-db-id)
-        page-db-id (:db/id page-block)
-        blocks (model/get-page-blocks-no-cache repo (:block/name page-block))]
+  (let [page-block (db/pull repo '[* {:block/file [:file/path]}] page-db-id)
+        file-path (get-in page-block [:block/file :file/path])
+        edn? (string/ends-with? file-path ".edn")
+        blocks (model/get-page-blocks-no-cache repo (:block/name page-block)
+                                               {:pull-keys (if edn? blocks-pull-keys-with-persisted-ids '[*])})]
     (when-not (and (= 1 (count blocks))
     (when-not (and (= 1 (count blocks))
                    (string/blank? (:block/content (first blocks)))
                    (string/blank? (:block/content (first blocks)))
                    (nil? (:block/file page-block)))
                    (nil? (:block/file page-block)))
-      (let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))]
-        (if page-block
-          (file/save-tree! page-block tree)
-          (js/console.error (str "can't find page id: " page-db-id)))))))
+      (if page-block
+        (file/save-tree! page-block (if edn?
+                                      blocks
+                                      (tree/blocks->vec-tree repo blocks (:block/name page-block))))
+        (js/console.error (str "can't find page id: " page-db-id))))))
 
 
 (defn write-files!
 (defn write-files!
   [pages]
   [pages]