Pārlūkot izejas kodu

feat: export custom css

Junyu Zhan 4 gadi atpakaļ
vecāks
revīzija
78afe3e602

+ 2 - 2
resources/js/preload.js

@@ -67,8 +67,8 @@ contextBridge.exposeInMainWorld('apis', {
    *
    * @param {string} html html file with embedded state
    */
-  exportPublishAssets(html) {
-    ipcRenderer.invoke('export-publish-assets', html)
+  exportPublishAssets(html, customCSSPath) {
+    ipcRenderer.invoke('export-publish-assets', html, customCSSPath)
   },
 
   /**

+ 12 - 9
src/electron/electron/core.cljs

@@ -58,37 +58,40 @@
        (callback #js {:path path}))))
   #(.unregisterProtocol protocol "assets"))
 
-(defn- handle-export-publish-assets [_event html]
+(defn- handle-export-publish-assets [_event html custom-css-path]
   (let [app-path (. app getAppPath)
         paths (js->clj (. dialog showOpenDialogSync (clj->js {:properties ["openDirectory" "createDirectory" "promptToCreate", "multiSelections"]})))]
     (when (seq paths)
       (let [root-dir (first paths)
             static-dir (path/join root-dir "static")
-            path (path/join root-dir "index.html")]
+            index-html-path (path/join root-dir "index.html")]
         (p/let [_ (. fs ensureDir static-dir)
                 _ (p/all  (concat
-                           [(. fs writeFile path html)
+                           [(. fs writeFile index-html-path html)
+
                             (. fs copy (path/join app-path "404.html") (path/join root-dir "404.html"))]
 
                            (map
-                             (fn [part]
-                               (. fs copy (path/join app-path part) (path/join static-dir part)))
-                             ["css" "fonts" "icons" "img" "js"])))
+                            (fn [part]
+                              (. fs copy (path/join app-path part) (path/join static-dir part)))
+                            ["css" "fonts" "icons" "img" "js"])))
+                custom-css (. fs readFile custom-css-path)
+                _ (. fs appendFile (path/join static-dir "css" "style.css") custom-css)
                 js-files ["main.js" "code-editor.js" "excalidraw.js"]
                 _ (p/all (map (fn [file]
                                 (. fs removeSync (path/join static-dir "js" file)))
-                           js-files))
+                              js-files))
                 _ (p/all (map (fn [file]
                                 (. fs moveSync
                                    (path/join static-dir "js" "publishing" file)
                                    (path/join static-dir "js" file)))
-                           js-files))
+                              js-files))
                 _ (. fs removeSync (path/join static-dir "js" "publishing"))
                 ;; remove source map files
                 ;; TODO: ugly, replace with ls-files and filter with ".map"
                 _ (p/all (map (fn [file]
                                 (. fs removeSync (path/join static-dir "js" (str file ".map"))))
-                           ["main.js" "code-editor.js" "excalidraw.js" "age-encryption.js"]))]
+                              ["main.js" "code-editor.js" "excalidraw.js" "age-encryption.js"]))]
           (. dialog showMessageBox (clj->js {:message (str "Export publish assets to " root-dir " successfully")})))))))
 
 (defn setup-app-manager!

+ 5 - 4
src/main/frontend/handler/export.cljs

@@ -3,6 +3,7 @@
             [frontend.db :as db]
             [frontend.format.protocol :as fp]
             [frontend.format :as f]
+            [frontend.config :as config]
             [datascript.core :as d]
             [frontend.util :as util]
             [cljs-bean.core :as bean]
@@ -88,7 +89,7 @@
           html-str     (str "data:text/html;charset=UTF-8,"
                             (js/encodeURIComponent raw-html-str))]
       (if (util/electron?)
-        (js/window.apis.exportPublishAssets raw-html-str)
+        (js/window.apis.exportPublishAssets raw-html-str (config/get-custom-css-path))
         (when-let [anchor (gdom/getElement "download-as-html")]
           (.setAttribute anchor "href" html-str)
           (.setAttribute anchor "download" "index.html")
@@ -185,9 +186,9 @@
                     [?e :page/name ?n]
                     [?e :page/original-name ?n2]] (db/get-conn repo))
              (mapv (fn [[name origin-name file-path]]
-                    (if (= name origin-name)
-                      [[name file-path]]
-                      [[name file-path] [origin-name file-path]])))
+                     (if (= name origin-name)
+                       [[name file-path]]
+                       [[name file-path] [origin-name file-path]])))
              (apply concat)
              (mapv (fn [[page-name file-path]] [page-name (:file/path file-path)]))
              (d/q '[:find ?n ?c