Skip to content

Commit 4f626e5

Browse files
committed
First pass at live mermaid cmd
1 parent ad9217d commit 4f626e5

6 files changed

+227
-16
lines changed

mermaid.behaviors

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
2-
[:app :lt.objs.plugins/load-js "mermaid_compiled.js"]
3-
[:myplugin.hello :lt.plugins.mermaid/on-close-destroy]
2+
[:app :lt.objs.plugins/load-js ["mermaid_compiled.js"]]
3+
[:app :lt.objs.plugins/load-css "mermaid.css"]
4+
[:mermaid.hello :lt.plugins.mermaid/on-close-destroy]
45
]

mermaid.css

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.mermaid g.node {
2+
color: black;
3+
}
4+
5+
.mermaid path.path {
6+
stroke: darkgrey !important;
7+
stroke-width: 2.5px !important;
8+
}
9+
10+
.mermaid marker path {
11+
fill: darkgrey !important;
12+
}

mermaid.full.min.js

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mermaid_compiled.js

+99
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mermaid_compiled.js.map

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lt/plugins/mermaid.cljs

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
11
(ns lt.plugins.mermaid
22
(:require [lt.object :as object]
33
[lt.objs.tabs :as tabs]
4-
[lt.objs.command :as cmd])
4+
[lt.objs.command :as cmd]
5+
[lt.objs.editor :as editor]
6+
[lt.objs.editor.pool :as pool]
7+
[lt.util.load :as load]
8+
[lt.objs.plugins :as plugins]
9+
[crate.binding :refer [bound]])
510
(:require-macros [lt.macros :refer [defui behavior]]))
611

7-
(defui hello-panel [this]
8-
[:h1 "Hello from mermaid"])
12+
(defui chart-panel [this]
13+
[:div.mermaid (bound this :user-input)])
914

10-
(object/object* ::mermaid.hello
11-
:tags [:mermaid.hello]
15+
(object/object* ::mermaid.chart
1216
:behaviors [::on-close-destroy]
1317
:name "mermaid"
14-
:init (fn [this]
15-
(hello-panel this)))
18+
:user-input ""
19+
:init (fn [this filename]
20+
(object/update! this [:name] (constantly (str filename " - Live Mermaid")))
21+
(chart-panel this)))
1622

1723
(behavior ::on-close-destroy
1824
:triggers #{:close}
1925
:desc "mermaid: Close tab and tabset as well if last tab"
2026
:reaction (fn [this]
21-
(when-let [ts (:lt.objs.tabs/tabset @this)]
22-
(when (= (count (:objs @ts)) 1)
23-
(tabs/rem-tabset ts)))
2427
(object/raise this :destroy)))
2528

26-
(def hello (object/create ::mermaid.hello))
29+
(behavior ::read-editor
30+
:triggers [:change ::read-editor]
31+
:debounce 350
32+
:desc "Mermaid: Read the content inside an editor"
33+
:reaction (fn [ed]
34+
(object/assoc-in! (:mermaid @ed) [:user-input] (editor/->val ed))
35+
(try
36+
(js/mermaid.init)
37+
(catch :default e
38+
(println (.-message e))))))
2739

28-
(cmd/command {:command ::say-hello
29-
:desc "mermaid: Say Hello"
40+
(defn ensure-and-focus-second-tabset []
41+
(when (< (-> @tabs/multi :tabsets count) 2)
42+
(cmd/exec! :tabset.new))
43+
(cmd/exec! :tabset.next))
44+
45+
(cmd/command {:command :mermaid.watch-editor
46+
:desc "Mermaid: Watch this editor for changes"
3047
:exec (fn []
31-
(tabs/add-or-focus! hello))})
48+
;; Load hefty dependency dynamically
49+
(when-not js/window.mermaid
50+
(load/js (str plugins/user-plugins-dir "/mermaid/mermaid.full.min.js") true))
51+
(let [ed (pool/last-active)
52+
filename (get-in @ed [:info :name])
53+
mermaid (object/create ::mermaid.chart filename)]
54+
(ensure-and-focus-second-tabset)
55+
(tabs/add-or-focus! mermaid)
56+
(cmd/exec! :tabset.prev)
57+
(object/assoc-in! ed [:mermaid] mermaid)
58+
(object/add-behavior! ed ::read-editor)
59+
(object/raise ed ::read-editor)))})
60+
61+
(comment
62+
(-> js/mermaid))

0 commit comments

Comments
 (0)