whiteboard.cljs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. (ns frontend.handler.whiteboard
  2. "Whiteboard related handlers"
  3. (:require [datascript.core :as d]
  4. [dommy.core :as dom]
  5. [frontend.db :as db]
  6. [frontend.db.model :as model]
  7. [frontend.handler.editor :as editor-handler]
  8. [frontend.handler.route :as route-handler]
  9. [frontend.handler.property.util :as pu]
  10. [frontend.state :as state]
  11. [frontend.config :as config]
  12. [frontend.storage :as storage]
  13. [frontend.util :as util]
  14. [logseq.common.util :as common-util]
  15. [logseq.graph-parser.whiteboard :as gp-whiteboard]
  16. [promesa.core :as p]
  17. [goog.object :as gobj]
  18. [clojure.set :as set]
  19. [clojure.string :as string]
  20. [cljs-bean.core :as bean]
  21. [logseq.db.sqlite.util :as sqlite-util]))
  22. (defn js->clj-keywordize
  23. [obj]
  24. (js->clj obj :keywordize-keys true))
  25. (defn- shape->block [shape page-id]
  26. (let [repo (state/get-current-repo)]
  27. (gp-whiteboard/shape->block repo (db/get-db repo) shape page-id)))
  28. (defn- build-shapes
  29. [page-block blocks]
  30. (let [page-metadata (pu/get-block-property-value page-block :logseq.property.tldraw/page)
  31. shapes-index (:shapes-index page-metadata)
  32. shape-id->index (zipmap shapes-index (range 0 (count shapes-index)))]
  33. (->> blocks
  34. (map (fn [block]
  35. (assoc block :index (get shape-id->index (str (:block/uuid block)) 0))))
  36. (filter pu/shape-block?)
  37. (map pu/block->shape)
  38. (sort-by :index))))
  39. (defn- whiteboard-clj->tldr [page-block blocks]
  40. (let [id (str (:block/uuid page-block))
  41. shapes (build-shapes page-block blocks)
  42. tldr-page (pu/page-block->tldr-page page-block)
  43. assets (:assets tldr-page)
  44. tldr-page (dissoc tldr-page :assets)]
  45. (clj->js {:currentPageId id
  46. :assets (or assets #js[])
  47. :selectedIds #js[]
  48. :pages [(merge tldr-page
  49. {:id id
  50. :name (:block/name page-block)
  51. :shapes shapes})]})))
  52. (defn db-build-page-block
  53. [page-entity page-name tldraw-page assets shapes-index]
  54. (let [get-k #(gobj/get tldraw-page %)
  55. tldraw-page {:id (get-k "id")
  56. :name (get-k "name")
  57. :bindings (js->clj-keywordize (get-k "bindings"))
  58. :nonce (get-k "nonce")
  59. :assets (js->clj-keywordize assets)
  60. :shapes-index shapes-index}]
  61. {:block/original-name page-name
  62. :block/name (util/page-name-sanity-lc page-name)
  63. :block/type "whiteboard"
  64. :block/properties (sqlite-util/build-properties page-entity
  65. {:logseq.property/ls-type :whiteboard-page
  66. :logseq.property.tldraw/page tldraw-page})
  67. :block/updated-at (util/time-ms)
  68. :block/created-at (or (:block/created-at page-entity)
  69. (util/time-ms))}))
  70. (defn file-build-page-block
  71. [page-entity page-name tldraw-page assets shapes-index]
  72. (let [get-k #(gobj/get tldraw-page %)]
  73. {:block/original-name page-name
  74. :block/name (util/page-name-sanity-lc page-name)
  75. :block/type "whiteboard"
  76. :block/properties {(pu/get-pid :logseq.property/ls-type)
  77. :whiteboard-page
  78. (pu/get-pid :logseq.property.tldraw/page)
  79. {:id (get-k "id")
  80. :name (get-k "name")
  81. :bindings (js->clj-keywordize (get-k "bindings"))
  82. :nonce (get-k "nonce")
  83. :assets (js->clj-keywordize assets)
  84. :shapes-index shapes-index}}
  85. :block/updated-at (util/time-ms)
  86. :block/created-at (or (:block/created-at page-entity)
  87. (util/time-ms))}))
  88. (defn build-page-block
  89. [page-entity page-name tldraw-page assets shapes-index]
  90. (let [f (if (config/db-based-graph? (state/get-current-repo))
  91. db-build-page-block
  92. file-build-page-block)]
  93. (f page-entity page-name tldraw-page assets shapes-index)))
  94. (defn- compute-tx
  95. [^js app ^js tl-page new-id-nonces db-id-nonces page-uuid replace?]
  96. (let [page-entity (db/get-page page-uuid)
  97. assets (js->clj-keywordize (.getCleanUpAssets app))
  98. new-shapes (.-shapes tl-page)
  99. shapes-index (map #(gobj/get % "id") new-shapes)
  100. shape-id->index (zipmap shapes-index (range (.-length new-shapes)))
  101. upsert-shapes (->> (set/difference new-id-nonces db-id-nonces)
  102. (map (fn [{:keys [id]}]
  103. (-> (.-serialized ^js (.getShapeById tl-page id))
  104. js->clj-keywordize
  105. (assoc :index (get shape-id->index id)))))
  106. (set))
  107. old-ids (set (map :id db-id-nonces))
  108. new-ids (set (map :id new-id-nonces))
  109. created-ids (->> (set/difference new-ids old-ids)
  110. (remove string/blank?)
  111. (set))
  112. created-shapes (set (filter #(created-ids (:id %)) upsert-shapes))
  113. deleted-ids (->> (set/difference old-ids new-ids)
  114. (remove string/blank?))
  115. repo (state/get-current-repo)
  116. deleted-shapes (when (seq deleted-ids)
  117. (->> (db/pull-many repo '[*] (mapv (fn [id] [:block/uuid (uuid id)]) deleted-ids))
  118. (mapv (fn [b] (pu/get-block-property-value b :logseq.property.tldraw/shape)))
  119. (remove nil?)))
  120. deleted-shapes-tx (mapv (fn [id] [:db/retractEntity [:block/uuid (uuid id)]]) deleted-ids)
  121. upserted-blocks (->> upsert-shapes
  122. (map #(shape->block % (:db/id page-entity)))
  123. (map sqlite-util/block-with-timestamps))
  124. page-name (or (:block/original-name page-entity) (str page-uuid))
  125. page-block (build-page-block page-entity page-name tl-page assets shapes-index)]
  126. (when (or (seq upserted-blocks)
  127. (seq deleted-shapes-tx)
  128. (not= (:block/properties page-block)
  129. (:block/properties page-entity)))
  130. {:page-block page-block
  131. :upserted-blocks upserted-blocks
  132. :delete-blocks deleted-shapes-tx
  133. :deleted-shapes deleted-shapes
  134. :new-shapes created-shapes
  135. :metadata {:whiteboard/transact? true
  136. :pipeline-replace? replace?}})))
  137. (defonce *last-shapes-nonce (atom {}))
  138. ;; FIXME: it seems that nonce for the page block will not be updated with new updates for the whiteboard
  139. (defn <transact-tldr-delta!
  140. [page-uuid ^js app replace?]
  141. (let [tl-page ^js (second (first (.-pages app)))
  142. shapes (.-shapes ^js tl-page)
  143. page-block (model/get-page page-uuid)
  144. new-id-nonces (set (map-indexed (fn [_idx shape]
  145. (let [id (.-id shape)]
  146. {:id id
  147. :nonce (or (.-nonce shape) (js/Date.now))})) shapes))
  148. repo (state/get-current-repo)
  149. db-id-nonces (or
  150. (get-in @*last-shapes-nonce [repo page-uuid])
  151. (set (->> (model/get-whiteboard-id-nonces repo (:db/id page-block))
  152. (map #(update % :id str)))))
  153. {:keys [page-block new-shapes deleted-shapes upserted-blocks delete-blocks metadata] :as result}
  154. (compute-tx app tl-page new-id-nonces db-id-nonces page-uuid replace?)]
  155. (when (seq result)
  156. (let [tx-data (concat delete-blocks [page-block] upserted-blocks)
  157. metadata' (cond
  158. ;; group
  159. (some #(= "group" (:type %)) new-shapes)
  160. (assoc metadata :whiteboard/op :group)
  161. ;; ungroup
  162. (and (not-empty deleted-shapes) (every? #(= "group" (:type %)) deleted-shapes))
  163. (assoc metadata :whiteboard/op :un-group)
  164. ;; arrow
  165. (some #(and (= "line" (:type %))
  166. (= "arrow " (:end (:decorations %)))) new-shapes)
  167. (assoc metadata :whiteboard/op :new-arrow)
  168. :else
  169. metadata)]
  170. (swap! *last-shapes-nonce assoc-in [repo page-uuid] new-id-nonces)
  171. (if (contains? #{:new-arrow} (:whiteboard/op metadata'))
  172. (state/set-state! :whiteboard/pending-tx-data
  173. {:tx-data tx-data
  174. :metadata metadata'})
  175. (let [pending-tx-data (:whiteboard/pending-tx-data @state/state)
  176. tx-data' (concat (:tx-data pending-tx-data) tx-data)
  177. metadata'' (merge metadata' (:metadata pending-tx-data))]
  178. (state/set-state! :whiteboard/pending-tx-data {})
  179. (db/transact! repo tx-data' metadata'')))))))
  180. (defn get-default-new-whiteboard-tx
  181. [page-name id]
  182. (let [db-based? (config/db-based-graph? (state/get-current-repo))
  183. tldraw-page {:id (str id),
  184. :name page-name,
  185. :ls-type :whiteboard-page,
  186. :bindings {},
  187. :nonce 1,
  188. :assets []}
  189. properties-map {(pu/get-pid :logseq.property/ls-type) :whiteboard-page,
  190. (pu/get-pid :logseq.property.tldraw/page) tldraw-page}
  191. properties (if db-based?
  192. (sqlite-util/build-properties nil properties-map)
  193. properties-map)
  194. m #:block{:uuid id
  195. :name (util/page-name-sanity-lc page-name),
  196. :original-name page-name
  197. :type "whiteboard",
  198. :journal? false
  199. :format :markdown
  200. :updated-at (util/time-ms),
  201. :created-at (util/time-ms)}]
  202. [(assoc m :block/properties properties)]))
  203. (defn <create-new-whiteboard-page!
  204. ([]
  205. (<create-new-whiteboard-page! nil))
  206. ([name]
  207. (p/let [uuid (or (and name (parse-uuid name)) (d/squuid))
  208. name (or name (str uuid))
  209. _ (db/transact! (get-default-new-whiteboard-tx name uuid))]
  210. uuid)))
  211. (defn <create-new-whiteboard-and-redirect!
  212. ([]
  213. (<create-new-whiteboard-and-redirect! (str (d/squuid))))
  214. ([name]
  215. (when-not config/publishing?
  216. (p/let [id (<create-new-whiteboard-page! name)]
  217. (route-handler/redirect-to-page! id {:new-whiteboard? true})))))
  218. (defn ->logseq-portal-shape
  219. [block-id point]
  220. {:blockType (if (parse-uuid (str block-id)) "B" "P")
  221. :id (str (d/squuid))
  222. :compact false
  223. :pageId (str block-id)
  224. :point point
  225. :size [400, 0]
  226. :type "logseq-portal"})
  227. (defn add-new-block-portal-shape!
  228. "Given the block uuid, add a new shape to the referenced block.
  229. By default it will be placed next to the given shape id"
  230. [block-uuid source-shape & {:keys [link? bottom?]}]
  231. (when-let [app (state/active-tldraw-app)]
  232. (let [^js api (.-api app)
  233. point (-> (.getShapeById app source-shape)
  234. (.-bounds)
  235. ((fn [bounds] (if bottom?
  236. [(.-minX ^js bounds) (+ 64 (.-maxY ^js bounds))]
  237. [(+ 64 (.-maxX ^js bounds)) (.-minY ^js bounds)]))))
  238. shape (->logseq-portal-shape block-uuid point)]
  239. (when (uuid? block-uuid) (editor-handler/set-blocks-id! [block-uuid]))
  240. (.createShapes api (clj->js shape))
  241. (when link?
  242. (.createNewLineBinding api source-shape (:id shape))))))
  243. (defn get-page-tldr
  244. [page-uuid]
  245. (let [page (model/get-page page-uuid)
  246. react-page (db/sub-block (:db/id page))
  247. blocks (:block/_page react-page)]
  248. (whiteboard-clj->tldr react-page blocks)))
  249. (defn <add-new-block!
  250. [page-uuid content]
  251. (p/let [repo (state/get-current-repo)
  252. new-block-id (db/new-block-id)
  253. page-entity (model/get-page page-uuid)
  254. tx (sqlite-util/block-with-timestamps
  255. {:block/uuid new-block-id
  256. :block/content (or content "")
  257. :block/format :markdown
  258. :block/page (:db/id page-entity)
  259. :block/parent (:db/id page-entity)})
  260. _ (db/transact! repo [tx] {:whiteboard/transact? true})]
  261. new-block-id))
  262. (defn inside-portal?
  263. [target]
  264. (some? (dom/closest target ".tl-logseq-cp-container")))
  265. (defn closest-shape
  266. [target]
  267. (when-let [shape-el (dom/closest target "[data-shape-id]")]
  268. (.getAttribute shape-el "data-shape-id")))
  269. (defn get-onboard-whiteboard-edn
  270. []
  271. (p/let [^js res (js/fetch "./whiteboard/onboarding.edn") ;; do we need to cache it?
  272. text (.text res)
  273. edn (common-util/safe-read-string text)]
  274. edn))
  275. (defn clone-whiteboard-from-edn
  276. "Given a tldr, clone the whiteboard page into current active whiteboard"
  277. ([edn]
  278. (when-let [app (state/active-tldraw-app)]
  279. (clone-whiteboard-from-edn edn (.-api app))))
  280. ([{:keys [pages blocks]} api]
  281. (let [page-block (first pages)
  282. ;; FIXME: should also clone normal blocks
  283. shapes (build-shapes page-block blocks)
  284. tldr-page (pu/page-block->tldr-page page-block)
  285. assets (:assets tldr-page)
  286. bindings (:bindings tldr-page)]
  287. (.cloneShapesIntoCurrentPage ^js api (clj->js {:shapes shapes
  288. :assets assets
  289. :bindings bindings})))))
  290. (defn should-populate-onboarding-whiteboard?
  291. "When there is no whiteboard, or there is only one whiteboard that has the given page name, we should populate the onboarding shapes"
  292. [page-uuid]
  293. (let [whiteboards (model/get-all-whiteboards (state/get-current-repo))]
  294. (and (or (empty? whiteboards)
  295. (and
  296. (= 1 (count whiteboards))
  297. (= (str page-uuid) (str (:block/uuid (first whiteboards))))))
  298. (not (state/get-onboarding-whiteboard?)))))
  299. (defn update-shapes!
  300. [shapes]
  301. (when-let [app (state/active-tldraw-app)]
  302. (let [^js api (.-api app)]
  303. (apply (.-updateShapes api) (bean/->js shapes)))))
  304. (defn update-shapes-index!
  305. [page-uuid]
  306. (when-let [app (state/active-tldraw-app)]
  307. (let [tl-page ^js (second (first (.-pages app)))]
  308. (when tl-page
  309. (when-let [page (db/get-page page-uuid)]
  310. (let [page-metadata (pu/get-block-property-value page :logseq.property.tldraw/page)
  311. shapes-index (:shapes-index page-metadata)]
  312. (when (seq shapes-index)
  313. (.updateShapesIndex tl-page (bean/->js shapes-index)))))))))
  314. (defn populate-onboarding-whiteboard
  315. [api]
  316. (when (some? api)
  317. (-> (p/let [edn (get-onboard-whiteboard-edn)]
  318. (clone-whiteboard-from-edn edn api)
  319. (state/set-onboarding-whiteboard! true))
  320. (p/catch
  321. (fn [e] (js/console.warn "Failed to populate onboarding whiteboard" e))))))
  322. (defn cleanup!
  323. [^js tl-page]
  324. (let [shapes (.-shapes tl-page)]
  325. (.cleanup tl-page (map #(.-id %) shapes))))
  326. (defn onboarding-show
  327. []
  328. (when (not (or (state/sub :whiteboard/onboarding-tour?)
  329. (config/demo-graph?)
  330. (util/mobile?)))
  331. (state/pub-event! [:whiteboard/onboarding])
  332. (state/set-state! [:whiteboard/onboarding-tour?] true)
  333. (storage/set :whiteboard-onboarding-tour? true)))