-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
diff.cljs
126 lines (115 loc) · 3.94 KB
/
diff.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
(ns portal.ui.viewer.diff
(:require [clojure.spec.alpha :as s]
[lambdaisland.deep-diff2.diff-impl :as diff]
[portal.colors :as c]
[portal.runtime.cson :as cson]
[portal.ui.commands :as commands]
[portal.ui.icons :as icons]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]))
(extend-protocol cson/ToJson
diff/Deletion
(-to-json [this buffer] (cson/tag buffer "diff/Deletion" (:- this)))
diff/Insertion
(-to-json [this buffer] (cson/tag buffer "diff/Insertion" (:+ this)))
diff/Mismatch
(-to-json [this buffer] (cson/tag buffer "diff/Mismatch" ((juxt :- :+) this))))
(defn ^:no-doc ->diff [op value]
(case op
"diff/Deletion" (diff/Deletion. value)
"diff/Insertion" (diff/Insertion. value)
"diff/Mismatch" (let [[a b] value]
(diff/Mismatch. a b))
nil))
;;; :spec
(defn diff? [value]
(or
(instance? diff/Deletion value)
(instance? diff/Insertion value)
(instance? diff/Mismatch value)))
(s/def ::diff (s/or :diff diff?
:seq (s/coll-of any? :min-count 2)))
;;;
(defn- can-view? [value]
(s/valid? ::diff value))
(defn- test-actual [value]
(or (and (= (first value) 'not)
(coll? (second value))
(= (first (second value)) '=)
(rest (second value)))
value))
(defn inspect-diff [value]
(let [theme (theme/use-theme)
bg (ins/get-background)]
(cond
(instance? diff/Deletion value) [ins/inspector (:- value)]
(instance? diff/Insertion value) [ins/inspector (:+ value)]
(instance? diff/Mismatch value) [ins/inspector value]
:else
[d/div
{:style
{:background (ins/get-background)}}
[d/div
{:style
{:display :flex
:justify-content :space-between}}
[d/div
{:style
{:flex "1"
:padding (:padding theme)
:background (::c/diff-remove theme)
:border-top-left-radius (:border-radius theme)}}
[icons/minus-circle {:style {:color bg}}]]
[d/div
{:style
{:padding (:padding theme)
:color (::c/border theme)
:border [1 :solid (::c/border theme)]}}
[icons/exchange-alt]]
[d/div
{:style
{:flex "1"
:padding (:padding theme)
:text-align :right
:background (::c/diff-add theme)
:border-top-right-radius (:border-radius theme)}}
[icons/plus-circle {:style {:color bg}}]]]
[d/div
{:style
{:display :flex
:flex-direction :column
:gap (:padding theme)
:padding (:padding theme)
:border-left [1 :solid (::c/border theme)]
:border-right [1 :solid (::c/border theme)]
:border-bottom [1 :solid (::c/border theme)]
:border-bottom-left-radius (:border-radius theme)
:border-bottom-right-radius (:border-radius theme)}}
[ins/dec-depth
(->> (test-actual value)
(partition 2 1)
(map-indexed
(fn [idx [a b]]
^{:key idx}
[ins/with-key
idx
[ins/with-collection
[a b]
[select/with-position
{:row idx :column 0}
[ins/inspector (diff/diff a b)]]]])))]]])))
(def viewer
{:predicate can-view?
:component inspect-diff
:name :portal.viewer/diff
:doc "Diff a collection of values successively starting with the first two."})
(let [var #'diff/diff
name (#'commands/var->name var)]
(swap! commands/registry
assoc name (commands/make-command
(merge (meta var)
{:predicate (fn [& args] (= 2 (count args)))
:f var
:name name}))))