From 448c8f9c3068cb7668fdeb86034b7a2f77752c58 Mon Sep 17 00:00:00 2001 From: Konstantinos Kaloutas Date: Thu, 29 Feb 2024 18:32:33 +0200 Subject: [PATCH] enhance file loading --- src/file.cljs | 11 ++++++----- src/renderer/document/events.cljs | 14 +++++++++----- src/renderer/document/handlers.cljs | 11 ++++++----- src/renderer/document/subs.cljs | 5 +++++ src/renderer/window/views.cljs | 4 +++- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/file.cljs b/src/file.cljs index 6f4bb165..3e700eed 100644 --- a/src/file.cljs +++ b/src/file.cljs @@ -2,6 +2,7 @@ (:require ["electron" :refer [app dialog]] ["fs" :as fs] + ["path" :as path] #_[cognitect.transit :as tr])) (def main-window (atom nil)) @@ -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 diff --git a/src/renderer/document/events.cljs b/src/renderer/document/events.cljs index 8d7024dc..b9daa0ba 100644 --- a/src/renderer/document/events.cljs +++ b/src/renderer/document/events.cljs @@ -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 diff --git a/src/renderer/document/handlers.cljs b/src/renderer/document/handlers.cljs index 4e56bef7..2e7db008 100644 --- a/src/renderer/document/handlers.cljs +++ b/src/renderer/document/handlers.cljs @@ -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))))) diff --git a/src/renderer/document/subs.cljs b/src/renderer/document/subs.cljs index 669baa9a..4629ee6d 100644 --- a/src/renderer/document/subs.cljs +++ b/src/renderer/document/subs.cljs @@ -40,6 +40,11 @@ :<- [:document/active] :-> :title) +(rf/reg-sub + :document/path + :<- [:document/active] + :-> :path) + (rf/reg-sub :document/elements :<- [:document/active] diff --git a/src/renderer/window/views.cljs b/src/renderer/window/views.cljs index 0ca52f97..5862f126 100644 --- a/src/renderer/window/views.cljs +++ b/src/renderer/window/views.cljs @@ -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")}