|
30 | 30 | (map (fn [e] |
31 | 31 | (if expand |
32 | 32 | (cond-> (into {} e) |
33 | | - true |
34 | | - (dissoc e :block/tags :block/order :block/refs :block/name :db/index |
35 | | - :logseq.property.embedding/hnsw-label-updated-at :logseq.property/default-value) |
36 | | - true |
37 | | - (update :block/uuid str) |
38 | | - (:logseq.property/classes e) |
39 | | - (update :logseq.property/classes #(mapv :db/ident %)) |
40 | | - (:logseq.property/description e) |
41 | | - (update :logseq.property/description db-property/property-value-content)) |
| 33 | + true |
| 34 | + (dissoc e :block/tags :block/order :block/refs :block/name :db/index |
| 35 | + :logseq.property.embedding/hnsw-label-updated-at :logseq.property/default-value) |
| 36 | + true |
| 37 | + (update :block/uuid str) |
| 38 | + (:logseq.property/classes e) |
| 39 | + (update :logseq.property/classes #(mapv :db/ident %)) |
| 40 | + (:logseq.property/description e) |
| 41 | + (update :logseq.property/description db-property/property-value-content)) |
42 | 42 | {:block/title (:block/title e) |
43 | 43 | :block/uuid (str (:block/uuid e))}))))) |
44 | 44 |
|
|
130 | 130 | (or (get idents title) |
131 | 131 | (throw (ex-info (str "No ident found for " (pr-str title)) {})))) |
132 | 132 |
|
| 133 | +(defn- build-add-block [op {:keys [class-idents]}] |
| 134 | + (cond-> {:block/title (get-in op [:data :title])} |
| 135 | + (get-in op [:data :tags]) |
| 136 | + (assoc :build/tags (mapv #(get-ident class-idents %) (get-in op [:data :tags]))))) |
| 137 | + |
| 138 | +(defn- ops->existing-pages-and-blocks |
| 139 | + "Converts block operations for existing pages and prepares them for :pages-and-blocks" |
| 140 | + [db operations idents] |
| 141 | + (let [new-blocks-for-existing-pages |
| 142 | + (->> (filter #(and (= "block" (:entityType %)) |
| 143 | + (= "add" (:operation %)) |
| 144 | + (common-util/uuid-string? (get-in % [:data :page-id]))) operations) |
| 145 | + (map (fn [op] (assoc op ::page-id (uuid (get-in op [:data :page-id])))))) |
| 146 | + edit-blocks |
| 147 | + (->> (filter #(and (= "block" (:entityType %)) (= "edit" (:operation %))) operations) |
| 148 | + (map (fn [op] |
| 149 | + (let [block-uuid (uuid (:id op)) |
| 150 | + ent (d/entity db [:block/uuid block-uuid])] |
| 151 | + (when-not (:block/page ent) |
| 152 | + (throw (ex-info "Block edit operation requires a block to have a page." {}))) |
| 153 | + (assoc op ::page-id (get-in ent [:block/page :block/uuid]))))))] |
| 154 | + (->> (concat new-blocks-for-existing-pages edit-blocks) |
| 155 | + (group-by ::page-id) |
| 156 | + (map (fn [[page-id ops]] |
| 157 | + {:page {:block/uuid page-id} |
| 158 | + :blocks (mapv (fn [op] |
| 159 | + (prn :block-op op) |
| 160 | + (if (= "add" (:operation op)) |
| 161 | + (build-add-block op idents) |
| 162 | + ;; edit :block |
| 163 | + (cond-> {:block/uuid (uuid (:id op))} |
| 164 | + (get-in op [:data :title]) |
| 165 | + (assoc :block/title (get-in op [:data :title]))))) |
| 166 | + ops)}))))) |
| 167 | + |
133 | 168 | (defn- ops->pages-and-blocks |
134 | | - [operations {:keys [class-idents]}] |
135 | | - (let [blocks-by-page |
| 169 | + [db operations idents] |
| 170 | + (let [new-blocks-by-page |
136 | 171 | (group-by #(get-in % [:data :page-id]) |
137 | | - (filter #(= "block" (:entityType %)) operations)) |
| 172 | + (filter #(and (= "block" (:entityType %)) (= "add" (:operation %))) operations)) |
138 | 173 | new-pages (filter #(and (= "page" (:entityType %)) (= "add" (:operation %))) operations) |
139 | | - add-block (fn add-block [op] |
140 | | - (cond-> {:block/title (get-in op [:data :title])} |
141 | | - (get-in op [:data :tags]) |
142 | | - (assoc :build/tags (mapv #(get-ident class-idents %) (get-in op [:data :tags]))))) |
143 | 174 | pages-and-blocks |
144 | 175 | (into (mapv (fn [op] |
145 | 176 | (cond-> {:page (if-let [journal-day (date-time-util/journal-title->int |
|
148 | 179 | (date-time-util/safe-journal-title-formatters nil))] |
149 | 180 | {:build/journal journal-day} |
150 | 181 | {:block/title (get-in op [:data :title])})} |
151 | | - (some->> (:id op) (get blocks-by-page)) |
| 182 | + (some->> (:id op) (get new-blocks-by-page)) |
152 | 183 | (assoc :blocks |
153 | | - (mapv add-block (get blocks-by-page (:id op)))))) |
| 184 | + (mapv #(build-add-block % idents) (get new-blocks-by-page (:id op)))))) |
154 | 185 | new-pages) |
155 | | - ;; existing pages |
156 | | - (map (fn [[page-id ops]] |
157 | | - (when-not (common-util/uuid-string? page-id) |
158 | | - (throw (ex-info (str "Existing page id " (pr-str page-id) " must be a uuid") {}))) |
159 | | - {:page {:block/uuid (uuid page-id)} |
160 | | - :blocks (mapv (fn [op] |
161 | | - (if (= "add" (:operation op)) |
162 | | - (add-block op) |
163 | | - ;; edit |
164 | | - (cond-> {:block/uuid (uuid (:id op))} |
165 | | - (get-in op [:data :title]) |
166 | | - (assoc :block/title (get-in op [:data :title]))))) |
167 | | - ops)}) |
168 | | - (apply dissoc blocks-by-page (map :id new-pages))))] |
| 186 | + (ops->existing-pages-and-blocks db operations idents))] |
169 | 187 | pages-and-blocks)) |
170 | 188 |
|
171 | 189 | (defn- ops->classes |
|
299 | 317 | [:id uuid-string] |
300 | 318 | ;; :tags not supported yet |
301 | 319 | [:data [:map {:closed true} |
302 | | - [:page-id uuid-string] |
303 | 320 | [:title :string]]]]] |
304 | 321 | ;; other edit's |
305 | 322 | [::m/default [:map [:id uuid-string]]]]]) |
|
365 | 382 | (throw (ex-info (str "Tool arguments are invalid:\n" (me/humanize errors)) |
366 | 383 | {:errors errors}))) |
367 | 384 | idents (operations->idents db operations) |
368 | | - pages-and-blocks (ops->pages-and-blocks operations idents) |
| 385 | + pages-and-blocks (ops->pages-and-blocks db operations idents) |
369 | 386 | classes (ops->classes operations idents) |
370 | 387 | properties (ops->properties operations idents) |
371 | 388 | import-edn |
|
0 commit comments