|
18 | 18 | [reagent.core :as r]
|
19 | 19 | [reagent.dom :as rdom]))
|
20 | 20 |
|
21 |
| -(declare state->blocks block-opts->decoration get-blocks) |
| 21 | +(declare state->blocks block-opts->decoration get-blocks eval-all! eval-widget!) |
22 | 22 |
|
23 | 23 | ;; Helpers
|
24 | 24 | (defn doc? [^js node] (== (.-Document lezer-markdown/parser.nodeTypes) (.. node -type -id)))
|
|
91 | 91 | ;; :doc-changed? Boolean }
|
92 | 92 |
|
93 | 93 | (defn decorate [state] (.set Decoration (state->blocks state)))
|
| 94 | +(defn decorate+eval! [state] (eval-all! state (.set Decoration (state->blocks state)))) |
94 | 95 |
|
95 | 96 | (defn edit-gap-blocks [{:keys [edit-from blocks]} state]
|
96 | 97 | (when edit-from
|
97 |
| - (state->blocks state |
98 |
| - {:from edit-from |
99 |
| - :to (or (:from (some (when-fn #(< edit-from (:from %))) (rangeset-seq blocks edit-from))) |
100 |
| - (.. state -doc -length))}))) |
| 98 | + (let [bs (state->blocks state |
| 99 | + {:from edit-from |
| 100 | + :to (or (:from (some (when-fn #(< edit-from (:from %))) (rangeset-seq blocks edit-from))) |
| 101 | + (.. state -doc -length))})] |
| 102 | + |
| 103 | + (doseq [^js b bs] (eval-widget! state (.. b -value -widget))) |
| 104 | + bs))) |
101 | 105 |
|
102 | 106 | (defn edit-at [{:as doc :keys [selected blocks edit-from]} ^js tr pos]
|
103 | 107 | ;; we patch the existing decoration with blocks arising from last editable region
|
|
133 | 137 | (defn preview-all+select+eval [doc tr]
|
134 | 138 | ;; rebuild all decorations to get new selection right (investigate filter/add)
|
135 | 139 | (-> doc
|
136 |
| - (assoc :blocks (decorate (.-state tr))) |
| 140 | + (assoc :blocks (decorate+eval! (.-state tr))) |
137 | 141 | (set-selected (->cursor-pos (.-state tr)))
|
138 | 142 | (dissoc :edit-all? :doc-changed? :edit-from)))
|
139 | 143 |
|
|
166 | 170 | doc)
|
167 | 171 |
|
168 | 172 | ;; eval
|
169 |
| -(defn eval-block! [state {:keys [val]}] |
170 |
| - (when (= :code (:type @(.. val -widget -state))) |
| 173 | +(defn eval-widget! [state ^js widget] |
| 174 | + (when (= :code (:type @(.-state widget))) |
171 | 175 | (when-some [eval-fn! (config-get state :eval-fn!)]
|
172 |
| - (eval-fn! (.. val -widget -state))))) |
| 176 | + (eval-fn! (.-state widget))))) |
173 | 177 |
|
174 | 178 | (defn eval-all! [state blocks]
|
175 |
| - (doseq [b (rangeset-seq blocks)] (eval-block! state b)) |
| 179 | + (doseq [{:keys [^js val]} (rangeset-seq blocks)] (eval-widget! state (.-widget val))) |
176 | 180 | blocks)
|
177 | 181 |
|
178 | 182 | (defn preview+eval [{:as doc :keys [selected edit-from edit-all? blocks]} tr all?]
|
|
184 | 188 | all?
|
185 | 189 | (eval-all! (.-state tr) blocks)
|
186 | 190 | (and selected (not all?))
|
187 |
| - (when-some [block (nth (rangeset-seq blocks) selected)] |
188 |
| - (eval-block! (.-state tr) block))) |
| 191 | + (when-some [{:keys [^js val]} (nth (rangeset-seq blocks) selected)] |
| 192 | + (eval-widget! (.-state tr) (.-widget val)))) |
189 | 193 | doc)))
|
190 | 194 |
|
191 | 195 | ;; Doc State Field
|
|
194 | 198 | (.define StateField
|
195 | 199 | (j/obj
|
196 | 200 | :provide (fn [field] (.. EditorView -decorations (from field #(get % :blocks))))
|
197 |
| - :create (fn [cm-state] {:selected 0 :blocks (decorate cm-state)}) |
| 201 | + :create (fn [cm-state] {:selected 0 :blocks (decorate+eval! cm-state)}) |
198 | 202 | :update (fn [{:as doc :keys [edit-all?]} ^js tr]
|
199 | 203 | (let [{:as apply-op :keys [op args]} (get-effect-value tr doc-apply-op)
|
200 | 204 | {:as me :strs [Meta Shift]} (get-effect-value tr eval-region/modifier-effect)]
|
|
218 | 222 | editor-state (.-state view)
|
219 | 223 | widget-state (.-state widget)
|
220 | 224 | render (config-get editor-state :render)]
|
221 |
| - ;; always eval once on decoration construction |
222 |
| - (when-some [eval-fn! (config-get editor-state :eval-fn!)] (eval-fn! widget-state)) |
223 | 225 | (rdom/render [:div.cursor-pointer
|
224 | 226 | {:on-click (fn [_e]
|
225 | 227 | ;; since decorations might have been mapped since widget creation we cannot argue by range from/to
|
|
0 commit comments