/
core.cljc
113 lines (93 loc) · 3.49 KB
/
core.cljc
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
(ns pinkgorilla.notebook.core
(:require
[clojure.string :as str]
[pinkgorilla.notebook.uuid :refer [uuid]]
[pinkgorilla.encoding.decode :refer [decode]]
[pinkgorilla.encoding.encode :refer [encode-notebook]]))
(defn empty-notebook
"creates an empty notebook"
[]
{:ns nil
:segments {}
:segment-order []
:queued-code-segments #{}
:active-segment nil})
(defn segments-ordered [notebook]
(let [segments (:segments notebook)
segment-ids-ordered (:segment-order notebook)]
(vec (map #(get segments %) segment-ids-ordered))))
(defn dehydrate-notebook [notebook]
(let [segments (segments-ordered notebook)
segments-no-id (vec (map #(dissoc % :id :exception :error-text) segments))]
{:segments segments-no-id}))
(defn to-key [segment]
{(:id segment) segment})
(defn hydrate-notebook [notebook]
(let [segments (:segments notebook)
segments-with-id (vec (map #(assoc % :id (uuid) :exception nil :error-text nil) segments))
ids (vec (map :id segments-with-id))
m (reduce conj (map to-key segments-with-id))]
(assoc (empty-notebook)
:segment-order ids
:segments m)))
(defn load-notebook-hydrated [str]
(hydrate-notebook (decode str)))
(defn save-notebook-hydrated [notebook]
(encode-notebook (dehydrate-notebook notebook)))
(defn create-free-segment
"creates a markdown segment"
[content]
{:id (uuid)
:type :free
:markup-visible false
:content {:value (or content "")
:type "text/x-markdown"}})
(defn create-code-segment
([content]
{:id (uuid)
:type :code
:kernel :clj
:content {:value (or content "")
:type "text/x-clojure"}
:console-response ""
:value-response {:type "html" :value [:span]}
:error-text nil
:exception nil}))
(defn to-code-segment
[free-segment]
{:id (:id free-segment)
:type :code
:kernel :clj
:content {:value (get-in free-segment [:content :value])
:type "text/x-clojure"}
:console-response nil
:value-response nil
:error-text nil
:exception nil})
(defn to-free-segment
[code-segment]
{:id (:id code-segment)
:type :free
:markup-visible false
:content {:value (get-in code-segment [:content :value])
:type "text/x-markdown"}})
(defn insert-segment-at
[worksheet new-index new-segment]
(let [segment-order (:segment-order worksheet)
segments (:segments worksheet)
new-id (:id new-segment)
[head tail] (split-at new-index segment-order)]
(merge worksheet {:active-segment new-id
:segments (assoc segments new-id new-segment)
:segment-order (into [] (concat head (conj tail new-id)))})))
(defn remove-segment
[worksheet seg-id]
(let [segment-order (:segment-order worksheet)
active-id (:active-segment worksheet)
seg-idx (.indexOf segment-order seg-id)
next-active-idx (if (and (= active-id seg-id) (> seg-idx 0))
(nth segment-order (- seg-idx 1)))
segments (:segments worksheet)]
(merge worksheet {:active-segment next-active-idx
:segments (dissoc segments seg-id)
:segment-order (into [] (remove #(= seg-id %) segment-order))})))