fix: code block can't be saved

pull/3274/head^2
Tienson Qin 2021-11-26 19:15:12 +08:00
parent 1ef6741282
commit a5674b1728
6 changed files with 39 additions and 11 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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"))))

View File

@ -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)

View File

@ -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]

View File

@ -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)))))