Skip to content

Commit

Permalink
use current dir on save as
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Mar 4, 2024
1 parent 4675858 commit e34411d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,30 @@
(.writeFile fs file-path (pr-str data) #js {:encoding "utf-8"}
(fn [_err] (f (serialize-document data file-path)))))

(defn save
(defn save-as
"Saves the provided data.
If there is no path defined, pick a new file.
https://www.electronjs.org/docs/api/dialog#dialogshowsavedialogbrowserwindow-options"
[data f]
(let [document (edn/read-string data)
file-path (:path document)
directory (and file-path (.dirname path file-path))
dialog-options (cond-> dialog-options
(and directory (.existsSync fs directory))
(assoc :defaultPath directory))]
(.then (.showSaveDialog dialog ^js @main-window (clj->js dialog-options))
(fn [^js/Promise file]
(when-not (.-canceled file)
(write-file (.-filePath file) document f))))))

(defn save
[data f]
(let [document (edn/read-string data)
file-path (:path document)]
(if (and file-path (.existsSync fs file-path))
(write-file file-path document f)
(.then (.showSaveDialog dialog ^js @main-window (clj->js dialog-options))
(fn [^js/Promise file]
(when-not (.-canceled file)
(write-file (.-filePath file) document f)))))))
(save-as data f))))

(defn read-file
[file-path f]
Expand Down
3 changes: 2 additions & 1 deletion src/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"openDirectory" (.showItemInFolder shell (.-data args))
"openDocument" (file/open (.-data args) #(send-to-renderer "fileLoaded" %))
"saveDocument" (file/save (.-data args) #(send-to-renderer "fileSaved" %))
"saveDocumentAs" (file/save-as (.-data args) #(send-to-renderer "fileSaved" %))
"export" (file/export (.-data args))))

(defn register-window-events!
Expand Down Expand Up @@ -94,7 +95,7 @@
:height (.-height win-state)
:backgroundColor "#313131"
:titleBarStyle (when (= (.platform os) "darwin") "hidden")
:trafficLightPosition #js { :x 8 :y 10 }
:trafficLightPosition #js {:x 8 :y 10}
:icon (.join path js/__dirname "/public/img/icon.png")
:frame false
:show false
Expand Down
7 changes: 2 additions & 5 deletions src/renderer/document/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,9 @@
(rf/reg-event-fx
:document/save-as
(fn [{:keys [db]} [_]]
(let [document (-> db
save-format
;; Remove the path to trigger a file selection dialog.
(dissoc :path))]
(let [document (save-format db)]
(if platform/electron?
{:send-to-main {:action "saveDocument" :data (pr-str document)}}
{:send-to-main {:action "saveDocumentAs" :data (pr-str document)}}
{::save document}))))

(rf/reg-event-db
Expand Down

0 comments on commit e34411d

Please sign in to comment.