Peng Xiao 3 years ago
parent
commit
51c40be94f

+ 49 - 0
src/main/frontend/handler/whiteboard.cljs

@@ -1,6 +1,8 @@
 (ns frontend.handler.whiteboard
   (:require [frontend.state :as state]
             [clojure.string :as string]
+            [frontend.db :as db]
+            [frontend.db.model :as model]
             [goog.object :as gobj]))
 
 ;; FIXME: embed /draw should be supported too
@@ -26,3 +28,50 @@
           (.updateShapes app (clj->js
                               [{:id (.-id fs)
                                 :logseqLink page-or-block-id}])))))))
+
+(defn- get-page-block [page-name]
+  (db/pull '[*] (:db/id (model/get-page page-name))))
+
+(defn- block->shape [block]
+  (let [properties (:block/properties block)]
+    (merge properties
+           ;; Use the block's id as the shape's id.
+           {:id (str (:block/uuid block))})))
+
+(defn- shape->block [blocks-by-uuid shape]
+  (let [properties shape
+        block (get blocks-by-uuid (:id shape))]
+    (merge block
+           {:properties properties})))
+
+(defn get-whiteboard-cjs [page-name]
+  (let [page-block (get-page-block page-name)
+        blocks (model/get-page-blocks-no-cache page-name)]
+    [page-block blocks]))
+
+(defn whiteboard-cjs->tldr [page-block blocks]
+  (let [shapes (map block->shape blocks)
+        page-name (:block/name page-block)
+        page-properties (:block/properties page-block)]
+    (clj->js {:currentPageId page-name
+              :pages [(merge page-properties
+                             {:id "page"
+                              :name "page"
+                              :shapes shapes})]})))
+
+(defn page-name->tldr [page-name]
+  (let [[page-block blocks] (get-whiteboard-cjs page-name)]
+    (whiteboard-cjs->tldr page-block blocks)))
+
+(defn transact-tldr! [page-name tldr]
+  (let [[page-block blocks] (get-whiteboard-cjs page-name)
+        {:keys [pages]} (js->clj tldr)
+        page (first pages) ;; should only contain one page
+        shapes (:shapes page)
+        blocks-by-uuid (reduce (fn [acc shape]
+                                 (assoc (:id shape) shape acc))
+                               blocks {})
+        blocks (map #(shape->block blocks-by-uuid %) shapes)]
+    [page-block blocks]))
+
+;; (set! (. js/window -foo) (page-name->tldr "edn-test"))

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

@@ -142,13 +142,16 @@
         (db/transact! tx)
         (when ok-handler (ok-handler))))))
 
+(defn- remove-db-id [block] (dissoc block :db/id))
+
 (defn save-tree-aux!
   [page-block tree]
   (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)})
+                      (util/pp-str {:blocks (map remove-db-id tree)
+                                    :pages (list (remove-db-id page-block))})
                       (tree->file-content tree {:init-level init-level}))
         _ (assert (string? file-path) "File path should satisfy string?")
         ;; FIXME: name conflicts between multiple graphs

+ 1 - 4
src/main/frontend/modules/outliner/file.cljs

@@ -22,10 +22,7 @@
   (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]}