mirror of
https://github.com/penpot/penpot.git
synced 2025-06-07 07:31:38 +02:00
♻️ Refactor html5 history.
This commit is contained in:
parent
d856b9aae3
commit
7fe7c3da6c
9 changed files with 114 additions and 128 deletions
|
@ -7,16 +7,19 @@
|
|||
(ns uxbox.util.router
|
||||
(:refer-clojure :exclude [resolve])
|
||||
(:require
|
||||
[beicon.core :as rx]
|
||||
[rumext.alpha :as mf]
|
||||
[reitit.core :as r]
|
||||
[goog.events :as e]
|
||||
[cuerdas.core :as str]
|
||||
[potok.core :as ptk]
|
||||
[uxbox.common.data :as d]
|
||||
[uxbox.util.html.history :as html-history])
|
||||
[uxbox.util.browser-history :as bhistory]
|
||||
[uxbox.common.data :as d])
|
||||
(:import
|
||||
goog.Uri
|
||||
goog.Uri.QueryData))
|
||||
|
||||
;; --- API
|
||||
;; --- Router API
|
||||
|
||||
(defn- parse-query-data
|
||||
[^QueryData qdata]
|
||||
|
@ -50,10 +53,17 @@
|
|||
(.setQueryData uri qdt)
|
||||
(.toString uri))))))
|
||||
|
||||
(defn init
|
||||
(defn create
|
||||
[routes]
|
||||
(r/router routes))
|
||||
|
||||
(defn initialize-router
|
||||
[routes]
|
||||
(ptk/reify ::initialize-router
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(assoc state :router (create routes)))))
|
||||
|
||||
(defn query-params
|
||||
"Given goog.Uri, read query parameters into Clojure map."
|
||||
[^goog.Uri uri]
|
||||
|
@ -63,13 +73,6 @@
|
|||
(map (juxt keyword #(.get q %)))
|
||||
(into {}))))
|
||||
|
||||
(defn navigate!
|
||||
([router id] (navigate! router id {} {}))
|
||||
([router id params] (navigate! router id params {}))
|
||||
([router id params qparams]
|
||||
(-> (resolve router id params qparams)
|
||||
(html-history/set-path!))))
|
||||
|
||||
(defn match
|
||||
"Given routing tree and current path, return match with possibly
|
||||
coerced parameters. Return nil if no match found."
|
||||
|
@ -87,8 +90,10 @@
|
|||
(deftype Navigate [id params qparams]
|
||||
ptk/EffectEvent
|
||||
(effect [_ state stream]
|
||||
(let [router (:router state)]
|
||||
(navigate! router id params qparams))))
|
||||
(let [router (:router state)
|
||||
history (:history state)
|
||||
path (resolve router id params qparams)]
|
||||
(bhistory/set-token! history path))))
|
||||
|
||||
(defn nav
|
||||
([id] (nav id nil nil))
|
||||
|
@ -99,3 +104,30 @@
|
|||
|
||||
(def navigate nav)
|
||||
|
||||
;; --- History API
|
||||
|
||||
(defn initialize-history
|
||||
[on-change]
|
||||
(ptk/reify ::initialize-history
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(let [history (bhistory/create)]
|
||||
(assoc state :history history)))
|
||||
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
(let [stoper (rx/filter (ptk/type? ::initialize-history) stream)
|
||||
history (:history state)
|
||||
router (:router state)]
|
||||
(->> (rx/create (fn [sink]
|
||||
(let [key (e/listen history "navigate" #(sink (.-token %)))]
|
||||
(bhistory/enable! history)
|
||||
(fn []
|
||||
(bhistory/disable! history)
|
||||
(e/unlistenByKey key)))))
|
||||
(rx/map #(on-change router %))
|
||||
(rx/take-until stoper))))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue