Pārlūkot izejas kodu

fix: can't read and write custom files

Tienson Qin 2 gadi atpakaļ
vecāks
revīzija
1bb93d3560

+ 7 - 2
src/main/frontend/components/file.cljs

@@ -78,7 +78,8 @@
   {:will-mount (fn [state]
                  (let [*content (atom nil)
                        [path format] (:rum/args state)
-                       repo-dir (config/get-repo-dir (state/get-current-repo))
+                       repo (state/get-current-repo)
+                       repo-dir (config/get-repo-dir repo)
                        [dir path] (cond
                                     ;; assume local file, relative path
                                     (not (string/starts-with? path "/"))
@@ -87,7 +88,11 @@
                                     :else ;; global file on native platform
                                     [nil path])]
                    (when (and format (contains? (gp-config/text-formats) format))
-                     (p/let [content (fs/read-file dir path)]
+                     (p/let [content (if (and (config/db-based-graph? repo)
+                                              ;; not global
+                                              (not (string/starts-with? path "/")))
+                                       (db/get-file path)
+                                       (fs/read-file dir path))]
                        (reset! *content (or content ""))))
                    (assoc state ::file-content *content)))
    :did-mount (fn [state]

+ 4 - 2
src/main/frontend/config.cljs

@@ -493,8 +493,10 @@
   ([]
    (get-custom-js-path (state/get-current-repo)))
   ([repo]
-   (when-let [repo-dir (get-repo-dir repo)]
-     (path/path-join repo-dir app-name custom-js-file))))
+   (if (db-based-graph? repo)
+     (str app-name "/" custom-js-file)
+     (when-let [repo-dir (get-repo-dir repo)]
+       (path/path-join repo-dir app-name custom-js-file)))))
 
 (defn get-block-hidden-properties
   []

+ 1 - 1
src/main/frontend/db/restore.cljs

@@ -149,7 +149,7 @@
           init-data' (doall
                       (keep (fn [b]
                               (let [eid (assign-id-to-uuid-fn (:uuid b))]
-                                (if (uuid-string? (:uuid b)) ; deleted blocks still refed
+                                (if (and (uuid-string? (:uuid b)) (not= (:type b) 3)) ; deleted blocks still refed
                                   [[eid :block/uuid (:uuid b)]]
                                   (->> b
                                        :datoms

+ 8 - 3
src/main/frontend/handler/code.cljs

@@ -8,7 +8,8 @@
             [frontend.state :as state]
             [goog.object :as gobj]
             [logseq.graph-parser.utf8 :as utf8]
-            [logseq.common.path :as path]))
+            [logseq.common.path :as path]
+            [frontend.handler.editor.impl.db :as db-impl]))
 
 (defn save-code-editor!
   []
@@ -20,7 +21,8 @@
       (let [textarea (.getTextArea editor)
             ds (.-dataset textarea)
             value (gobj/get textarea "value")
-            default-value (or (.-v ds) (gobj/get textarea "defaultValue"))]
+            default-value (or (.-v ds) (gobj/get textarea "defaultValue"))
+            repo (state/get-current-repo)]
         (when (not= value default-value)
           ;; update default value for the editor initial state
           (set! ds -v value)
@@ -40,9 +42,12 @@
               (state/set-edit-content! (state/get-edit-input-id) new-content)
               (editor-handler/save-block-if-changed! block new-content))
 
+            (and (not-empty (:file-path config))
+                 (config/db-based-graph? repo))
+            (db-impl/save-file! (:file-path config) value)
+
             (not-empty (:file-path config))
             (let [path (:file-path config)
-                  repo (state/get-current-repo)
                   repo-dir (config/get-repo-dir repo)
                   rpath (when (string/starts-with? path repo-dir)
                           (path/trim-dir-prefix repo-dir path))]

+ 10 - 1
src/main/frontend/handler/editor/impl/db.cljs

@@ -7,7 +7,8 @@
             [frontend.format.mldoc :as mldoc]
             [frontend.util :as util]
             [logseq.graph-parser.mldoc :as gp-mldoc]
-            [logseq.graph-parser.util.page-ref :as page-ref]))
+            [logseq.graph-parser.util.page-ref :as page-ref]
+            [frontend.handler.ui :as ui-handler]))
 
 (defn- remove-non-existed-refs!
   [refs]
@@ -82,3 +83,11 @@
                    (merge (if level {:block/level level} {}))
                    (replace-page-refs-with-ids))]
     result))
+
+(defn save-file!
+  [path content]
+  (when path
+    (db/transact! [{:file/path path
+                    :file/content content}])
+    (when (= path "logseq/custom.css")
+      (ui-handler/add-style-if-exists!))))

+ 6 - 3
src/main/frontend/handler/repo.cljs

@@ -548,11 +548,14 @@
           _ (ipc/ipc :db-new full-graph-name)
           _ (db/transact! full-graph-name [(react/kv :db/type "db")]
               {:skip-persist? true})
-          initial-data [{:file/path (str "logseq/" "config.edn")
+          initial-data [{:block/uuid (db/new-block-id)
+                         :file/path (str "logseq/" "config.edn")
                          :file/content config/config-default-content}
-                        {:file/path (str "logseq/" "custom.css")
+                        {:block/uuid (db/new-block-id)
+                         :file/path (str "logseq/" "custom.css")
                          :file/content ""}
-                        {:file/path (str "logseq/" "custom.js")
+                        {:block/uuid (db/new-block-id)
+                         :file/path (str "logseq/" "custom.js")
                          :file/content ""}]
           _ (db/transact! full-graph-name initial-data)
           _ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)

+ 42 - 35
src/main/frontend/handler/ui.cljs

@@ -128,41 +128,48 @@
 
 (defn exec-js-if-exists-&-allowed!
   [t]
-  (when-not (config/db-based-graph? (state/get-current-repo))
-    (when-let [href (or
-                     (state/get-custom-js-link)
-                     (config/get-custom-js-path))]
-      (let [k (str "ls-js-allowed-" href)
-            execed #(swap! *js-execed conj href)
-            execed? (contains? @*js-execed href)
-            ask-allow #(let [r (js/confirm (t :plugin/custom-js-alert))]
-                         (if r
-                           (storage/set k (js/Date.now))
-                           (storage/set k false))
-                         r)
-            allowed! (storage/get k)
-            should-ask? (or (nil? allowed!)
-                            (> (- (js/Date.now) allowed!) 604800000))]
-        (when (and (not execed?)
-                   (not= false allowed!))
-          (if (string/starts-with? href "http")
-            (when (or (not should-ask?)
-                      (ask-allow))
-              (load href #(do (js/console.log "[custom js]" href) (execed))))
-            (let [repo-dir (config/get-repo-dir (state/get-current-repo))
-                  rpath (path/relative-path repo-dir href)]
-              (p/let [exists? (fs/file-exists? repo-dir rpath)]
-                (when exists?
-                  (util/p-handle
-                   (fs/read-file repo-dir rpath)
-                   #(when-let [scripts (and % (string/trim %))]
-                      (when-not (string/blank? scripts)
-                        (when (or (not should-ask?) (ask-allow))
-                          (try
-                            (js/eval scripts)
-                            (execed)
-                            (catch :default e
-                              (js/console.error "[custom js]" e))))))))))))))))
+  (when-let [href (or
+                   (state/get-custom-js-link)
+                   (config/get-custom-js-path))]
+    (let [k (str "ls-js-allowed-" href)
+          execed #(swap! *js-execed conj href)
+          execed? (contains? @*js-execed href)
+          ask-allow #(let [r (js/confirm (t :plugin/custom-js-alert))]
+                       (if r
+                         (storage/set k (js/Date.now))
+                         (storage/set k false))
+                       r)
+          allowed! (storage/get k)
+          should-ask? (or (nil? allowed!)
+                          (> (- (js/Date.now) allowed!) 604800000))
+          exec-fn #(when-let [scripts (and % (string/trim %))]
+                     (when-not (string/blank? scripts)
+                       (when (or (not should-ask?) (ask-allow))
+                         (try
+                           (js/eval scripts)
+                           (execed)
+                           (catch :default e
+                             (js/console.error "[custom js]" e))))))]
+      (when (and (not execed?)
+                 (not= false allowed!))
+        (cond
+          (string/starts-with? href "http")
+          (when (or (not should-ask?)
+                    (ask-allow))
+            (load href #(do (js/console.log "[custom js]" href) (execed))))
+
+          (config/db-based-graph? (state/get-current-repo))
+          (when-let [script (db/get-file href)]
+            (exec-fn script))
+
+          :else
+          (let [repo-dir (config/get-repo-dir (state/get-current-repo))
+                rpath (path/relative-path repo-dir href)]
+            (p/let [exists? (fs/file-exists? repo-dir rpath)]
+              (when exists?
+                (util/p-handle
+                 (fs/read-file repo-dir rpath)
+                 exec-fn)))))))))
 
 (defn toggle-wide-mode!
   []