-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.cirru
307 lines (306 loc) · 13.5 KB
/
compact.cirru
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
:modules $ [] |respo.calcit/ |lilac/ |memof/ |respo-ui.calcit/ |respo-markdown.calcit/ |reel.calcit/
:entries $ {}
:files $ {}
|app.comp.container $ %{} :FileEntry
:defs $ {}
|comp-checked $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-checked (checked? text handler)
div
{} (:class-name css/row-center)
:style $ {} (:cursor :pointer)
:on-click handler
input $ {} (:type "\"checkbox") (:checked checked?)
:style $ {} (:cursor :pointer)
<> text css/font-fancy
|comp-container $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-container (reel)
let
store $ :store reel
states $ :states store
sorted? $ :sorted? store
show-result? $ :show-result? store
by-word? $ w-js-log (:by-word? store)
differ $ if by-word? diff/diffWords diff/diffLines
changes $ tagging-data
to-calcit-data $ if sorted?
differ
sort-by-line $ :old-text store
sort-by-line $ :new-text store
differ (:old-text store) (:new-text store)
div
{} $ :class-name (str-spaced css/global css/fullscreen css/column)
comp-toolbar show-result? sorted? by-word?
div
{}
:class-name $ str-spaced css/flex css/row
:style $ {} (:overflow :auto)
if show-result? (comp-diff-view changes by-word?)
div
{} $ :class-name (str-spaced css/expand css/row css/flex)
textarea $ {}
:class-name $ str-spaced css/textarea css/expand style-text
:value $ :old-text store
:placeholder "\"Old text"
:on-input $ fn (e d!)
d! $ :: :write-old (:value e)
:spell-check false
:autofocus true
comp-divider
textarea $ {}
:class-name $ str-spaced css/textarea css/expand style-text
:value $ :new-text store
:placeholder "\"New text"
:on-input $ fn (e d!)
d! $ :: :write-new (:value e)
:spellcheck false
when dev? $ comp-reel (>> states :reel) reel ({})
when dev? $ comp-inspect "\"Store" store
{} $ :bottom 0
|comp-diff-view $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-diff-view (changes by-word?)
list->
{} (:class-name css/flex)
:style $ {} (:padding-bottom 80) (:overflow :auto)
:line-height $ if by-word? "\"20px"
-> changes $ map-indexed
fn (idx chunk)
[] idx $ let
tok $ :value chunk
cond
:removed chunk
div $ {} (:inner-text tok)
:class-name $ str-spaced style-line style-removed (if by-word? style-word-mode)
:title $ str "\"Removed " (:count chunk) "\" chunks"
(:added chunk)
div $ {} (:inner-text tok)
:class-name $ str-spaced style-line style-added (if by-word? style-word-mode)
:title $ str "\"Added " (:count chunk) "\" chunks"
true $ div
{} (:inner-text tok)
:class-name $ str-spaced style-line style-no-change (if by-word? style-word-mode)
; :title $ str (:count chunk) "\" chunks reversed"
|comp-divider $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-divider () $ div
{} $ :style
{} (:width 1)
:background-color $ hsl 0 0 94
|comp-toolbar $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-toolbar (show-result? sorted? by-word?)
div
{} $ :class-name (str-spaced css/row-parted style-toolbar)
<> "\"Diff View" $ {}
:color $ hsl 0 0 40
:font-family ui/font-fancy
div
{} $ :class-name (merge css/row-center)
comp-checked show-result? "\"Result?(⌘ e)" $ fn (e d!)
d! $ :: :toggle-result
=< 16 nil
comp-checked sorted? "\"Sorted" $ fn (e d!)
d! $ :: :toggle-sorted
=< 16 nil
comp-checked by-word? "\"ByWord" $ fn (e d!)
d! $ :: :toggle-word
=< 16 nil
a $ {} (:class-name css/link) (:inner-text "\"Swap") (:title "\"⌘ i")
:on-click $ fn (e d!)
d! $ :: :swap-text
=< 16 nil
a $ {} (:class-name css/link) (:inner-text "\"Clear") (:title "\"⌘ k")
:on-click $ fn (e d!)
d! $ :: :clear-text
|sort-by-line $ %{} :CodeEntry (:doc |)
:code $ quote
defn sort-by-line (text)
-> text (.split-lines) (.sort-by identity) (.join-str &newline)
|style-added $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-added $ {}
"\"&" $ {}
:background-color $ hsl 200 100 92
|style-line $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-line $ {}
"\"&" $ {} (:line-height "\"24px") (:font-size 12) (:font-family ui/font-code) (:margin 0) (:padding "\"0 8px") (:white-space :pre) (:overflow-x :auto)
|style-no-change $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-no-change $ {}
"\"&" $ {}
:color $ hsl 0 0 80
:line-height "\"15px"
|style-removed $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-removed $ {}
"\"&" $ {}
:background-color $ hsl 0 100 78
:color :white
|style-text $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-text $ {}
"\"&" $ {} (:font-family ui/font-code) (:line-height "\"20px") (:font-size 12) (:white-space :pre) (:overflow :auto) (:border :none) (:padding "\"8px 8px 80px 8px") (:resize :none)
|style-toolbar $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-toolbar $ {}
"\"&" $ {}
:border-bottom $ str "\"1px solid " (hsl 0 0 90)
:line-height "\"32px"
:padding "\"0 8px"
|style-word-mode $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-word-mode $ {}
"\"&" $ {} (:display :inline) (:white-space :pre-wrap)
|tagging-data $ %{} :CodeEntry (:doc |)
:code $ quote
defn tagging-data (xs)
if (map? xs)
map-kv xs $ fn (k v)
[] (turn-tag k) v
if (list? xs) (map xs tagging-data) xs
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.comp.container $ :require
respo-ui.core :refer $ hsl
respo.css :refer $ defstyle
respo-ui.css :as css
respo-ui.core :as ui
respo.core :refer $ defcomp >> list-> <> div button textarea pre span input a
respo.comp.space :refer $ =<
respo.comp.inspect :refer $ comp-inspect
reel.comp.reel :refer $ comp-reel
respo-md.comp.md :refer $ comp-md
app.config :refer $ dev?
"\"diff" :as diff
|app.config $ %{} :FileEntry
:defs $ {}
|dev? $ %{} :CodeEntry (:doc |)
:code $ quote
def dev? $ = "\"dev" (get-env "\"mode")
|site $ %{} :CodeEntry (:doc |)
:code $ quote
def site $ {} (:title "\"Diffview") (:storage-key "\"diffview")
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.config)
|app.main $ %{} :FileEntry
:defs $ {}
|*reel $ %{} :CodeEntry (:doc |)
:code $ quote
defatom *reel $ -> reel-schema/reel (assoc :base schema/store) (assoc :store schema/store)
|dispatch! $ %{} :CodeEntry (:doc |)
:code $ quote
defn dispatch! (op)
when config/dev? $ js/console.log "\"Dispatch:" op
reset! *reel $ reel-updater updater @*reel op
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! ()
if config/dev? $ load-console-formatter!
println "\"Running mode:" $ if config/dev? "\"dev" "\"release"
render-app!
add-watch *reel :changes $ fn (r p) (render-app!)
listen-devtools! |a dispatch!
js/window.addEventListener |beforeunload persist-storage!
repeat! 60 persist-storage!
let
raw $ js/localStorage.getItem (:storage-key config/site)
when (some? raw)
dispatch! $ :: :hydrate-storage (parse-cirru-edn raw)
js/window.addEventListener "\"keydown" $ fn (event)
cond
and (.-metaKey event)
= "\"e" $ .-key event
dispatch! $ :: :toggle-result
(and (.-metaKey event) (= "\"k" (.-key event)))
dispatch! $ :: :clear-text
(and (.-metaKey event) (= "\"i" (.-key event)))
dispatch! $ :: :swap-text
println "|App started."
|mount-target $ %{} :CodeEntry (:doc |)
:code $ quote
def mount-target $ js/document.querySelector |.app
|persist-storage! $ %{} :CodeEntry (:doc |)
:code $ quote
defn persist-storage! (? e)
js/localStorage.setItem (:storage-key config/site)
format-cirru-edn $ :store @*reel
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! () $ if (nil? build-errors)
do (remove-watch *reel :changes) (clear-cache!)
add-watch *reel :changes $ fn (reel prev) (render-app!)
reset! *reel $ refresh-reel @*reel schema/store updater
hud! "\"ok~" "\"Ok"
hud! "\"error" build-errors
|render-app! $ %{} :CodeEntry (:doc |)
:code $ quote
defn render-app! () $ render! mount-target (comp-container @*reel) dispatch!
|repeat! $ %{} :CodeEntry (:doc |)
:code $ quote
defn repeat! (duration cb)
js/setTimeout
fn () (cb)
repeat! (* 1000 duration) cb
* 1000 duration
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.main $ :require
respo.core :refer $ render! clear-cache! realize-ssr!
app.comp.container :refer $ comp-container
app.updater :refer $ updater
app.schema :as schema
reel.util :refer $ listen-devtools!
reel.core :refer $ reel-updater refresh-reel
reel.schema :as reel-schema
cljs.reader :refer $ read-string
app.config :as config
"\"./calcit.build-errors" :default build-errors
"\"bottom-tip" :default hud!
|app.schema $ %{} :FileEntry
:defs $ {}
|store $ %{} :CodeEntry (:doc |)
:code $ quote
def store $ {}
:states $ {}
:page :editor
:sorted? false
:show-result? false
:by-word? false
:old-text "\""
:new-text "\""
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.schema)
|app.updater $ %{} :FileEntry
:defs $ {}
|updater $ %{} :CodeEntry (:doc |)
:code $ quote
defn updater (store op op-id op-time)
tag-match op
:states cursor s
update-states store cursor s
(:write-old d) (assoc store :old-text d)
(:write-new d) (assoc store :new-text d)
(:hydrate-storage d) d
(:toggle-sorted) (update store :sorted? not)
(:toggle-result) (update store :show-result? not)
(:toggle-word)
if
nil? $ :by-word? store
assoc store :by-word? true
update store :by-word? not
(:clear-text)
-> store (assoc :old-text "\"") (assoc :new-text "\"") (assoc :show-result? false)
(:swap-text)
-> store
assoc :old-text $ :new-text store
assoc :new-text $ :old-text store
_ $ do (eprintln "\"Unkown op:" op) store
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.updater $ :require
respo.cursor :refer $ update-states