/
tabops.cljs
134 lines (104 loc) · 3.61 KB
/
tabops.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
(ns aerial.saite.tabops
(:require
[cljs.core.async
:as async
:refer (<! >! put! chan)
:refer-macros [go go-loop]]
[clojure.string :as cljstr]
[aerial.hanami.core
:as hmi
:refer [printchan user-msg
re-com-xref xform-recom
default-header-fn start
update-adb get-adb
get-vspec update-vspecs
get-tab-field add-tab update-tab-field
add-to-tab-body remove-from-tab-body
active-tabs
md vgl app-stop]]
[aerial.hanami.common
:as hc
:refer [RMV]]
[aerial.saite.savrest
:refer [update-ddb get-ddb]]
[com.rpl.specter :as sp]
[reagent.core :as rgt]
))
(defn insert-val [x v &{:keys [at pos] :or {pos :before}}]
(let [index (sp/select-one [sp/INDEXED-VALS #(-> % second (= at)) sp/FIRST] v)
index (if (= pos :before) index (inc index))]
(sp/setval (sp/before-index index) x v)))
;;;com.rpl.specter.zipper
;;;
;;; (setval [z/VECTOR-ZIP (z/find-first #(= "b" %)) z/INNER-LEFT]
;;; [77] ["a" "b" "c"])
;;; => ["a" 77 "b" "c"]
(def undo-redo-stacks (atom {:undo [] :redo []}))
(defn get-undo [] (@undo-redo-stacks :undo))
(defn get-redo [] (@undo-redo-stacks :redo))
(defn push-undo [x]
(swap! undo-redo-stacks (fn[m] (assoc m :undo (conj (m :undo) x)))))
(defn pop-undo []
(let [x (peek (@undo-redo-stacks :undo))]
(when x
(swap! undo-redo-stacks (fn[m] (assoc m :undo (pop (m :undo))))))
x))
(defn push-redo [x]
(swap! undo-redo-stacks (fn[m] (assoc m :redo (conj (m :redo) x)))))
(defn pop-redo []
(let [x (peek (@undo-redo-stacks :redo))]
(when x
(swap! undo-redo-stacks (fn[m] (assoc m :redo (pop (m :redo))))))
x))
(defn undo []
(let [x (pop-undo)]
(if x
(let [cur (sp/select-one [sp/ATOM :tabs :active sp/ATOM] hmi/app-db)]
(sp/setval [sp/ATOM :tabs :active sp/ATOM] x hmi/app-db)
(hmi/set-cur-tab (-> x last :id))
(push-redo cur))
(js/alert "Nothing more to undo"))))
(defn redo []
(let [x (pop-redo)]
(if x
(let [cur (sp/select-one [sp/ATOM :tabs :active sp/ATOM] hmi/app-db)]
(sp/setval [sp/ATOM :tabs :active sp/ATOM] x hmi/app-db)
(hmi/set-cur-tab (-> x last :id))
(push-undo cur))
(js/alert "Nothing more to redo"))))
(defn get-tab-frames []
(->> (hmi/get-cur-tab :specs)
(mapv #(-> % :usermeta :frame :fid))
(filter identity)
(mapv #(do {:id % :label (-> % name cljstr/capitalize)}))))
(defn remove-frame [fid]
(when fid
(let [tid (hmi/get-cur-tab :id)
x (sp/select-one [sp/ATOM :tabs :active sp/ATOM] hmi/app-db)
eids (keep (fn[[k v]] (when (= fid (-> v :opts :fid)) k))
(get-ddb [:editors tid]))]
(push-undo x)
(doseq [eid eids]
(update-ddb [:editors tid eid] :rm)
(update-ddb [:tabs :cms tid eid] :rm))
(hmi/remove-from-tab-body tid fid))))
(defn add-frame [picframe at pos]
(let [tid (hmi/get-cur-tab :id)
x (sp/select-one [sp/ATOM :tabs :active sp/ATOM] hmi/app-db)]
(push-undo x)
(hmi/add-to-tab-body
tid picframe
:at at :position pos)))
(defn update-frame [element specinfo]
(let [tid (hmi/get-cur-tab :id)
fid (get-in specinfo [:usermeta :frame :fid])
vid (get-in specinfo [:usermeta :vid])
x (sp/select-one [sp/ATOM :tabs :active sp/ATOM] hmi/app-db)]
(push-undo x)
(if (= element :vis)
(let [elt (js/document.getElementById (name vid))]
(if elt
(hmi/visualize specinfo elt)
(add-frame specinfo 1 2)))
(hmi/update-frame
tid fid element specinfo))))