Skip to content

Commit

Permalink
enhance file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Feb 29, 2024
1 parent a37a794 commit 448c8f9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
["electron" :refer [app dialog]]
["fs" :as fs]
["path" :as path]
#_[cognitect.transit :as tr]))

(def main-window (atom nil))
Expand Down Expand Up @@ -35,11 +36,11 @@
(.then (.showOpenDialog dialog ^js @main-window (clj->js dialog-options))
(fn [^js/Promise file]
(when-not (.-canceled file)
(.readFile fs
(first (js->clj (.-filePaths file)))
#js {:encoding "utf-8"}
(fn [_err data]
(f data)))))))
(let [file-path (first (js->clj (.-filePaths file)))]
(.readFile fs file-path #js {:encoding "utf-8"}
(fn [_err data] (f {:path file-path
:name (.basename path file-path)
:data data}))))))))

(def export-options
{:defaultPath default-path
Expand Down
14 changes: 9 additions & 5 deletions src/renderer/document/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,23 @@
(rf/reg-event-fx
:document/load
(fn [{:keys [db]} [_ data]]
(let [_ (js/console.log data)
document (-> data
(let [document (-> (.-data data)
edn/read-string
(update-in [:history :states] dd/expand))]
(assoc :path (.-path data)
:title (.-name data))
(update-in [:history] dissoc :states :position)
;; FIXME: Still contains cached values after expand.
#_(update-in [:history :states] dd/expand))]
{:db (-> db
(h/create-tab document))
(h/create-tab document)
(history.h/finalize "Load document"))
:dispatch [:center]})))

(rf/reg-event-fx
:document/save
(fn [{:keys [db]} [_]]
(let [document (get-in db [:documents (:active-document db)])
duped (update-in document [:history :states] dd/de-dupe)]
duped (update-in document [:history :states] dd/de-dupe-eq)]
{:send-to-main {:action "saveDocument" :data (pr-str duped)}})))

(rf/reg-event-fx
Expand Down
11 changes: 6 additions & 5 deletions src/renderer/document/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
(defn create-tab
[db document]
(let [key (or (:key document) (uuid/generate))
title (str "Untitled-" (inc (count (:documents db))))
title (or (:title document) (str "Untitled-" (inc (count (:documents db)))))
document-tabs (:document-tabs db)
active-index (.indexOf document-tabs (:active-document db))
open? (some #{key} document-tabs)
document (merge document {:key key :title title})]
(-> db
(assoc-in [:documents key] document)
(update :document-tabs #(vec/add % (inc active-index) key))
(assoc :active-document key))))
(cond-> db
(not open?) (update :document-tabs #(vec/add % (inc active-index) key))
:always (-> (assoc-in [:documents key] document)
(assoc :active-document key)))))
5 changes: 5 additions & 0 deletions src/renderer/document/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
:<- [:document/active]
:-> :title)

(rf/reg-sub
:document/path
:<- [:document/active]
:-> :path)

(rf/reg-sub
:document/elements
:<- [:document/active]
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/window/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
:height "14px"}}]])
[:div.flex.relative.bg-secondary
[menubar/root]]
[:div.title-bar @(rf/subscribe [:document/title])]
[:div.title-bar (str (or @(rf/subscribe [:document/path])
@(rf/subscribe [:document/title]))
" - Repath Studio")]
[:div.flex.h-full.flex-1.drag]
[:div.bg-primary
{:class (when-not (or platform/electron? fullscreen?) "mr-1.5")}
Expand Down

0 comments on commit 448c8f9

Please sign in to comment.