Skip to content

Commit

Permalink
fix: export issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Jan 22, 2024
1 parent 6ed39e7 commit 6725c83
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/main/frontend/db_worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@
(get-all-pages
[this repo]
(when-let [conn (worker-state/get-datascript-conn repo)]
(bean/->js (worker-export/get-all-pages repo @conn))))
(pr-str (worker-export/get-all-pages repo @conn))))

(get-all-page->content
[this repo]
(when-let [conn (worker-state/get-datascript-conn repo)]
(bean/->js (worker-export/get-all-page->content repo @conn))))
(pr-str (worker-export/get-all-page->content repo @conn))))

;; RTC
(rtc-start
Expand Down
15 changes: 6 additions & 9 deletions src/main/frontend/extensions/zip.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns frontend.extensions.zip
(:require [clojure.string :as string]
[frontend.config :as config]
["jszip" :as JSZip]
[promesa.core :as p]))

Expand All @@ -11,15 +10,13 @@
(aset args "lastModified" last-modified)
(js/File. blob-content file-name args)))

(defn make-zip [zip-filename file-name->content repo]
(defn make-zip [zip-filename file-name->content _repo]
(let [zip (JSZip.)
zip-foldername (subs zip-filename (inc (string/last-index-of zip-filename "/")))
src-filepath (string/replace repo config/local-db-prefix "")
folder (.folder zip zip-foldername)]
folder (.folder zip zip-filename)]
(doseq [[file-name content] file-name->content]
(.file folder (-> file-name
(string/replace src-filepath "")
(string/replace #"^/+" ""))
content))
(when-not (string/blank? content)
(.file folder (-> file-name
(string/replace #"^/+" ""))
content)))
(p/let [zip-blob (.generateAsync zip #js {:type "blob"})]
(make-file zip-blob (str zip-filename ".zip") {:type "application/zip"}))))
32 changes: 17 additions & 15 deletions src/main/frontend/handler/export.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

(defn export-repo-as-zip!
[repo]
(p/let [files (export-common-handler/<get-file-contents repo)
(p/let [files (export-common-handler/<get-file-contents repo "md")
[owner repo-name] (util/get-git-owner-and-repo repo)
repo-name (str owner "-" repo-name)
files (map (fn [{:keys [path content]}] [path content]) files)]
Expand Down Expand Up @@ -102,16 +102,16 @@
(p/let [pages (export-common-handler/<get-all-pages repo)]
{:version 1
:blocks
(nested-select-keys pages
[:block/id
(nested-select-keys [:block/id
:block/type
:block/page-name
:block/properties
:block/format
:block/children
:block/content
:block/created-at
:block/updated-at])}))
:block/updated-at]
pages)}))

(defn- file-name [repo extension]
(-> (string/replace repo config/local-db-prefix "")
Expand All @@ -121,23 +121,25 @@

(defn- <export-repo-as-edn-str [repo]
(p/let [result (<build-blocks repo)]
(prn :debug :result result)
(let [sb (StringBuffer.)]
(pprint/pprint result (StringBufferWriter. sb))
(str sb))))

(defn export-repo-as-edn!
[repo]
(when-let [edn-str (<export-repo-as-edn-str repo)]
(let [data-str (some->> edn-str
js/encodeURIComponent
(str "data:text/edn;charset=utf-8,"))
filename (file-name repo :edn)]
(if (mobile-util/native-platform?)
(export-file-on-mobile edn-str filename)
(when-let [anchor (gdom/getElement "download-as-edn-v2")]
(.setAttribute anchor "href" data-str)
(.setAttribute anchor "download" filename)
(.click anchor))))))
(p/let [edn-str (<export-repo-as-edn-str repo)]
(when edn-str
(let [data-str (some->> edn-str
js/encodeURIComponent
(str "data:text/edn;charset=utf-8,"))
filename (file-name repo :edn)]
(if (mobile-util/native-platform?)
(export-file-on-mobile edn-str filename)
(when-let [anchor (gdom/getElement "download-as-edn-v2")]
(.setAttribute anchor "href" data-str)
(.setAttribute anchor "download" filename)
(.click anchor)))))))

(defn- nested-update-id
[vec-tree]
Expand Down
11 changes: 6 additions & 5 deletions src/main/frontend/handler/export/common.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[promesa.core :as p]
[frontend.persist-db.browser :as db-browser]
[cljs-bean.core :as bean]
[frontend.worker.export :as worker-export]))
[frontend.worker.export :as worker-export]
[clojure.edn :as edn]))

;;; TODO: split frontend.handler.export.text related states
(def ^:dynamic *state*
Expand Down Expand Up @@ -194,19 +195,19 @@
[repo]
(when-let [^object worker @db-browser/*worker]
(p/let [result (.get-all-pages worker repo)]
(bean/->clj result))))
(edn/read-string result))))

(defn <get-all-page->content
[repo]
(when-let [^object worker @db-browser/*worker]
(p/let [result (.get-all-page->content worker repo)]
(bean/->clj result))))
(edn/read-string result))))

(defn <get-file-contents
[repo]
[repo suffix]
(p/let [page->content (<get-all-page->content repo)]
(clojure.core/map (fn [[page-title content]]
{:path (str page-title ".md")
{:path (str page-title "."suffix)
:content content
:title page-title
:format :markdown})
Expand Down
13 changes: 9 additions & 4 deletions src/main/frontend/handler/export/opml.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
[goog.dom :as gdom]
[hiccups.runtime :as h]
[frontend.format.mldoc :as mldoc]
[promesa.core :as p]))
[promesa.core :as p]
[frontend.config :as config]))

;;; *opml-state*
(def ^:private ^:dynamic
Expand Down Expand Up @@ -451,16 +452,20 @@
[files options]
(mapv
(fn [{:keys [path content title format]}]
(when title
(when (and title (not (string/blank? content)))
(util/profile (print-str :export-files-as-opml path)
[path (export-helper content format options :title title)])))
files))

(defn export-repo-as-opml!
[repo]
(p/let [files (common/<get-file-contents repo)]
(p/let [files (common/<get-file-contents repo "opml")]
(when (seq files)
(let [files (export-files-as-opml files nil)
(let [repo (-> repo
(string/replace config/db-version-prefix "")
(string/replace config/local-db-prefix ""))
files (->> (export-files-as-opml files nil)
(clojure.core/remove nil?))
zip-file-name (str repo "_opml_" (quot (util/time-ms) 1000))]
(p/let [zipfile (zip/make-zip zip-file-name files repo)]
(when-let [anchor (gdom/getElement "export-as-opml")]
Expand Down
Loading

0 comments on commit 6725c83

Please sign in to comment.