Skip to content

Commit

Permalink
(wip) save/load document
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Feb 28, 2024
1 parent 4065428 commit 4dee047
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 45 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"@sentry/electron": "4.18.0",
"@webref/css": "6.12.0",
"electron-extension-installer": "1.2.0",
"electron-log": "5.1.1",
"electron-updater": "6.1.8",
"electron-window-state": "5.0.3",
"font-scanner": "0.2.1",
Expand Down
17 changes: 8 additions & 9 deletions src/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
(def dialog-options
{:defaultPath default-path
;; https://www.electronjs.org/docs/api/structures/file-filter#filefilter-object
:filters [{:name "rp"
:extensions ["rp"]}]})
:filters [{:name "edn"
:extensions ["edn"]}]})

(defn save
"Saves the provided data.
Expand All @@ -28,19 +28,18 @@
(when-not (.-canceled file)
(.writeFileSync fs (.-filePath file) data "utf-8")))))

(defn open-to-renderer
[_err data]
(let [web-contents (.-webContents ^js @main-window)]
(.send web-contents "fromMain" (clj->js {:action "openDocument" :data data}))))

(defn open
"Opens a file.
https://www.electronjs.org/docs/api/dialog#dialogshowopendialogsyncbrowserwindow-options"
[]
[f]
(.then (.showOpenDialog dialog ^js @main-window (clj->js dialog-options))
(fn [^js/Promise file]
(when-not (.-canceled file)
(.readFileSync fs (.-filePath file) "utf-8" open-to-renderer)))))
(.readFile fs
(first (js->clj (.-filePaths file)))
#js {:encoding "utf-8"}
(fn [_err data]
(f data)))))))

(def export-options
{:defaultPath default-path
Expand Down
21 changes: 13 additions & 8 deletions src/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#_["@sentry/electron/main" :as sentry-electron-main]
["electron-extension-installer" :refer [REACT_DEVELOPER_TOOLS]]
["electron-extension-installer$default" :as installExtension]
["electron-log/main" :as log]
#_["electron-updater" :as updater]
["electron-window-state" :as window-state-keeper]
["electron" :refer [app shell ipcMain BrowserWindow clipboard nativeTheme]]
Expand All @@ -13,6 +14,16 @@
(def main-window (atom nil))
(def loading-window (atom nil))

(defn send-to-renderer
([action]
(send-to-renderer action nil))
([action data]
(.send (.-webContents ^js @main-window) "fromMain" (clj->js {:action action
:data data}))))
(defn load-file
[data]
(send-to-renderer "loadDocument" data))

(defn to-main-api
[args]
(case (.-action args)
Expand All @@ -27,17 +38,10 @@
"openRemoteUrl" (.openExternal shell (.-data args))
;; https://www.electronjs.org/docs/api/clipboard#clipboardwritedata-type
"writeToClipboard" (.write clipboard (.-data args))
"openDocument" (file/open)
"openDocument" (file/open load-file)
"saveDocument" (file/save (.-data args))
"export" (file/export (.-data args))))

(defn send-to-renderer
([action]
(send-to-renderer action nil))
([action data]
(.send (.-webContents ^js @main-window) "fromMain" (clj->js {:action action
:data data}))))

(defn register-window-events!
[]
(doseq
Expand Down Expand Up @@ -130,6 +134,7 @@

(defn ^:export init []
#_(sentry-electron-main/init (clj->js config/sentry-options))
(.initialize log)
(.on app "window-all-closed" #(when-not (= js/process.platform "darwin")
(.quit app)))
(.on app "ready" init-loading-window))
3 changes: 2 additions & 1 deletion src/renderer/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
#_["@sentry/electron/renderer" :as sentry-electron-renderer]
#_["@sentry/react" :as sentry-react]
["electron-log/renderer"]
["paper" :refer [paper]]
[config]
[devtools.core :as devtools]
Expand Down Expand Up @@ -78,7 +79,7 @@
"windowLeavedFullscreen" (rf/dispatch [:window/set-fullscreen? false])
"windowMinimized" (rf/dispatch [:window/set-minimized? true])
"windowRestored" (rf/dispatch [:window/set-minimized? false])
"openDocument" (js/console.log (.-data data))))))
"loadDocument" (rf/dispatch [:document/load (.-data data)])))))

(defn load-system-fonts
[]
Expand Down
35 changes: 19 additions & 16 deletions src/renderer/document/events.cljs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(ns renderer.document.events
(:require
[clojure.edn :as edn]
[de-dupe.core :as dd]
[re-frame.core :as rf]
[re-frame.interceptor :refer [->interceptor get-effect get-coeffect assoc-coeffect assoc-effect]]
[renderer.document.db :as db]
[renderer.document.handlers :as h]
[renderer.element.handlers :as element.h]
[renderer.history.handlers :as history.h]
[renderer.utils.uuid :as uuid]
[renderer.utils.vec :as vec]))

(def active-document-path
Expand Down Expand Up @@ -130,32 +130,35 @@
(rf/reg-event-fx
:document/new
(fn [{:keys [db]} [_]]
{:db (let [key (uuid/generate)
title (str "Untitled-" (inc (count (:documents db))))
document-tabs (:document-tabs db)
active-index (.indexOf document-tabs (:active-document db))
document (merge db/default-document {:key key
:title title})]
(-> db
(assoc-in [:documents key] document)
(update :document-tabs #(vec/add % (inc active-index) key))
(assoc :active-document key)
(element.h/create {:tag :svg
:attrs {:width "800" :height "600"}})
element.h/deselect
(history.h/finalize "Create document")))
{:db (-> db
(h/create-tab db/default-document)
(element.h/create {:tag :svg
:attrs {:width "800" :height "600"}})
element.h/deselect
(history.h/finalize "Create document"))
:dispatch [:center]}))

(rf/reg-event-fx
:document/open
(fn [_ [_]]
{:send-to-main {:action "openDocument"}}))

(rf/reg-event-fx
:document/load
(fn [{:keys [db]} [_ data]]
(let [_ (js/console.log data)
document (-> data
edn/read-string
(update-in [:history :states] dd/expand))]
{:db (-> db
(h/create-tab 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] dd/de-dupe)]
duped (update-in document [:history :states] dd/de-dupe)]
{:send-to-main {:action "saveDocument" :data (pr-str duped)}})))

(rf/reg-event-fx
Expand Down
17 changes: 16 additions & 1 deletion src/renderer/document/handlers.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(ns renderer.document.handlers)
(ns renderer.document.handlers
(:require
[renderer.utils.uuid :as uuid]
[renderer.utils.vec :as vec]))

(defn close
([{:keys [active-document] :as db}]
Expand All @@ -18,3 +21,15 @@
(defn expand-el
[{:keys [active-document] :as db} el-k]
(update-in db [:documents active-document :collapsed-keys] disj el-k))

(defn create-tab
[db document]
(let [key (or (:key document) (uuid/generate))
title (str "Untitled-" (inc (count (:documents db))))
document-tabs (:document-tabs db)
active-index (.indexOf document-tabs (:active-document db))
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))))
18 changes: 9 additions & 9 deletions src/renderer/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@
(fn [db [_]]
(dissoc db :restored?)))

#_(rf/reg-event-db
:set-lang
(fn [db [_ lang]]
(assoc db :lang lang)))

#_(rf/reg-event-db
:set-repl-mode
(fn [db [_ mode]]
(assoc db :repl-mode mode)))
(rf/reg-event-db
:set-lang
(fn [db [_ lang]]
(assoc db :lang lang)))

(rf/reg-event-db
:set-repl-mode
(fn [db [_ mode]]
(assoc db :repl-mode mode)))

(rf/reg-event-db
:toggle-debug-info
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/reepl/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
[mode]
(let [repl-mode @(rf/subscribe [:repl-mode])
active? (= repl-mode mode)]
[:button.button.p-1.rounded.mx-1.uppercase.leading-none.text-2xs.h-auto
[:button.button.p-1.rounded.mx-1.leading-none.text-2xs.h-auto
{:class (when active? "selected")
:on-click #(rf/dispatch [:set-repl-mode mode])}
mode]))
Expand Down

0 comments on commit 4dee047

Please sign in to comment.