/
hud.cljs
292 lines (258 loc) · 9.15 KB
/
hud.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
(ns shadow.cljs.devtools.client.hud
(:require
[shadow.dom :as dom]
[shadow.xhr :as xhr]
[shadow.animate :as anim]
[shadow.cljs.devtools.client.env :as env]
[cljs.core.async :as async :refer (go)]
[goog.string.format]
[goog.string :refer (format)]
[clojure.string :as str]))
(defn open-file [file line column]
(js/console.log "opening file" file line column)
(let [req
(xhr/chan :POST
(str (env/get-url-base) "/api/open-file")
{:file file
:line line
:column column}
{:with-credentials false
:body-only true})]
(go (when-some [{:keys [exit] :as result} (<! req)]
(when-not (zero? exit)
(js/console.warn "file open failed" result))
))))
(defn dom-insert
([node]
(dom-insert js/document.body node))
([where node]
(let [el (dom/dom-node node)
id (.-id el)]
(assert (seq id))
(when-some [x (dom/by-id id)]
(dom/remove x))
(dom/append where el)
)))
(def hud-id "shadow-hud-container")
(def load-id "shadow-hud-loading-container")
(def logo-svg
(delay
(let [s-path
"M247.183941,141.416413 C247.183941,74.7839971 148.383423,78.9723529 148.383423,141.416413 C148.383423,203.860473 265.090698,171.864644 265.090698,248.900057 C265.090698,325.93547 135,325.851749 135,251.708304"]
(dom/svg
{:id "shadow-cljs-logo"
:version "1.1"
:viewBox "0 0 400 400"
:style {:display "block"}
:height "60px"
:width "60px"}
[:title "shadow-cljs"]
[:defs
[:mask#shadow-cljs-logo-mask {:fill "#fff"}
[:circle {:r "200" :cy "200" :cx "200"}]]]
[:g
{:fill-rule "evenodd"
:fill "none"
:stroke-width "0"
:stroke "none"
:mask "url(#shadow-cljs-logo-mask)"}
[:g.circles
[:circle.blue {:r "200" :cy "200" :cx "200" :fill "#4F80DF"}]
[:circle.light-blue {:r "71.5" :cy "200" :cx "370" :fill "#89B4FF"}]
[:circle.dark-green {:r "180" :cy "360" :cx "60" :fill "#40B400"}]
[:circle.light-green {:r "129" :cy "320" :cx "280" :fill "#76E013"}]
[:animateTransform
{:attributeType "xml"
:attributeName "transform"
:type "rotate"
:from "0 200 200"
:to "360 200 200"
:dur "3s"
:repeatCount "indefinite"}]]
;; S shadow
[:g {:transform "translate(10,10)"}
[:path
{:stroke-linecap "square"
:stroke-width "16"
:stroke "#aaa"
:d s-path}]]
;; S
[:path
{:stroke-linecap "square"
:stroke-width "16"
:stroke "#FFFFFF"
:d s-path}]
]))))
(defn load-start []
(dom-insert
[:div {:id load-id
:style {:position "fixed"
:pointer-events "none"
:left "0px"
:bottom "20px"
:z-index "10000"}}
[:div {:style {:background "#eee"
:border-top-right-radius "40px"
:border-bottom-right-radius "40px"
:box-shadow "2px 2px 10px #aaa"
:padding "10px"}}
@logo-svg]]))
(defn load-end-success []
(when-some [container-el (dom/by-id load-id)]
(let [el (.-firstChild container-el)]
(anim/start 500 {el (anim/transition :background "#eee" "#40B400" "ease-out")})
(go (<! (async/timeout 250))
(<! (anim/start 250 {el (anim/transition :opacity "1" "0" "ease-in")}))
(dom/remove container-el)
))))
(defn load-end []
(when-some [el (dom/by-id load-id)]
(dom/remove el)
))
(defn hud-hide []
(when-some [d (dom/by-id hud-id)]
(dom/remove d)))
(def source-line-styles
{:padding "0"
:margin "0"})
(def source-highlight-styles
(assoc source-line-styles
:font-weight "bold"))
(defn source-line-html
[start-idx lines styles]
(->> (for [[idx text] (map-indexed vector lines)]
[:pre {:style styles} (format "%4d | %s" (+ 1 idx start-idx) text)])))
(def sep-length 80)
(defn sep-line
([]
(sep-line "" 0))
([label offset]
(let [sep-len (js/Math.max sep-length offset)
len (count label)
sep
(fn [c]
(->> (repeat c "-")
(str/join "")))]
(str (sep offset) label (sep (- sep-len (+ offset len)))))))
(defn file-link [{:keys [resource-name file line column] :as warning}]
(if-not file
[:span resource-name]
[:span {:style {:text-decoration "underline"
:color "blue"
:cursor "pointer"}
:on {:click (fn [e]
(dom/ev-stop e)
(open-file file line column)
)}}
resource-name]))
(defn html-for-warning [{:keys [resource-name msg file line column source-excerpt] :as warning}]
[:div {:style {:border "2px solid #ccc"
:margin-bottom "10px"}}
[:div {:style {:line-height "16px"
:background-color "#fadb64"
:padding "10px"
:font-size "1.2em"
:font-weight "bold"}}
[:span "WARNING in "]
(file-link warning)]
(when source-excerpt
(let [{:keys [start-idx before line after]} source-excerpt]
[:div {:style {:padding "10px 10px"
:background-color "#fff"
:border-top "2px solid #ccc"}}
(source-line-html start-idx before source-line-styles)
(source-line-html (+ start-idx (count before)) [line] source-highlight-styles)
(let [arrow-idx (+ 6 (or column 1))]
[:pre {:style source-highlight-styles} (sep-line "^" arrow-idx)])
[:div {:style {:font-weight "bold" :font-size "1.2em" :padding "10px 0"}} msg]
[:pre {:style source-highlight-styles} (sep-line)]
(source-line-html (+ start-idx (count before) 1) after source-line-styles)]
))])
(defn hud-warnings [{:keys [type info] :as msg}]
(let [{:keys [sources]}
info
sources-with-warnings
(->> sources
(remove :from-jar)
(filter #(seq (:warnings %)))
(into []))]
(when (seq sources-with-warnings)
;; TODO: fancy transition from logo to warnings
(load-end)
(dom-insert
[:div
{:id hud-id
:style {:position "fixed"
:z-index "10000"
:left "0px"
:bottom "0px"
:right "0px"
:display "flex"
:flex-direction "column"
:font-family "monospace"
:font-size "12px"}}
[:div {:style {:align-self "flex-end"
:background "#fff"
:border "2px solid #ccc"
:border-bottom "0px"
:cursor "pointer"
:padding "6px"
:margin-bottom "-2px"
:z-index "10000"}
:on {:click hud-hide}}
"Close"]
[:div {:style {:background "#fff"
:border-top "2px solid #ccc"
:flex "1"
:max-height "300px"
:padding "10px 10px 0"
:overflow "auto"}}
(for [{:keys [warnings]} sources-with-warnings
warning warnings]
(html-for-warning warning))]]))))
(defn hud-error [{:keys [report] :as msg}]
(dom-insert
[:div
{:id hud-id
:style {:position "fixed"
:left "0px"
:top "0px"
:bottom "0px"
:right "0px"
:color "#000"
:background-color "#fff"
:border "5px solid red"
:z-index "10000"
:padding "20px"
:overflow "auto"
:font-family "monospace"
:font-size "12px"}}
[:div {:style "color: red; margin-bottom: 10px; font-size: 2em;"} "Compilation failed!"]
[:pre report]]))
(def connection-error-id "shadow-connection-error")
(defn connection-error-clear! []
(when-some [x (dom/by-id connection-error-id)]
(dom/remove x)))
(defn connection-error [msg]
(dom-insert
[:div {:id connection-error-id
:style {:position "fixed"
:pointer-events "none"
:left "0px"
:bottom "20px"
:z-index "10000"}}
[:div {:style {:background "#c00"
:border-top-right-radius "40px"
:border-bottom-right-radius "40px"
:box-shadow "2px 2px 10px #aaa"
:padding "10px"
:font-family "monospace"
:font-size "14px"
:font-weight "bold"
:color "#fff"}}
(str "shadow-cljs - " msg)
]]))
(defn load-failure [error task remaining]
(load-end)
(js/console.error "reload-failed" error)
(connection-error (str "[reload failed] " (.-message error))))