pull/6112/head
Peng Xiao 2022-07-10 19:01:06 +08:00
parent e0296b90ee
commit 51c40be94f
3 changed files with 54 additions and 5 deletions

View File

@ -1,6 +1,8 @@
(ns frontend.handler.whiteboard (ns frontend.handler.whiteboard
(:require [frontend.state :as state] (:require [frontend.state :as state]
[clojure.string :as string] [clojure.string :as string]
[frontend.db :as db]
[frontend.db.model :as model]
[goog.object :as gobj])) [goog.object :as gobj]))
;; FIXME: embed /draw should be supported too ;; FIXME: embed /draw should be supported too
@ -26,3 +28,50 @@
(.updateShapes app (clj->js (.updateShapes app (clj->js
[{:id (.-id fs) [{:id (.-id fs)
:logseqLink page-or-block-id}]))))))) :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"))

View File

@ -142,13 +142,16 @@
(db/transact! tx) (db/transact! tx)
(when ok-handler (ok-handler)))))) (when ok-handler (ok-handler))))))
(defn- remove-db-id [block] (dissoc block :db/id))
(defn save-tree-aux! (defn save-tree-aux!
[page-block tree] [page-block tree]
(let [page-block (db/pull '[* {:block/file [:file/path]}] (:db/id page-block)) (let [page-block (db/pull '[* {:block/file [:file/path]}] (:db/id page-block))
file-path (get-in page-block [:block/file :file/path]) file-path (get-in page-block [:block/file :file/path])
edn? (string/ends-with? file-path ".edn") edn? (string/ends-with? file-path ".edn")
new-content (if 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})) (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

View File

@ -22,10 +22,7 @@
(empty? @write-chan-batch-buf)) (empty? @write-chan-batch-buf))
(def blocks-pull-keys-with-persisted-ids (def blocks-pull-keys-with-persisted-ids
'[:block/uuid '[*
:block/format
:block/content
:block/unordered
{:block/page [:block/name :block/uuid]} {:block/page [:block/name :block/uuid]}
{:block/left [:block/name :block/uuid]} {:block/left [:block/name :block/uuid]}
{:block/parent [:block/name :block/uuid]} {:block/parent [:block/name :block/uuid]}