|
310 | 310 | [removed-links added-links])) |
311 | 311 |
|
312 | 312 |
|
| 313 | +(defn throw-unknown-k |
| 314 | + [k] |
| 315 | + (throw (str "Key " k " must be either string or ::first/::last."))) |
| 316 | + |
| 317 | + |
313 | 318 | (defn- new-prop |
314 | | - [db [a v :as uid-or-eid] prop-uid k] |
| 319 | + [db [a v :as uid-or-eid] next-uid k] |
315 | 320 | (let [uid? (-> uid-or-eid vector? not) |
316 | 321 | uid (if uid? |
317 | 322 | uid-or-eid |
318 | 323 | (common-db/get-block-uid db uid-or-eid)) |
319 | 324 | title (or (common-db/get-page-title db uid) |
320 | 325 | (and (= a :node/title) v)) |
321 | | - position (merge {:relation {:page/title k}} |
| 326 | + ;; here too |
| 327 | + position (merge {:relation (cond |
| 328 | + (= ::first k) :first |
| 329 | + (= ::last k) :last |
| 330 | + (string? k) {:page/title k} |
| 331 | + :else (throw-unknown-k k))} |
322 | 332 | (if title |
323 | 333 | {:page/title title} |
324 | 334 | {:block/uid uid}))] |
325 | | - (build-block-new-op db prop-uid position))) |
| 335 | + (build-block-new-op db next-uid position))) |
326 | 336 |
|
327 | 337 |
|
328 | | -(defn build-property-path |
| 338 | +(defn build-path |
| 339 | + "Return uid at ks path and operations to create path, if needed, as [uid ops]. |
| 340 | + uid can be a string or a datascript eid. |
| 341 | + ks can be properties names as strings, or ::first/::last for children." |
329 | 342 | ([db uid ks] |
330 | | - (build-property-path db uid ks [])) |
| 343 | + (build-path db uid ks [])) |
331 | 344 | ([db uid-or-eid [k & ks] ops] |
332 | 345 | (if-not k |
333 | 346 | [uid-or-eid ops] |
334 | 347 | (let [uid? (-> uid-or-eid vector? not) |
335 | 348 | block (common-db/get-block db (if uid? |
336 | 349 | [:block/uid uid-or-eid] |
337 | 350 | uid-or-eid)) |
338 | | - prop-block (-> block :block/properties (get k)) |
339 | | - prop-uid (or (:block/uid prop-block) |
| 351 | + next-block (cond |
| 352 | + (= ::first k) (-> block :block/children first) |
| 353 | + (= ::last k) (-> block :block/children last) |
| 354 | + (string? k) (-> block :block/properties (get k)) |
| 355 | + :else (throw-unknown-k k)) |
| 356 | + next-uid (or (:block/uid next-block) |
340 | 357 | (common.utils/gen-block-uid)) |
341 | 358 | ops' (cond-> ops |
342 | | - (not prop-block) (conj (new-prop db uid-or-eid prop-uid k)))] |
343 | | - (recur db prop-uid ks ops'))))) |
| 359 | + (not next-block) (conj (new-prop db uid-or-eid next-uid k)))] |
| 360 | + (recur db next-uid ks ops'))))) |
| 361 | + |
| 362 | + |
| 363 | +(defn get-path |
| 364 | + "Return uid at ks path." |
| 365 | + [db uid-or-eid [k & ks]] |
| 366 | + (if-not (and uid-or-eid k) |
| 367 | + uid-or-eid |
| 368 | + (let [uid? (-> uid-or-eid vector? not) |
| 369 | + block (common-db/get-block db (if uid? |
| 370 | + [:block/uid uid-or-eid] |
| 371 | + uid-or-eid)) |
| 372 | + next-block (cond |
| 373 | + (= ::first k) (-> block :block/children first) |
| 374 | + (= ::last k) (-> block :block/children last) |
| 375 | + (string? k) (-> block :block/properties (get k)) |
| 376 | + :else (throw-unknown-k k)) |
| 377 | + next-uid (:block/uid next-block)] |
| 378 | + (recur db next-uid ks)))) |
0 commit comments