Browse Source

fix: code block can't be saved

Tienson Qin 3 years ago
parent
commit
a5674b1728

+ 1 - 1
package.json

@@ -89,7 +89,7 @@
         "ignore": "5.1.8",
         "is-svg": "4.2.2",
         "jszip": "3.5.0",
-        "mldoc": "1.2.3",
+        "mldoc": "1.2.4",
         "path": "0.12.7",
         "pixi-graph-fork": "0.1.6",
         "pixi.js": "6.2.0",

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

@@ -2555,7 +2555,7 @@
     (let [{:keys [lines language]} options
           attr (when language
                  {:data-lang language})
-          code (join-lines lines)]
+          code (apply str lines)]
       (cond
         html-export?
         (highlight/html-export attr code)

+ 4 - 2
src/main/frontend/components/lazy_editor.cljs

@@ -2,7 +2,8 @@
   (:require [rum.core :as rum]
             [shadow.lazy :as lazy]
             [frontend.ui :as ui]
-            [frontend.state :as state]))
+            [frontend.state :as state]
+            [frontend.text :as text]))
 
 (def lazy-editor (lazy/loadable frontend.extensions.code/editor))
 
@@ -16,7 +17,8 @@
                  state)}
   [config id attr code options]
   (let [loaded? (rum/react loaded?)
-        theme (state/sub :ui/theme)]
+        theme (state/sub :ui/theme)
+        code (when code (text/remove-indentations code))]
     (if loaded?
       (@lazy-editor config id attr code theme options)
       (ui/loading "CodeMirror"))))

+ 19 - 6
src/main/frontend/extensions/code.cljs

@@ -46,6 +46,7 @@
             [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]))
@@ -57,6 +58,7 @@
 (def textarea-ref-name "textarea")
 (def codemirror-ref-name "codemirror-instance")
 
+
 (defn- save-file-or-block-when-blur-or-esc!
   [editor textarea config state]
   (.save editor)
@@ -68,16 +70,27 @@
         (let [block (db/pull [:block/uuid (:block/uuid config)])
               format (:block/format block)
               content (:block/content block)
-              full-content (:full_content (last (:rum/args state)))]
-          (when (and full-content (string/includes? content full-content))
+              {: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 [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'))))))
+                (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'))))))))
 
         (:file-path config)
         (let [path (:file-path config)

+ 3 - 1
src/main/frontend/modules/outliner/file.cljs

@@ -27,7 +27,9 @@
                    (string/blank? (:block/content (first blocks)))
                    (nil? (:block/file page-block)))
       (let [tree (tree/blocks->vec-tree blocks (:block/name page-block))]
-        (file/save-tree page-block tree)))))
+        (if page-block
+          (file/save-tree page-block tree)
+          (js/console.error (str "can't find page id: " page-db-id)))))))
 
 (defn write-files!
   [page-db-ids]

+ 11 - 0
src/main/frontend/text.cljs

@@ -337,3 +337,14 @@
                  (vec (take-last 2 (conj acc k)))))
              []
              ks))))
+
+(defn remove-indentations
+  [text]
+  (when (string? text)
+    (let [lines (string/split-lines text)
+          spaces (re-find #"^[\s\t]+" (first lines))
+          spaces-count (count spaces)]
+      (string/join "\n" (map (fn [line]
+                               (let [spaces (re-find #"^[\s\t]+" line)
+                                     spaces-count (min (count spaces) spaces-count)]
+                                 (util/safe-subs line spaces-count))) lines)))))