Browse Source

fix: Enter not working in code block

Also, shift+enter doesn't editing new block.
Tienson Qin 9 months ago
parent
commit
141d123655

+ 14 - 8
src/main/frontend/components/lazy_editor.cljs

@@ -14,6 +14,19 @@
 
 (defonce loaded? (atom false))
 
+(rum/defc editor-aux
+  [config id attr code theme options codemirror-loaded?]
+  (let [^js state (ui/useInView #js {:rootMargin "0px"})
+        in-view? (.-inView state)
+        placeholder [:div
+                     {:style {:height (min
+                                       (* 23.2 (count (string/split-lines code)))
+                                       600)}}]]
+    [:div {:ref (.-ref state)}
+     (if (and codemirror-loaded? in-view?)
+       (@lazy-editor config id attr code theme options)
+       placeholder)]))
+
 (rum/defc editor <
   rum/reactive
   {:will-mount
@@ -36,11 +49,4 @@
         theme   (state/sub :ui/theme)
         code    (or code "")
         code    (string/replace-first code #"\n$" "")]      ;; See-also: #3410
-    (ui/lazy-visible
-     (fn []
-       (when loaded?' (@lazy-editor config id attr code theme options)))
-     {:trigger-once? true
-      :placeholder [:div
-                    {:style {:height (min
-                                      (* 23.2 (count (string/split-lines code)))
-                                      600)}}]})))
+    (editor-aux config id attr code theme options loaded?')))

+ 4 - 1
src/main/frontend/extensions/code.cljs

@@ -470,7 +470,10 @@
                                  (code-handler/save-code-editor!))
                                (state/set-block-component-editing-mode! false)
                                (state/set-state! :editor/code-block-context nil)
-                               (when (not esc?) (state/clear-edit!))
+                               (when (and (not esc?)
+                                          (= (:db/id (state/get-edit-block))
+                                             (:db/id edit-block)))
+                                 (state/clear-edit!))
                                (vreset! *cursor-curr nil)
                                (vreset! *cursor-prev nil))))
         (.on editor "focus" (fn [_e]