Skip to content

Commit 37b2f14

Browse files
committed
Major step to componentize reset-editors!
1 parent 3e4fc0a commit 37b2f14

File tree

5 files changed

+175
-150
lines changed

5 files changed

+175
-150
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Update to react 18
44
* Add data.xml support
5+
* Initial steps taken to make reset-editors! function componentized
56

67
# 0.2.3
78

src/interactive_syntax/core.cljs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@
277277
[:> (oget Modal :Footer)
278278
[:> Button {:variant "danger"
279279
:on-click #(cb-thread
280-
#(do (swap! menu conj :hold) (%))
280+
:do #(swap! menu conj :hold)
281281
#(git/pull db @selected %)
282-
#(do (swap! menu pop) (swap! menu pop)))}
282+
:do #(swap! menu pop)
283+
:do #(swap! menu pop))}
283284
strings/PULL]
284285
[:> Button {:variant "secondary"
285286
:on-click #(swap! menu pop)}
@@ -307,9 +308,10 @@
307308
name]))]]]
308309
[:> (oget Modal :Footer)
309310
[:> Button {:on-click #(cb-thread
310-
#(do (swap! menu conj :hold) (%))
311+
:do #(swap! menu conj :hold)
311312
#(git/push db @selected %)
312-
#(do (swap! menu pop) (swap! menu pop)))}
313+
:do #(swap! menu pop)
314+
:do #(swap! menu pop))}
313315
strings/PUSH]
314316
[:> Button {:variant "secondary"
315317
:on-click #(swap! menu pop)}
@@ -644,13 +646,16 @@
644646
(when (condp = style
645647
:simple (not= @text "")
646648
:upload @file)
647-
(choice-callback @text @file)
648-
(swap! menu #(let [item (peek %)
649-
rest (pop %)]
650-
(if (and (coll? item)
651-
(= (count item) 2))
652-
(conj rest (second item))
653-
rest)))))
649+
(cb-thread
650+
:do #(swap! menu conj :hold)
651+
#(choice-callback @text @file %)
652+
:do #(swap! menu pop)
653+
#(swap! menu #(let [item (peek %)
654+
rest (pop %)]
655+
(if (and (coll? item)
656+
(= (count item) 2))
657+
(conj rest (second item))
658+
rest))))))
654659
ref (or ref #js {:current nil})]
655660
(when list-ref
656661
(reset! list-ref dir-list)) ; don't deref dir-list here
@@ -832,7 +837,7 @@
832837
[:> (oget Modal :Header) {:close-button true}
833838
[:h3 strings/ADD-RESOURCE]]
834839
[file-browser db strings/ADD-lower :upload
835-
(fn [optname files]
840+
(fn [optname files cb]
836841
(cb-loop (oget files :target.files)
837842
#(let [name (if (= optname "")
838843
(oget %2 :name)
@@ -843,7 +848,7 @@
843848
(js/path.join @file-browser-folder name)
844849
v %)))
845850
(.catch js/console.error)))
846-
#()))]])
851+
cb))]])
847852

848853
(defn save-dialog [{:keys [menu
849854
file-browser-folder
@@ -859,10 +864,10 @@
859864
[:> (oget Modal :Header) {:close-button true}
860865
[:h3 strings/SAVE]]
861866
[file-browser db strings/SAVE :simple
862-
(fn [file]
867+
(fn [file _ cb]
863868
(reset! current-folder @file-browser-folder)
864869
(reset! current-file file)
865-
(save-buffer db))
870+
(save-buffer db cb))
866871
{:file-browser fb-ref
867872
:file-browser-list fb-list-ref}]]))
868873

@@ -880,10 +885,10 @@
880885
[:h3 strings/LOAD]]
881886
[:> (oget Modal :Body)
882887
[file-browser db strings/LOAD :simple
883-
(fn [file]
888+
(fn [file _ cb]
884889
(reset! current-folder @file-browser-folder)
885890
(reset! current-file file)
886-
(load-buffer db))
891+
(load-buffer db cb))
887892
{:file-browser fb-ref
888893
:file-browser-list fb-list-ref}]]])
889894

@@ -1160,7 +1165,7 @@
11601165
editor-reset-ref :editor-reset
11611166
visr-run-ref :visr-run}]]
11621167
(let [edit (atom nil)
1163-
cache (atom nil)
1168+
cache (env/make-reset-editors-cache)
11641169
visrs (atom {})
11651170
key (random-uuid)
11661171
set-text (fn [txt]
@@ -1176,9 +1181,9 @@
11761181
(let [fc @file-changed]
11771182
(set! js/window.edit @edit)
11781183
(-> @edit (ocall :getDoc) (ocall :setValue @input))
1179-
(reset! cache nil)
1184+
;; TODO, this should probably be uncommented?
1185+
;; (env/make-reset-editors-cache cache)
11801186
(reset! file-changed fc))))
1181-
reset-queue (clojure.core/atom #queue [])
11821187
codemirror-options #(conj (env/codemirror-options db) print-options)]
11831188
(add-watch current-file key watch-updater)
11841189
(add-watch menu key watch-updater)
@@ -1198,20 +1203,20 @@
11981203
(add-watch deps key
11991204
(fn [k r o n]
12001205
(when-not (= o n)
1201-
(reset! cache nil)
1206+
(env/make-reset-editors-cache cache)
12021207
(doseq [[k v] @visrs]
12031208
(ocall @(:mark v) :clear))
12041209
(reset! visrs {})
12051210
(when editor-reset-ref
12061211
(reset! editor-reset-ref true))
1207-
(env/reset-editors! @input set-text edit visrs nil cache
1208-
reset-queue (codemirror-options) db #()
1212+
(env/reset-editors! @input set-text edit visrs cache
1213+
(codemirror-options) db #()
12091214
{:for-print for-print
12101215
:hider-bars hider-bars
12111216
:visr-run visr-run-ref}))))
1212-
(add-watch reset-queue ::set-running?
1217+
(add-watch cache ::set-running?
12131218
(fn [k r o n]
1214-
(when (and editor-reset-ref (empty? n))
1219+
(when (and editor-reset-ref (empty? (:queue n)))
12151220
(reset! editor-reset-ref false))))
12161221
(reset! visr-commit!
12171222
(doseq [[k v] @visrs]
@@ -1250,8 +1255,8 @@
12501255
(reset! input value)
12511256
(when editor-reset-ref
12521257
(reset! editor-reset-ref true))
1253-
(env/reset-editors! @input set-text edit visrs operation
1254-
cache reset-queue (codemirror-options) db
1258+
(env/reset-editors! @input set-text edit visrs
1259+
cache (codemirror-options) db
12551260
#() {:for-print for-print
12561261
:hider-bars hider-bars
12571262
:visr-run visr-run-ref}))
@@ -1288,9 +1293,8 @@
12881293
(reset! editor-ref e))
12891294
(when editor-reset-ref
12901295
(reset! editor-reset-ref true))
1291-
(env/reset-editors! @input set-text edit visrs nil
1292-
cache reset-queue
1293-
(codemirror-options) db
1296+
(env/reset-editors! @input set-text edit visrs
1297+
cache (codemirror-options) db
12941298
#(reset! mounted? true)
12951299
{:for-print for-print
12961300
:hider-bars hider-bars
@@ -1308,6 +1312,7 @@
13081312
:as db}
13091313
& [repl-ref]]
13101314
(let [edit (atom nil)
1315+
cache (env/make-reset-editors-cache)
13111316
instances (atom [])
13121317
watch-updater
13131318
(fn [k r o n]

src/interactive_syntax/env.cljs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,23 @@
752752
(ocall "setValue"
753753
(stx->stx-str @stx))))}]]]]])]]))))
754754

755-
(defn reset-editors! [source set-text editor instances operation cache queue
755+
(defn make-reset-editors-cache [& [cache]]
756+
(let [c {:cache nil
757+
:queue #queue []}]
758+
(if cache
759+
(reset! cache c)
760+
(atom c))))
761+
762+
(defn reset-editors! [source set-text editor instances cache
756763
codemirror-options
757764
{{:keys [show-editors visr-default sandbox]} :options
758765
:keys [fs deps] :as db}
759766
cb & [{visr-run-ref :visr-run
760767
:as editor-options}]]
761768
(when (and @show-editors @editor)
762-
(let [old-instances (atom @instances)
769+
(let [queue (r/cursor cache [:queue])
770+
cache (r/cursor cache [:cache])
771+
old-instances (atom @instances)
763772
prog (indexing-push-back-reader source)
764773
eof (atom nil)
765774
fresh-cache (atom false)

src/interactive_syntax/utils.cljs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
(defn cb-thread [& funcs]
1313
((fn rec [funcs ret]
14-
(if (empty? funcs)
15-
ret
16-
(apply (first funcs) #(rec (rest funcs) %&) ret)))
14+
(cond
15+
(empty? funcs) ret
16+
(= (first funcs) :do)
17+
(let [ret (apply (second funcs) ret)]
18+
(rec (rest (rest funcs)) (list ret))),
19+
:else (apply (first funcs) #(rec (rest funcs) %&) ret)))
1720
funcs nil))
1821

1922
(defn cb-loop [lst body cb & ret]

0 commit comments

Comments
 (0)