Browse Source

fix: create a file when adding properties to an empty page

Tienson Qin 4 years ago
parent
commit
bb6386dbc0
2 changed files with 22 additions and 17 deletions
  1. 1 1
      src/main/frontend/db/model.cljs
  2. 21 16
      src/main/frontend/handler/page.cljs

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

@@ -561,7 +561,7 @@
 
 (defn get-page-properties-content
   [page]
-  (when-let [content (let [blocks (get-page-blocks page)]
+  (when-let [content (let [blocks (get-page-blocks-no-cache page)]
                        (and (:block/pre-block? (first blocks))
                             (:block/content (first blocks))))]
     (let [format (get-page-format page)]

+ 21 - 16
src/main/frontend/handler/page.cljs

@@ -91,22 +91,27 @@
 (defn page-add-properties!
   [page-name properties]
   (let [page (db/entity [:page/name page-name])
-        page-format (db/get-page-format page-name)
-        properties-content (db/get-page-properties-content page-name)
-        properties-content (if properties-content
-                             (string/trim properties-content)
-                             (config/properties-wrapper page-format))]
-    (let [file (db/entity (:db/id (:page/file page)))
-          file-path (:file/path file)
-          file-content (db/get-file file-path)
-          after-content (subs file-content (inc (count properties-content)))
-          new-properties-content (db/add-properties! page-format properties-content properties)
-          full-content (str new-properties-content "\n\n" (string/trim after-content))]
-      (file-handler/alter-file (state/get-current-repo)
-                               file-path
-                               full-content
-                               {:reset? true
-                                :re-render-root? true}))))
+        page-title (:or (:page/original-name page) (:page/name page))
+        file (:page/file page)]
+    (if file
+      (let [page-format (db/get-page-format page-name)
+            properties-content (db/get-page-properties-content page-name)
+            properties-content (if properties-content
+                                 (string/trim properties-content)
+                                 (config/properties-wrapper page-format))
+            file (db/entity (:db/id (:page/file page)))
+            file-path (:file/path file)
+            file-content (db/get-file file-path)
+            after-content (subs file-content (inc (count properties-content)))
+            new-properties-content (db/add-properties! page-format properties-content properties)
+            full-content (str new-properties-content "\n\n" (string/trim after-content))]
+        (file-handler/alter-file (state/get-current-repo)
+                                 file-path
+                                 full-content
+                                 {:reset? true
+                                  :re-render-root? true}))
+      (p/let [_ (create! page-name)]
+        (page-add-properties! page-name properties)))))
 
 (defn page-remove-property!
   [page-name k]