Something went wrong. Try again.
A ebook and PDF reader, annotator, and citation manager.
Something went wrong. Try again.
10 kB 217 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218== Lazy document drafts and promotion <document-drafts>
UI creation allocates a local tab key, not a SQLite document ID. A document isinserted only when its title, metadata or serialized content differs from theinitial defaults. Merely opening, focusing, closing, refreshing or tearing offan untouched draft never inserts a row. Once created, reverting an edit doesnot delete the row. REPL/native create commands remain explicitly immediate.
Negative local keys distinguish drafts from positive SQLite IDs. They remainstable for the life of the tab even after promotion, so saving does not remountthe editor or move its cursor. Native arguments are translated at the UIboundary; local keys never become database IDs. The FIFO save coordinatorpreserves edit order, and a shared in-flight creation Promise prevents multipleinserts. Failed creation remains retryable and prevents losing edited drafts.
Attachment/link controls require an initial title, metadata or text edit. Thiskeeps cancelling a picker from producing an empty row; pristine-draft imagepastes show an actionable message rather than implicitly inserting a document.Controls enable as soon as creation succeeds. Inline wiki links work as sourceedits and therefore can themselves trigger creation. New draft windows carryonly their document mode; each window constructs its own default draft locally.
=== Promotion and ID translation
```clojure;| file: tangled/cljs/candlelight/ui_drafts.cljs(ns candlelight.ui-drafts (:require [candlelight.bridge :as bridge] [candlelight.document-operations :as operations] [candlelight.documents :as documents]))
(defonce ^:private next-key (atom 0))(defonce ^:private entries (atom {}))
(defn defaults "Return the initial UI document values for a mode and its resolved type ID." [mode type-id] (documents/draft mode type-id))
(defn create! "Register a local draft without IPC. Returns its document map with a local tab key." ([document] (create! document bridge/invoke)) ([document invoke!] (let [key (swap! next-key dec) document (assoc document :id key)] (swap! entries assoc key {:document document :defaults document :invoke! invoke!}) document)))
(defn persisted-id "Return a tab's SQLite ID, or nil if it is an unpersisted local draft." [key] (if (pos? key) key (get-in @entries [key :persisted-id])))
(defn forget! "Discard a closed tab's local mapping after its pending saves have finished." [key] (swap! entries dissoc key))
(defn- ensure-created! [key] (let [{:keys [document defaults pending persisted-id invoke!]} (get @entries key)] (cond persisted-id (js/Promise.resolve persisted-id) pending pending (= document defaults) (js/Promise.resolve nil) :else (let [promise (-> (js/Promise.resolve nil) (.then (fn [_] (let [document (get-in @entries [key :document])] (operations/create-document! {:type-id (:typeId document) :name (:name document) :content (:content document) :metadata (:metadata document) :attachments (:attachments document)} invoke!)))) (.then (fn [id] (swap! entries update key assoc :persisted-id id :pending nil) id)) (.catch (fn [error] (swap! entries update key assoc :pending nil) (throw error))))] (swap! entries assoc-in [key :pending] promise) promise))))
(defn save! "Save a title/metadata/content change, promoting a draft only after an actual edit." [command args] (let [key (.-id args)] (if-not (contains? @entries key) (bridge/invoke command args) (let [field (case command "rename_document" :name "set_content" :content "set_metadata" :metadata) value (case command "rename_document" (.-name args) "set_content" (.-content args) "set_metadata" (js->clj (.-metadata args) :keywordize-keys true))] (swap! entries assoc-in [key :document field] value) (-> (ensure-created! key) (.then (fn [id] (when id ((get-in @entries [key :invoke!]) command (js/Object.assign #js {} args #js {:id id}))))))))))
(defn invoke "Route UI document arguments to persisted IDs without creating pristine drafts." ([command] (bridge/invoke command)) ([command args] (let [mapped (js/Object.assign #js {} args)] (doseq [field ["id" "documentId" "sourceId" "targetId"]] (let [key (aget mapped field)] (when (contains? @entries key) (aset mapped field (or (persisted-id key) 0))))) (bridge/invoke command mapped))))```
=== Draft regressions
Run draft regressions explicitly with`clojure -M -m cljs.main -re node -m candlelight.ui-drafts-test`.
```clojure;| file: tangled/cljs/candlelight/ui_drafts_test.cljs(ns candlelight.ui-drafts-test (:require [candlelight.bridge :as bridge] [candlelight.ui-drafts :as drafts] [cljs.test :refer-macros [async deftest is run-tests]]))
(deftest opening-a-draft-does-not-invoke-native-creation (let [calls (atom [])] (with-redefs [bridge/invoke (fn [& args] (swap! calls conj args))] (let [document (drafts/create! (drafts/defaults "note" 2))] (drafts/forget! (:id document)))) (is (empty? @calls))))
(deftest untouched-field-save-does-not-create-a-row (async done (let [document (drafts/create! (drafts/defaults "note" 2)) key (:id document)] (-> (drafts/save! "rename_document" #js {:id key :name "Untitled note"}) (.then (fn [_] (is (nil? (drafts/persisted-id key))))) (.catch (fn [error] (is (nil? error)))) (.finally (fn [] (drafts/forget! key) (done)))))))
(deftest concurrent-first-edits-create-only-one-row (async done (let [calls (atom []) document (drafts/create! (drafts/defaults "note" 2) (fn [command args] (swap! calls conj [command (js->clj args :keywordize-keys true)]) (js/Promise.resolve (when (= command "create_document") 42)))) key (:id document)] (-> (js/Promise.all #js [(drafts/save! "rename_document" #js {:id key :name "Changed"}) (drafts/save! "set_content" #js {:id key :content "Hello"})]) (.then (fn [_] (is (= 1 (count (filter #(= "create_document" (first %)) @calls)))) (is (= 42 (drafts/persisted-id key))) (is (every? #(= 42 (:id (second %))) (remove #(= "create_document" (first %)) @calls))))) (.catch (fn [error] (is (nil? error)))) (.finally (fn [] (drafts/forget! key) (done)))))))
(deftest failed-creation-can-be-retried (async done (let [attempts (atom 0) document (drafts/create! (drafts/defaults "note" 2) (fn [command _] (if (= command "create_document") (if (= 1 (swap! attempts inc)) (js/Promise.reject (js/Error. "Disk unavailable")) (js/Promise.resolve 43)) (js/Promise.resolve nil)))) key (:id document) args #js {:id key :content "Keep this"}] (-> (drafts/save! "set_content" args) (.then (fn [_] (is false "First creation should fail"))) (.catch (fn [_] (drafts/save! "set_content" args))) (.then (fn [_] (is (= 43 (drafts/persisted-id key))))) (.catch (fn [error] (is (nil? error)))) (.finally (fn [] (drafts/forget! key) (done)))))))
(deftest unchanged-metadata-and-scratch-content-do-not-create-rows (async done (let [calls (atom []) invoke! (fn [command _] (swap! calls conj command) (js/Promise.resolve nil)) citation (drafts/create! (drafts/defaults "citation" 1) invoke!) scratch (drafts/create! (drafts/defaults "clojure-mode" 3) invoke!)] (-> (js/Promise.all #js [(drafts/save! "set_metadata" #js {:id (:id citation) :metadata (clj->js (:metadata citation))}) (drafts/save! "set_content" #js {:id (:id scratch) :content (:content scratch)})]) (.then (fn [_] (is (empty? @calls)))) (.catch (fn [error] (is (nil? error)))) (.finally (fn [] (drafts/forget! (:id citation)) (drafts/forget! (:id scratch)) (done)))))))
(deftest changing-only-citation-metadata-creates-a-document (async done (let [created (atom nil) document (drafts/create! (drafts/defaults "citation" 1) (fn [command args] (when (= command "create_document") (reset! created (js->clj args :keywordize-keys true))) (js/Promise.resolve (when (= command "create_document") 44)))) key (:id document)] (-> (drafts/save! "set_metadata" #js {:id key :metadata #js {:title "Untitled citation" :author "New author"}}) (.then (fn [_] (is (= "New author" (get-in @created [:metadata :author]))))) (.catch (fn [error] (is (nil? error)))) (.finally (fn [] (drafts/forget! key) (done)))))))
(deftest citation-defaults-match-visible-title (is (= {:title "Untitled citation"} (:metadata (drafts/defaults "citation" 1)))))
(deftest scratch-defaults-are-empty-edn (is (= (pr-str {:source "" :annotations []}) (:content (drafts/defaults "clojure-mode" 3)))))
(defn -main "Run UI draft regressions under Node." [] (run-tests 'candlelight.ui-drafts-test))```