mirror of https://github.com/status-im/reagent.git
Simplify link handling
This commit is contained in:
parent
ebade509bf
commit
f5c80e820e
|
@ -2,15 +2,14 @@
|
||||||
(:require [reagent.core :as reagent :refer [atom]]
|
(:require [reagent.core :as reagent :refer [atom]]
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[reagentdemo.common :as common
|
[reagentdemo.common :as common
|
||||||
:refer [demo-component page link reverse-page-map prefix]]
|
:refer [demo-component page link page-map prefix]]
|
||||||
[reagentdemo.intro :as intro]
|
[reagentdemo.intro :as intro]
|
||||||
[reagentdemo.news :as news]
|
[reagentdemo.news :as news]
|
||||||
[reagent.debug :refer-macros [dbg println]]))
|
[reagent.debug :refer-macros [dbg println]]))
|
||||||
|
|
||||||
(common/set-page-map {:index ["index.html" [intro/main]]
|
(swap! page-map assoc
|
||||||
:news ["news/index.html" [news/main]]
|
"index.html" intro/main
|
||||||
:undo-demo ["news/cloact-reagent-undo-demo.html"
|
"news/index.html" news/main)
|
||||||
[news/main]]})
|
|
||||||
|
|
||||||
(defn github-badge []
|
(defn github-badge []
|
||||||
[:a.github-badge
|
[:a.github-badge
|
||||||
|
@ -23,13 +22,11 @@
|
||||||
[:div
|
[:div
|
||||||
[:div.nav
|
[:div.nav
|
||||||
[:ul.nav
|
[:ul.nav
|
||||||
[:li.brand [link {:href :index} "Reagent:"]]
|
[:li.brand [link {:href intro/main} "Reagent:"]]
|
||||||
[:li [link {:href :index} "Introduction"]]
|
[:li [link {:href intro/main} "Introduction"]]
|
||||||
[:li [link {:href :news} "News"]]]]
|
[:li [link {:href news/main} "News"]]]]
|
||||||
(let [p @page
|
(let [comp (get @page-map @page intro/main)]
|
||||||
[_ comp] (get @reverse-page-map p
|
[comp])
|
||||||
(:index @common/page-map))]
|
|
||||||
comp)
|
|
||||||
[github-badge]])
|
[github-badge]])
|
||||||
|
|
||||||
(defn ^:export mountdemo [p]
|
(defn ^:export mountdemo [p]
|
||||||
|
@ -38,6 +35,7 @@
|
||||||
|
|
||||||
(defn gen-page [p timestamp]
|
(defn gen-page [p timestamp]
|
||||||
(reset! page p)
|
(reset! page p)
|
||||||
|
(dbg @page)
|
||||||
(let [body (reagent/render-component-to-string [demo])
|
(let [body (reagent/render-component-to-string [demo])
|
||||||
title @common/title-atom]
|
title @common/title-atom]
|
||||||
(str "<!doctype html>
|
(str "<!doctype html>
|
||||||
|
@ -60,7 +58,7 @@
|
||||||
|
|
||||||
(defn ^:export genpages []
|
(defn ^:export genpages []
|
||||||
(let [timestamp (str "?" (.now js/Date))]
|
(let [timestamp (str "?" (.now js/Date))]
|
||||||
(->> (keys @reverse-page-map)
|
(->> (keys @page-map)
|
||||||
(map #(vector % (gen-page % timestamp)))
|
(map #(vector % (gen-page % timestamp)))
|
||||||
(into {})
|
(into {})
|
||||||
clj->js)))
|
clj->js)))
|
||||||
|
|
|
@ -13,10 +13,11 @@
|
||||||
(def page-map (atom nil))
|
(def page-map (atom nil))
|
||||||
(def reverse-page-map (atom nil))
|
(def reverse-page-map (atom nil))
|
||||||
|
|
||||||
(defn set-page-map [m]
|
(add-watch page-map ::page-map-watch
|
||||||
(reset! page-map m)
|
(fn [_ _ _ new-map]
|
||||||
(reset! reverse-page-map (into {} (for [[k [p c]] m]
|
(reset! reverse-page-map
|
||||||
[p [k c]]))))
|
(into {} (for [[k v] new-map]
|
||||||
|
[v k])))))
|
||||||
|
|
||||||
(defn prefix [href]
|
(defn prefix [href]
|
||||||
(let [depth (-> #"/" (re-seq @page) count)
|
(let [depth (-> #"/" (re-seq @page) count)
|
||||||
|
@ -24,9 +25,9 @@
|
||||||
(str pref href)))
|
(str pref href)))
|
||||||
|
|
||||||
(defn link [props children]
|
(defn link [props children]
|
||||||
(let [pm @page-map
|
(let [pm @reverse-page-map
|
||||||
href (-> props :href pm first)]
|
href (-> props :href pm)]
|
||||||
(assert href)
|
(assert (string? href))
|
||||||
(apply vector :a (assoc props
|
(apply vector :a (assoc props
|
||||||
:href (prefix href)
|
:href (prefix href)
|
||||||
:on-click (if rpage/history
|
:on-click (if rpage/history
|
||||||
|
|
|
@ -43,9 +43,15 @@
|
||||||
(reset! undo-list nil)
|
(reset! undo-list nil)
|
||||||
(remove-watch state ::undo-watcher))}))
|
(remove-watch state ::undo-watcher))}))
|
||||||
|
|
||||||
(defn main []
|
(defn undo-example []
|
||||||
(let [head "This should become news"]
|
(let [head "This should become news"]
|
||||||
[:div.reagent-demo
|
[:div.reagent-demo
|
||||||
[:h1 [link {:href :undo-demo} head]]
|
[:h1 [link {:href undo-example} head]]
|
||||||
[title head]
|
[title head]
|
||||||
[undo-demo-cleanup]]))
|
[undo-demo-cleanup]]))
|
||||||
|
|
||||||
|
(defn main []
|
||||||
|
[undo-example])
|
||||||
|
|
||||||
|
(swap! common/page-map assoc
|
||||||
|
"news/cloact-reagent-undo-demo.html" undo-example)
|
||||||
|
|
Loading…
Reference in New Issue