فهرست منبع

fix: block content not saved when exit code mode

Tienson Qin 1 سال پیش
والد
کامیت
a53943e19e

+ 9 - 9
src/main/frontend/components/block.cljs

@@ -92,7 +92,8 @@
             [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]
-            [shadow.loader :as loader]))
+            [shadow.loader :as loader]
+            [frontend.storage :as storage]))
 
 ;; local state
 (defonce *dragging?
@@ -2411,7 +2412,8 @@
                           content
                           block
                           cursor-range
-                          {:move-cursor? false
+                          {:db (db/get-db)
+                           :move-cursor? false
                            :container-id (:container-id config)}))]
                 ;; wait a while for the value of the caret range
                 (p/do!
@@ -2790,7 +2792,8 @@
                                                           (:block/title block))]
                                           (editor-handler/clear-selection!)
                                           (editor-handler/unhighlight-blocks!)
-                                          (state/set-editing! edit-input-id content block "" {:container-id (:container-id config)}))}})
+                                          (state/set-editing! edit-input-id content block "" {:db (db/get-db)
+                                                                                              :container-id (:container-id config)}))}})
            (block-content config block edit-input-id block-id slide?))
 
           (when (and db-based? (not table?)) (block-positioned-properties config block :block-right))
@@ -3659,16 +3662,13 @@
                                (let [target (.-target e)]
                                  (shui/popup-show! target
                                                    #(src-lang-picker block
-                                                                     (fn [lang ^js e]
-                                                                       (when-let [^js cm (util/get-cm-instance target)]
+                                                                     (fn [lang ^js _e]
+                                                                       (when-let [^js cm (util/get-cm-instance (util/rec-get-node target "ls-block"))]
                                                                          (if-let [mode (get-code-mode-by-lang lang)]
                                                                            (.setOption cm "mode" mode)
                                                                            (throw (ex-info "code mode not found"
                                                                                            {:lang lang})))
-                                                                         (when (or (string/blank? (util/trim-safe code))
-                                                                                   (not= (some-> e (.-type)) "click"))
-                                                                           (.focus cm)
-                                                                           (.setCursor cm (.lineCount cm) 0))
+                                                                         (storage/set :latest-code-lang lang)
                                                                          (db-property-handler/set-block-property!
                                                                           (:db/id block) :logseq.property.code/lang lang))))
                                                    {:align :end})))}

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

@@ -390,11 +390,10 @@
   [config]
   (p/do!
    (code-handler/save-code-editor!)
-   (when-let [block-id (:block/uuid config)]
-     (let [block (db/entity [:block/uuid block-id])]
+   (when-let [block (:block config)]
+     (let [block (db/entity [:block/uuid (:block/uuid block)])]
        (state/set-state! :editor/raw-mode-block block)
-       (when-not (= (:db/id block) (:db/id (state/get-edit-block)))
-         (editor-handler/edit-block! block :max))))))
+       (editor-handler/edit-block! block :max {:save-code-editor? false})))))
 
 (defn ^:large-vars/cleanup-todo render!
   [state]

+ 8 - 8
src/main/frontend/handler/block.cljs

@@ -22,8 +22,7 @@
    [frontend.handler.property.util :as pu]
    [dommy.core :as dom]
    [goog.object :as gobj]
-   [promesa.core :as p]
-   [datascript.impl.entity :as de]))
+   [promesa.core :as p]))
 
 ;;  Fns
 
@@ -178,7 +177,8 @@
                            (state/get-current-editor-container-id)
                            :unknown-container)]
       (state/set-editing! (str "edit-block-" (:block/uuid block)) content block text-range
-                          {:container-id container-id :direction direction :event event :pos pos}))
+                          {:db (db/get-db)
+                           :container-id container-id :direction direction :event event :pos pos}))
     (mark-last-input-time! repo)))
 
 (defn sanity-block-content
@@ -189,18 +189,18 @@
         (drawer/remove-logbook))))
 
 (defn edit-block!
-  [block pos & {:keys [_container-id custom-content tail-len]
-                :or {tail-len 0}
+  [block pos & {:keys [_container-id custom-content tail-len save-code-editor?]
+                :or {tail-len 0
+                     save-code-editor? true}
                 :as opts}]
   (when (and (not config/publishing?) (:block/uuid block))
     (p/do!
-     (state/pub-event! [:editor/save-code-editor])
+     (when save-code-editor? (state/pub-event! [:editor/save-code-editor]))
      (when (not= (:block/uuid block) (:block/uuid (state/get-edit-block)))
        (state/clear-edit! {:clear-editing-block? false}))
      (when-let [block-id (:block/uuid block)]
        (let [repo (state/get-current-repo)
-             block (if (de/entity? block) block
-                       (or (db/entity [:block/uuid block-id]) block))
+             block (or (db/entity [:block/uuid block-id]) block)
              content (or custom-content (:block/title block) "")
              content-length (count content)
              text-range (cond

+ 39 - 29
src/main/frontend/handler/events.cljs

@@ -81,7 +81,9 @@
             [logseq.db :as ldb]
             [frontend.persist-db :as persist-db]
             [frontend.handler.export :as export]
-            [frontend.extensions.fsrs :as fsrs]))
+            [frontend.extensions.fsrs :as fsrs]
+            [frontend.storage :as storage]
+            [frontend.modules.outliner.ui :as ui-outliner-tx]))
 
 ;; TODO: should we move all events here?
 
@@ -886,26 +888,21 @@
 (defmethod handle :editor/save-code-editor [_]
   (code-handler/save-code-editor!))
 
-(defmethod handle :editor/focus-code-editor [[_ editing-block]]
-  (when (= :code (:logseq.property.node/display-type editing-block))
-    (let [container-id (some-> (:editor/container-id @state/state) (deref))
-          uuid' (:block/uuid editing-block)
-          target (js/document.querySelector
-                  (util/format ".select-language[blockid=\"%s\"][containerid=\"%s\"]" uuid' container-id))]
-      (when-let [^js cm (util/get-cm-instance target)]
-        (when-not (.hasFocus cm)
-          (let [cursor-pos (some-> (:editor/cursor-range @state/state) (deref) (count))
-                direction (:block.editing/direction editing-block)
-                pos (:block.editing/pos editing-block)
-                to-line (case direction
-                          :up (.lastLine cm)
-                          (case pos
-                            :max (.lastLine cm)
-                            0))]
+(defmethod handle :editor/focus-code-editor [[_ editing-block container]]
+  (when-let [^js cm (util/get-cm-instance container)]
+    (when-not (.hasFocus cm)
+      (let [cursor-pos (some-> (:editor/cursor-range @state/state) (deref) (count))
+            direction (:block.editing/direction editing-block)
+            pos (:block.editing/pos editing-block)
+            to-line (case direction
+                      :up (.lastLine cm)
+                      (case pos
+                        :max (.lastLine cm)
+                        0))]
                  ;; move to friendly cursor
-            (doto cm
-              (.focus)
-              (.setCursor to-line (or cursor-pos 0)))))))))
+        (doto cm
+          (.focus)
+          (.setCursor to-line (or cursor-pos 0)))))))
 
 (defmethod handle :editor/toggle-children-number-list [[_ block]]
   (when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))]
@@ -974,15 +971,28 @@
    (let [block (db/entity (:db/id block))
          block-type (:logseq.property.node/display-type block)
          block-title (:block/title block)
-         turn-type! #(db-property-handler/set-block-property!
-                      (:block/uuid %) :logseq.property.node/display-type (keyword type))]
-     (if (or (not (nil? block-type))
-             (not (string/blank? block-title)))
-       ;; insert a new block
-       (let [[p _ block'] (editor-handler/insert-new-block-aux! {} block "")]
-         (some-> p
-                 (p/then #(turn-type! block'))))
-       (turn-type! block)))))
+         latest-code-lang (storage/get :latest-code-lang)
+         turn-type! #(if (and (= (keyword type) :code) latest-code-lang)
+                       (db-property-handler/set-block-properties!
+                        (:block/uuid %)
+                        {:logseq.property.node/display-type (keyword type)
+                         :logseq.property.code/lang latest-code-lang})
+                       (db-property-handler/set-block-property!
+                        (:block/uuid %) :logseq.property.node/display-type (keyword type)))]
+     (p/let [block (if (or (not (nil? block-type))
+                           (not (string/blank? block-title)))
+                     (p/let [result (ui-outliner-tx/transact!
+                                     {:outliner-op :insert-blocks}
+                                     ;; insert a new block
+                                     (let [[_p _ block'] (editor-handler/insert-new-block-aux! {} block "")]
+                                       (turn-type! block')))
+                             result' (ldb/read-transit-str result)]
+                       (when-let [id (:block/uuid (first (:blocks result')))]
+                         (db/entity [:block/uuid id])))
+                     (p/do!
+                      (turn-type! block)
+                      (db/entity [:block/uuid (:block/uuid block)])))]
+       (js/setTimeout #(editor-handler/edit-block! block :max) 100)))))
 
 (rum/defc multi-tabs-dialog
   []

+ 3 - 4
src/main/frontend/state.cljs

@@ -2034,7 +2034,7 @@ Similar to re-frame subscriptions"
    (set-selection-blocks! blocks direction)))
 
 (defn set-editing!
-  [edit-input-id content block cursor-range & {:keys [move-cursor? container-id property-block direction event pos]
+  [edit-input-id content block cursor-range & {:keys [db move-cursor? container-id property-block direction event pos]
                                                :or {move-cursor? true}}]
   (when-not (exists? js/process)
     (if (> (count content)
@@ -2069,9 +2069,8 @@ Similar to re-frame subscriptions"
           (set-state! :editor/last-key-code nil)
           (set-state! :editor/set-timestamp-block nil)
           (set-state! :editor/cursor-range cursor-range)
-
-          (when (= :code (:logseq.property.node/display-type block))
-            (pub-event! [:editor/focus-code-editor block]))
+          (when (= :code (:logseq.property.node/display-type (d/entity db (:db/id block))))
+            (pub-event! [:editor/focus-code-editor block block-element]))
           (when-let [input (gdom/getElement edit-input-id)]
             (let [pos (count cursor-range)]
               (when content

+ 1 - 2
src/main/frontend/util.cljc

@@ -1541,5 +1541,4 @@ Arg *stop: atom, reset to true to stop the loop"
    (defn get-cm-instance
      [^js target]
      (when target
-       (when-let [node (rec-get-node target "ls-code-editor-wrap")]
-         (some-> node (.querySelector ".CodeMirror") (.-CodeMirror))))))
+       (some-> target (.querySelector ".CodeMirror") (.-CodeMirror)))))