 ;; and then a random number. Or just the provided scratch-key, but we'd have to
 ;; do check for collisions, which could be ugly.
 (defn get-scratch
-  "Adds a scratch area to the app-db and returns a pair [eid ratom]. eid is the
-  ID of an entity with two attributes: :scratch/key (a unique key identifying
-  this scratch area) and :scratch/val (a map, the scratch area itself). ratom is
-  a reactive atom holding the current value of :scratch/val."
+  "Adds a scratch entity to the app-db and returns a pair [eid ratom].
+
+  eid is the ID of the scratch entity. It has at least one attribute,
+  :scratch/key, a unique key which identifies it. Being a scratch space, you can
+  attach arbitrary other attributes to it. Their values should probably be
+  scalar, but you might try collections. I'm not sure about this. See also
+  https://github.com/tonsky/datascript/issues/69.
+
+  ratom is a reactive atom holding all the attributes of the scratch entity."
   []
   (let [scratch-key (uuid/make-random-squuid)]
     (rf/dispatch-sync [:new-scratch scratch-key])
     (let [scratch-entid @(rf/subscribe [:get-scratch-entid scratch-key])
-          scratch-val-ra (rf/subscribe [:get-scratch-val scratch-entid])]
-      [scratch-entid scratch-val-ra])))
+
+          scratch-contents-ra
+          (rf/subscribe [:get-scratch-contents scratch-entid])]
+      [scratch-entid scratch-contents-ra])))
 
 (defn dispatch-scratch
   "Convenience fn around rf/dispatch. Suppose s is what (get-scratch) returned.


(defn get-scratch-entid
  "Entity ID of the scratch area with SCRATCH-KEY as the value of its
  :scratch/key attribute."
  [conn [_ scratch-key]]
  (tsky/bind '[:find ?e .
               :in $ ?key
               :where [?e :scratch/key ?key]]
             conn scratch-key))
(th-utils/register-sub* get-scratch-entid)

(defn get-scratch-val
  "Scratch area with entity ID SCRATCH-ENTID."
  [conn [_ scratch-entid]]
  (reaction
    (safe-get (d/pull @conn [:scratch/val] scratch-entid) :scratch/val)))
(th-utils/register-sub* get-scratch-val)

(defn search-result
  "Result of the material search."

  [[scratch-id scratch-ratom] & attrs]
  {:pre [(even? (count attrs))]}
  (fn [node]
    (let [id (keyword (plumbing/safe-get-in node [:attrs :id]))
          default-attrs
          [:value (get @scratch-ratom id "")
           :onChange #(rf/dispatch [:set-scratch-val scratch-id id (value %)])]]
