Skip to content

Commit

Permalink
feat: V1 new library export
Browse files Browse the repository at this point in the history
  • Loading branch information
motform committed Oct 5, 2024
1 parent f1adefe commit e1bc5f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/org/motform/multiverse/components/library.cljs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(ns org.motform.multiverse.components.library
(:require
[clojure.string :as str]
[nano-id.core :refer [nano-id]]
[org.motform.multiverse.routes :as routes]
[org.motform.multiverse.story :as story]
[org.motform.multiverse.util :as util]
[re-frame.core :as rf]))

(defn- format-date [date]
(-> (js/Intl.DateTimeFormat. "en-US" #js {:year "numeric" :month "2-digit" :day "2-digit"})
(.format date)))

(defn open-story [id]
(rf/dispatch [:story/active id])
(rf/dispatch [:page/active :page/story])
Expand All @@ -23,7 +21,7 @@
[:td model]
[:td (or prompt-version "N/A")]
[:td (count sentences)]
[:td (format-date updated)]]))
[:td (util/format-date updated)]]))

(defn LibraryItems []
[:table.library-items
Expand All @@ -37,13 +35,16 @@
[:th "Date"]]]
[:tbody
(for [story (reverse @(rf/subscribe [:db/stories]))]
^{:key (get-in story [:story/meta :story/id])} [LibraryItemRow story])]])
^{:key (get-in story [:story/meta :story/id])}
[LibraryItemRow story])]])

(defn export-library
"SOURCE: https://gist.github.com/zoren/cc74758198b503b1755b75d1a6b376e7"
[]
(let [library (js/Blob. #js [(prn-str @(rf/subscribe [:db/stories]))] #js {:type "application/edn"})
file-name (nano-id)
(let [stories @(rf/subscribe [:db/stories])
md (->> stories (map story/->md) (str/join "\n\n"))
library (js/Blob. #js [(prn-str md)] #js {:type "application/edn"})
file-name (str "multiverse-library-export-" (nano-id) ".md")
edn-url (js/URL.createObjectURL library)
anchor (doto (js/document.createElement "a")
(-> .-href (set! edn-url))
Expand Down
22 changes: 22 additions & 0 deletions src/org/motform/multiverse/story.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns org.motform.multiverse.story
(:require
[clojure.string :as str]
[org.motform.multiverse.util :as util]))

(defn ->sentence [text & {:keys [id path children]}]
Expand Down Expand Up @@ -30,3 +31,24 @@
:path (conj parent-path id)
:children [])))
{} child-pairs)))

(defn longest-sentence [story]
(let [sentences (:story/sentences story)
sentence-with-children (apply max-key #(count (:sentence/children (second %))) sentences)
base-sentence (second sentence-with-children)
children (:sentence/children base-sentence)
children-texts (map #(get-in sentences [% :sentence/text]) children)]
(str/join "\n" (cons (:sentence/text base-sentence) children-texts))))

;; TODO: Respect newline formatting in export
(defn ->md [story]
(let [meta (:story/meta story)]
(str
"# " (:story/title meta) "\n"
"---"
"id:" (:story/id meta) "\n"
"model:" (:story/model meta) "\n"
"prompt:" (:story/prompt meta) "\n"
"date:" (util/format-date (:story/updated meta)) "\n"
"---"
(longest-sentence story))))
4 changes: 4 additions & 0 deletions src/org/motform/multiverse/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
(update $ 0 str/capitalize) ; always capitalize the leading word
(str/join " " $))))

(defn format-date [date]
(-> (js/Intl.DateTimeFormat. "en-US" #js {:year "numeric" :month "2-digit" :day "2-digit"})
(.format date)))

;; Graphical elements

(defn Spinner []
Expand Down

0 comments on commit e1bc5f1

Please sign in to comment.