|
1 | 1 | (ns lt.plugins.mermaid
|
2 | 2 | (:require [lt.object :as object]
|
3 | 3 | [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]]) |
5 | 10 | (:require-macros [lt.macros :refer [defui behavior]]))
|
6 | 11 |
|
7 |
| -(defui hello-panel [this] |
8 |
| - [:h1 "Hello from mermaid"]) |
| 12 | +(defui chart-panel [this] |
| 13 | + [:div.mermaid (bound this :user-input)]) |
9 | 14 |
|
10 |
| -(object/object* ::mermaid.hello |
11 |
| - :tags [:mermaid.hello] |
| 15 | +(object/object* ::mermaid.chart |
12 | 16 | :behaviors [::on-close-destroy]
|
13 | 17 | :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))) |
16 | 22 |
|
17 | 23 | (behavior ::on-close-destroy
|
18 | 24 | :triggers #{:close}
|
19 | 25 | :desc "mermaid: Close tab and tabset as well if last tab"
|
20 | 26 | :reaction (fn [this]
|
21 |
| - (when-let [ts (:lt.objs.tabs/tabset @this)] |
22 |
| - (when (= (count (:objs @ts)) 1) |
23 |
| - (tabs/rem-tabset ts))) |
24 | 27 | (object/raise this :destroy)))
|
25 | 28 |
|
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)))))) |
27 | 39 |
|
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" |
30 | 47 | :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