forked from cljfx/cljfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe20_markdown_editor.clj
271 lines (243 loc) · 9.52 KB
/
e20_markdown_editor.clj
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
(ns e20-markdown-editor
(:require [cljfx.api :as fx]
[clojure.string :as str]
[clojure.core.cache :as cache])
(:import [de.codecentric.centerdevice.javafxsvg SvgImageLoaderFactory]
[de.codecentric.centerdevice.javafxsvg.dimension PrimitiveDimensionProvider]
[java.awt Desktop]
[java.io File]
[java.net URI]
[org.commonmark.node Node]
[org.commonmark.parser Parser]))
;; does not work any more :(
#_(SvgImageLoaderFactory/install (PrimitiveDimensionProvider.))
(def *context
(atom
(fx/create-context {:typed-text (subs (slurp "README.md") 0 2000)}
#(cache/lru-cache-factory % :threshold 4096))))
(defn commonmark->clj [^Node node]
(let [tag (->> node
.getClass
.getSimpleName
(re-seq #"[A-Z][a-z]+")
(map str/lower-case)
(str/join "-")
keyword)
all-attrs (->> node
bean
(map (fn [[k v]]
[(->> k
name
(re-seq #"[A-Z]?[a-z]+")
(map str/lower-case)
(str/join "-")
keyword)
v]))
(into {}))]
{:tag tag
:attrs (dissoc all-attrs :next :previous :class :first-child :last-child :parent)
:children (->> node
.getFirstChild
(iterate #(.getNext ^Node %))
(take-while some?)
(mapv commonmark->clj))}))
(defn node-sub [context]
(-> (Parser/builder)
.build
(.parse (fx/sub-val context :typed-text))
commonmark->clj))
(defmulti handle-event :event/type)
(defmethod handle-event :default [e]
(prn e))
(defmethod handle-event ::type-text [{:keys [fx/event fx/context]}]
{:context (fx/swap-context context assoc :typed-text event)})
(defmulti md->fx :tag)
(defn md-view [{:keys [node]}]
(md->fx node))
(defmethod md->fx :heading [{children :children {:keys [level]} :attrs}]
{:fx/type :text-flow
:style-class ["heading" (str "level-" level)]
:children (for [node children]
{:fx/type md-view
:node node})})
(defmethod md->fx :paragraph [{children :children}]
{:fx/type :text-flow
:style-class "paragraph"
:children (for [node children]
{:fx/type md-view
:node node})})
(defmethod md->fx :text [{{:keys [literal]} :attrs}]
{:fx/type :text
:cache true
:cache-hint :speed
:text literal})
(defmethod md->fx :code [{{:keys [literal]} :attrs}]
{:fx/type :label
:cache true
:cache-hint :speed
:style-class "code"
:text literal})
(defmethod md->fx :fenced-code-block [{{:keys [literal]} :attrs}]
{:fx/type :v-box
:padding {:top 9}
:children [{:fx/type :scroll-pane
:style-class ["scroll-pane" "code-block"]
:fit-to-width true
:content {:fx/type :label
:cache true
:cache-hint :speed
:max-width ##Inf
:min-width :use-pref-size
:text literal}}]})
(defmethod md->fx :indented-code-block [{{:keys [literal]} :attrs}]
{:fx/type :v-box
:padding {:top 9}
:children [{:fx/type :scroll-pane
:style-class ["scroll-pane" "code-block"]
:fit-to-width true
:content {:fx/type :label
:cache true
:cache-hint :speed
:max-width ##Inf
:min-width :use-pref-size
:text literal}}]})
(defmethod md->fx :link [{{:keys [^String destination]} :attrs children :children}]
(let [link {:fx/type :hyperlink
:on-action (fn [_]
(future
(try
(if (str/starts-with? destination "http")
(.browse (Desktop/getDesktop) (URI. destination))
(.open (Desktop/getDesktop) (File. destination)))
(catch Exception e
(.printStackTrace e)))))}]
(if (and (= 1 (count children))
(= :text (:tag (first children))))
(assoc link :text (-> children first :attrs :literal))
(assoc link :graphic {:fx/type :h-box
:children (for [node children]
{:fx/type md-view
:node node})}))))
(defmethod md->fx :strong-emphasis [{:keys [children]}]
(if (and (= 1 (count children))
(= :text (:tag (first children))))
{:fx/type :text
:cache true
:cache-hint :speed
:style-class "strong-emphasis"
:text (-> children first :attrs :literal)}
{:fx/type :h-box
:cache true
:style-class "strong-emphasis"
:children (for [node children]
{:fx/type md-view
:node node})}))
(defmethod md->fx :emphasis [{:keys [children]}]
(if (and (= 1 (count children))
(= :text (:tag (first children))))
{:fx/type :text
:cache true
:cache-hint :speed
:style-class "emphasis"
:text (-> children first :attrs :literal)}
{:fx/type :h-box
:style-class "emphasis"
:children (for [node children]
{:fx/type md-view
:node node})}))
(defmethod md->fx :soft-line-break [_]
{:fx/type :text
:text " "})
(defmethod md->fx :document [{:keys [children]}]
{:fx/type :v-box
:style-class "document"
:children (for [node children]
{:fx/type md-view
:node node})})
(defmethod md->fx :image [{{:keys [destination]} :attrs}]
{:fx/type :image-view
:image {:url (if (str/starts-with? destination "http")
destination
(str "file:" destination))
:background-loading true}})
(defmethod md->fx :bullet-list [{{:keys [bullet-marker]} :attrs children :children}]
{:fx/type :v-box
:style-class "md-list"
:children (for [node children]
{:fx/type :h-box
:alignment :baseline-left
:spacing 4
:children [{:fx/type :label
:min-width :use-pref-size
:cache true
:cache-hint :speed
:text (str bullet-marker)}
{:fx/type md-view
:node node}]})})
(defmethod md->fx :ordered-list [{{:keys [delimiter start-number]} :attrs
children :children}]
{:fx/type :v-box
:style-class "md-list"
:children (map (fn [child number]
{:fx/type :h-box
:alignment :baseline-left
:spacing 4
:children [{:fx/type :label
:cache true
:cache-hint :speed
:min-width :use-pref-size
:text (str number delimiter)}
(assoc (md->fx child)
:h-box/hgrow :always)]})
children
(range start-number ##Inf))})
(defmethod md->fx :list-item [{:keys [children]}]
{:fx/type :v-box
:children (for [node children]
{:fx/type md-view
:node node})})
(defmethod md->fx :default [{:keys [tag attrs children]}]
{:fx/type :v-box
:children [{:fx/type :label
:cache true
:cache-hint :speed
:style {:-fx-background-color :red}
:text (str tag " " attrs)}
{:fx/type :v-box
:padding {:left 10}
:children (for [node children]
{:fx/type md-view
:node node})}]})
(defn note-input [{:keys [fx/context]}]
{:fx/type :text-area
:style-class "input"
:text (fx/sub-val context :typed-text)
:on-text-changed {:event/type ::type-text :fx/sync true}})
(defn note-preview [{:keys [fx/context]}]
{:fx/type :scroll-pane
:fit-to-width true
:content {:fx/type md-view
:node (fx/sub-ctx context node-sub)}})
(def app
(fx/create-app *context
:event-handler handle-event
:desc-fn (fn [_]
{:fx/type :stage
:showing true
:width 960
:height 540
:scene {:fx/type :scene
:stylesheets #{"markdown.css"}
:root {:fx/type :grid-pane
:padding 10
:hgap 10
:column-constraints [{:fx/type :column-constraints
:percent-width 100/2}
{:fx/type :column-constraints
:percent-width 100/2}]
:row-constraints [{:fx/type :row-constraints
:percent-height 100}]
:children [{:fx/type note-input
:grid-pane/column 0}
{:fx/type note-preview
:grid-pane/column 1}]}}})))