-
Notifications
You must be signed in to change notification settings - Fork 58
/
parser.cljs
91 lines (75 loc) · 2.64 KB
/
parser.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(ns markright.parser
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [om.next :as om :refer-macros [defui]]
[electron.ipc :as ipc]
[cljs.core.async :as async :refer [chan put! pub sub unsub]]))
(defonce app-state (atom {:app/text ""
:app/force-overwrite false
:app/filepath ""
:app/saved-text ""}))
(defonce root-channel
(chan))
(defmulti read om/dispatch)
(defmethod read :default
[{:keys [state]} k _]
(let [st @state]
(find st k)
(if-let [[_ v] (find st k)]
{:value v}
{:value :not-found})))
(defmulti mutate om/dispatch)
(defmethod mutate 'app/html
[{:keys [state]} _ {:keys [html]}]
{:value [:app/html]
:action #(swap! state assoc-in [:app/html] html)})
(defmethod mutate 'app/text
[{:keys [state]} _ {:keys [text]}]
{:value [:app/text]
:action #(swap! state assoc-in [:app/text] text)})
(defmethod mutate 'app/transact-overwrite
[{:keys [state]} _ {:keys [text]}]
{:value [:app/force-overwrite]
:action #(swap! state assoc-in [:app/force-overwrite] false)})
(defmethod mutate 'app/reset-saved-text
[{:keys [state]} _ _]
{:action #(swap! state assoc-in [:app/saved-text] (@state :app/text))})
(defmethod mutate 'app/load-content
[{:keys [state]} _ {:keys [filepath content]}]
{:action #(swap! state assoc :app/filepath filepath
:app/force-overwrite true
:app/text content
:app/saved-text content)})
(def reconciler
(om/reconciler
{:state app-state
:parser (om/parser {:read read :mutate mutate :force-overwrite false})}))
(defmethod ipc/process-call :get-current-file
[msg reply]
(reply (get @app-state :app/filepath)))
(defmethod ipc/process-call :get-current-content
[msg reply]
(reply (get @app-state :app/text)))
(defmethod ipc/process-call :get-is-saved
[msg reply]
(reply (= (get @app-state :app/saved-text) (get @app-state :app/text))))
(defmethod ipc/process-cast :load-file
[{:keys [file content]}]
(swap! app-state assoc
:app/force-overwrite true
:app/text content
:app/saved-text content
:app/filepath file))
(defmethod ipc/process-cast :set-current-file
[{:keys [file content]}]
(swap! app-state assoc
:app/saved-text content
:app/filepath file))
(defmethod ipc/process-cast :set-saved-content
[{:keys [content]}]
(go (>! root-channel #(om/transact! % `[(app/reset-saved-text)]))))
(comment
(let [content]
(if (nil? state-path)
(save-file-as!)
(do
(swap! parser/app-state assoc :app/saved-text content)))))