refactor(fs): handle global file write

This commit is contained in:
Andelf
2023-03-15 16:56:18 +08:00
parent e4c37b7089
commit 1cd7f89cd9
11 changed files with 117 additions and 76 deletions

View File

@@ -1,12 +1,15 @@
(ns frontend.handler.code
"Codemirror editor related."
(:require [frontend.state :as state]
[goog.object :as gobj]
(:require [clojure.string :as string]
[frontend.config :as config]
[frontend.db :as db]
[frontend.handler.editor :as editor-handler]
[frontend.handler.file :as file-handler]
[frontend.state :as state]
[goog.object :as gobj]
[logseq.graph-parser.utf8 :as utf8]
[clojure.string :as string]))
[frontend.fs2.path :as fs2-path]
[frontend.components.content :as content]))
(defn save-code-editor!
[]
@@ -20,6 +23,7 @@
default-value (gobj/get textarea "defaultValue")]
(when (not= value default-value)
(cond
;; save block content
(:block/uuid config)
(let [block (db/pull [:block/uuid (:block/uuid config)])
content (:block/content block)
@@ -34,16 +38,25 @@
(state/set-edit-content! (state/get-edit-input-id) new-content)
(editor-handler/save-block-if-changed! block new-content))
(:file-path config)
(not-empty (: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})))
repo (state/get-current-repo)
repo-dir (config/get-repo-dir repo)
rpath (fs2-path/trim-dir-prefix repo-dir path)
;; old-content (db/get-file rpath)
_ (prn ::calc rpath)]
(if rpath
;; in-db file
(let [old-content (db/get-file rpath)]
(when (and
(not (string/blank? value))
(not= (string/trim value) (string/trim old-content)))
(file-handler/alter-file (state/get-current-repo)
rpath
(str (string/trim value) "\n")
{:re-render-root? true})))
;; global file
(file-handler/alter-global-file path (str (string/trim value) "\n"))))
:else
nil))))))