This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
/
events.cljs
67 lines (58 loc) · 2.15 KB
/
events.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns tincture.events
(:import [goog.dom ViewportSizeMonitor])
(:require [reagent.core :as r]
[goog.events.EventType :as event-type]
[goog.dom :as dom]
[goog.events :as gevents]
[tincture.async :as async]
[tincture.grid :as grid]
[reagent.debug :as d]
[re-frame.core :refer [reg-event-db reg-event-fx reg-fx inject-cofx path trim-v
after debug dispatch]]
[clojure.spec.alpha :as s]))
(defonce ^:private vsm (ViewportSizeMonitor.))
(def ^:private default-db
{:tincture/viewport-size nil
:tincture/font {:font/family nil
:font/url nil}})
(defn- on-resize [e]
(let [size (.getSize vsm)]
(dispatch [:tincture/set-viewport-size [(.-width size) (.-height size)]])))
(defonce ^:private vsm-listener
(gevents/listen vsm event-type/RESIZE
(async/debounce
on-resize
200)))
(reg-event-fx
:tincture/initialize
(fn [{:keys [db]} [_ {:keys [font-family font-url]}]]
(let [size (.getSize vsm)]
(cond-> {:db (-> db
(merge default-db)
(assoc :tincture/viewport-size [(.-width size) (.-height size)]))}
font-family (assoc-in [:db :tincture/font :font/family] font-family)
font-url (assoc-in [:db :tincture/font :font/url] font-url)
font-url (assoc :tincture.font/attach font-url)))))
(reg-event-db
:tincture/set-viewport-size
(fn [db [_ new]]
(assoc db :tincture/viewport-size new)))
#_(reg-event-fx
:tincture/set-font
(fn [{:keys [db]} [_ font-family font-url]]
{:db (-> (assoc-in db [:tincture/font :font/family] font-family)
(assoc-in [:tincture/font :font/url] font-url))
:tincture.font/attach font-url}))
(defn attach-font! [font-url]
(let [el (dom/getElement "tincture-font")
head (.-head js/document)]
(when (not el)
(let [el (.createElement js/document "link")]
(set! (.-id el) "tincture-font")
(.setAttribute el "href" font-url)
(.setAttribute el "rel" "stylesheet")
(.appendChild head el)))))
(reg-fx
:tincture.font/attach
(fn [url]
(attach-font! url)))