mirror of
https://github.com/penpot/penpot.git
synced 2025-01-26 08:29:42 -05:00
Implement page persistence.
This commit is contained in:
parent
a90ef042d5
commit
a17b79da77
3 changed files with 116 additions and 64 deletions
|
@ -25,7 +25,7 @@
|
||||||
(defrecord PagesFetched [pages]
|
(defrecord PagesFetched [pages]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(reduce stpr/assoc-page state pages)))
|
(reduce stpr/unpack-page state pages)))
|
||||||
|
|
||||||
(defn pages-fetched?
|
(defn pages-fetched?
|
||||||
[v]
|
[v]
|
||||||
|
@ -70,7 +70,6 @@
|
||||||
:layout [sc/required sc/string]
|
:layout [sc/required sc/string]
|
||||||
:width [sc/required sc/integer]
|
:width [sc/required sc/integer]
|
||||||
:height [sc/required sc/integer]
|
:height [sc/required sc/integer]
|
||||||
:data [sc/required]
|
|
||||||
:project [sc/required sc/uuid]})
|
:project [sc/required sc/uuid]})
|
||||||
|
|
||||||
(defn create-page
|
(defn create-page
|
||||||
|
@ -106,7 +105,6 @@
|
||||||
{:name [sc/required sc/string]
|
{:name [sc/required sc/string]
|
||||||
:width [sc/required sc/integer]
|
:width [sc/required sc/integer]
|
||||||
:height [sc/required sc/integer]
|
:height [sc/required sc/integer]
|
||||||
:data [sc/required]
|
|
||||||
:layout [sc/required sc/string]})
|
:layout [sc/required sc/string]})
|
||||||
|
|
||||||
(defn update-page
|
(defn update-page
|
||||||
|
@ -132,7 +130,6 @@
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [this state s]
|
(-apply-watch [this state s]
|
||||||
(println "UpdatePageMetadata" "-apply-watch")
|
|
||||||
(letfn [(on-success [{page :payload}]
|
(letfn [(on-success [{page :payload}]
|
||||||
(println "on-success")
|
(println "on-success")
|
||||||
#(assoc-in % [:pages-by-id id :version] (:version page)))
|
#(assoc-in % [:pages-by-id id :version] (:version page)))
|
||||||
|
@ -144,12 +141,9 @@
|
||||||
(rx/map on-success)
|
(rx/map on-success)
|
||||||
(rx/catch on-failure)))))
|
(rx/catch on-failure)))))
|
||||||
|
|
||||||
(def ^:static +update-page-metadata-schema+
|
|
||||||
(dissoc +update-page-schema+ :data))
|
|
||||||
|
|
||||||
(defn update-page-metadata
|
(defn update-page-metadata
|
||||||
[data]
|
[data]
|
||||||
(sc/validate! +update-page-metadata-schema+ data)
|
(sc/validate! +update-page-schema+ data)
|
||||||
(map->UpdatePageMetadata (dissoc data :data)))
|
(map->UpdatePageMetadata (dissoc data :data)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,31 @@
|
||||||
acc))
|
acc))
|
||||||
shapes
|
shapes
|
||||||
shapes))))
|
shapes))))
|
||||||
|
(defn pack-page
|
||||||
|
"Return a packed version of page object ready
|
||||||
|
for send to remore storage service."
|
||||||
|
[state id]
|
||||||
|
(let [page (get-in state [:pages-by-id id])
|
||||||
|
xf (filter #(= (:page (second %)) id))
|
||||||
|
shapes (into {} xf (:shapes-by-id state))]
|
||||||
|
(-> page
|
||||||
|
(assoc-in [:data :shapes] (into [] (:shapes page)))
|
||||||
|
(assoc-in [:data :shapes-by-id] shapes)
|
||||||
|
(update-in [:data] dissoc :items)
|
||||||
|
(dissoc :shapes))))
|
||||||
|
|
||||||
|
(defn unpack-page
|
||||||
|
"Unpacks packed page object and assocs it to the
|
||||||
|
provided state."
|
||||||
|
[state page]
|
||||||
|
(let [shapes (get-in page [:data :shapes])
|
||||||
|
shapes-by-id (get-in page [:data :shapes-by-id])
|
||||||
|
page (-> page
|
||||||
|
(dissoc page :data)
|
||||||
|
(assoc :shapes shapes))]
|
||||||
|
(-> state
|
||||||
|
(update :shapes-by-id merge shapes-by-id)
|
||||||
|
(assoc-page page))))
|
||||||
|
|
||||||
(defn dissoc-page
|
(defn dissoc-page
|
||||||
"Remove page and all related stuff from the state."
|
"Remove page and all related stuff from the state."
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
(ns uxbox.ui.workspace
|
(ns uxbox.ui.workspace
|
||||||
|
(:refer-clojure :exclude [dedupe])
|
||||||
(:require [sablono.core :as html :refer-macros [html]]
|
(:require [sablono.core :as html :refer-macros [html]]
|
||||||
[rum.core :as rum]
|
[rum.core :as rum]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[lentes.core :as l]
|
[lentes.core :as l]
|
||||||
[uxbox.rstore :as rs]
|
[uxbox.rstore :as rs]
|
||||||
[uxbox.state :as st]
|
[uxbox.state :as st]
|
||||||
|
[uxbox.state.project :as stpr]
|
||||||
[uxbox.data.workspace :as dw]
|
[uxbox.data.workspace :as dw]
|
||||||
[uxbox.data.projects :as dp]
|
[uxbox.data.projects :as dp]
|
||||||
[uxbox.data.pages :as udp]
|
[uxbox.data.pages :as udp]
|
||||||
|
[uxbox.util.lens :as ul]
|
||||||
[uxbox.util.geom.point :as gpt]
|
[uxbox.util.geom.point :as gpt]
|
||||||
[uxbox.util.data :refer (classnames)]
|
[uxbox.util.data :refer (classnames)]
|
||||||
[uxbox.ui.core :as uuc]
|
[uxbox.ui.core :as uuc]
|
||||||
|
@ -25,6 +28,92 @@
|
||||||
;; Workspace
|
;; Workspace
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
(defn focus-page
|
||||||
|
[id]
|
||||||
|
(as-> (ul/getter #(stpr/pack-page % id)) $
|
||||||
|
(l/focus-atom $ st/state)))
|
||||||
|
|
||||||
|
(defn on-page-change
|
||||||
|
[page]
|
||||||
|
(rs/emit! (udp/update-page page)))
|
||||||
|
|
||||||
|
(defn subscribe-to-page-changes
|
||||||
|
[pageid]
|
||||||
|
(as-> (focus-page pageid) $
|
||||||
|
(rx/from-atom $)
|
||||||
|
(rx/skip 1 $)
|
||||||
|
(rx/dedupe #(dissoc % :version) $)
|
||||||
|
(rx/debounce 1000 $)
|
||||||
|
(rx/subscribe $ on-page-change #(throw %))))
|
||||||
|
|
||||||
|
(defn- workspace-will-mount
|
||||||
|
[own]
|
||||||
|
(let [[projectid pageid] (:rum/props own)]
|
||||||
|
(rs/emit! (dw/initialize projectid pageid)
|
||||||
|
(dp/fetch-projects)
|
||||||
|
(udp/fetch-pages projectid))
|
||||||
|
own))
|
||||||
|
|
||||||
|
(defn- workspace-did-mount
|
||||||
|
[own]
|
||||||
|
(letfn [(handle-scroll-interaction []
|
||||||
|
(let [stoper (->> uuc/actions-s
|
||||||
|
(rx/map :type)
|
||||||
|
(rx/filter #(not= % :scroll/viewport))
|
||||||
|
(rx/take 1))
|
||||||
|
local (:rum/local own)
|
||||||
|
initial @uuwb/mouse-viewport-a]
|
||||||
|
(swap! local assoc :scrolling true)
|
||||||
|
(as-> uuwb/mouse-viewport-s $
|
||||||
|
(rx/take-until stoper $)
|
||||||
|
(rx/subscribe $ #(on-scroll % initial) nil on-scroll-end))))
|
||||||
|
|
||||||
|
(on-scroll-end []
|
||||||
|
(let [local (:rum/local own)]
|
||||||
|
(swap! local assoc :scrolling false)))
|
||||||
|
|
||||||
|
(on-scroll [pt initial]
|
||||||
|
(let [{:keys [x y]} (gpt/subtract pt initial)
|
||||||
|
el (mx/get-ref-dom own "workspace-canvas")
|
||||||
|
cx (.-scrollLeft el)
|
||||||
|
cy (.-scrollTop el)]
|
||||||
|
(set! (.-scrollLeft el) (- cx x))
|
||||||
|
(set! (.-scrollTop el) (- cy y))))]
|
||||||
|
(let [[projectid pageid] (:rum/props own)
|
||||||
|
el (mx/get-ref-dom own "workspace-canvas")
|
||||||
|
sub1 (as-> uuc/actions-s $
|
||||||
|
(rx/map :type $)
|
||||||
|
(rx/dedupe $)
|
||||||
|
(rx/filter #(= :scroll/viewport %) $)
|
||||||
|
(rx/on-value $ handle-scroll-interaction))
|
||||||
|
sub2 (subscribe-to-page-changes pageid)]
|
||||||
|
(set! (.-scrollLeft el) uuwb/canvas-start-scroll-x)
|
||||||
|
(set! (.-scrollTop el) uuwb/canvas-start-scroll-y)
|
||||||
|
(assoc own ::sub1 sub1 ::sub2 sub2))))
|
||||||
|
|
||||||
|
(defn- workspace-will-unmount
|
||||||
|
[own]
|
||||||
|
(let [sub1 (::sub1 own)
|
||||||
|
sub2 (::sub2 own)]
|
||||||
|
(.close sub1)
|
||||||
|
(.close sub2)
|
||||||
|
(dissoc own ::sub1 ::sub2)))
|
||||||
|
|
||||||
|
(defn- workspace-transfer-state
|
||||||
|
[old-state state]
|
||||||
|
(let [[projectid pageid] (:rum/props state)
|
||||||
|
[oldprojectid oldpageid] (:rum/props old-state)]
|
||||||
|
(if (not= pageid oldpageid)
|
||||||
|
(do
|
||||||
|
(rs/emit! (dw/initialize projectid pageid))
|
||||||
|
(.close (::sub2 old-state))
|
||||||
|
(assoc state
|
||||||
|
::sub1 (::sub1 old-state)
|
||||||
|
::sub2 (subscribe-to-page-changes pageid)))
|
||||||
|
(assoc state
|
||||||
|
::sub1 (::sub1 old-state)
|
||||||
|
::sub2 (::sub2 old-state)))))
|
||||||
|
|
||||||
(defn- on-scroll
|
(defn- on-scroll
|
||||||
[event]
|
[event]
|
||||||
(let [target (.-target event)
|
(let [target (.-target event)
|
||||||
|
@ -66,62 +155,6 @@
|
||||||
(right-sidebar))
|
(right-sidebar))
|
||||||
]])))
|
]])))
|
||||||
|
|
||||||
(defn- workspace-will-mount
|
|
||||||
[own]
|
|
||||||
(let [[projectid pageid] (:rum/props own)]
|
|
||||||
(rs/emit! (dw/initialize projectid pageid)
|
|
||||||
(dp/fetch-projects)
|
|
||||||
(udp/fetch-pages projectid))
|
|
||||||
own))
|
|
||||||
|
|
||||||
(defn- workspace-did-mount
|
|
||||||
[own]
|
|
||||||
(letfn [(handle-scroll-interaction []
|
|
||||||
(let [stoper (->> uuc/actions-s
|
|
||||||
(rx/map :type)
|
|
||||||
(rx/filter #(not= % :scroll/viewport))
|
|
||||||
(rx/take 1))
|
|
||||||
local (:rum/local own)
|
|
||||||
initial @uuwb/mouse-viewport-a]
|
|
||||||
(swap! local assoc :scrolling true)
|
|
||||||
(as-> uuwb/mouse-viewport-s $
|
|
||||||
(rx/take-until stoper $)
|
|
||||||
(rx/subscribe $ #(on-scroll % initial) nil on-scroll-end))))
|
|
||||||
|
|
||||||
(on-scroll-end []
|
|
||||||
(let [local (:rum/local own)]
|
|
||||||
(swap! local assoc :scrolling false)))
|
|
||||||
|
|
||||||
(on-scroll [pt initial]
|
|
||||||
(let [{:keys [x y]} (gpt/subtract pt initial)
|
|
||||||
el (mx/get-ref-dom own "workspace-canvas")
|
|
||||||
cx (.-scrollLeft el)
|
|
||||||
cy (.-scrollTop el)]
|
|
||||||
(set! (.-scrollLeft el) (- cx x))
|
|
||||||
(set! (.-scrollTop el) (- cy y))))]
|
|
||||||
|
|
||||||
(let [el (mx/get-ref-dom own "workspace-canvas")
|
|
||||||
sub (as-> uuc/actions-s $
|
|
||||||
(rx/map :type $)
|
|
||||||
(rx/dedupe $)
|
|
||||||
(rx/filter #(= :scroll/viewport %) $)
|
|
||||||
(rx/on-value $ handle-scroll-interaction))]
|
|
||||||
(set! (.-scrollLeft el) uuwb/canvas-start-scroll-x)
|
|
||||||
(set! (.-scrollTop el) uuwb/canvas-start-scroll-y)
|
|
||||||
(assoc own ::sub sub))))
|
|
||||||
|
|
||||||
(defn- workspace-will-unmount
|
|
||||||
[own]
|
|
||||||
(let [unsub (::sub own)]
|
|
||||||
(unsub)
|
|
||||||
(dissoc own ::sub)))
|
|
||||||
|
|
||||||
(defn- workspace-transfer-state
|
|
||||||
[old-state state]
|
|
||||||
(let [[projectid pageid] (:rum/props state)]
|
|
||||||
(rs/emit! (dw/initialize projectid pageid))
|
|
||||||
(assoc state ::sub (::sub old-state))))
|
|
||||||
|
|
||||||
(def ^:static workspace
|
(def ^:static workspace
|
||||||
(mx/component
|
(mx/component
|
||||||
{:render workspace-render
|
{:render workspace-render
|
||||||
|
|
Loading…
Add table
Reference in a new issue