Browse Source

refactor(editor): refine saving of code editor

- won't remove indentations
- use pos-meta to save block
- debounce esc event against blur
Andelf 3 years ago
parent
commit
b3f2871414

+ 1 - 3
src/main/frontend/components/lazy_editor.cljs

@@ -3,8 +3,7 @@
             [rum.core :as rum]
             [shadow.lazy :as lazy]
             [frontend.ui :as ui]
-            [frontend.state :as state]
-            [frontend.text :as text]))
+            [frontend.state :as state]))
 
 (def lazy-editor (lazy/loadable frontend.extensions.code/editor))
 
@@ -20,7 +19,6 @@
   (let [loaded? (rum/react loaded?)
         theme (state/sub :ui/theme)
         code (or code "")
-        code (text/remove-indentations code)
         code (string/replace-first code #"\n$" "")] ;; See-also: #3410
     (if loaded?
       (@lazy-editor config id attr code theme options)

+ 11 - 30
src/main/frontend/extensions/code.cljs

@@ -46,7 +46,6 @@
             [frontend.handler.file :as file-handler]
             [frontend.state :as state]
             [frontend.util :as util]
-            [frontend.text :as text]
             [goog.dom :as gdom]
             [goog.object :as gobj]
             [rum.core :as rum]))
@@ -68,29 +67,14 @@
       (cond
         (:block/uuid config)
         (let [block (db/pull [:block/uuid (:block/uuid config)])
-              format (:block/format block)
               content (:block/content block)
-              {:keys [lines]} (last (:rum/args state))
-              full-content (:full_content (last (:rum/args state)))
-              value (text/remove-indentations value)]
-          (when full-content
-            (let [lines (string/split-lines full-content)
-                  fl (first lines)
-                  ll (last lines)]
-              (when (and fl ll)
-                (let [src (->> (subvec (vec lines) 1 (dec (count lines)))
-                               (string/join "\n"))
-                      src (text/remove-indentations src)
-                      full-content (str (string/trim fl)
-                                        (if (seq src)
-                                          (str "\n" src "\n")
-                                          "\n")
-                                        (string/trim ll))]
-                  (when (string/includes? content full-content)
-                    (let [value' (str (string/trim fl) "\n" value "\n" (string/trim ll))
-                          ;; FIXME: What if there're multiple code blocks with the same value?
-                          content' (string/replace-first content full-content value')]
-                      (editor-handler/save-block-if-changed! block content'))))))))
+              {:keys [start_pos end_pos]} (:pos_meta (last (:rum/args state)))
+              prefix (subs content 0 (- start_pos 2))
+              surfix (subs content (- end_pos 2))
+              new-content (if (string/blank? value)
+                            (str prefix surfix)
+                            (str prefix value "\n" surfix))]
+          (editor-handler/save-block-if-changed! block new-content))
 
         (:file-path config)
         (let [path (:file-path config)
@@ -137,9 +121,8 @@
                               (get-in config [:block :block/uuid])))
         _ (state/set-state! :editor/code-mode? false)
         original-mode (get attr :data-lang)
-        mode original-mode
-        clojure? (contains? #{"clojure" "clj" "text/x-clojure" "cljs" "cljc"} mode)
-        mode (if clojure? "clojure" (text->cm-mode mode))
+        clojure? (contains? #{"clojure" "clj" "text/x-clojure" "cljs" "cljc"} original-mode)
+        mode (if clojure? "clojure" (text->cm-mode original-mode))
         lisp? (or clojure?
                   (contains? #{"scheme" "racket" "lisp"} mode))
         textarea (gdom/getElement id)
@@ -152,14 +135,12 @@
                                      :lineNumbers true
                                      :styleActiveLine true
                                      :extraKeys #js {"Esc" (fn [cm]
+                                                             (reset! esc-pressed? true)
                                                              (save-file-or-block-when-blur-or-esc! cm textarea config state)
                                                              (when-let [block-id (:block/uuid config)]
-                                                               (let [block (db/pull [:block/uuid block-id])
-                                                                     value (.getValue cm)
-                                                                     textarea-value (gobj/get textarea "value")]
+                                                               (let [block (db/pull [:block/uuid block-id])]
                                                                  (editor-handler/edit-block! block :max block-id)))
                                                              ;; TODO: return "handled" or false doesn't always prevent event bubbles
-                                                             (reset! esc-pressed? true)
                                                              (js/setTimeout #(reset! esc-pressed? false) 10))}}))]
     (when editor
       (let [textarea-ref (rum/ref-node state textarea-ref-name)]

+ 1 - 12
src/main/frontend/text.cljs

@@ -1,9 +1,7 @@
 (ns frontend.text
   (:require [frontend.config :as config]
             [frontend.util :as util]
-            [clojure.string :as string]
-            [clojure.set :as set]
-            [medley.core :as medley]))
+            [clojure.string :as string]))
 
 (def page-ref-re-0 #"\[\[(.*)\]\]")
 (def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
@@ -346,12 +344,3 @@
       (if (not= (first parts) "0")
         (string/join "/" parts)
         (last parts)))))
-
-(defn remove-indentations
-  [text]
-  (when (string? text)
-    (let [lines (string/split text #"\r?\n" -1)
-          spaces-count (apply min (map #(count (re-find #"^[\s\t]+" %)) lines))]
-      (if (zero? spaces-count)
-        text
-        (string/join "\n" (map #(util/safe-subs % spaces-count) lines))))))