فهرست منبع

feat(plugin): add save_focused_code_editor

Tienson Qin 2 سال پیش
والد
کامیت
4a93d27f40
4فایلهای تغییر یافته به همراه68 افزوده شده و 48 حذف شده
  1. 10 45
      src/main/frontend/extensions/code.cljs
  2. 48 0
      src/main/frontend/handler/code.cljs
  3. 2 0
      src/main/frontend/state.cljs
  4. 8 3
      src/main/logseq/api.cljs

+ 10 - 45
src/main/frontend/extensions/code.cljs

@@ -131,6 +131,7 @@
             [frontend.extensions.calc :as calc]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.file :as file-handler]
+            [frontend.handler.code :as code-handler]
             [frontend.state :as state]
             [logseq.graph-parser.utf8 :as utf8]
             [frontend.util :as util]
@@ -153,47 +154,6 @@
   (get (state/get-config)
        :editor/extra-codemirror-options {}))
 
-(defn- save-file-or-block!
-  [editor textarea config state]
-  (.save editor)
-  (let [value (gobj/get textarea "value")
-        default-value (gobj/get textarea "defaultValue")]
-    (when (not= value default-value)
-      (cond
-        (:block/uuid config)
-        (let [block (db/pull [:block/uuid (:block/uuid config)])
-              content (:block/content block)
-              {:keys [start_pos end_pos]} (:pos_meta @(:code-options state))
-              offset (if (:block/pre-block? block) 0 2)
-              raw-content (utf8/encode content) ;; NOTE: :pos_meta is based on byte position
-              prefix (utf8/decode (.slice raw-content 0 (- start_pos offset)))
-              surfix (utf8/decode (.slice raw-content (- end_pos offset)))
-              new-content (if (string/blank? value)
-                            (str prefix surfix)
-                            (str prefix value "\n" surfix))]
-          (state/set-edit-content! (state/get-edit-input-id) new-content)
-          (editor-handler/save-block-if-changed! block new-content))
-
-        (:file-path config)
-        (let [path (:file-path config)
-              content (or (db/get-file path) "")]
-          (when (and
-                 (not (string/blank? value))
-                 (not= (string/trim value) (string/trim content)))
-            (file-handler/alter-file (state/get-current-repo)
-                                     path
-                                     (str (string/trim value) "\n")
-                                     {:re-render-root? true})))
-
-        :else
-        nil))))
-
-(defn- save-file-or-block-when-blur-or-esc!
-  [editor textarea config state]
-  (state/set-state! :editor/skip-saving-current-block? true)
-  (state/set-block-component-editing-mode! false)
-  (save-file-or-block! editor textarea config state))
-
 (defn- text->cm-mode
   ([text]
    (text->cm-mode text :name))
@@ -236,7 +196,7 @@
                            :extraKeys #js {"Esc" (fn [cm]
                                                    ;; Avoid reentrancy
                                                    (gobj/set cm "escPressed" true)
-                                                   (save-file-or-block-when-blur-or-esc! cm textarea config state)
+                                                   (code-handler/save-code-editor!)
                                                    (when-let [block-id (:block/uuid config)]
                                                      (let [block (db/pull [:block/uuid block-id])]
                                                        (editor-handler/edit-block! block :max block-id))))}}
@@ -259,10 +219,15 @@
                              (when (or
                                     (= :file (state/get-current-route))
                                     (not (gobj/get cm "escPressed")))
-                               (save-file-or-block-when-blur-or-esc! editor textarea config state))
-                             (state/set-block-component-editing-mode! false)))
+                               (code-handler/save-code-editor!))
+                             (state/set-block-component-editing-mode! false)
+                             (state/set-state! :editor/code-block-context nil)))
         (.on editor "focus" (fn [_e]
-                              (state/set-block-component-editing-mode! true)))
+                              (state/set-block-component-editing-mode! true)
+                              (state/set-state! :editor/code-block-context
+                                                {:editor editor
+                                                 :config config
+                                                 :state state})))
         (.addEventListener element "mousedown"
                            (fn [e]
                              (util/stop e)

+ 48 - 0
src/main/frontend/handler/code.cljs

@@ -0,0 +1,48 @@
+(ns frontend.handler.code
+  (:require [frontend.state :as state]
+            [goog.object :as gobj]
+            [frontend.db :as db]
+            [frontend.handler.editor :as editor-handler]
+            [frontend.handler.file :as file-handler]
+            [logseq.graph-parser.utf8 :as utf8]
+            [clojure.string :as string]))
+
+(defn save-code-editor!
+  []
+  (let [{:keys [config state editor]} (get @state/state :editor/code-block-context)]
+    (state/set-state! :editor/skip-saving-current-block? true)
+    (state/set-block-component-editing-mode! false)
+    (when editor
+      (.save editor)
+      (let [textarea (.getTextArea editor)
+            value (gobj/get textarea "value")
+            default-value (gobj/get textarea "defaultValue")]
+        (when (not= value default-value)
+          (cond
+            (:block/uuid config)
+            (let [block (db/pull [:block/uuid (:block/uuid config)])
+                  content (:block/content block)
+                  {:keys [start_pos end_pos]} (:pos_meta @(:code-options state))
+                  offset (if (:block/pre-block? block) 0 2)
+                  raw-content (utf8/encode content) ;; NOTE: :pos_meta is based on byte position
+                  prefix (utf8/decode (.slice raw-content 0 (- start_pos offset)))
+                  surfix (utf8/decode (.slice raw-content (- end_pos offset)))
+                  new-content (if (string/blank? value)
+                                (str prefix surfix)
+                                (str prefix value "\n" surfix))]
+              (state/set-edit-content! (state/get-edit-input-id) new-content)
+              (editor-handler/save-block-if-changed! block new-content))
+
+            (:file-path config)
+            (let [path (:file-path config)
+                  content (or (db/get-file path) "")]
+              (when (and
+                     (not (string/blank? value))
+                     (not= (string/trim value) (string/trim content)))
+                (file-handler/alter-file (state/get-current-repo)
+                                         path
+                                         (str (string/trim value) "\n")
+                                         {:re-render-root? true})))
+
+            :else
+            nil))))))

+ 2 - 0
src/main/frontend/state.cljs

@@ -131,6 +131,8 @@
      ;; Whether to skip saving the current block
      :editor/skip-saving-current-block?     false
 
+     :editor/code-block-context             {}
+
      :db/last-transact-time                 {}
      ;; whether database is persisted
      :db/persisted?                         {}

+ 8 - 3
src/main/logseq/api.cljs

@@ -39,7 +39,8 @@
             [sci.core :as sci]
             [frontend.version :as fv]
             [frontend.handler.shell :as shell]
-            [frontend.modules.layout.core]))
+            [frontend.modules.layout.core]
+            [frontend.handler.code :as code-handler]))
 
 ;; helpers
 (defn- normalize-keyword-for-json
@@ -187,6 +188,10 @@
           path (util/node-path.join path "package.json")]
       (fs/write-file! repo "" path (js/JSON.stringify data nil 2) {:skip-compare? true}))))
 
+(def ^:export save_focused_code_editor
+  (fn []
+    (code-handler/save-code-editor!)))
+
 (defn ^:private write_rootdir_file
   [file content sub-root root-dir]
   (p/let [repo           ""
@@ -623,9 +628,9 @@
         (let [bb (if-not (vector? bb) (vector bb) bb)
               {:keys [sibling keepUUID]} (bean/->clj opts)
               keep-uuid? (or keepUUID false)
-              _ (when keep-uuid? (doseq 
+              _ (when keep-uuid? (doseq
                   [block (outliner/tree-vec-flatten bb :children)]
-                  (let [uuid (:id (:properties block))] 
+                  (let [uuid (:id (:properties block))]
                     (when (and uuid (db-model/query-block-by-uuid (uuid-or-throw-error uuid)))
                       (throw (js/Error.
                               (util/format "Custom block UUID already exists (%s)." uuid)))))))